/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
1
# Copyright (C) 2004, 2005 by Canonical Ltd
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
18
import re
19
20
import bzrlib.commands
21
from bzrlib.trace import warning, mutter
22
from bzrlib.revisionspec import RevisionSpec
23
24
25
def _parse_revision_str(revstr):
26
    """This handles a revision string -> revno.
27
28
    This always returns a list.  The list will have one element for
29
    each revision specifier supplied.
30
31
    >>> _parse_revision_str('234')
32
    [<RevisionSpec_int 234>]
33
    >>> _parse_revision_str('234..567')
34
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
35
    >>> _parse_revision_str('..')
36
    [<RevisionSpec None>, <RevisionSpec None>]
37
    >>> _parse_revision_str('..234')
38
    [<RevisionSpec None>, <RevisionSpec_int 234>]
39
    >>> _parse_revision_str('234..')
40
    [<RevisionSpec_int 234>, <RevisionSpec None>]
41
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
42
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
43
    >>> _parse_revision_str('234....789') #Error ?
44
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
45
    >>> _parse_revision_str('revid:test@other.com-234234')
46
    [<RevisionSpec_revid revid:test@other.com-234234>]
47
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
48
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
49
    >>> _parse_revision_str('revid:test@other.com-234234..23')
50
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
51
    >>> _parse_revision_str('date:2005-04-12')
52
    [<RevisionSpec_date date:2005-04-12>]
53
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
54
    [<RevisionSpec_date date:2005-04-12 12:24:33>]
55
    >>> _parse_revision_str('date:2005-04-12T12:24:33')
56
    [<RevisionSpec_date date:2005-04-12T12:24:33>]
57
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
58
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
59
    >>> _parse_revision_str('-5..23')
60
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
61
    >>> _parse_revision_str('-5')
62
    [<RevisionSpec_int -5>]
63
    >>> _parse_revision_str('123a')
64
    Traceback (most recent call last):
65
      ...
66
    BzrError: No namespace registered for string: '123a'
67
    >>> _parse_revision_str('abc')
68
    Traceback (most recent call last):
69
      ...
70
    BzrError: No namespace registered for string: 'abc'
71
    >>> _parse_revision_str('branch:../branch2')
72
    [<RevisionSpec_branch branch:../branch2>]
73
    """
74
    # TODO: Maybe move this into revisionspec.py
75
    old_format_re = re.compile('\d*:\d*')
76
    m = old_format_re.match(revstr)
77
    revs = []
78
    if m:
79
        warning('Colon separator for revision numbers is deprecated.'
80
                ' Use .. instead')
81
        for rev in revstr.split(':'):
82
            if rev:
83
                revs.append(RevisionSpec(int(rev)))
84
            else:
85
                revs.append(RevisionSpec(None))
86
    else:
87
        next_prefix = None
88
        for x in revstr.split('..'):
89
            if not x:
90
                revs.append(RevisionSpec(None))
91
            elif x[-1] == ':':
92
                # looks like a namespace:.. has happened
93
                next_prefix = x + '..'
94
            else:
95
                if next_prefix is not None:
96
                    x = next_prefix + x
97
                revs.append(RevisionSpec(x))
98
                next_prefix = None
99
        if next_prefix is not None:
100
            revs.append(RevisionSpec(next_prefix))
101
    return revs
102
103
104
def _parse_merge_type(typestring):
1185.12.62 by Aaron Bentley
Restored merge-type selection
105
    return get_merge_type(typestring)
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
106
1185.12.62 by Aaron Bentley
Restored merge-type selection
107
def get_merge_type(typestring):
108
    """Attempt to find the merge class/factory associated with a string."""
109
    from merge import merge_types
110
    try:
111
        return merge_types[typestring][0]
112
    except KeyError:
113
        templ = '%s%%7s: %%s' % (' '*12)
114
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
115
        type_list = '\n'.join(lines)
116
        msg = "No known merge type %s. Supported types are:\n%s" %\
117
            (typestring, type_list)
118
        raise BzrCommandError(msg)
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
119
120
class Option(object):
1185.16.45 by Martin Pool
- refactor handling of short option names
121
    """Description of a command line option"""
122
    # TODO: Some way to show in help a description of the option argument
123
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
124
    OPTIONS = {}
125
    SHORT_OPTIONS = {}
126
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
127
    def __init__(self, name, help='', type=None, argname=None):
1185.16.45 by Martin Pool
- refactor handling of short option names
128
        """Make a new command option.
129
130
        name -- regular name of the command, used in the double-dash
131
            form and also as the parameter to the command's run() 
132
            method.
133
134
        help -- help message displayed in command help
135
136
        type -- function called to parse the option argument, or 
137
            None (default) if this option doesn't take an argument.
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
138
139
        argname -- name of option argument, if any
1185.16.45 by Martin Pool
- refactor handling of short option names
140
        """
141
        # TODO: perhaps a subclass that automatically does 
142
        # --option, --no-option for reversable booleans
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
143
        self.name = name
144
        self.help = help
145
        self.type = type
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
146
        if type is None:
147
            assert argname is None
148
        elif argname is None:
149
            argname = 'ARG'
150
        self.argname = argname
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
151
1185.16.45 by Martin Pool
- refactor handling of short option names
152
    def short_name(self):
153
        """Return the single character option for this command, if any.
154
155
        Short options are globally registered.
156
        """
157
        return Option.SHORT_OPTIONS.get(self.name)
158
159
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
160
def _global_option(name, **kwargs):
161
    """Register o as a global option."""
162
    Option.OPTIONS[name] = Option(name, **kwargs)
163
1185.16.45 by Martin Pool
- refactor handling of short option names
164
_global_option('all')
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
165
_global_option('clobber')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
166
_global_option('basis', type=str)
167
_global_option('diff-options', type=str)
1185.16.55 by mbp at sourcefrog
- more option help
168
_global_option('help',
169
               help='show help message')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
170
_global_option('file', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
171
_global_option('force')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
172
_global_option('format', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
173
_global_option('forward')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
174
_global_option('message', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
175
_global_option('no-recurse')
1185.16.55 by mbp at sourcefrog
- more option help
176
_global_option('profile',
177
               help='show performance profiling information')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
178
_global_option('revision', type=_parse_revision_str)
1185.16.45 by Martin Pool
- refactor handling of short option names
179
_global_option('short')
1185.16.47 by Martin Pool
- help for global options
180
_global_option('show-ids', 
181
               help='show internal object ids')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
182
_global_option('timezone', 
183
               type=str,
184
               help='display timezone as local, original, or utc')
185
_global_option('verbose',
186
               help='display more information')
1185.16.45 by Martin Pool
- refactor handling of short option names
187
_global_option('version')
188
_global_option('email')
189
_global_option('update')
190
_global_option('long')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
191
_global_option('root', type=str)
1185.16.45 by Martin Pool
- refactor handling of short option names
192
_global_option('no-backup')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
193
_global_option('merge-type', type=_parse_merge_type)
194
_global_option('pattern', type=str)
1185.16.45 by Martin Pool
- refactor handling of short option names
195
_global_option('quiet')
196
_global_option('remember')
197
198
199
def _global_short(short_name, long_name):
200
    assert short_name not in Option.SHORT_OPTIONS
201
    Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
202
    
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
203
204
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
205
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
206
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
207
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
208
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
209
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
210
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']