/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-18 18:57:54 UTC
  • mto: This revision was merged to the branch mainline in revision 1868.
  • Revision ID: john@arbash-meinel.com-20060718185754-4007745748e28db9
Commit timestamp restricted to 1ms precision.

The old code would restrict to 1s resolution if the timestamp was
supplied, while it preserved full resolution if the timestamp was
auto generated. Now both paths preserve only 1ms resolution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005, 2006 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
# TODO: For things like --diff-prefix, we want a way to customize the display
 
18
# of the option argument.
 
19
 
 
20
import re
 
21
 
 
22
from bzrlib.trace import warning
 
23
from bzrlib.revisionspec import RevisionSpec
 
24
from bzrlib.errors import BzrCommandError
 
25
 
 
26
 
 
27
def _parse_revision_str(revstr):
 
28
    """This handles a revision string -> revno.
 
29
 
 
30
    This always returns a list.  The list will have one element for
 
31
    each revision specifier supplied.
 
32
 
 
33
    >>> _parse_revision_str('234')
 
34
    [<RevisionSpec_int 234>]
 
35
    >>> _parse_revision_str('234..567')
 
36
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
 
37
    >>> _parse_revision_str('..')
 
38
    [<RevisionSpec None>, <RevisionSpec None>]
 
39
    >>> _parse_revision_str('..234')
 
40
    [<RevisionSpec None>, <RevisionSpec_int 234>]
 
41
    >>> _parse_revision_str('234..')
 
42
    [<RevisionSpec_int 234>, <RevisionSpec None>]
 
43
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
 
44
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
 
45
    >>> _parse_revision_str('234....789') #Error ?
 
46
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
 
47
    >>> _parse_revision_str('revid:test@other.com-234234')
 
48
    [<RevisionSpec_revid revid:test@other.com-234234>]
 
49
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
 
50
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
 
51
    >>> _parse_revision_str('revid:test@other.com-234234..23')
 
52
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
 
53
    >>> _parse_revision_str('date:2005-04-12')
 
54
    [<RevisionSpec_date date:2005-04-12>]
 
55
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
 
56
    [<RevisionSpec_date date:2005-04-12 12:24:33>]
 
57
    >>> _parse_revision_str('date:2005-04-12T12:24:33')
 
58
    [<RevisionSpec_date date:2005-04-12T12:24:33>]
 
59
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
 
60
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
 
61
    >>> _parse_revision_str('-5..23')
 
62
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
 
63
    >>> _parse_revision_str('-5')
 
64
    [<RevisionSpec_int -5>]
 
65
    >>> _parse_revision_str('123a')
 
66
    Traceback (most recent call last):
 
67
      ...
 
68
    BzrError: No namespace registered for string: '123a'
 
69
    >>> _parse_revision_str('abc')
 
70
    Traceback (most recent call last):
 
71
      ...
 
72
    BzrError: No namespace registered for string: 'abc'
 
73
    >>> _parse_revision_str('branch:../branch2')
 
74
    [<RevisionSpec_branch branch:../branch2>]
 
75
    >>> _parse_revision_str('branch:../../branch2')
 
76
    [<RevisionSpec_branch branch:../../branch2>]
 
77
    >>> _parse_revision_str('branch:../../branch2..23')
 
78
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_int 23>]
 
79
    """
 
80
    # TODO: Maybe move this into revisionspec.py
 
81
    old_format_re = re.compile('\d*:\d*')
 
82
    m = old_format_re.match(revstr)
 
83
    revs = []
 
84
    if m:
 
85
        warning('Colon separator for revision numbers is deprecated.'
 
86
                ' Use .. instead')
 
87
        for rev in revstr.split(':'):
 
88
            if rev:
 
89
                revs.append(RevisionSpec(int(rev)))
 
90
            else:
 
91
                revs.append(RevisionSpec(None))
 
92
    else:
 
93
        sep = re.compile("\\.\\.(?!/)")
 
94
        for x in sep.split(revstr):
 
95
            revs.append(RevisionSpec(x or None))
 
96
    return revs
 
97
 
 
98
 
 
99
def _parse_merge_type(typestring):
 
100
    return get_merge_type(typestring)
 
101
 
 
102
def get_merge_type(typestring):
 
103
    """Attempt to find the merge class/factory associated with a string."""
 
104
    from merge import merge_types
 
105
    try:
 
106
        return merge_types[typestring][0]
 
