~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-31 16:12:57 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060731161257-91a231523255332c
new official bzr.ico

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
# TODO: "--profile=cum", to change sort order.  Is there any value in leaving
29
29
# the profile output behind so it can be interactively examined?
30
30
 
31
 
import codecs
32
 
import errno
 
31
import sys
33
32
import os
34
33
from warnings import warn
35
 
import sys
 
34
import errno
 
35
import codecs
36
36
 
37
37
import bzrlib
38
38
import bzrlib.errors as errors
40
40
                           BzrCommandError,
41
41
                           BzrCheckError,
42
42
                           NotBranchError)
43
 
from bzrlib import option
44
43
from bzrlib.option import Option
45
44
import bzrlib.osutils
 
45
from bzrlib.revisionspec import RevisionSpec
46
46
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
47
47
import bzrlib.trace
48
48
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
64
64
        k_unsquished = _unsquish_command_name(k)
65
65
    else:
66
66
        k_unsquished = k
67
 
    if k_unsquished not in plugin_cmds:
 
67
    if not plugin_cmds.has_key(k_unsquished):
68
68
        plugin_cmds[k_unsquished] = cmd
69
69
        mutter('registered plugin command %s', k_unsquished)
70
70
        if decorate and k_unsquished in builtin_command_names():
129
129
    """
130
130
    from bzrlib.externalcommand import ExternalCommand
131
131
 
132
 
    # We want only 'ascii' command names, but the user may have typed
133
 
    # in a Unicode name. In that case, they should just get a
134
 
    # 'command not found' error later.
135
 
    # In the future, we may actually support Unicode command names.
 
132
    cmd_name = str(cmd_name)            # not unicode
136
133
 
137
134
    # first look up this command under the specified name
138
135
    cmds = _get_cmd_dict(plugins_override=plugins_override)
224
221
        r = dict()
225
222
        r['help'] = Option.OPTIONS['help']
226
223
        for o in self.takes_options:
227
 
            if isinstance(o, basestring):
 
224
            if not isinstance(o, Option):
228
225
                o = Option.OPTIONS[o]
229
226
            r[o.name] = o
230
227
        return r
259
256
 
260
257
    def run_argv_aliases(self, argv, alias_argv=None):
261
258
        """Parse the command line and run with extra aliases in alias_argv."""
262
 
        if argv is None:
263
 
            warn("Passing None for [] is deprecated from bzrlib 0.10", 
264
 
                 DeprecationWarning, stacklevel=2)
265
 
            argv = []
266
259
        args, opts = parse_args(self, argv, alias_argv)
267
260
        if 'help' in opts:  # e.g. bzr add --help
268
261
            from bzrlib.help import help_on_command
269
262
            help_on_command(self.name())
270
263
            return 0
 
264
        # XXX: This should be handled by the parser
 
265
        allowed_names = self.options().keys()
 
266
        for oname in opts:
 
267
            if oname not in allowed_names:
 
268
                raise BzrOptionError("option '--%s' is not allowed for"
 
269
                                " command %r" % (oname, self.name()))
271
270
        # mix arguments and options into one dictionary
272
271
        cmdargs = _match_argform(self.name(), self.takes_args, args)
273
272
        cmdopts = {}
355
354
    lookup table, something about the available options, what optargs
356
355
    they take, and which commands will accept them.
357
356
    """
358
 
    # TODO: make it a method of the Command?
359
 
    parser = option.get_optparser(command.options())
360
 
    if alias_argv is not None:
361
 
        args = alias_argv + argv
362
 
    else:
363
 
        args = argv
364
 
 
365
 
    options, args = parser.parse_args(args)
366
 
    opts = dict([(k, v) for k, v in options.__dict__.iteritems() if 
367
 
                 v is not option.OptionParser.DEFAULT_VALUE])
 
357
    # TODO: chop up this beast; make it a method of the Command
 
358
    args = []
 
359
    opts = {}
 
360
    alias_opts = {}
 
361
 
 
362
    cmd_options = command.options()
 
363
    argsover = False
 
364
    proc_aliasarg = True # Are we processing alias_argv now?
 
365
    for proc_argv in alias_argv, argv:
 
366
        while proc_argv:
 
367
            a = proc_argv.pop(0)
 
368
            if argsover:
 
369
                args.append(a)
 
370
                continue
 
371
            elif a == '-':
 
372
                args.append(a)
 
373
                continue
 
374
            elif a == '--':
 
375
                # We've received a standalone -- No more flags
 
376
                argsover = True
 
377
                continue
 
378
            if a[0] == '-':
 
379
                # option names must not be unicode
 
380
                a = str(a)
 
381
                optarg = None
 
382
                if a[1] == '-':
 
383
                    mutter("  got option %r", a)
 
