/usr/lib/python2.7/dist-packages/autopilot/application/_launcher.py is in python-autopilot 1.4.1+17.04.20170305-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2013 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""Base module for application launchers."""
import fixtures
from gi.repository import GLib
import json
import logging
import os
import psutil
import six
import subprocess
import signal
from testtools.content import content_from_file, text_content
from autopilot._timeout import Timeout
from autopilot.utilities import _raise_on_unknown_kwargs
from autopilot.application._environment import (
GtkApplicationEnvironment,
QtApplicationEnvironment,
)
_logger = logging.getLogger(__name__)
class ApplicationLauncher(fixtures.Fixture):
"""A class that knows how to launch an application with a certain type of
introspection enabled.
"""
def __init__(self, case_addDetail):
self.case_addDetail = case_addDetail
super(ApplicationLauncher, self).__init__()
def launch(self, *arguments):
raise NotImplementedError("Sub-classes must implement this method.")
class NormalApplicationLauncher(ApplicationLauncher):
def __init__(self, case_addDetail, **kwargs):
super(NormalApplicationLauncher, self).__init__(case_addDetail)
self.app_type = kwargs.pop('app_type', None)
self.cwd = kwargs.pop('launch_dir', None)
self.capture_output = kwargs.pop('capture_output', True)
self.dbus_bus = kwargs.pop('dbus_bus', 'session')
self.emulator_base = kwargs.pop('emulator_base', None)
_raise_on_unknown_kwargs(kwargs)
def launch(self, application, *arguments):
app_path = _get_application_path(application)
app_path, arguments = self._setup_environment(app_path, *arguments)
self.process = self._launch_application_process(app_path, *arguments)
return self.process.pid
def _setup_environment(self, app_path, *arguments):
app_env = self.useFixture(
_get_application_environment(self.app_type, app_path)
)
return app_env.prepare_environment(
app_path,
list(arguments),
)
def _launch_application_process(self, app_path, *arguments):
process = launch_process(
app_path,
arguments,
self.capture_output,
cwd=self.cwd,
)
self.addCleanup(self._kill_process_and_attach_logs, process)
return process
def _kill_process_and_attach_logs(self, process):
stdout, stderr, return_code = _kill_process(process)
self.case_addDetail(
'process-return-code',
text_content(str(return_code))
)
self.case_addDetail(
'process-stdout',
text_content(stdout)
)
self.case_addDetail(
'process-stderr',
text_content(stderr)
)
def launch_process(application, args, capture_output=False, **kwargs):
"""Launch an autopilot-enabled process and return the process object."""
commandline = [application]
commandline.extend(args)
_logger.info("Launching process: %r", commandline)
cap_mode = None
if capture_output:
cap_mode = subprocess.PIPE
process = subprocess.Popen(
commandline,
stdin=subprocess.PIPE,
stdout=cap_mode,
stderr=cap_mode,
close_fds=True,
preexec_fn=os.setsid,
universal_newlines=True,
**kwargs
)
return process
def _get_application_environment(app_type=None, app_path=None):
if app_type is None and app_path is None:
raise ValueError("Must specify either app_type or app_path.")
try:
if app_type is not None:
return _get_app_env_from_string_hint(app_type)
else:
return get_application_launcher_wrapper(app_path)
except (RuntimeError, ValueError) as e:
_logger.error(str(e))
raise RuntimeError(
"Autopilot could not determine the correct introspection type "
"to use. You can specify this by providing app_type."
)
def get_application_launcher_wrapper(app_path):
"""Return an instance of :class:`ApplicationLauncher` that knows how to
launch the application at 'app_path'.
"""
# TODO: this is a teeny bit hacky - we call ldd to check whether this
# application links to certain library. We're assuming that linking to
# libQt* or libGtk* means the application is introspectable. This excludes
# any non-dynamically linked executables, which we may need to fix further
# down the line.
try:
ldd_output = subprocess.check_output(
["ldd", app_path],
universal_newlines=True
).strip().lower()
except subprocess.CalledProcessError as e:
raise RuntimeError(str(e))
if 'libqtcore' in ldd_output or 'libqt5core' in ldd_output:
return QtApplicationEnvironment()
elif 'libgtk' in ldd_output:
return GtkApplicationEnvironment()
return None
def _get_application_path(application):
try:
return subprocess.check_output(
['which', application],
universal_newlines=True
).strip()
except subprocess.CalledProcessError as e:
raise ValueError(
"Unable to find path for application {app}: {reason}"
.format(app=application, reason=str(e))
)
def _get_app_env_from_string_hint(hint):
lower_hint = hint.lower()
if lower_hint == 'qt':
return QtApplicationEnvironment()
elif lower_hint == 'gtk':
return GtkApplicationEnvironment()
raise ValueError("Unknown hint string: {hint}".format(hint=hint))
def _kill_process(process):
"""Kill the process, and return the stdout, stderr and return code."""
stdout_parts = []
stderr_parts = []
_logger.info("waiting for process to exit.")
_attempt_kill_pid(process.pid)
for _ in Timeout.default():
tmp_out, tmp_err = process.communicate()
if isinstance(tmp_out, six.binary_type):
tmp_out = tmp_out.decode('utf-8', errors='replace')
if isinstance(tmp_err, six.binary_type):
tmp_err = tmp_err.decode('utf-8', errors='replace')
stdout_parts.append(tmp_out)
stderr_parts.append(tmp_err)
if not _is_process_running(process.pid):
break
else:
_logger.info(
"Killing process group, since it hasn't exited after "
"10 seconds."
)
_attempt_kill_pid(process.pid, signal.SIGKILL)
return u''.join(stdout_parts), u''.join(stderr_parts), process.returncode
def _attempt_kill_pid(pid, sig=signal.SIGTERM):
try:
_logger.info("Killing process %d", pid)
os.killpg(pid, sig)
except OSError:
_logger.info("Appears process has already exited.")
def _is_process_running(pid):
return psutil.pid_exists(pid)
|