@@ -0,0 +1,116 @@
1+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3+# Unity Autopilot Utilities
4+# Copyright (C) 2013 Canonical
6+# This program is free software: you can redistribute it and/or modify
7+# it under the terms of the GNU General Public License as published by
8+# the Free Software Foundation, either version 3 of the License, or
9+# (at your option) any later version.
11+# This program is distributed in the hope that it will be useful,
12+# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+# GNU General Public License for more details.
16+# You should have received a copy of the GNU General Public License
17+# along with this program. If not, see <http://www.gnu.org/licenses/>.
20+from autopilot.introspection import (
21+ get_proxy_object_for_existing_process,
25+from unity8.shell.emulators import UnityEmulatorBase
26+from unity8.shell.emulators.main_window import MainWindow
29+class CannotAccessUnity(Exception):
34+ """Helper function that attempts to unlock the unity greeter.
36+ :raises RuntimeError: if the greeter attempts and fails to be unlocked.
38+ :raises RuntimeWarning: when the greeter cannot be found because it is
40+ :raises CannotAccessUnity: if unity is not introspectable or cannot be
42+ :raises CannotAccessUnity: if unity's upstart status is not "start" or the
43+ upstart job cannot be found at all.
46+ pid = _get_unity_pid()
48+ unity = get_proxy_object_for_existing_process(
50+ emulator_base=UnityEmulatorBase,
52+ main_window = MainWindow(unity)
54+ greeter = main_window.get_greeter()
56+ raise RuntimeWarning("Greeter appears to be already unlocked.")
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."
68+def restart_unity_with_testability():
69+ """Restarts (or just starts) unity8 with the testability driver loaded
71+ :raises subprocess.CalledProcessError: if unable to stop or start the
75+ status = _get_unity_status()
76+ if "start/" in status:
78+ print("Stopping unity.")
79+ subprocess.check_call([
84+ except subprocess.CalledProcessError as e:
85+ e.args += ("Failed to stop running instance of unity8", )
89+ print("Starting unity with testability.")
90+ subprocess.check_call([
94+ 'QT_LOAD_TESTABILITY=1',
96+ except subprocess.CalledProcessError as e:
97+ e.args += ("Failed to start unity8 with testability ", )
101+def _get_unity_status():
103+ return subprocess.check_output([
108+ except subprocess.CalledProcessError as e:
109+ raise CannotAccessUnity("Unable to get unity's status: %s" % str(e))
112+def _get_unity_pid():
113+ status = _get_unity_status()
114+ if not "start/" in status:
115+ raise CannotAccessUnity("Unity is not in the running state.")
116+ return int(status.split()[-1])