~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

Nathaniel McCallums patch for urandom friendliness on aix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
# would help with validation and shell completion.
26
26
 
27
27
 
28
 
# TODO: Help messages for options.
29
 
 
30
 
# TODO: Define arguments by objects, rather than just using names.
31
 
# Those objects can specify the expected type of the argument, which
32
 
# would help with validation and shell completion.
33
 
 
34
 
 
35
28
 
36
29
import sys
37
30
import os
41
34
import bzrlib
42
35
import bzrlib.trace
43
36
from bzrlib.trace import mutter, note, log_error, warning
44
 
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError, NotBranchError
45
 
from bzrlib.revisionspec import RevisionSpec
 
37
from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError
 
38
from bzrlib.branch import find_branch
46
39
from bzrlib import BZRDIR
47
40
 
48
41
plugin_cmds = {}
76
69
def _parse_revision_str(revstr):
77
70
    """This handles a revision string -> revno.
78
71
 
79
 
    This always returns a list.  The list will have one element for
80
 
    each revision.
 
72
    This always returns a list.  The list will have one element for 
 
73
 
 
74
    It supports integers directly, but everything else it
 
75
    defers for passing to Branch.get_revision_info()
81
76
 
82
77
    >>> _parse_revision_str('234')
83
 
    [<RevisionSpec_int 234>]
 
78
    [234]
84
79
    >>> _parse_revision_str('234..567')
85
 
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
 
80
    [234, 567]
86
81
    >>> _parse_revision_str('..')
87
 
    [<RevisionSpec None>, <RevisionSpec None>]
 
82
    [None, None]
88
83
    >>> _parse_revision_str('..234')
89
 
    [<RevisionSpec None>, <RevisionSpec_int 234>]
 
84
    [None, 234]
90
85
    >>> _parse_revision_str('234..')
91
 
    [<RevisionSpec_int 234>, <RevisionSpec None>]
 
86
    [234, None]
92
87
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
93
 
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
 
88
    [234, 456, 789]
94
89
    >>> _parse_revision_str('234....789') # Error?
95
 
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
 
90
    [234, None, 789]
96
91
    >>> _parse_revision_str('revid:test@other.com-234234')
97
 
    [<RevisionSpec_revid revid:test@other.com-234234>]
 
92
    ['revid:test@other.com-234234']
98
93
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
99
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
 
94
    ['revid:test@other.com-234234', 'revid:test@other.com-234235']
100
95
    >>> _parse_revision_str('revid:test@other.com-234234..23')
101
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
 
96
    ['revid:test@other.com-234234', 23]
102
97
    >>> _parse_revision_str('date:2005-04-12')
103
 
    [<RevisionSpec_date date:2005-04-12>]
 
98
    ['date:2005-04-12']
104
99
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
105
 
    [<RevisionSpec_date date:2005-04-12 12:24:33>]
 
100
    ['date:2005-04-12 12:24:33']
106
101
    >>> _parse_revision_str('date:2005-04-12T12:24:33')
107
 
    [<RevisionSpec_date date:2005-04-12T12:24:33>]
 
102
    ['date:2005-04-12T12:24:33']
108
103
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
109
 
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
 
104
    ['date:2005-04-12,12:24:33']
110
105
    >>> _parse_revision_str('-5..23')
111
 
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
 
106
    [-5, 23]
112
107
    >>> _parse_revision_str('-5')
113
 
    [<RevisionSpec_int -5>]
 
108
    [-5]
114
109
    >>> _parse_revision_str('123a')
115
 
    Traceback (most recent call last):
116
 
      ...
117
 
    BzrError: No namespace registered for string: '123a'
 
110
    ['123a']
118
111
    >>> _parse_revision_str('abc')
119
 
    Traceback (most recent call last):
120
 
      ...
121
 
    BzrError: No namespace registered for string: 'abc'
 
112
    ['abc']
122
113
    """
123
114
    import re
