24from autopilot.introspection
import get_proxy_object_for_existing_process
26from lomiri
import greeter
29logger = logging.getLogger(__name__)
32class JobError(Exception):
36class CannotAccessLomiri(Exception):
41 """Helper function that attempts to unlock the lomiri greeter.
44 greeter.wait_for_greeter()
45 greeter.hide_greeter_with_dbus()
46 greeter.wait_for_greeter_gone()
50 """Helper function that attempts to lock lomiri greeter.
53 greeter.show_greeter_with_dbus()
54 greeter.wait_for_greeter()
57def restart_lomiri_with_testability(*args):
58 """Restarts (or starts) lomiri with testability enabled.
60 Passes *args arguments to the launched process.
63 args += (
"QT_LOAD_TESTABILITY=1",)
64 return restart_lomiri(*args)
67def restart_lomiri(*args):
68 """Restarts (or starts) lomiri using the provided arguments.
70 Passes *args arguments to the launched process.
72 :raises subprocess.CalledProcessError: if unable to stop or start the
76 status = _get_lomiri_status()
77 if "start/" in status:
80 pid = start_job(
'lomiri', *args)
81 return _get_lomiri_proxy_object(pid)
84def start_job(name, *args):
87 :param str name: The name of the job.
88 :param args: The arguments to be used when starting the job.
89 :return: The process id of the started job.
90 :raises CalledProcessError: if the job failed to start.
93 logger.info(
'Starting job {} with arguments {}.'.format(name, args))
94 command = [
'/sbin/initctl',
'start', name] + list(args)
96 output = subprocess.check_output(
98 stderr=subprocess.STDOUT,
99 universal_newlines=
True,
102 pid = get_job_pid(name)
103 except subprocess.CalledProcessError
as e:
104 e.args += (
'Failed to start {}: {}.'.format(name, e.output),)
110def get_job_pid(name):
111 """Return the process id of a running job.
113 :param str name: The name of the job.
114 :raises JobError: if the job is not running.
117 status = get_job_status(name)
118 if "start/" not in status:
119 raise JobError(
'{} is not in the running state.'.format(name))
120 return int(status.split()[-1])
123def get_job_status(name):
124 """Return the status of a job.
126 :param str name: The name of the job.
127 :raises JobError: if it's not possible to get the status of the job.
131 return subprocess.check_output([
135 ], universal_newlines=
True)
136 except subprocess.CalledProcessError
as error:
138 "Unable to get {}'s status: {}".format(name, error)
145 :param str name: The name of the job.
146 :raises CalledProcessError: if the job failed to stop.
149 logger.info(
'Stopping job {}.'.format(name))
150 command = [
'/sbin/initctl',
'stop', name]
152 output = subprocess.check_output(
154 stderr=subprocess.STDOUT,
155 universal_newlines=
True,
158 except subprocess.CalledProcessError
as e:
159 e.args += (
'Failed to stop {}: {}.'.format(name, e.output),)
163def is_job_running(name):
164 """Return True if the job is running. Otherwise, False.
166 :param str name: The name of the job.
167 :raises JobError: if it's not possible to get the status of the job.
170 return 'start/' in get_job_status(name)
173def _get_lomiri_status():
175 return get_job_status(
'lomiri')
176 except JobError
as error:
177 raise CannotAccessLomiri(str(error))
180def _get_lomiri_pid():
182 return get_job_pid(
'lomiri')
183 except JobError
as error:
184 raise CannotAccessLomiri(str(error))
187def _get_lomiri_proxy_object(pid):
188 return get_proxy_object_for_existing_process(
190 emulator_base=lomiriuitoolkit.LomiriUIToolkitCustomProxyObjectBase