~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: John Arbash Meinel
  • Date: 2006-06-18 02:21:57 UTC
  • mfrom: (1787 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1794.
  • Revision ID: john@arbash-meinel.com-20060618022157-6e33aa9b67c25e4f
[merge] bzr.dev 1787

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
39
38
from bzrlib.errors import (BzrError,
 
39
                           BzrCheckError,
40
40
                           BzrCommandError,
41
 
                           BzrCheckError,
 
41
                           BzrOptionError,
42
42
                           NotBranchError)
43
43
from bzrlib.option import Option
44
 
import bzrlib.osutils
45
44
from bzrlib.revisionspec import RevisionSpec
46
 
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
47
 
from bzrlib import trace
 
45
from bzrlib.symbol_versioning import *
 
46
import bzrlib.trace
48
47
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
49
48
 
50
49
plugin_cmds = {}
147
146
    if cmd_obj:
148
147
        return cmd_obj
149
148
 
150
 
    raise BzrCommandError('unknown command "%s"' % cmd_name)
 
149
    raise BzrCommandError("unknown command %r" % cmd_name)
151
150
 
152
151
 
153
152
class Command(object):
236
235
            self.outf = sys.stdout
237
236
            return
238
237
 
239
 
        output_encoding = bzrlib.osutils.get_terminal_encoding()
 
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)
240
249
 
241
250
        # use 'replace' so that we don't abort if trying to write out
242
251
        # in e.g. the default C locale.
265
274
        allowed_names = self.options().keys()
266
275
        for oname in opts:
267
276
            if oname not in allowed_names:
268
 
                raise BzrOptionError("option '--%s' is not allowed for"
269
 
                                " command %r" % (oname, self.name()))
 
277
                raise BzrCommandError("option '--%s' is not allowed for"
 
278
                                      " command %r" % (oname, self.name()))
270
279
        # mix arguments and options into one dictionary
271
280
        cmdargs = _match_argform(self.name(), self.takes_args, args)
272
281
        cmdopts = {}
303
312
    def name(self):
304
313
        return _unsquish_command_name(self.__class__.__name__)
305
314
 
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
 
 
317
315
 
318
316
def parse_spec(spec):
319
317
    """
383
381
                    else:
384
382
                        optname = a[2:]
385
383
                    if optname not in cmd_options:
386
 
                        raise BzrCommandError('unknown option "%s"' % a)
 
384
                        raise BzrOptionError('unknown long option %r for'
 
385
                                             ' command %s' % 
 
386
                                             (a, command.name()))
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 BzrCommandError('unknown option "%s"' % a)
 
401
                            raise BzrError('unknown short option %r' % 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
                
418
419
                    if optname not in cmd_options:
419
 
                        raise BzrCommandError('unknown option "%s"' % shortopt)
 
420
                        raise BzrOptionError('unknown short option %r for'
 
421
                                             ' command %s' % 
 
422
                                             (shortopt, command.name()))
420
423
                if optname in opts:
421
424
                    # XXX: Do we ever want to support this, e.g. for -r?
422
425
                    if proc_aliasarg:
423
 
                        raise BzrCommandError('repeated option %r' % a)
 
426
                        raise BzrError('repeated option %r' % a)
424
427
                    elif optname in alias_opts:
425
428
                        # Replace what's in the alias with what's in the real
426
429
                        # argument
429
432
                        proc_argv.insert(0, a)
430
433
                        continue
431
434
                    else:
432
 
                        raise BzrCommandError('repeated option %r' % a)
 
435
                        raise BzrError('repeated option %r' % a)
433
436
                    
434
437
                option_obj = cmd_options[optname]
435
438
                optargfn = option_obj.type
436
439
                if optargfn:
437
440
                    if optarg == None:
438
441
                        if not proc_argv:
439
 
                            raise BzrCommandError('option %r needs an argument' % a)
 
442
                            raise BzrError('option %r needs an argument' % a)
440
443
                        else:
441
444
                            optarg = proc_argv.pop(0)
442
445
                    opts[optname] = optargfn(optarg)
444
447
                        alias_opts[optname] = optargfn(optarg)
445
448
                else:
446
449
                    if optarg != None:
447
 
                        raise BzrCommandError('option %r takes no argument' % optname)
 
450
                        raise BzrError('option %r takes no argument' % optname)
448
451
                    opts[optname] = True
449
452
                    if proc_aliasarg:
450
453
                        alias_opts[optname] = True
481
484
                raise BzrCommandError("command %r needs one or more %s"
482
485
                        % (cmd, argname.upper()))
483
486
            argdict[argname + '_list'] = args[:-1]
484
 
            args[:-1] = []
 
487
            args[:-1] = []                
485
488
        else:
486
489
            # just a plain arg
487
490
            argname = ap
679
682
def main(argv):
680
683
    import bzrlib.ui
681
684
    from bzrlib.ui.text import TextUIFactory
 
685
    ## bzrlib.trace.enable_default_logging()
 
686
    bzrlib.trace.log_startup(argv)
682
687
    bzrlib.ui.ui_factory = TextUIFactory()
 
688
 
683
689
    argv = [a.decode(bzrlib.user_encoding) for a in argv[1:]]
684
690
    ret = run_bzr_catch_errors(argv)
685
691
    mutter("return code %d", ret)
696
702
    except Exception, e:
697
703
        # used to handle AssertionError and KeyboardInterrupt
698
704
        # specially here, but hopefully they're handled ok by the logger now
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
 
705
        import errno
 
706
        if (isinstance(e, IOError) 
 
707
            and hasattr(e, 'errno')
 
708
            and e.errno == errno.EPIPE):
 
709
            bzrlib.trace.note('broken pipe')
 
710
            return 3
 
711
        else:
 
712
            bzrlib.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
705
718
 
706
719
if __name__ == '__main__':
707
720
    sys.exit(main(sys.argv))