124
115
    old_format_re = re.compile('\d*:\d*')
125
116
    m = old_format_re.match(revstr)
126
 
    revs = []
127
117
    if m:
128
118
        warning('Colon separator for revision numbers is deprecated.'
129
119
                ' Use .. instead')
 
120
        revs = []
130
121
        for rev in revstr.split(':'):
131
122
            if rev:
132
 
                revs.append(RevisionSpec(int(rev)))
133
 
            else:
134
 
                revs.append(RevisionSpec(None))
135
 
    else:
136
 
        for x in revstr.split('..'):
137
 
            if not x:
138
 
                revs.append(RevisionSpec(None))
139
 
            else:
140
 
                revs.append(RevisionSpec(x))
 
123
                revs.append(int(rev))
 
124
            else:
 
125
                revs.append(None)
 
126
        return revs
 
127
    revs = []
 
128
    for x in revstr.split('..'):
 
129
        if not x:
 
130
            revs.append(None)
 
131
        else:
 
132
            try:
 
133
                revs.append(int(x))
 
134
            except ValueError:
 
135
                revs.append(x)
141
136
    return revs
142
137
 
143
138
 
153
148
        msg = "No known merge type %s. Supported types are:\n%s" %\
154
149
            (typestring, type_list)
155
150
        raise BzrCommandError(msg)
 
151
    
 
152
 
 
153
def get_merge_type(typestring):
 
154
    """Attempt to find the merge class/factory associated with a string."""
 
155
    from merge import merge_types
 
156
    try:
 
157
        return merge_types[typestring][0]
 
158
    except KeyError:
 
159
        templ = '%s%%7s: %%s' % (' '*12)
 
160
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
161
        type_list = '\n'.join(lines)
 
162
        msg = "No known merge type %s. Supported types are:\n%s" %\
 
163
            (typestring, type_list)
 
164
        raise BzrCommandError(msg)
156
165
 
157
166
 
158
167
def _builtin_commands():
347
356
    return parsed
348
357
 
349
358
 
 
359
 
 
360
 
350
361
# list of all available options; the rhs can be either None for an
351
362
# option that takes no argument, or a constructor function that checks
352
363
# the type.
406
417
    >>> parse_args('commit --message=biter'.split())
407
418
    (['commit'], {'message': u'biter'})
408
419
    >>> parse_args('log -r 500'.split())
409
 
    (['log'], {'revision': [<RevisionSpec_int 500>]})
 
420
    (['log'], {'revision': [500]})
410
421
    >>> parse_args('log -r500..600'.split())
411
 
    (['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
 
422
    (['log'], {'revision': [500, 600]})
412
423
    >>> parse_args('log -vr500..600'.split())
413
 
    (['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
414
 
    >>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
415
 
    (['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})
 
424
    (['log'], {'verbose': True, 'revision': [500, 600]})
 
425
    >>> parse_args('log -rv500..600'.split()) #the r takes an argument
 
426
    (['log'], {'revision': ['v500', 600]})
416
427
    """
417
428
    args = []
418
429
    opts = {}
638
649
    bzrlib.trace.log_startup(argv)
639
650
    bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
640
651
 
641
 
    return run_bzr_catch_errors(argv[1:])
642
 
 
643
 
 
644
 
def run_bzr_catch_errors(argv):
645
652
    try:
646
653
        try:
647
 
            try:
648
 
                return run_bzr(argv)
649
 
            finally:
650
 
                # do this here inside the exception wrappers to catch EPIPE
651
 
                sys.stdout.flush()
652
 
        #wrap common errors as CommandErrors.
653
 
        except (NotBranchError,), e:
654
 
            raise BzrCommandError(str(e))
 
654
            return run_bzr(argv[1:])
 
655
        finally:
 
656
            # do this here inside the exception wrappers to catch EPIPE
 
657
            sys.stdout.flush()
655
658
    except BzrCommandError, e:
656
659
        # command line syntax error, etc
657
660
        log_error(str(e))