blob: cdecd2d8430ccead8b8f3ddf30efc42b41261973 [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
10from subprocess import check_call, check_output
11import os
12import sys
13import argparse
14import re
15import tempfile
16
17def launch_session_dbus(dbus_dir, dbus_config_file):
18 """
19 Launches a session debus using a modified config file and
20 sets the DBUS_SESSION_BUS_ADDRESS environment variable
21
22 Parameter descriptions:
23 dbus_dir Directory location for generated files
24 dbus_config_file File location of dbus sys config file
25 """
26 dbus_pid = os.path.join(dbus_dir,'pid')
27 dbus_socket = os.path.join(dbus_dir,'system_bus_socket')
28 dbus_local_conf = os.path.join(dbus_dir,'system-local.conf')
29 if os.path.isfile(dbus_pid):
30 os.remove(dbus_pid)
31 with open(dbus_config_file) as infile, \
32 open(dbus_local_conf, 'w') as outfile:
33 for line in infile:
34 line = re.sub('<type>.*</type>','<type>session</type>', \
35 line, flags=re.DOTALL)
36 line = re.sub('<pidfile>.*</pidfile>', \
37 '<pidfile>%s</pidfile>' % dbus_pid, \
38 line, flags=re.DOTALL)
39 line = re.sub('<listen>.*</listen>', \
40 '<listen>unix:path=%s</listen>' % dbus_socket, \
41 line, flags=re.DOTALL)
42 line = re.sub('<deny','<allow', line)
43 outfile.write(line)
44 infile.close()
45 outfile.close()
46 command = ['dbus-daemon', '--config-file=%s' % dbus_local_conf, \
47 '--print-address']
48 out = check_output(command).splitlines()
Andrew Geissler8439ac52020-05-20 13:20:39 -050049 os.environ['DBUS_SESSION_BUS_ADDRESS'] = out[0].decode("utf-8")
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060050 os.environ['DBUS_STARTER_BUS_TYPE'] = 'session'
51
52def dbus_cleanup(dbus_dir):
53 """
54 Kills the dbus session started by launch_session_dbus
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060055
56 Parameter descriptions:
57 dbus_dir Directory location of generated files
58 """
59
60 dbus_pid = os.path.join(dbus_dir,'pid')
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060061 if os.path.isfile(dbus_pid):
62 dbus_pid = open(dbus_pid,'r').read().replace('\n','')
63 check_call(['kill', dbus_pid])
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060064
65
66if __name__ == '__main__':
67
68 # Set command line arguments
69 parser = argparse.ArgumentParser()
70
71 parser.add_argument("-f", "--dbussysconfigfile",
72 dest="DBUS_SYS_CONFIG_FILE",
73 required=True, help="Dbus sys config file location")
74 parser.add_argument("-u", "--unittestandparams",
75 dest="UNIT_TEST",
76 required=True, help="Unit test script and params \
77 as comma delimited string")
78 args = parser.parse_args(sys.argv[1:])
Andrew Geissler3c2e7472022-11-18 09:35:03 -060079 DBUS_DIR = tempfile.TemporaryDirectory(dir='/tmp/')
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060080 DBUS_SYS_CONFIG_FILE = args.DBUS_SYS_CONFIG_FILE
81 UNIT_TEST = args.UNIT_TEST
82
Andrew Geissler3c2e7472022-11-18 09:35:03 -060083 launch_session_dbus(DBUS_DIR.name, DBUS_SYS_CONFIG_FILE)
Leonel Gonzalez13ca3802017-03-07 14:08:44 -060084 check_call(UNIT_TEST.split(','), env=os.environ)
Andrew Geissler3c2e7472022-11-18 09:35:03 -060085 dbus_cleanup(DBUS_DIR.name)