/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/commands.py

  • Committer: Robey Pointer
  • Date: 2006-07-01 19:03:33 UTC
  • mfrom: (1829 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: robey@lag.net-20060701190333-f58465aec4bd3412
merge from 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
 
from bzrlib.symbol_versioning import *
46
 
import bzrlib.trace
 
46
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
 
47
from bzrlib import trace
47
48
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
48
49
 
49
50
plugin_cmds = {}
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
673
670
            if not hasattr(e, 'errno'):
674
671
                raise
675
672
            if e.errno != errno.EPIPE:
676
 
                raise
 
673
                # Win32 raises IOError with errno=0 on a broken pipe
 
674
                if sys.platform != 'win32' or e.errno != 0:
 
675
                    raise
677
676
            pass
678
677
        except KeyboardInterrupt:
679
678
            pass
683
682
def main(argv):
684
683
    import bzrlib.ui
685
684
    from bzrlib.ui.text import TextUIFactory
686
 
    ## bzrlib.trace.enable_default_logging()
687
 
    bzrlib.trace.log_startup(argv)
688
685
    bzrlib.ui.ui_factory = TextUIFactory()
689
 
 
690
686
    argv = [a.decode(bzrlib.user_encoding) for a in argv[1:]]
691
687
    ret = run_bzr_catch_errors(argv)
692
688
    mutter("return code %d", ret)
695
691
 
696
692
def run_bzr_catch_errors(argv):
697
693
    try:
698
 
        try:
699
 
            return run_bzr(argv)
700
 
        finally:
701
 
            # do this here inside the exception wrappers to catch EPIPE
702
 
            sys.stdout.flush()
 
694
        return run_bzr(argv)
 
695
        # do this here inside the exception wrappers to catch EPIPE
 
696
        sys.stdout.flush()
703
697
    except Exception, e:
704
698
        # used to handle AssertionError and KeyboardInterrupt
705
699
        # specially here, but hopefully they're handled ok by the logger now
706
 
        import errno
707
 
        if (isinstance(e, IOError) 
708
 
            and hasattr(e, 'errno')
709
 
            and e.errno == errno.EPIPE):
710
 
            bzrlib.trace.note('broken pipe')
711
 
            return 3
712
 
        else:
713
 
            bzrlib.trace.log_exception()
714
 
            if os.environ.get('BZR_PDB'):
715
 
                print '**** entering debugger'
716
 
                import pdb
717
 
                pdb.post_mortem(sys.exc_traceback)
718
 
            return 3
 
700
        bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
 
701
        if os.environ.get('BZR_PDB'):
 
702
            print '**** entering debugger'
 
703
            import pdb
 
704
            pdb.post_mortem(sys.exc_traceback)
 
705
        return 3
719
706
 
720
707
if __name__ == '__main__':
721
708
    sys.exit(main(sys.argv))