@@ -0,0 +1,58 @@
1+import subprocess
2+
3+from autopilot.introspection import (
4+ get_proxy_object_for_existing_process,
5+ ProcessSearchError,
6+)
7+from unity8.shell.emulators.main_window import MainWindow
8+from unity8.shell.emulators import UnityEmulatorBase
9+
10+
11+class CannotAccessUnity(Exception):
12+ pass
13+
14+
15+def unlock_unity():
16+ """Helper function that attempts to unlock the unity greeter.
17+
18+ :raises: RuntimeError if the greeter attempts and fails to be unlocked.
19+
20+ :raises: RuntimeWarning when the greeter cannot be found because it is
21+ already unlocked.
22+ :raises: CannotAccessUnity if unity is not introspectable or cannot be
23+ found on dbus.
24+ :raises: CannotAccessUnity if unity's upstart status is not "start" or the
25+ upstart job cannot be found at all.
26+
27+ """
28+ pid = _get_unity_pid()
29+ try:
30+ unity = get_proxy_object_for_existing_process(
31+ pid=pid,
32+ emulator_base=UnityEmulatorBase,
33+ )
34+ main_window = MainWindow(unity)
35+
36+ greeter = main_window.get_greeter()
37+ if greeter is None:
38+ raise RuntimeWarning("Greeter appears to be already unlocked.")
39+ greeter.swipe()
40+ except ProcessSearchError as e:
41+ raise CannotAccessUnity(
42+ "Cannot introspect unity, make sure that it has been started "
43+ "with 'testability' (%s)" % e.message
44+ )
45+
46+
47+def _get_unity_pid():
48+ try:
49+ status = subprocess.check_output([
50+ 'initctl',
51+ 'status',
52+ 'unity8'
53+ ])
54+ if not "start/" in status:
55+ raise CannotAccessUnity("Unity is not in the running state.")
56+ return int(status.split()[-1])
57+ except subprocess.CalledProcessError as e:
58+ raise CannotAccessUnity("Unable to get unity's status: %s" % e.message)