1
# arch-tag: cb40743b-1a3d-4c56-9647-512721976ad2
2
# Copyright (C) 2004 Canonical Ltd.
3
# Author: David Allouche <david@canonical.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Command-line back-end glue."""
24
def default_backend():
25
"""Default command-line backend.
27
:rtype: `CommandLineBackend`
31
return CommandLineBackend(
34
spawning = DelayedGuessedSpawningStrategy,
35
logger = logger.Logger())
38
class CommandLineBackend(object):
40
"""Facade for command-line back-end options."""
42
def __init__(self, command, module, spawning, logger):
43
self._command = command
45
self._spawning = spawning
49
def _get_command(self):
52
def _set_command(self, command):
54
self._command = command
58
_get_command, _set_command, None,
59
"""Name of the command line program.
63
def _get_spawning(self):
66
def _set_spawning(self, spawning):
68
self._spawning = spawning
71
spawning_strategy = property(
72
_get_spawning, _set_spawning, None,
73
"""`SpawningStrategy` factory used to create the spawner.
75
:type: factory of `SpawningStrategy`""")
77
def _get_logger(self):
80
def _set_logger(self, logger):
86
_get_logger, _set_logger, None,
87
"""Command line logger.
89
:type: `logger.Logger`""")
91
def _get_spawner(self):
92
if self._spawner is None:
96
self._spawner = self._spawning(self._command, self._logger)
100
_get_spawner, None, None,
101
"""Command line program spawner. (read-only)
103
:type: `SpawningStrategy`""")
106
class SpawningStrategy(object):
108
"""Abstract class for process spawning strategies."""
110
def status_cmd(self, args, chdir, expected):
111
raise NotImplementedError
113
def status_text_cmd(self, args, chdir, expected):
114
raise NotImplementedError
116
def sequence_cmd(self, args, chdir=None, expected=(0,)):
117
raise NotImplementedError
120
class DelayedGuessedSpawningStrategy(SpawningStrategy):
121
"""SpawningStrategy that uses Twisted if it is present in ``sys.modules``.
123
This SpawningStrategy tries to do the right thing my looking at
124
the contents of ``sys.modules``: if "twisted" or a module of that
125
package is loaded when the first spawning method is run, it will
126
use the Twisted spawning strategy. Otherwise, it will use a simple
127
fork/exec spawning strategy which does not depend on Twisted.
130
__pychecker__ = 'no-override'
132
def __init__(self, *args, **kwargs):
133
self._init_args = args
134
self._init_kwargs = kwargs
135
self._cached_guess = None
138
if self._cached_guess is not None:
139
return self._cached_guess
141
for name in sys.modules:
142
if name == 'twisted' or name.startswith('twisted.'):
144
strategy = knotted.TwistedSpawningStrategy
148
strategy = forkexec.PyArchSpawningStrategy
149
self._cached_guess = strategy(*self._init_args, **self._init_kwargs)
150
return self._cached_guess
152
def sequence_cmd(self, *args, **kwargs):
153
return self._guess().sequence_cmd(*args, **kwargs)
155
def status_cmd(self, *args, **kwargs):
156
return self._guess().status_cmd(*args, **kwargs)
158
def status_text_cmd(self, *args, **kwargs):
159
return self._guess().status_text_cmd(*args, **kwargs)