Andrew Geissler | 8439ac5 | 2020-05-20 13:20:39 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 2 | |
| 3 | """ |
| 4 | This script launches a dbus session, sets the DBUS_SESSION_BUS_ADDRESS |
| 5 | and DBUS_STARTER_BUS_TYPE Eenvironment variables and puts the generated files |
Gunnar Mills | 5f81180 | 2017-10-25 16:10:27 -0500 | [diff] [blame] | 6 | in the dbus dir location passed as a parameter. It then runs the unit test |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 7 | script, and then cleans up the generated dbus files. |
| 8 | """ |
| 9 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 10 | import argparse |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 11 | import os |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 12 | import re |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 13 | import sys |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 14 | import tempfile |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 15 | from subprocess import check_call, check_output |
| 16 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 17 | |
| 18 | def launch_session_dbus(dbus_dir, dbus_config_file): |
| 19 | """ |
| 20 | Launches a session debus using a modified config file and |
| 21 | sets the DBUS_SESSION_BUS_ADDRESS environment variable |
| 22 | |
| 23 | Parameter descriptions: |
| 24 | dbus_dir Directory location for generated files |
| 25 | dbus_config_file File location of dbus sys config file |
| 26 | """ |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 27 | dbus_pid = os.path.join(dbus_dir, "pid") |
| 28 | dbus_socket = os.path.join(dbus_dir, "system_bus_socket") |
| 29 | dbus_local_conf = os.path.join(dbus_dir, "system-local.conf") |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 30 | if os.path.isfile(dbus_pid): |
| 31 | os.remove(dbus_pid) |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 32 | with open(dbus_config_file) as infile, open( |
| 33 | dbus_local_conf, "w" |
| 34 | ) as outfile: |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 35 | for line in infile: |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 36 | line = re.sub( |
| 37 | "<type>.*</type>", |
| 38 | "<type>session</type>", |
| 39 | line, |
| 40 | flags=re.DOTALL, |
| 41 | ) |
| 42 | line = re.sub( |
| 43 | "<pidfile>.*</pidfile>", |
| 44 | "<pidfile>%s</pidfile>" % dbus_pid, |
| 45 | line, |
| 46 | flags=re.DOTALL, |
| 47 | ) |
| 48 | line = re.sub( |
| 49 | "<listen>.*</listen>", |
| 50 | "<listen>unix:path=%s</listen>" % dbus_socket, |
| 51 | line, |
| 52 | flags=re.DOTALL, |
| 53 | ) |
| 54 | line = re.sub("<deny", "<allow", line) |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 55 | outfile.write(line) |
| 56 | infile.close() |
| 57 | outfile.close() |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 58 | command = [ |
| 59 | "dbus-daemon", |
| 60 | "--config-file=%s" % dbus_local_conf, |
| 61 | "--print-address", |
| 62 | ] |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 63 | out = check_output(command).splitlines() |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 64 | os.environ["DBUS_SESSION_BUS_ADDRESS"] = out[0].decode("utf-8") |
| 65 | os.environ["DBUS_STARTER_BUS_TYPE"] = "session" |
| 66 | |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 67 | |
| 68 | def dbus_cleanup(dbus_dir): |
| 69 | """ |
| 70 | Kills the dbus session started by launch_session_dbus |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 71 | |
| 72 | Parameter descriptions: |
| 73 | dbus_dir Directory location of generated files |
| 74 | """ |
| 75 | |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 76 | dbus_pid = os.path.join(dbus_dir, "pid") |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 77 | if os.path.isfile(dbus_pid): |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 78 | dbus_pid = open(dbus_pid, "r").read().replace("\n", "") |
| 79 | check_call(["kill", dbus_pid]) |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 80 | |
| 81 | |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 82 | if __name__ == "__main__": |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 83 | # Set command line arguments |
| 84 | parser = argparse.ArgumentParser() |
| 85 | |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 86 | parser.add_argument( |
| 87 | "-f", |
| 88 | "--dbussysconfigfile", |
| 89 | dest="DBUS_SYS_CONFIG_FILE", |
| 90 | required=True, |
| 91 | help="Dbus sys config file location", |
| 92 | ) |
| 93 | parser.add_argument( |
| 94 | "-u", |
| 95 | "--unittestandparams", |
| 96 | dest="UNIT_TEST", |
| 97 | required=True, |
| 98 | help=( |
| 99 | "Unit test script and params as comma" |
| 100 | " delimited string" |
| 101 | ), |
| 102 | ) |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 103 | args = parser.parse_args(sys.argv[1:]) |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 104 | DBUS_DIR = tempfile.TemporaryDirectory(dir="/tmp/") |
Leonel Gonzalez | 13ca380 | 2017-03-07 14:08:44 -0600 | [diff] [blame] | 105 | DBUS_SYS_CONFIG_FILE = args.DBUS_SYS_CONFIG_FILE |
| 106 | UNIT_TEST = args.UNIT_TEST |
| 107 | |
Andrew Geissler | 3c2e747 | 2022-11-18 09:35:03 -0600 | [diff] [blame] | 108 | launch_session_dbus(DBUS_DIR.name, DBUS_SYS_CONFIG_FILE) |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame] | 109 | check_call(UNIT_TEST.split(","), env=os.environ) |
Andrew Geissler | 3c2e747 | 2022-11-18 09:35:03 -0600 | [diff] [blame] | 110 | dbus_cleanup(DBUS_DIR.name) |