1
# -*- coding: utf-8 -*-
3
# Copyright 2012 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
# In addition, as a special exception, the copyright holders give
18
# permission to link the code of portions of this program with the
19
# OpenSSL library under certain conditions as described in each
20
# individual source file, and distribute linked combinations
22
# You must obey the GNU General Public License in all respects
23
# for all of the code used other than OpenSSL. If you modify
24
# file(s) with this exception, you may extend this exception to your
25
# version of the file(s), but you are not obligated to do so. If you
26
# do not wish to do so, delete this exception statement from your
27
# version. If you delete this exception statement from all source
28
# files in the program, then also delete it here.
29
"""Utility to spawn another program from a mainloop."""
34
from twisted.internet import utils
36
from ubuntu_sso.logger import setup_logging
39
logger = setup_logging("ubuntu_sso.utils.runner.tx")
41
NO_SUCH_FILE_OR_DIR = 'OSError: [Errno 2]'
45
if sys.platform == 'win32':
49
def spawn_program(args, reply_handler, error_handler):
50
"""Spawn the program specified by 'args' using the twisted reactor.
52
When the program finishes, 'reply_handler' will be called with a single
53
argument that will be the porgram status code.
55
If there is an error, error_handler will be called with an instance of
60
def child_watch((stdout, stderr, exit_code)):
61
"""Handle child termination."""
63
logger.debug('Returned stdout is (exit code was %r): %r',
66
logger.warning('Returned stderr is (exit code was %r): %r',
69
if OSError.__name__ in stderr:
70
failed_to_start = NO_SUCH_FILE_OR_DIR in stderr
71
error_handler(msg=stderr, failed_to_start=failed_to_start)
73
reply_handler(exit_code)
75
def handle_error(failure):
76
"""Handle error when spawning the process."""
77
error_handler(msg=failure.getErrorMessage())
85
if isinstance(arg, unicode):
86
arg = arg.encode('utf-8')
87
if not isinstance(arg, basestring):
89
bytes_args.append(arg)
91
if program and not os.access(program, os.X_OK):
92
# handle searching the executable in the PATH, since
93
# twisted will not solve that for us :-/
94
paths = os.environ['PATH'].split(os.pathsep)
96
target = os.path.join(path, program)
97
if not target.endswith(EXE_EXT):
99
if os.access(target, os.X_OK):
104
d = utils.getProcessOutputAndValue(program, bytes_args, env=os.environ)
106
error_handler(msg=e, failed_to_start=True)
108
error_handler(msg=e, failed_to_start=False)
110
logger.debug('Spawning the program %r with the twisted reactor '
111
'(returned deferred is %r).', repr(args), d)
112
d.addCallback(child_watch)
113
d.addErrback(handle_error)