blob: baf134cdb13e2f162994a29f2de55f95127fc55e [file] [log] [blame]
Andrew Geissler8439ac52020-05-20 13:20:39 -05001#!/usr/bin/env python3
Leonel Gonzalez13ca3802017-03-07 14:08:44 -06002
3"""
4This script launches a dbus session, sets the DBUS_SESSION_BUS_ADDRESS
5and DBUS_STARTER_BUS_TYPE Eenvironment variables and puts the generated files
Gunnar Mills5f811802017-10-25 16:10:27 -05006in the dbus dir location passed as a parameter. It then runs the unit test
Leonel Gonzalez13ca3802017-03-07 14:08:44 -06007script, and then cleans up the generated dbus files.
8"""
9
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060010import argparse
Patrick Williamse08ffba2022-12-05 10:33:46 -060011import os
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060012import re
Patrick Williamse08ffba2022-12-05 10:33:46 -060013import sys
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060014import tempfile
Patrick Williamse08ffba2022-12-05 10:33:46 -060015from subprocess import check_call, check_output
16
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060017
18def 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 Williamse08ffba2022-12-05 10:33:46 -060027 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 Gonzalez13ca3802017-03-07 14:08:44 -060030 if os.path.isfile(dbus_pid):
31 os.remove(dbus_pid)
Patrick Williamse08ffba2022-12-05 10:33:46 -060032 with open(dbus_config_file) as infile, open(
33 dbus_local_conf, "w"
34 ) as outfile:
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060035 for line in infile:
Patrick Williamse08ffba2022-12-05 10:33:46 -060036 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 Gonzalez13ca3802017-03-07 14:08:44 -060055 outfile.write(line)
56 infile.close()
57 outfile.close()
Patrick Williamse08ffba2022-12-05 10:33:46 -060058 command = [
59 "dbus-daemon",
60 "--config-file=%s" % dbus_local_conf,
61 "--print-address",
62 ]
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060063 out = check_output(command).splitlines()
Patrick Williamse08ffba2022-12-05 10:33:46 -060064 os.environ["DBUS_SESSION_BUS_ADDRESS"] = out[0].decode("utf-8")
65 os.environ["DBUS_STARTER_BUS_TYPE"] = "session"
66
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060067
68def dbus_cleanup(dbus_dir):
69 """
70 Kills the dbus session started by launch_session_dbus
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060071
72 Parameter descriptions:
73 dbus_dir Directory location of generated files
74 """
75
Patrick Williamse08ffba2022-12-05 10:33:46 -060076 dbus_pid = os.path.join(dbus_dir, "pid")
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060077 if os.path.isfile(dbus_pid):
Patrick Williamse08ffba2022-12-05 10:33:46 -060078 dbus_pid = open(dbus_pid, "r").read().replace("\n", "")
79 check_call(["kill", dbus_pid])
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060080
81
Patrick Williamse08ffba2022-12-05 10:33:46 -060082if __name__ == "__main__":
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060083 # Set command line arguments
84 parser = argparse.ArgumentParser()
85
Patrick Williamse08ffba2022-12-05 10:33:46 -060086 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 Gonzalez13ca3802017-03-07 14:08:44 -0600103 args = parser.parse_args(sys.argv[1:])
Patrick Williamse08ffba2022-12-05 10:33:46 -0600104 DBUS_DIR = tempfile.TemporaryDirectory(dir="/tmp/")
Leonel Gonzalez13ca3802017-03-07 14:08:44 -0600105 DBUS_SYS_CONFIG_FILE = args.DBUS_SYS_CONFIG_FILE
106 UNIT_TEST = args.UNIT_TEST
107
Andrew Geissler3c2e7472022-11-18 09:35:03 -0600108 launch_session_dbus(DBUS_DIR.name, DBUS_SYS_CONFIG_FILE)
Patrick Williamse08ffba2022-12-05 10:33:46 -0600109 check_call(UNIT_TEST.split(","), env=os.environ)
Andrew Geissler3c2e7472022-11-18 09:35:03 -0600110 dbus_cleanup(DBUS_DIR.name)