bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
329
by Martin Pool
- refactor command functions into command classes |
1 |
# Copyright (C) 2004, 2005 by Canonical Ltd
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
2 |
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
||
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
18 |
# TODO: probably should say which arguments are candidates for glob
|
19 |
# expansion on windows and do that at the command level.
|
|
20 |
||
|
1095
by Martin Pool
todo |
21 |
# TODO: Help messages for options.
|
22 |
||
23 |
# TODO: Define arguments by objects, rather than just using names.
|
|
24 |
# Those objects can specify the expected type of the argument, which
|
|
25 |
# would help with validation and shell completion.
|
|
26 |
||
|
1393.1.27
by Martin Pool
- clean up profile code and change default sort order |
27 |
# TODO: "--profile=cum", to change sort order. Is there any value in leaving
|
28 |
# the profile output behind so it can be interactively examined?
|
|
29 |
||
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
30 |
import sys |
31 |
import os |
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
32 |
from warnings import warn |
33 |
from inspect import getdoc |
|
|
1185.12.56
by Aaron Bentley
Prevented display commands from printing broken pipe errors |
34 |
import errno |
|
1185.85.22
by John Arbash Meinel
Updated cmd_inventory. Changing from having each Command request an encoded stdout to providing one before calling run() |
35 |
import codecs |
|
1
by mbp at sourcefrog
import from baz patch-364 |
36 |
|
37 |
import bzrlib |
|
|
1112
by Martin Pool
- disable standard logging to .bzr.log and stderr while running |
38 |
import bzrlib.trace |
|
1185.33.42
by Martin Pool
[patch] make --quiet a global option (robey) |
39 |
from bzrlib.trace import mutter, note, log_error, warning, be_quiet |
|
1495
by Robert Collins
Add a --create-prefix to the new push command. |
40 |
from bzrlib.errors import (BzrError, |
41 |
BzrCheckError, |
|
42 |
BzrCommandError, |
|
43 |
BzrOptionError, |
|
44 |
NotBranchError) |
|
|
1185.11.5
by John Arbash Meinel
Merged up-to-date against mainline, still broken. |
45 |
from bzrlib.revisionspec import RevisionSpec |
|
800
by Martin Pool
Merge John's import-speedup branch: |
46 |
from bzrlib import BZRDIR |
|
1185.16.41
by Martin Pool
[patch] define cli options as objects, not strings |
47 |
from bzrlib.option import Option |
|
1
by mbp at sourcefrog
import from baz patch-364 |
48 |
|
|
731
by Martin Pool
- merge plugin patch from john |
49 |
plugin_cmds = {} |
50 |
||
51 |
||
|
1492
by Robert Collins
Support decoration of commands. |
52 |
def register_command(cmd, decorate=False): |
|
731
by Martin Pool
- merge plugin patch from john |
53 |
"Utility function to help register a command"
|
54 |
global plugin_cmds |
|
55 |
k = cmd.__name__ |
|
56 |
if k.startswith("cmd_"): |
|
57 |
k_unsquished = _unsquish_command_name(k) |
|
58 |
else: |
|
59 |
k_unsquished = k |
|
60 |
if not plugin_cmds.has_key(k_unsquished): |
|
61 |
plugin_cmds[k_unsquished] = cmd |
|
|
1137
by Martin Pool
- additional trace messages for plugins |
62 |
mutter('registered plugin command %s', k_unsquished) |
|
1492
by Robert Collins
Support decoration of commands. |
63 |
if decorate and k_unsquished in builtin_command_names(): |
64 |
return _builtin_commands()[k_unsquished] |
|
65 |
elif decorate: |
|
66 |
result = plugin_cmds[k_unsquished] |
|
67 |
plugin_cmds[k_unsquished] = cmd |
|
68 |
return result |
|
|
731
by Martin Pool
- merge plugin patch from john |
69 |
else: |
70 |
log_error('Two plugins defined the same command: %r' % k) |
|
71 |
log_error('Not loading the one in %r' % sys.modules[cmd.__module__]) |
|
72 |
||
73 |
||
|
350
by Martin Pool
- refactor command aliases into command classes |
74 |
def _squish_command_name(cmd): |
75 |
return 'cmd_' + cmd.replace('-', '_') |
|
76 |
||
77 |
||
78 |
def _unsquish_command_name(cmd): |
|
79 |
assert cmd.startswith("cmd_") |
|
80 |
return cmd[4:].replace('_','-') |
|
81 |
||
|
914
by Martin Pool
- fix up breakage of 'bzr log -v' by root_id patch |
82 |
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
83 |
def _builtin_commands(): |
|
1147
by Martin Pool
- split builtin commands into separate module bzrlib.builtins; |
84 |
import bzrlib.builtins |
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
85 |
r = {} |
|
1147
by Martin Pool
- split builtin commands into separate module bzrlib.builtins; |
86 |
builtins = bzrlib.builtins.__dict__ |
87 |
for name in builtins: |
|
88 |
if name.startswith("cmd_"): |
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
89 |
real_name = _unsquish_command_name(name) |
90 |
r[real_name] = builtins[name] |
|
91 |
return r |
|
92 |
||
93 |
||
94 |
||
95 |
def builtin_command_names(): |
|
96 |
"""Return list of builtin command names.""" |
|
97 |
return _builtin_commands().keys() |
|
98 |
||
99 |
||
100 |
def plugin_command_names(): |
|
101 |
return plugin_cmds.keys() |
|
102 |
||
103 |
||
104 |
def _get_cmd_dict(plugins_override=True): |
|
105 |
"""Return name->class mapping for all commands.""" |
|
106 |
d = _builtin_commands() |
|
|
731
by Martin Pool
- merge plugin patch from john |
107 |
if plugins_override: |
108 |
d.update(plugin_cmds) |
|
|
641
by Martin Pool
- improved external-command patch from john |
109 |
return d |
|
731
by Martin Pool
- merge plugin patch from john |
110 |
|
|
641
by Martin Pool
- improved external-command patch from john |
111 |
|
|
731
by Martin Pool
- merge plugin patch from john |
112 |
def get_all_cmds(plugins_override=True): |
|
641
by Martin Pool
- improved external-command patch from john |
113 |
"""Return canonical name and class for all registered commands.""" |
|
731
by Martin Pool
- merge plugin patch from john |
114 |
for k, v in _get_cmd_dict(plugins_override=plugins_override).iteritems(): |
|
641
by Martin Pool
- improved external-command patch from john |
115 |
yield k,v |
116 |
||
117 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
118 |
def get_cmd_object(cmd_name, plugins_override=True): |
|
350
by Martin Pool
- refactor command aliases into command classes |
119 |
"""Return the canonical name and command class for a command. |
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
120 |
|
121 |
plugins_override
|
|
122 |
If true, plugin commands can override builtins.
|
|
|
350
by Martin Pool
- refactor command aliases into command classes |
123 |
"""
|
|
1163
by Martin Pool
- split ExternalCommand class into its own file |
124 |
from bzrlib.externalcommand import ExternalCommand |
125 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
126 |
cmd_name = str(cmd_name) # not unicode |
|
350
by Martin Pool
- refactor command aliases into command classes |
127 |
|
128 |
# first look up this command under the specified name
|
|
|
731
by Martin Pool
- merge plugin patch from john |
129 |
cmds = _get_cmd_dict(plugins_override=plugins_override) |
|
272
by Martin Pool
- Add command aliases |
130 |
try: |
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
131 |
return cmds[cmd_name]() |
|
272
by Martin Pool
- Add command aliases |
132 |
except KeyError: |
|
350
by Martin Pool
- refactor command aliases into command classes |
133 |
pass
|
134 |
||
135 |
# look for any command which claims this as an alias
|
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
136 |
for real_cmd_name, cmd_class in cmds.iteritems(): |
137 |
if cmd_name in cmd_class.aliases: |
|
138 |
return cmd_class() |
|
139 |
||
140 |
cmd_obj = ExternalCommand.find_command(cmd_name) |
|
141 |
if cmd_obj: |
|
142 |
return cmd_obj |
|
143 |
||
144 |
raise BzrCommandError("unknown command %r" % cmd_name) |
|
|
272
by Martin Pool
- Add command aliases |
145 |
|
|
329
by Martin Pool
- refactor command functions into command classes |
146 |
|
|
558
by Martin Pool
- All top-level classes inherit from object |
147 |
class Command(object): |
|
329
by Martin Pool
- refactor command functions into command classes |
148 |
"""Base class for commands. |
149 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
150 |
Commands are the heart of the command-line bzr interface.
|
151 |
||
152 |
The command object mostly handles the mapping of command-line
|
|
153 |
parameters into one or more bzrlib operations, and of the results
|
|
154 |
into textual output.
|
|
155 |
||
156 |
Commands normally don't have any state. All their arguments are
|
|
157 |
passed in to the run method. (Subclasses may take a different
|
|
158 |
policy if the behaviour of the instance needs to depend on e.g. a
|
|
159 |
shell plugin and not just its Python class.)
|
|
160 |
||
|
329
by Martin Pool
- refactor command functions into command classes |
161 |
The docstring for an actual command should give a single-line
|
162 |
summary, then a complete description of the command. A grammar
|
|
163 |
description will be inserted.
|
|
164 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
165 |
aliases
|
166 |
Other accepted names for this command.
|
|
167 |
||
|
329
by Martin Pool
- refactor command functions into command classes |
168 |
takes_args
|
169 |
List of argument forms, marked with whether they are optional,
|
|
170 |
repeated, etc.
|
|
171 |
||
|
1185.37.4
by Jamie Wilkinson
add some examples to the documentation for Command.takes_args |
172 |
Examples:
|
173 |
||
174 |
['to_location', 'from_branch?', 'file*']
|
|
175 |
||
176 |
'to_location' is required
|
|
177 |
'from_branch' is optional
|
|
178 |
'file' can be specified 0 or more times
|
|
179 |
||
|
329
by Martin Pool
- refactor command functions into command classes |
180 |
takes_options
|
|
1185.16.43
by Martin Pool
- clean up handling of option objects |
181 |
List of options that may be given for this command. These can
|
182 |
be either strings, referring to globally-defined options,
|
|
183 |
or option objects. Retrieve through options().
|
|
|
329
by Martin Pool
- refactor command functions into command classes |
184 |
|
185 |
hidden
|
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
186 |
If true, this command isn't advertised. This is typically
|
187 |
for commands intended for expert users.
|
|
|
1185.85.22
by John Arbash Meinel
Updated cmd_inventory. Changing from having each Command request an encoded stdout to providing one before calling run() |
188 |
|
189 |
encoding_type
|
|
190 |
Command objects will get a 'outf' attribute, which has been
|
|
191 |
setup to properly handle encoding of unicode strings.
|
|
192 |
encoding_type determines what will happen when characters cannot
|
|
193 |
be encoded
|
|
194 |
strict - abort if we cannot decode
|
|
195 |
replace - put in a bogus character (typically '?')
|
|
196 |
exact - do not encode sys.stdout
|
|
197 |
||
|
329
by Martin Pool
- refactor command functions into command classes |
198 |
"""
|
199 |
aliases = [] |
|
200 |
takes_args = [] |
|
201 |
takes_options = [] |
|
|
1185.85.22
by John Arbash Meinel
Updated cmd_inventory. Changing from having each Command request an encoded stdout to providing one before calling run() |
202 |
encoding_type = 'strict' |
|
329
by Martin Pool
- refactor command functions into command classes |
203 |
|
204 |
hidden = False |
|
205 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
206 |
def __init__(self): |
207 |
"""Construct an instance of this command.""" |
|
|
973
by Martin Pool
- various refactorings of command interpreter |
208 |
if self.__doc__ == Command.__doc__: |
209 |
warn("No help message set for %r" % self) |
|
|
329
by Martin Pool
- refactor command functions into command classes |
210 |
|
|
1185.16.43
by Martin Pool
- clean up handling of option objects |
211 |
def options(self): |
212 |
"""Return dict of valid options for this command. |
|
213 |
||
214 |
Maps from long option name to option object."""
|
|
215 |
r = dict() |
|
|
1185.16.48
by mbp at sourcefrog
- more refactoring of and tests for option parsing |
216 |
r['help'] = Option.OPTIONS['help'] |
|
1185.16.43
by Martin Pool
- clean up handling of option objects |
217 |
for o in self.takes_options: |
218 |
if not isinstance(o, Option): |
|
219 |
o = Option.OPTIONS[o] |
|
220 |
r[o.name] = o |
|
221 |
return r |
|
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
222 |
|
|
1185.85.22
by John Arbash Meinel
Updated cmd_inventory. Changing from having each Command request an encoded stdout to providing one before calling run() |
223 |
def _setup_stdout(self): |
224 |
"""Return a file linked to stdout, which has proper encoding.""" |
|
225 |
assert self.encoding_type in ['strict', 'exact', 'replace'] |
|
226 |
||
227 |
# Originally I was using self.stdout, but that looks
|
|
228 |
# *way* too much like sys.stdout
|
|
229 |
if self.encoding_type == 'exact': |
|
230 |
self.outf = sys.stdout |
|
231 |
return
|
|
232 |
||
233 |
output_encoding = getattr(sys.stdout, 'encoding', None) |
|
234 |
if not output_encoding: |
|
235 |
output_encoding = bzrlib.user_encoding |
|
236 |
mutter('encoding stdout bzrlib.user_encoding %r', output_encoding) |
|
237 |
else: |
|
238 |
mutter('encoding stdout log as sys.stdout encoding %r', output_encoding) |
|
239 |
||
240 |
# use 'replace' so that we don't abort if trying to write out
|
|
241 |
# in e.g. the default C locale.
|
|
242 |
self.outf = codecs.getwriter(output_encoding)(sys.stdout, errors=self.encoding_type) |
|
243 |
||
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
244 |
def run_argv(self, argv): |
245 |
"""Parse command line and run.""" |
|
|
1185.16.48
by mbp at sourcefrog
- more refactoring of and tests for option parsing |
246 |
args, opts = parse_args(self, argv) |
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
247 |
if 'help' in opts: # e.g. bzr add --help |
248 |
from bzrlib.help import help_on_command |
|
249 |
help_on_command(self.name()) |
|
250 |
return 0 |
|
|
1185.16.48
by mbp at sourcefrog
- more refactoring of and tests for option parsing |
251 |
# XXX: This should be handled by the parser
|
|
1185.16.43
by Martin Pool
- clean up handling of option objects |
252 |
allowed_names = self.options().keys() |
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
253 |
for oname in opts: |
|
1185.16.43
by Martin Pool
- clean up handling of option objects |
254 |
if oname not in allowed_names: |
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
255 |
raise BzrCommandError("option '--%s' is not allowed for command %r" |
256 |
% (oname, self.name())) |
|
257 |
# mix arguments and options into one dictionary
|
|
258 |
cmdargs = _match_argform(self.name(), self.takes_args, args) |
|
259 |
cmdopts = {} |
|
260 |
for k, v in opts.items(): |
|
261 |
cmdopts[k.replace('-', '_')] = v |
|
262 |
||
263 |
all_cmd_args = cmdargs.copy() |
|
264 |
all_cmd_args.update(cmdopts) |
|
265 |
||
|
1185.85.22
by John Arbash Meinel
Updated cmd_inventory. Changing from having each Command request an encoded stdout to providing one before calling run() |
266 |
self._setup_stdout() |
267 |
||
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
268 |
return self.run(**all_cmd_args) |
|
329
by Martin Pool
- refactor command functions into command classes |
269 |
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
270 |
def run(self): |
271 |
"""Actually run the command. |
|
|
329
by Martin Pool
- refactor command functions into command classes |
272 |
|
273 |
This is invoked with the options and arguments bound to
|
|
274 |
keyword parameters.
|
|
275 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
276 |
Return 0 or None if the command was successful, or a non-zero
|
277 |
shell error code if not. It's OK for this method to allow
|
|
278 |
an exception to raise up.
|
|
|
329
by Martin Pool
- refactor command functions into command classes |
279 |
"""
|
|
1147
by Martin Pool
- split builtin commands into separate module bzrlib.builtins; |
280 |
raise NotImplementedError() |
|
329
by Martin Pool
- refactor command functions into command classes |
281 |
|
282 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
283 |
def help(self): |
284 |
"""Return help message for this class.""" |
|
285 |
if self.__doc__ is Command.__doc__: |
|
286 |
return None |
|
287 |
return getdoc(self) |
|
288 |
||
289 |
def name(self): |
|
290 |
return _unsquish_command_name(self.__class__.__name__) |
|
291 |
||
292 |
||
|
493
by Martin Pool
- Merge aaron's merge command |
293 |
def parse_spec(spec): |
|
622
by Martin Pool
Updated merge patch from Aaron |
294 |
""" |
295 |
>>> parse_spec(None)
|
|
296 |
[None, None]
|
|
297 |
>>> parse_spec("./")
|
|
298 |
['./', None]
|
|
299 |
>>> parse_spec("../@")
|
|
300 |
['..', -1]
|
|
301 |
>>> parse_spec("../f/@35")
|
|
302 |
['../f', 35]
|
|
|
897
by Martin Pool
- merge john's revision-naming code |
303 |
>>> parse_spec('./@revid:john@arbash-meinel.com-20050711044610-3ca0327c6a222f67')
|
304 |
['.', 'revid:john@arbash-meinel.com-20050711044610-3ca0327c6a222f67']
|
|
|
622
by Martin Pool
Updated merge patch from Aaron |
305 |
"""
|
306 |
if spec is None: |
|
307 |
return [None, None] |
|
|
493
by Martin Pool
- Merge aaron's merge command |
308 |
if '/@' in spec: |
309 |
parsed = spec.split('/@') |
|
310 |
assert len(parsed) == 2 |
|
311 |
if parsed[1] == "": |
|
312 |
parsed[1] = -1 |
|
313 |
else: |
|
|
897
by Martin Pool
- merge john's revision-naming code |
314 |
try: |
315 |
parsed[1] = int(parsed[1]) |
|
316 |
except ValueError: |
|
317 |
pass # We can allow stuff like ./@revid:blahblahblah |
|
318 |
else: |
|
319 |
assert parsed[1] >=0 |
|
|
493
by Martin Pool
- Merge aaron's merge command |
320 |
else: |
321 |
parsed = [spec, None] |
|
322 |
return parsed |
|
323 |
||
|
1185.16.48
by mbp at sourcefrog
- more refactoring of and tests for option parsing |
324 |
def parse_args(command, argv): |
|
1
by mbp at sourcefrog
import from baz patch-364 |
325 |
"""Parse command line. |
326 |
|
|
327 |
Arguments and options are parsed at this level before being passed
|
|
328 |
down to specific command handlers. This routine knows, from a
|
|
329 |
lookup table, something about the available options, what optargs
|
|
330 |
they take, and which commands will accept them.
|
|
331 |
"""
|
|
|
1185.16.49
by mbp at sourcefrog
- more refactoring and tests of commandline |
332 |
# TODO: chop up this beast; make it a method of the Command
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
333 |
args = [] |
334 |
opts = {} |
|
335 |
||
|
1185.16.48
by mbp at sourcefrog
- more refactoring of and tests for option parsing |
336 |
cmd_options = command.options() |
|
1144
by Martin Pool
- accept -- to terminate options |
337 |
argsover = False |
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
338 |
while argv: |
339 |
a = argv.pop(0) |
|
|
1185.16.49
by mbp at sourcefrog
- more refactoring and tests of commandline |
340 |
if argsover: |
341 |
args.append(a) |
|
342 |
continue
|
|
343 |
elif a == '--': |
|
344 |
# We've received a standalone -- No more flags
|
|
345 |
argsover = True |
|
346 |
continue
|
|
347 |
if a[0] == '-': |
|
|
264
by Martin Pool
parse_args: option names must be ascii |
348 |
# option names must not be unicode
|
349 |
a = str(a) |
|
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
350 |
optarg = None |
|
1
by mbp at sourcefrog
import from baz patch-364 |
351 |
if a[1] == '-': |
|
1185.31.4
by John Arbash Meinel
Fixing mutter() calls to not have to do string processing. |
352 |
mutter(" got option %r", a) |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
353 |
if '=' in a: |
354 |
optname, optarg = a[2:].split('=', 1) |
|
355 |
else: |
|
356 |
optname = a[2:] |
|
|
1185.16.48
by mbp at sourcefrog
- more refactoring of and tests for option parsing |
357 |
if optname not in cmd_options: |
|
1495
by Robert Collins
Add a --create-prefix to the new push command. |
358 |
raise BzrOptionError('unknown long option %r for command %s' |
359 |
% (a, command.name())) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
360 |
else: |
361 |
shortopt = a[1:] |
|
|
1185.16.41
by Martin Pool
[patch] define cli options as objects, not strings |
362 |
if shortopt in Option.SHORT_OPTIONS: |
|
683
by Martin Pool
- short option stacking patch from John A Meinel |
363 |
# Multi-character options must have a space to delimit
|
364 |
# their value
|
|
|
1185.16.44
by Martin Pool
doc |
365 |
# ^^^ what does this mean? mbp 20051014
|
|
1185.16.41
by Martin Pool
[patch] define cli options as objects, not strings |
366 |
optname = Option.SHORT_OPTIONS[shortopt].name |
|
683
by Martin Pool
- short option stacking patch from John A Meinel |
367 |
else: |
368 |
# Single character short options, can be chained,
|
|
369 |
# and have their value appended to their name
|
|
370 |
shortopt = a[1:2] |
|
|
1185.16.41
by Martin Pool
[patch] define cli options as objects, not strings |
371 |
if shortopt not in Option.SHORT_OPTIONS: |
|
683
by Martin Pool
- short option stacking patch from John A Meinel |
372 |
# We didn't find the multi-character name, and we
|
373 |
# didn't find the single char name
|
|
|
694
by Martin Pool
- weed out all remaining calls to bailout() and remove the function |
374 |
raise BzrError('unknown short option %r' % a) |
|
1185.16.41
by Martin Pool
[patch] define cli options as objects, not strings |
375 |
optname = Option.SHORT_OPTIONS[shortopt].name |
|
683
by Martin Pool
- short option stacking patch from John A Meinel |
376 |
|
377 |
if a[2:]: |
|
378 |
# There are extra things on this option
|
|
379 |
# see if it is the value, or if it is another
|
|
380 |
# short option
|
|
|
1185.16.41
by Martin Pool
[patch] define cli options as objects, not strings |
381 |
optargfn = Option.OPTIONS[optname].type |
|
683
by Martin Pool
- short option stacking patch from John A Meinel |
382 |
if optargfn is None: |
383 |
# This option does not take an argument, so the
|
|
384 |
# next entry is another short option, pack it back
|
|
385 |
# into the list
|
|
386 |
argv.insert(0, '-' + a[2:]) |
|
387 |
else: |
|
388 |
# This option takes an argument, so pack it
|
|
389 |
# into the array
|
|
390 |
optarg = a[2:] |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
391 |
|
|
1185.35.24
by Aaron Bentley
Fixed handling of short options not accepted by the command |
392 |
if optname not in cmd_options: |
393 |
raise BzrOptionError('unknown short option %r for command' |
|
394 |
' %s' % (shortopt, command.name())) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
395 |
if optname in opts: |
396 |
# XXX: Do we ever want to support this, e.g. for -r?
|
|
|
694
by Martin Pool
- weed out all remaining calls to bailout() and remove the function |
397 |
raise BzrError('repeated option %r' % a) |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
398 |
|
|
1185.16.48
by mbp at sourcefrog
- more refactoring of and tests for option parsing |
399 |
option_obj = cmd_options[optname] |
400 |
optargfn = option_obj.type |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
401 |
if optargfn: |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
402 |
if optarg == None: |
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
403 |
if not argv: |
|
694
by Martin Pool
- weed out all remaining calls to bailout() and remove the function |
404 |
raise BzrError('option %r needs an argument' % a) |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
405 |
else: |
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
406 |
optarg = argv.pop(0) |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
407 |
opts[optname] = optargfn(optarg) |
|
1
by mbp at sourcefrog
import from baz patch-364 |
408 |
else: |
|
17
by mbp at sourcefrog
allow --option=ARG syntax |
409 |
if optarg != None: |
|
694
by Martin Pool
- weed out all remaining calls to bailout() and remove the function |
410 |
raise BzrError('option %r takes no argument' % optname) |
|
1
by mbp at sourcefrog
import from baz patch-364 |
411 |
opts[optname] = True |
412 |
else: |
|
413 |
args.append(a) |
|
414 |
return args, opts |
|
415 |
||
416 |
||
|
329
by Martin Pool
- refactor command functions into command classes |
417 |
def _match_argform(cmd, takes_args, args): |
|
1
by mbp at sourcefrog
import from baz patch-364 |
418 |
argdict = {} |
|
26
by mbp at sourcefrog
fix StopIteration error on python2.3(?) |
419 |
|
|
329
by Martin Pool
- refactor command functions into command classes |
420 |
# step through args and takes_args, allowing appropriate 0-many matches
|
421 |
for ap in takes_args: |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
422 |
argname = ap[:-1] |
423 |
if ap[-1] == '?': |
|
|
62
by mbp at sourcefrog
- new find_branch_root function; based on suggestion from aaron |
424 |
if args: |
425 |
argdict[argname] = args.pop(0) |
|
|
196
by mbp at sourcefrog
selected-file diff |
426 |
elif ap[-1] == '*': # all remaining arguments |
427 |
if args: |
|
428 |
argdict[argname + '_list'] = args[:] |
|
429 |
args = [] |
|
430 |
else: |
|
431 |
argdict[argname + '_list'] = None |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
432 |
elif ap[-1] == '+': |
433 |
if not args: |
|
|
329
by Martin Pool
- refactor command functions into command classes |
434 |
raise BzrCommandError("command %r needs one or more %s" |
|
1
by mbp at sourcefrog
import from baz patch-364 |
435 |
% (cmd, argname.upper())) |
436 |
else: |
|
437 |
argdict[argname + '_list'] = args[:] |
|
438 |
args = [] |
|
|
160
by mbp at sourcefrog
- basic support for moving files to different directories - have not done support for renaming them yet, but should be straightforward - some tests, but many cases are not handled yet i think |
439 |
elif ap[-1] == '$': # all but one |
440 |
if len(args) < 2: |
|
|
329
by Martin Pool
- refactor command functions into command classes |
441 |
raise BzrCommandError("command %r needs one or more %s" |
|
160
by mbp at sourcefrog
- basic support for moving files to different directories - have not done support for renaming them yet, but should be straightforward - some tests, but many cases are not handled yet i think |
442 |
% (cmd, argname.upper())) |
443 |
argdict[argname + '_list'] = args[:-1] |
|
444 |
args[:-1] = [] |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
445 |
else: |
446 |
# just a plain arg
|
|
447 |
argname = ap |
|
448 |
if not args: |
|
|
329
by Martin Pool
- refactor command functions into command classes |
449 |
raise BzrCommandError("command %r requires argument %s" |
|
1
by mbp at sourcefrog
import from baz patch-364 |
450 |
% (cmd, argname.upper())) |
451 |
else: |
|
452 |
argdict[argname] = args.pop(0) |
|
453 |
||
454 |
if args: |
|
|
329
by Martin Pool
- refactor command functions into command classes |
455 |
raise BzrCommandError("extra argument to command %s: %s" |
456 |
% (cmd, args[0])) |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
457 |
|
458 |
return argdict |
|
459 |
||
460 |
||
461 |
||
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
462 |
def apply_profiled(the_callable, *args, **kwargs): |
463 |
import hotshot |
|
464 |
import tempfile |
|
|
1393.1.27
by Martin Pool
- clean up profile code and change default sort order |
465 |
import hotshot.stats |
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
466 |
pffileno, pfname = tempfile.mkstemp() |
467 |
try: |
|
468 |
prof = hotshot.Profile(pfname) |
|
469 |
try: |
|
470 |
ret = prof.runcall(the_callable, *args, **kwargs) or 0 |
|
471 |
finally: |
|
472 |
prof.close() |
|
473 |
stats = hotshot.stats.load(pfname) |
|
|
1393.1.27
by Martin Pool
- clean up profile code and change default sort order |
474 |
stats.strip_dirs() |
475 |
stats.sort_stats('cum') # 'time' |
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
476 |
## XXX: Might like to write to stderr or the trace file instead but
|
477 |
## print_stats seems hardcoded to stdout
|
|
478 |
stats.print_stats(20) |
|
479 |
return ret |
|
480 |
finally: |
|
481 |
os.close(pffileno) |
|
482 |
os.remove(pfname) |
|
483 |
||
484 |
||
|
1185.33.85
by Martin Pool
New --lsprof option from Denys Duchier |
485 |
def apply_lsprofiled(the_callable, *args, **kwargs): |
486 |
from bzrlib.lsprof import profile |
|
487 |
ret,stats = profile(the_callable,*args,**kwargs) |
|
488 |
stats.sort() |
|
489 |
stats.pprint() |
|
490 |
return ret |
|
491 |
||
|
1
by mbp at sourcefrog
import from baz patch-364 |
492 |
def run_bzr(argv): |
493 |
"""Execute a command. |
|
494 |
||
495 |
This is similar to main(), but without all the trappings for
|
|
|
245
by mbp at sourcefrog
- control files always in utf-8-unix format |
496 |
logging and error handling.
|
|
973
by Martin Pool
- various refactorings of command interpreter |
497 |
|
498 |
argv
|
|
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
499 |
The command-line arguments, without the program name from argv[0]
|
|
1185.85.4
by John Arbash Meinel
currently broken, trying to fix things up. |
500 |
These should already be decoded. All library/test code calling
|
501 |
run_bzr should be passing valid strings (don't need decoding).
|
|
|
973
by Martin Pool
- various refactorings of command interpreter |
502 |
|
503 |
Returns a command status or raises an exception.
|
|
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
504 |
|
505 |
Special master options: these must come before the command because
|
|
506 |
they control how the command is interpreted.
|
|
507 |
||
508 |
--no-plugins
|
|
509 |
Do not load plugin modules at all
|
|
510 |
||
511 |
--builtin
|
|
512 |
Only use builtin commands. (Plugins are still allowed to change
|
|
513 |
other behaviour.)
|
|
514 |
||
515 |
--profile
|
|
|
1185.33.85
by Martin Pool
New --lsprof option from Denys Duchier |
516 |
Run under the Python hotshot profiler.
|
517 |
||
518 |
--lsprof
|
|
519 |
Run under the Python lsprof profiler.
|
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
520 |
"""
|
|
1185.85.4
by John Arbash Meinel
currently broken, trying to fix things up. |
521 |
argv = list(argv) |
|
907.1.21
by John Arbash Meinel
Adding http transport as a valid transport protocol. |
522 |
|
|
1185.33.85
by Martin Pool
New --lsprof option from Denys Duchier |
523 |
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = False |
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
524 |
|
525 |
# --no-plugins is handled specially at a very early stage. We need
|
|
526 |
# to load plugins before doing other command parsing so that they
|
|
527 |
# can override commands, but this needs to happen first.
|
|
528 |
||
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
529 |
for a in argv: |
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
530 |
if a == '--profile': |
531 |
opt_profile = True |
|
|
1185.33.85
by Martin Pool
New --lsprof option from Denys Duchier |
532 |
elif a == '--lsprof': |
533 |
opt_lsprof = True |
|
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
534 |
elif a == '--no-plugins': |
535 |
opt_no_plugins = True |
|
536 |
elif a == '--builtin': |
|
537 |
opt_builtin = True |
|
|
1185.33.42
by Martin Pool
[patch] make --quiet a global option (robey) |
538 |
elif a in ('--quiet', '-q'): |
539 |
be_quiet() |
|
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
540 |
else: |
|
1185.33.42
by Martin Pool
[patch] make --quiet a global option (robey) |
541 |
continue
|
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
542 |
argv.remove(a) |
543 |
||
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
544 |
if (not argv) or (argv[0] == '--help'): |
545 |
from bzrlib.help import help |
|
546 |
if len(argv) > 1: |
|
547 |
help(argv[1]) |
|
548 |
else: |
|
549 |
help() |
|
550 |
return 0 |
|
551 |
||
552 |
if argv[0] == '--version': |
|
553 |
from bzrlib.builtins import show_version |
|
554 |
show_version() |
|
555 |
return 0 |
|
556 |
||
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
557 |
if not opt_no_plugins: |
|
973
by Martin Pool
- various refactorings of command interpreter |
558 |
from bzrlib.plugin import load_plugins |
559 |
load_plugins() |
|
560 |
||
|
1165
by Martin Pool
- move parsing of argv into arguments and options into Command.run_argv |
561 |
cmd = str(argv.pop(0)) |
|
974.1.26
by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472 |
562 |
|
|
1162
by Martin Pool
- change Command infrastructure to use (mostly stateless) objects to |
563 |
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin) |
|
1
by mbp at sourcefrog
import from baz patch-364 |
564 |
|
|
1185.33.42
by Martin Pool
[patch] make --quiet a global option (robey) |
565 |
try: |
|
1185.33.85
by Martin Pool
New --lsprof option from Denys Duchier |
566 |
if opt_lsprof: |
567 |
ret = apply_lsprofiled(cmd_obj.run_argv, argv) |
|
568 |
elif opt_profile: |
|
|
1185.33.42
by Martin Pool
[patch] make --quiet a global option (robey) |
569 |
ret = apply_profiled(cmd_obj.run_argv, argv) |
570 |
else: |
|
571 |
ret = cmd_obj.run_argv(argv) |
|
572 |
return ret or 0 |
|
573 |
finally: |
|
574 |
# reset, in case we may do other commands later within the same process
|
|
575 |
be_quiet(False) |
|
|
267
by Martin Pool
- better reporting of errors |
576 |
|
|
1185.12.56
by Aaron Bentley
Prevented display commands from printing broken pipe errors |
577 |
def display_command(func): |
|
1185.33.18
by Martin Pool
[patch] handle bad IOError subclass raised by urlopen |
578 |
"""Decorator that suppresses pipe/interrupt errors.""" |
|
1185.12.56
by Aaron Bentley
Prevented display commands from printing broken pipe errors |
579 |
def ignore_pipe(*args, **kwargs): |
580 |
try: |
|
|
1185.35.22
by Aaron Bentley
Handled more pipe errors for display commands. |
581 |
result = func(*args, **kwargs) |
582 |
sys.stdout.flush() |
|
583 |
return result |
|
|
1185.12.56
by Aaron Bentley
Prevented display commands from printing broken pipe errors |
584 |
except IOError, e: |
|
1185.33.18
by Martin Pool
[patch] handle bad IOError subclass raised by urlopen |
585 |
if not hasattr(e, 'errno'): |
586 |
raise
|
|
|
1185.12.56
by Aaron Bentley
Prevented display commands from printing broken pipe errors |
587 |
if e.errno != errno.EPIPE: |
588 |
raise
|
|
|
1185.33.18
by Martin Pool
[patch] handle bad IOError subclass raised by urlopen |
589 |
pass
|
|
1185.12.69
by Aaron Bentley
Ignored ^C in display commands |
590 |
except KeyboardInterrupt: |
|
1490
by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1. |
591 |
pass
|
|
1185.12.56
by Aaron Bentley
Prevented display commands from printing broken pipe errors |
592 |
return ignore_pipe |
|
267
by Martin Pool
- better reporting of errors |
593 |
|
|
1185.43.6
by Martin Pool
Enable logging early enough to save argv |
594 |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
595 |
def main(argv): |
|
1104
by Martin Pool
- Add a simple UIFactory |
596 |
import bzrlib.ui |
|
1185.49.21
by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms. |
597 |
from bzrlib.ui.text import TextUIFactory |
|
1185.43.8
by Martin Pool
Don't enable default logging twice |
598 |
## bzrlib.trace.enable_default_logging()
|
|
1111
by Martin Pool
- add functions to enable and disable default logging, so that we can |
599 |
bzrlib.trace.log_startup(argv) |
|
1185.49.21
by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms. |
600 |
bzrlib.ui.ui_factory = TextUIFactory() |
|
1185.85.4
by John Arbash Meinel
currently broken, trying to fix things up. |
601 |
|
602 |
argv = [a.decode(bzrlib.user_encoding) for a in argv[1:]] |
|
603 |
ret = run_bzr_catch_errors(argv) |
|
|
1185.43.6
by Martin Pool
Enable logging early enough to save argv |
604 |
mutter("return code %d", ret) |
605 |
return ret |
|
|
1185.3.19
by Martin Pool
- split out commandline error reporting for ease of testing |
606 |
|
607 |
||
608 |
def run_bzr_catch_errors(argv): |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
609 |
try: |
|
260
by Martin Pool
- remove atexit() dependency for writing out execution times |
610 |
try: |
|
1393.1.64
by Martin Pool
- improved display of some errors, including NotBranchError |
611 |
return run_bzr(argv) |
612 |
finally: |
|
613 |
# do this here inside the exception wrappers to catch EPIPE
|
|
614 |
sys.stdout.flush() |
|
|
1097
by Martin Pool
- send trace messages out through python logging module |
615 |
except Exception, e: |
|
1185.33.19
by Martin Pool
Run postmortem debugger if BZR_PDB is set |
616 |
# used to handle AssertionError and KeyboardInterrupt
|
617 |
# specially here, but hopefully they're handled ok by the logger now
|
|
|
1097
by Martin Pool
- send trace messages out through python logging module |
618 |
import errno |
619 |
if (isinstance(e, IOError) |
|
620 |
and hasattr(e, 'errno') |
|
621 |
and e.errno == errno.EPIPE): |
|
622 |
bzrlib.trace.note('broken pipe') |
|
|
1185.35.21
by Aaron Bentley
Changed error status to 3 |
623 |
return 3 |
|
1097
by Martin Pool
- send trace messages out through python logging module |
624 |
else: |
|
1185.33.20
by Martin Pool
Really enter the debugger on failure |
625 |
bzrlib.trace.log_exception() |
|
1185.33.19
by Martin Pool
Run postmortem debugger if BZR_PDB is set |
626 |
if os.environ.get('BZR_PDB'): |
|
1185.33.20
by Martin Pool
Really enter the debugger on failure |
627 |
print '**** entering debugger' |
|
1185.33.19
by Martin Pool
Run postmortem debugger if BZR_PDB is set |
628 |
import pdb |
|
1185.33.20
by Martin Pool
Really enter the debugger on failure |
629 |
pdb.post_mortem(sys.exc_traceback) |
|
1185.35.21
by Aaron Bentley
Changed error status to 3 |
630 |
return 3 |
|
1
by mbp at sourcefrog
import from baz patch-364 |
631 |
|
632 |
if __name__ == '__main__': |
|
633 |
sys.exit(main(sys.argv)) |