20 from autopilot.introspection
import (
21 get_proxy_object_for_existing_process,
29 logger = logging.getLogger(__name__)
32 class CannotAccessUnity(Exception):
36 def unlock_unity(unity_proxy_obj=None):
37 """Helper function that attempts to unlock the unity greeter.
39 If unity_proxy_object is None create a proxy object by querying for the
40 running unity process.
41 Otherwise re-use the passed proxy object.
43 :raises RuntimeError: if the greeter attempts and fails to be unlocked.
45 :raises RuntimeWarning: when the greeter cannot be found because it is
47 :raises CannotAccessUnity: if unity is not introspectable or cannot be
49 :raises CannotAccessUnity: if unity's upstart status is not "start" or the
50 upstart job cannot be found at all.
53 if unity_proxy_obj
is None:
55 pid = _get_unity_pid()
56 unity = _get_unity_proxy_object(pid)
57 main_window = unity.select_single(main_window_emulator.QQuickView)
58 except ProcessSearchError
as e:
59 raise CannotAccessUnity(
60 "Cannot introspect unity, make sure that it has been started "
61 "with testability. Perhaps use the function "
62 "'restart_unity_with_testability' this module provides."
67 main_window = unity_proxy_obj.select_single(
68 main_window_emulator.QQuickView)
70 greeter = main_window.get_greeter()
71 if greeter.created ==
False:
72 raise RuntimeWarning(
"Greeter appears to be already unlocked.")
82 except AssertionError:
86 logger.info(
"Failed to unlock greeter, trying again...")
88 logger.info(
"Greeter unlocked, continuing.")
92 def lock_unity(unity_proxy_obj=None):
93 """Helper function that attempts to lock the unity greeter."""
95 uinput = evdev.UInput(name=
'unity8-autopilot-power-button',
96 devnode=
'/dev/autopilot-uinput')
98 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
99 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
103 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 1)
104 uinput.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_POWER, 0)
108 def restart_unity_with_testability(*args):
109 """Restarts (or starts) unity with testability enabled.
111 Passes *args arguments to the launched process.
114 args += (
"QT_LOAD_TESTABILITY=1",)
115 return restart_unity(*args)
118 def restart_unity(*args):
119 """Restarts (or starts) unity8 using the provided arguments.
121 Passes *args arguments to the launched process.
123 :raises subprocess.CalledProcessError: if unable to stop or start the
127 status = _get_unity_status()
128 if "start/" in status:
130 output = subprocess.check_output(
131 [
'/sbin/initctl',
'stop',
'unity8'])
133 except subprocess.CalledProcessError
as e:
135 "Failed to stop running instance of unity8: %s" % e.output,
140 command = [
'/sbin/initctl',
'start',
'unity8'] + list(args)
141 output = subprocess.check_output(
143 stderr=subprocess.STDOUT,
144 universal_newlines=
True,
147 pid = _get_unity_pid()
148 except subprocess.CalledProcessError
as e:
149 e.args += (
"Failed to start unity8: %s" % e.output,)
152 return _get_unity_proxy_object(pid)
155 def _get_unity_status():
157 return subprocess.check_output([
161 ], universal_newlines=
True)
162 except subprocess.CalledProcessError
as e:
163 raise CannotAccessUnity(
"Unable to get unity's status: %s" % str(e))
166 def _get_unity_pid():
167 status = _get_unity_status()
168 if not "start/" in status:
169 raise CannotAccessUnity(
"Unity is not in the running state.")
170 return int(status.split()[-1])
173 def _get_unity_proxy_object(pid):
174 return get_proxy_object_for_existing_process(
176 emulator_base=emulators.UnityEmulatorBase,