~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/arch/backends/commandline.py

  • Committer: Robert Collins
  • Date: 2005-09-08 11:21:38 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050908112138-033369d3cacacb55
test and deliver basic pending-merges into bzr so that merging is recorded

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# arch-tag: cb40743b-1a3d-4c56-9647-512721976ad2
 
2
# Copyright (C) 2004 Canonical Ltd.
 
3
#               Author: David Allouche <david@canonical.com>
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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
 
18
 
 
19
"""Command-line back-end glue."""
 
20
 
 
21
import sys
 
22
 
 
23
 
 
24
def default_backend():
 
25
    """Default command-line backend.
 
26
 
 
27
    :rtype: `CommandLineBackend`
 
28
    """
 
29
    import tla
 
30
    import logger
 
31
    return CommandLineBackend(
 
32
        command = 'tla',
 
33
        module = tla,
 
34
        spawning = DelayedGuessedSpawningStrategy,
 
35
        logger = logger.Logger())
 
36
 
 
37
 
 
38
class CommandLineBackend(object):
 
39
 
 
40
    """Facade for command-line back-end options."""
 
41
 
 
42
    def __init__(self, command, module, spawning, logger):
 
43
        self._command = command
 
44
        self._module = module
 
45
        self._spawning = spawning
 
46
        self._logger = logger
 
47
        self._spawner = None
 
48
 
 
49
    def _get_command(self):
 
50
        return self._command
 
51
 
 
52
    def _set_command(self, command):
 
53
        assert command
 
54
        self._command = command
 
55
        self._spawner = None
 
56
 
 
57
    command = property(
 
58
        _get_command, _set_command, None,
 
59
        """Name of the command line program.
 
60
 
 
61
        :type: str""")
 
62
 
 
63
    def _get_spawning(self):
 
64
        return self._spawning
 
65
 
 
66
    def _set_spawning(self, spawning):
 
67
        assert spawning
 
68
        self._spawning = spawning
 
69
        self._spawner = None
 
70
 
 
71
    spawning_strategy = property(
 
72
        _get_spawning, _set_spawning, None,
 
73
        """`SpawningStrategy` factory used to create the spawner.
 
74
 
 
75
        :type: factory of `SpawningStrategy`""")
 
76
 
 
77
    def _get_logger(self):
 
78
        return self._logger
 
79
 
 
80
    def _set_logger(self, logger):
 
81
        assert logger
 
82
        self._logger = logger
 
83
        self._spawner = None
 
84
 
 
85
    logger = property(
 
86
        _get_logger, _set_logger, None,
 
87
        """Command line logger.
 
88
 
 
89
        :type: `logger.Logger`""")
 
90
 
 
91
    def _get_spawner(self):
 
92
        if self._spawner is None:
 
93
            assert self._spawning
 
94
            assert self._command
 
95
            assert self._logger
 
96
            self._spawner = self._spawning(self._command, self._logger)
 
97
        return self._spawner
 
98
 
 
99
    spawner = property(
 
100
        _get_spawner, None, None,
 
101
        """Command line program spawner. (read-only)
 
102
 
 
103
        :type: `SpawningStrategy`""")
 
104
 
 
105
 
 
106
class SpawningStrategy(object):
 
107
 
 
108
    """Abstract class for process spawning strategies."""
 
109
 
 
110
    def status_cmd(self, args, chdir, expected):
 
111
        raise NotImplementedError
 
112
 
 
113
    def status_text_cmd(self, args, chdir, expected):
 
114
        raise NotImplementedError
 
115
 
 
116
    def sequence_cmd(self, args, chdir=None, expected=(0,)):
 
117
        raise NotImplementedError
 
118
 
 
119
 
 
120
class DelayedGuessedSpawningStrategy(SpawningStrategy):
 
121
    """SpawningStrategy that uses Twisted if it is present in ``sys.modules``.
 
122
 
 
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.
 
128
    """
 
129
 
 
130
    __pychecker__ = 'no-override'
 
131
 
 
132
    def __init__(self, *args, **kwargs):
 
133
        self._init_args = args
 
134
        self._init_kwargs = kwargs
 
135
        self._cached_guess = None
 
136
 
 
137
    def _guess(self):
 
138
        if self._cached_guess is not None:
 
139
            return self._cached_guess
 
140
        strategy = None
 
141
        for name in sys.modules:
 
142
            if name == 'twisted' or name.startswith('twisted.'):
 
143
                import knotted
 
144
                strategy = knotted.TwistedSpawningStrategy
 
145
                break
 
146
        else:
 
147
            import forkexec
 
148
            strategy = forkexec.PyArchSpawningStrategy
 
149
        self._cached_guess = strategy(*self._init_args, **self._init_kwargs)
 
150
        return self._cached_guess
 
151
 
 
152
    def sequence_cmd(self, *args, **kwargs):
 
153
        return self._guess().sequence_cmd(*args, **kwargs)
 
154
 
 
155
    def status_cmd(self, *args, **kwargs):
 
156
        return self._guess().status_cmd(*args, **kwargs)
 
157
 
 
158
    def status_text_cmd(self, *args, **kwargs):
 
159
        return self._guess().status_text_cmd(*args, **kwargs)