~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Robert Collins
  • Date: 2006-02-15 08:11:37 UTC
  • mto: (1534.1.24 integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: robertc@robertcollins.net-20060215081137-4c27377517e96dd1
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
import bzrlib
37
37
import bzrlib.trace
38
 
from bzrlib.trace import mutter, note, log_error, warning
 
38
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
39
39
from bzrlib.errors import (BzrError, 
40
40
                           BzrCheckError,
41
41
                           BzrCommandError,
168
168
        List of argument forms, marked with whether they are optional,
169
169
        repeated, etc.
170
170
 
171
 
                Examples:
172
 
 
173
 
                ['to_location', 'from_branch?', 'file*']
174
 
 
175
 
                'to_location' is required
176
 
                'from_branch' is optional
177
 
                'file' can be specified 0 or more times
 
171
                Examples:
 
172
 
 
173
                ['to_location', 'from_branch?', 'file*']
 
174
 
 
175
                'to_location' is required
 
176
                'from_branch' is optional
 
177
                'file' can be specified 0 or more times
178
178
 
179
179
    takes_options
180
180
        List of options that may be given for this command.  These can
447
447
        os.remove(pfname)
448
448
 
449
449
 
 
450
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
 
451
    from bzrlib.lsprof import profile
 
452
    import cPickle
 
453
    ret, stats = profile(the_callable, *args, **kwargs)
 
454
    stats.sort()
 
455
    if filename is None:
 
456
        stats.pprint()
 
457
    else:
 
458
        stats.freeze()
 
459
        cPickle.dump(stats, open(filename, 'w'), 2)
 
460
        print 'Profile data written to %r.' % filename
 
461
    return ret
 
462
 
450
463
def run_bzr(argv):
451
464
    """Execute a command.
452
465
 
469
482
        other behaviour.)
470
483
 
471
484
    --profile
472
 
        Run under the Python profiler.
 
485
        Run under the Python hotshot profiler.
 
486
 
 
487
    --lsprof
 
488
        Run under the Python lsprof profiler.
473
489
    """
474
490
    argv = [a.decode(bzrlib.user_encoding) for a in argv]
475
491
 
476
 
    opt_profile = opt_no_plugins = opt_builtin = False
 
492
    opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = False
 
493
    opt_lsprof_file = None
477
494
 
478
495
    # --no-plugins is handled specially at a very early stage. We need
479
496
    # to load plugins before doing other command parsing so that they
480
497
    # can override commands, but this needs to happen first.
481
498
 
482
 
    for a in argv:
 
499
    argv_copy = []
 
500
    i = 0
 
501
    while i < len(argv):
 
502
        a = argv[i]
483
503
        if a == '--profile':
484
504
            opt_profile = True
 
505
        elif a == '--lsprof':
 
506
            opt_lsprof = True
 
507
        elif a == '--lsprof-file':
 
508
            opt_lsprof_file = argv[i + 1]
 
509
            i += 1
485
510
        elif a == '--no-plugins':
486
511
            opt_no_plugins = True
487
512
        elif a == '--builtin':
488
513
            opt_builtin = True
 
514
        elif a in ('--quiet', '-q'):
 
515
            be_quiet()
489
516
        else:
490
 
            break
491
 
        argv.remove(a)
 
517
            argv_copy.append(a)
 
518
        i += 1
492
519
 
 
520
    argv = argv_copy
493
521
    if (not argv) or (argv[0] == '--help'):
494
522
        from bzrlib.help import help
495
523
        if len(argv) > 1:
506
534
    if not opt_no_plugins:
507
535
        from bzrlib.plugin import load_plugins
508
536
        load_plugins()
 
537
    else:
 
538
        from bzrlib.plugin import disable_plugins
 
539
        disable_plugins()
509
540
 
510
541
    cmd = str(argv.pop(0))
511
542
 
512
543
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
513
544
 
514
 
    if opt_profile:
515
 
        ret = apply_profiled(cmd_obj.run_argv, argv)
516
 
    else:
517
 
        ret = cmd_obj.run_argv(argv)
518
 
    return ret or 0
 
545
    try:
 
546
        if opt_lsprof:
 
547
            ret = apply_lsprofiled(opt_lsprof_file, cmd_obj.run_argv, argv)
 
548
        elif opt_profile:
 
549
            ret = apply_profiled(cmd_obj.run_argv, argv)
 
550
        else:
 
551
            ret = cmd_obj.run_argv(argv)
 
552
        return ret or 0
 
553
    finally:
 
554
        # reset, in case we may do other commands later within the same process
 
555
        be_quiet(False)
519
556
 
520
557
def display_command(func):
521
558
    """Decorator that suppresses pipe/interrupt errors."""
534
571
            pass
535
572
    return ignore_pipe
536
573
 
 
574
 
537
575
def main(argv):
538
576
    import bzrlib.ui
 
577
    from bzrlib.ui.text import TextUIFactory
 
578
    ## bzrlib.trace.enable_default_logging()
539
579
    bzrlib.trace.log_startup(argv)
540
 
    bzrlib.ui.ui_factory = bzrlib.ui.TextUIFactory()
541
 
 
542
 
    return run_bzr_catch_errors(argv[1:])
 
580
    bzrlib.ui.ui_factory = TextUIFactory()
 
581
    ret = run_bzr_catch_errors(argv[1:])
 
582
    mutter("return code %d", ret)
 
583
    return ret
543
584
 
544
585
 
545
586
def run_bzr_catch_errors(argv):