107
    except KeyError:
 
108
        templ = '%s%%7s: %%s' % (' '*12)
 
109
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
110
        type_list = '\n'.join(lines)
 
111
        msg = "No known merge type %s. Supported types are:\n%s" %\
 
112
            (typestring, type_list)
 
113
        raise BzrCommandError(msg)
 
114
 
 
115
class Option(object):
 
116
    """Description of a command line option"""
 
117
    # TODO: Some way to show in help a description of the option argument
 
118
 
 
119
    OPTIONS = {}
 
120
    SHORT_OPTIONS = {}
 
121
 
 
122
    def __init__(self, name, help='', type=None, argname=None):
 
123
        """Make a new command option.
 
124
 
 
125
        name -- regular name of the command, used in the double-dash
 
126
            form and also as the parameter to the command's run() 
 
127
            method.
 
128
 
 
129
        help -- help message displayed in command help
 
130
 
 
131
        type -- function called to parse the option argument, or 
 
132
            None (default) if this option doesn't take an argument.
 
133
 
 
134
        argname -- name of option argument, if any
 
135
        """
 
136
        # TODO: perhaps a subclass that automatically does 
 
137
        # --option, --no-option for reversible booleans
 
138
        self.name = name
 
139
        self.help = help
 
140
        self.type = type
 
141
        if type is None:
 
142
            assert argname is None
 
143
        elif argname is None:
 
144
            argname = 'ARG'
 
145
        self.argname = argname
 
146
 
 
147
    def short_name(self):
 
148
        """Return the single character option for this command, if any.
 
149
 
 
150
        Short options are globally registered.
 
151
        """
 
152
        for short, option in Option.SHORT_OPTIONS.iteritems():
 
153
            if option is self:
 
154
                return short
 
155
 
 
156
 
 
157
def _global_option(name, **kwargs):
 
158
    """Register o as a global option."""
 
159
    Option.OPTIONS[name] = Option(name, **kwargs)
 
160
 
 
161
_global_option('all')
 
162
_global_option('overwrite', help='Ignore differences between branches and '
 
163
               'overwrite unconditionally')
 
164
_global_option('basis', type=str)
 
165
_global_option('bound')
 
166
_global_option('diff-options', type=str)
 
167
_global_option('help',
 
168
               help='show help message')
 
169
_global_option('file', type=unicode)
 
170
_global_option('force')
 
171
_global_option('format', type=unicode)
 
172
_global_option('forward')
 
173
_global_option('message', type=unicode)
 
174
_global_option('no-recurse')
 
175
_global_option('prefix', type=str, 
 
176
               help='Set prefixes to added to old and new filenames, as '
 
177
                    'two values separated by a colon.')
 
178
_global_option('profile',
 
179
               help='show performance profiling information')
 
180
_global_option('revision', type=_parse_revision_str)
 
181
_global_option('show-ids', 
 
182
               help='show internal object ids')
 
183
_global_option('timezone', 
 
184
               type=str,
 
185
               help='display timezone as local, original, or utc')
 
186
_global_option('unbound')
 
187
_global_option('verbose',
 
188
               help='display more information')
 
189
_global_option('version')
 
190
_global_option('email')
 
191
_global_option('update')
 
192
_global_option('log-format', type=str, help="Use this log format")
 
193
_global_option('long', help='Use detailed log format. Same as --log-format long')
 
194
_global_option('short', help='Use moderately short log format. Same as --log-format short')
 
195
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
 
196
_global_option('root', type=str)
 
197
_global_option('no-backup')
 
198
_global_option('merge-type', type=_parse_merge_type, 
 
199
               help='Select a particular merge algorithm')
 
200
_global_option('pattern', type=str)
 
201
_global_option('quiet')
 
202
_global_option('remember', help='Remember the specified location as a'
 
203
               ' default.')
 
204
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
 
205
_global_option('kind', type=str)
 
206
_global_option('dry-run',
 
207
               help="show what would be done, but don't actually do anything")
 
208
 
 
209
 
 
210
def _global_short(short_name, long_name):
 
211
    assert short_name not in Option.SHORT_OPTIONS
 
212
    Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
 
213
    
 
214
 
 
215
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
 
216
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
 
217
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
 
218
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
 
219
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
 
220
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
 
221
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']
 
222
Option.SHORT_OPTIONS['p'] = Option.OPTIONS['prefix']