384
                    if '=' in a:
 
385
                        optname, optarg = a[2:].split('=', 1)
 
386
                    else:
 
387
                        optname = a[2:]
 
388
                    if optname not in cmd_options:
 
389
                        raise BzrCommandError('unknown option "%s"' % a)
 
390
                else:
 
391
                    shortopt = a[1:]
 
392
                    if shortopt in Option.SHORT_OPTIONS:
 
393
                        # Multi-character options must have a space to delimit
 
394
                        # their value
 
395
                        # ^^^ what does this mean? mbp 20051014
 
396
                        optname = Option.SHORT_OPTIONS[shortopt].name
 
397
                    else:
 
398
                        # Single character short options, can be chained,
 
399
                        # and have their value appended to their name
 
400
                        shortopt = a[1:2]
 
401
                        if shortopt not in Option.SHORT_OPTIONS:
 
402
                            # We didn't find the multi-character name, and we
 
403
                            # didn't find the single char name
 
404
                            raise BzrCommandError('unknown option "%s"' % a)
 
405
                        optname = Option.SHORT_OPTIONS[shortopt].name
 
406
 
 
407
                        if a[2:]:
 
408
                            # There are extra things on this option
 
409
                            # see if it is the value, or if it is another
 
410
                            # short option
 
411
                            optargfn = Option.OPTIONS[optname].type
 
412
                            if optargfn is None:
 
413
                                # This option does not take an argument, so the
 
414
                                # next entry is another short option, pack it
 
415
                                # back into the list
 
416
                                proc_argv.insert(0, '-' + a[2:])
 
417
                            else:
 
418
                                # This option takes an argument, so pack it
 
419
                                # into the array
 
420
                                optarg = a[2:]
 
421
                    if optname not in cmd_options:
 
422
                        raise BzrCommandError('unknown option "%s"' % shortopt)
 
423
                if optname in opts:
 
424
                    # XXX: Do we ever want to support this, e.g. for -r?
 
425
                    if proc_aliasarg:
 
426
                        raise BzrCommandError('repeated option %r' % a)
 
427
                    elif optname in alias_opts:
 
428
                        # Replace what's in the alias with what's in the real
 
429
                        # argument
 
430
                        del alias_opts[optname]
 
431
                        del opts[optname]
 
432
                        proc_argv.insert(0, a)
 
433
                        continue
 
434
                    else:
 
435
                        raise BzrCommandError('repeated option %r' % a)
 
436
                    
 
437
                option_obj = cmd_options[optname]
 
438
                optargfn = option_obj.type
 
439
                if optargfn:
 
440
                    if optarg == None:
 
441
                        if not proc_argv:
 
442
                            raise BzrCommandError('option %r needs an argument' % a)
 
443
                        else:
 
444
                            optarg = proc_argv.pop(0)
 
445
                    opts[optname] = optargfn(optarg)
 
446
                    if proc_aliasarg:
 
447
                        alias_opts[optname] = optargfn(optarg)
 
448
                else:
 
449
                    if optarg != None:
 
450
                        raise BzrCommandError('option %r takes no argument' % optname)
 
451
                    opts[optname] = True
 
452
                    if proc_aliasarg:
 
453
                        alias_opts[optname] = True
 
454
            else:
 
455
                args.append(a)
 
456
        proc_aliasarg = False # Done with alias argv
368
457
    return args, opts
369
458
 
370
459
 
532
621
        return 0
533
622
 
534
623
    if argv[0] == '--version':
535
 
        from bzrlib.version import show_version
 
624
        from bzrlib.builtins import show_version
536
625
        show_version()
537
626
        return 0
538
627
        
551
640
            alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
552
641
            argv[0] = alias_argv.pop(0)
553
642
 
554
 
    cmd = argv.pop(0)
555
 
    # We want only 'ascii' command names, but the user may have typed
556
 
    # in a Unicode name. In that case, they should just get a
557
 
    # 'command not found' error later.
 
643
    cmd = str(argv.pop(0))
558
644
 
559
645
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
560
646
    if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
584
670
            sys.stdout.flush()
585
671
            return result
586
672
        except IOError, e:
587
 
            if getattr(e, 'errno', None) is None:
 
673
            if not hasattr(e, 'errno'):
588
674
                raise
589
675
            if e.errno != errno.EPIPE:
590
676
                # Win32 raises IOError with errno=0 on a broken pipe
611
697
        return run_bzr(argv)
612
698
        # do this here inside the exception wrappers to catch EPIPE
613
699
        sys.stdout.flush()
614
 
    except (KeyboardInterrupt, Exception), e:
 
700
    except Exception, e:
615
701
        # used to handle AssertionError and KeyboardInterrupt
616
702
        # specially here, but hopefully they're handled ok by the logger now
617
703
        bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)