#!/usr/bin/env python3
"""
This script launches a dbus session, sets the DBUS_SESSION_BUS_ADDRESS
and DBUS_STARTER_BUS_TYPE Eenvironment variables and puts the generated files
in the dbus dir location passed as a parameter. It then runs the unit test
script, and then cleans up the generated dbus files.
"""
import argparse
import os
import re
import sys
import tempfile
from subprocess import check_call, check_output
def launch_session_dbus(dbus_dir, dbus_config_file):
"""
Launches a session debus using a modified config file and
sets the DBUS_SESSION_BUS_ADDRESS environment variable
Parameter descriptions:
dbus_dir Directory location for generated files
dbus_config_file File location of dbus sys config file
"""
dbus_pid = os.path.join(dbus_dir, "pid")
dbus_socket = os.path.join(dbus_dir, "system_bus_socket")
dbus_local_conf = os.path.join(dbus_dir, "system-local.conf")
if os.path.isfile(dbus_pid):
os.remove(dbus_pid)
with open(dbus_config_file) as infile, open(
dbus_local_conf, "w"
) as outfile:
for line in infile:
line = re.sub(
".*",
"session",
line,
flags=re.DOTALL,
)
line = re.sub(
".*",
"%s" % dbus_pid,
line,
flags=re.DOTALL,
)
line = re.sub(
".*",
"unix:path=%s" % dbus_socket,
line,
flags=re.DOTALL,
)
line = re.sub("