~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2006-06-20 05:32:16 UTC
  • mfrom: (1797 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1798.
  • Revision ID: mbp@sourcefrog.net-20060620053216-817857d7ca3e9d1f
[merge] bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
import codecs
36
36
 
37
37
import bzrlib
 
38
import bzrlib.errors as errors
38
39
from bzrlib.errors import (BzrError,
 
40
                           BzrCommandError,
39
41
                           BzrCheckError,
40
 
                           BzrCommandError,
41
 
                           BzrOptionError,
42
42
                           NotBranchError)
43
43
from bzrlib.option import Option
 
44
import bzrlib.osutils
44
45
from bzrlib.revisionspec import RevisionSpec
45
46
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
46
47
from bzrlib import trace
146
147
    if cmd_obj:
147
148
        return cmd_obj
148
149
 
149
 
    raise BzrCommandError("unknown command %r" % cmd_name)
 
150
    raise BzrCommandError('unknown command "%s"' % cmd_name)
150
151
 
151
152
 
152
153
class Command(object):
235
236
            self.outf = sys.stdout
236
237
            return
237
238
 
238
 
        output_encoding = getattr(sys.stdout, 'encoding', None)
239
 
        if not output_encoding:
240
 
            input_encoding = getattr(sys.stdin, 'encoding', None)
241
 
            if not input_encoding:
242
 
                output_encoding = bzrlib.user_encoding
243
 
                mutter('encoding stdout as bzrlib.user_encoding %r', output_encoding)
244
 
            else:
245
 
                output_encoding = input_encoding
246
 
                mutter('encoding stdout as sys.stdin encoding %r', output_encoding)
247
 
        else:
248
 
            mutter('encoding stdout as sys.stdout encoding %r', output_encoding)
 
239
        output_encoding = bzrlib.osutils.get_terminal_encoding()
249
240
 
250
241
        # use 'replace' so that we don't abort if trying to write out
251
242
        # in e.g. the default C locale.
274
265
        allowed_names = self.options().keys()
275
266
        for oname in opts:
276
267
            if oname not in allowed_names:
277
 
                raise BzrCommandError("option '--%s' is not allowed for"
278
 
                                      " command %r" % (oname, self.name()))
 
268
                raise BzrOptionError("option '--%s' is not allowed for"
 
269
                                " command %r" % (oname, self.name()))
279
270
        # mix arguments and options into one dictionary
280
271
        cmdargs = _match_argform(self.name(), self.takes_args, args)
281
272
        cmdopts = {}
312
303
    def name(self):
313
304
        return _unsquish_command_name(self.__class__.__name__)
314
305
 
 
306
    def plugin_name(self):
 
307
        """Get the name of the plugin that provides this command.
 
308
 
 
309
        :return: The name of the plugin or None if the command is builtin.
 
310
        """
 
311
        mod_parts = self.__module__.split('.')
 
312
        if len(mod_parts) >= 3 and mod_parts[1] == 'plugins':
 
313
            return mod_parts[2]
 
314
        else:
 
315
            return None
 
316
 
315
317
 
316
318
def parse_spec(spec):
317
319
    """
381
383
                    else:
382
384
                        optname = a[2:]
383
385
                    if optname not in cmd_options:
384
 
                        raise BzrOptionError('unknown long option %r for'
385
 
                                             ' command %s' % 
386
 
                                             (a, command.name()))
 
386
                        raise BzrCommandError('unknown option "%s"' % a)
387
387
                else:
388
388
                    shortopt = a[1:]
389
389
                    if shortopt in Option.SHORT_OPTIONS:
398
398
                        if shortopt not in Option.SHORT_OPTIONS:
399
399
                            # We didn't find the multi-character name, and we
400
400
                            # didn't find the single char name
401
 
                            raise BzrError('unknown short option %r' % a)
 
401
                            raise BzrCommandError('unknown option "%s"' % a)
402
402
                        optname = Option.SHORT_OPTIONS[shortopt].name
403
403
 
404
404
                        if a[2:]:
415
415
                                # This option takes an argument, so pack it
416
416
                                # into the array
417
417
                                optarg = a[2:]
418
 
                
419
418
                    if optname not in cmd_options:
420
 
                        raise BzrOptionError('unknown short option %r for'
421
 
                                             ' command %s' % 
422
 
                                             (shortopt, command.name()))
 
419
                        raise BzrCommandError('unknown option "%s"' % shortopt)
423
420
                if optname in opts:
424
421
                    # XXX: Do we ever want to support this, e.g. for -r?
425
422
                    if proc_aliasarg:
426
 
                        raise BzrError('repeated option %r' % a)
 
423
                        raise BzrCommandError('repeated option %r' % a)
427
424
                    elif optname in alias_opts:
428
425
                        # Replace what's in the alias with what's in the real
429
426
                        # argument
432
429
                        proc_argv.insert(0, a)
433
430
                        continue
434
431
                    else:
435
 
                        raise BzrError('repeated option %r' % a)
 
432
                        raise BzrCommandError('repeated option %r' % a)
436
433
                    
437
434
                option_obj = cmd_options[optname]
438
435
                optargfn = option_obj.type
439
436
                if optargfn:
440
437
                    if optarg == None:
441
438
                        if not proc_argv:
442
 
                            raise BzrError('option %r needs an argument' % a)
 
439
                            raise BzrCommandError('option %r needs an argument' % a)
443
440
                        else:
444
441
                            optarg = proc_argv.pop(0)
445
442
                    opts[optname] = optargfn(optarg)
447
444
                        alias_opts[optname] = optargfn(optarg)
448
445
                else:
449
446
                    if optarg != None:
450
 
                        raise BzrError('option %r takes no argument' % optname)
 
447
                        raise BzrCommandError('option %r takes no argument' % optname)
451
448
                    opts[optname] = True
452
449
                    if proc_aliasarg:
453
450
                        alias_opts[optname] = True
484
481
                raise BzrCommandError("command %r needs one or more %s"
485
482
                        % (cmd, argname.upper()))
486
483
            argdict[argname + '_list'] = args[:-1]
487
 
            args[:-1] = []                
 
484
            args[:-1] = []
488
485
        else:
489
486
            # just a plain arg
490
487
            argname = ap
682
679
def main(argv):
683
680
    import bzrlib.ui
684
681
    from bzrlib.ui.text import TextUIFactory
685
 
    ## bzrlib.trace.enable_default_logging()
686
 
    trace.log_startup(argv)
687
682
    bzrlib.ui.ui_factory = TextUIFactory()
688
 
 
689
683
    argv = [a.decode(bzrlib.user_encoding) for a in argv[1:]]
690
684
    ret = run_bzr_catch_errors(argv)
691
685
    mutter("return code %d", ret)
702
696
    except Exception, e:
703
697
        # used to handle AssertionError and KeyboardInterrupt
704
698
        # specially here, but hopefully they're handled ok by the logger now
705
 
        import errno
706
 
        if (isinstance(e, IOError) 
707
 
            and hasattr(e, 'errno')
708
 
            and e.errno == errno.EPIPE):
709
 
            note('broken pipe')
710
 
            return 3
711
 
        else:
712
 
            trace.log_exception()
713
 
            if os.environ.get('BZR_PDB'):
714
 
                print '**** entering debugger'
715
 
                import pdb
716
 
                pdb.post_mortem(sys.exc_traceback)
717
 
            return 3
 
699
        bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
 
700
        if os.environ.get('BZR_PDB'):
 
701
            print '**** entering debugger'
 
702
            import pdb
 
703
            pdb.post_mortem(sys.exc_traceback)
 
704
        return 3
718
705
 
719
706
if __name__ == '__main__':
720
707
    sys.exit(main(sys.argv))