/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: Martin Pool
  • Date: 2005-07-27 21:51:24 UTC
  • Revision ID: mbp@sourcefrog.net-20050727215124-4ebbdcb49f7bc0e3
- Factor away _parse_master_args, which seems a bit overcomplicated.

  Instead just look for --profile, --no-plugins or --builtin at start
  of the command.  

  Incidentally fix up handling of --version which was previously 
  broken.

- Add tests for 'bzr --version'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1665
1665
    return argdict
1666
1666
 
1667
1667
 
1668
 
def _parse_master_args(argv):
1669
 
    """Parse the arguments that always go with the original command.
1670
 
    These are things like bzr --no-plugins, etc.
1671
 
 
1672
 
    There are now 2 types of option flags. Ones that come *before* the command,
1673
 
    and ones that come *after* the command.
1674
 
    Ones coming *before* the command are applied against all possible commands.
1675
 
    And are generally applied before plugins are loaded.
1676
 
 
1677
 
    The current list are:
1678
 
        --builtin   Allow plugins to load, but don't let them override builtin commands,
1679
 
                    they will still be allowed if they do not override a builtin.
1680
 
        --no-plugins    Don't load any plugins. This lets you get back to official source
1681
 
                        behavior.
1682
 
        --profile   Enable the hotspot profile before running the command.
1683
 
                    For backwards compatibility, this is also a non-master option.
1684
 
        --version   Spit out the version of bzr that is running and exit.
1685
 
                    This is also a non-master option.
1686
 
        --help      Run help and exit, also a non-master option (I think that should stay, though)
1687
 
 
1688
 
    >>> argv, opts = _parse_master_args(['--test'])
1689
 
    Traceback (most recent call last):
1690
 
    ...
1691
 
    BzrCommandError: Invalid master option: 'test'
1692
 
    >>> argv, opts = _parse_master_args(['--version', 'command'])
1693
 
    >>> print argv
1694
 
    ['command']
1695
 
    >>> print opts['version']
1696
 
    True
1697
 
    >>> argv, opts = _parse_master_args(['--profile', 'command', '--more-options'])
1698
 
    >>> print argv
1699
 
    ['command', '--more-options']
1700
 
    >>> print opts['profile']
1701
 
    True
1702
 
    >>> argv, opts = _parse_master_args(['--no-plugins', 'command'])
1703
 
    >>> print argv
1704
 
    ['command']
1705
 
    >>> print opts['no-plugins']
1706
 
    True
1707
 
    >>> print opts['profile']
1708
 
    False
1709
 
    >>> argv, opts = _parse_master_args(['command', '--profile'])
1710
 
    >>> print argv
1711
 
    ['command', '--profile']
1712
 
    >>> print opts['profile']
1713
 
    False
1714
 
    """
1715
 
    master_opts = {'builtin':False,
1716
 
        'no-plugins':False,
1717
 
        'version':False,
1718
 
        'profile':False,
1719
 
        'help':False
1720
 
    }
1721
 
 
1722
 
    for arg in argv[:]:
1723
 
        if arg[:2] != '--': # at the first non-option, we return the rest
1724
 
            break
1725
 
        arg = arg[2:] # Remove '--'
1726
 
        if arg not in master_opts:
1727
 
            # We could say that this is not an error, that we should
1728
 
            # just let it be handled by the main section instead
1729
 
            raise BzrCommandError('Invalid master option: %r' % arg)
1730
 
        argv.pop(0) # We are consuming this entry
1731
 
        master_opts[arg] = True
1732
 
    return argv, master_opts
1733
 
 
1734
 
 
1735
1668
 
1736
1669
def run_bzr(argv):
1737
1670
    """Execute a command.
1740
1673
    logging and error handling.  
1741
1674
    
1742
1675
    argv
1743
 
       The command-line arguments, without the program name.
 
1676
       The command-line arguments, without the program name from argv[0]
1744
1677
    
1745
1678
    Returns a command status or raises an exception.
 
1679
 
 
1680
    Special master options: these must come before the command because
 
1681
    they control how the command is interpreted.
 
1682
 
 
1683
    --no-plugins
 
1684
        Do not load plugin modules at all
 
1685
 
 
1686
    --builtin
 
1687
        Only use builtin commands.  (Plugins are still allowed to change
 
1688
        other behaviour.)
 
1689
 
 
1690
    --profile
 
1691
        Run under the Python profiler.
1746
1692
    """
 
1693
    
1747
1694
    argv = [a.decode(bzrlib.user_encoding) for a in argv]
1748
1695
 
1749
 
    # some options like --builtin and --no-plugins have special effects
1750
 
    argv, master_opts = _parse_master_args(argv)
1751
 
    if not master_opts['no-plugins']:
 
1696
    opt_profile = opt_no_plugins = opt_builtin = False
 
1697
 
 
1698
    # --no-plugins is handled specially at a very early stage. We need
 
1699
    # to load plugins before doing other command parsing so that they
 
1700
    # can override commands, but this needs to happen first.
 
1701
 
 
1702
    for a in argv[:]:
 
1703
        if a == '--profile':
 
1704
            opt_profile = True
 
1705
        elif a == '--no-plugins':
 
1706
            opt_no_plugins = True
 
1707
        elif a == '--builtin':
 
1708
            opt_builtin = True
 
1709
        else:
 
1710
            break
 
1711
        argv.remove(a)
 
1712
 
 
1713
    if not opt_no_plugins:
1752
1714
        from bzrlib.plugin import load_plugins
1753
1715
        load_plugins()
1754
1716
 
1755
1717
    args, opts = parse_args(argv)
1756
1718
 
1757
 
    if master_opts.get('help') or 'help' in opts:
 
1719
    if 'help' in opts:
1758
1720
        from bzrlib.help import help
1759
 
        if argv:
1760
 
            help(argv[0])
 
1721
        if args:
 
1722
            help(args[0])
1761
1723
        else:
1762
1724
            help()
1763
1725
        return 0            
1766
1728
        show_version()
1767
1729
        return 0
1768
1730
    
1769
 
    if args and args[0] == 'builtin':
1770
 
        include_plugins=False
1771
 
        args = args[1:]
1772
 
    
1773
 
    try:
1774
 
        cmd = str(args.pop(0))
1775
 
    except IndexError:
 
1731
    if not args:
1776
1732
        print >>sys.stderr, "please try 'bzr help' for help"
1777
1733
        return 1
1778
 
 
1779
 
    plugins_override = not (master_opts['builtin'])
1780
 
    canonical_cmd, cmd_class = get_cmd_class(cmd, plugins_override=plugins_override)
1781
 
 
1782
 
    profile = master_opts['profile']
1783
 
    # For backwards compatibility, I would rather stick with --profile being a
1784
 
    # master/global option
1785
 
    if 'profile' in opts:
1786
 
        profile = True
1787
 
        del opts['profile']
 
1734
    
 
1735
    cmd = str(args.pop(0))
 
1736
 
 
1737
    canonical_cmd, cmd_class = \
 
1738
                   get_cmd_class(cmd, plugins_override=not opt_builtin)
1788
1739
 
1789
1740
    # check options are reasonable
1790
1741
    allowed = cmd_class.takes_options
1799
1750
    for k, v in opts.items():
1800
1751
        cmdopts[k.replace('-', '_')] = v
1801
1752
 
1802
 
    if profile:
 
1753
    if opt_profile:
1803
1754
        import hotshot, tempfile
1804
1755
        pffileno, pfname = tempfile.mkstemp()
1805
1756
        try: