~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

 * bzr add now lists how many files were ignored per glob.  add --verbose
   lists the specific files.  (Aaron Bentley)

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
import errno
35
35
 
36
36
import bzrlib
 
37
import bzrlib.trace
 
38
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
37
39
from bzrlib.errors import (BzrError, 
38
40
                           BzrCheckError,
39
41
                           BzrCommandError,
40
42
                           BzrOptionError,
41
43
                           NotBranchError)
 
44
from bzrlib.revisionspec import RevisionSpec
 
45
from bzrlib import BZRDIR
42
46
from bzrlib.option import Option
43
 
from bzrlib.revisionspec import RevisionSpec
44
 
from bzrlib.symbol_versioning import *
45
 
import bzrlib.trace
46
 
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
47
47
 
48
48
plugin_cmds = {}
49
49
 
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
208
208
            r[o.name] = o
209
209
        return r
210
210
 
211
 
    @deprecated_method(zero_eight)
212
211
    def run_argv(self, argv):
213
 
        """Parse command line and run.
214
 
        
215
 
        See run_argv_aliases for the 0.8 and beyond api.
216
 
        """
217
 
        return self.run_argv_aliases(argv)
218
 
 
219
 
    def run_argv_aliases(self, argv, alias_argv=None):
220
 
        """Parse the command line and run with extra aliases in alias_argv."""
221
 
        args, opts = parse_args(self, argv, alias_argv)
 
212
        """Parse command line and run."""
 
213
        args, opts = parse_args(self, argv)
222
214
        if 'help' in opts:  # e.g. bzr add --help
223
215
            from bzrlib.help import help_on_command
224
216
            help_on_command(self.name())
227
219
        allowed_names = self.options().keys()
228
220
        for oname in opts:
229
221
            if oname not in allowed_names:
230
 
                raise BzrCommandError("option '--%s' is not allowed for"
231
 
                                      " command %r" % (oname, self.name()))
 
222
                raise BzrCommandError("option '--%s' is not allowed for command %r"
 
223
                                      % (oname, self.name()))
232
224
        # mix arguments and options into one dictionary
233
225
        cmdargs = _match_argform(self.name(), self.takes_args, args)
234
226
        cmdopts = {}
294
286
        parsed = [spec, None]
295
287
    return parsed
296
288
 
297
 
def parse_args(command, argv, alias_argv=None):
 
289
def parse_args(command, argv):
298
290
    """Parse command line.
299
291
    
300
292
    Arguments and options are parsed at this level before being passed
305
297
    # TODO: chop up this beast; make it a method of the Command
306
298
    args = []
307
299
    opts = {}
308
 
    alias_opts = {}
309
300
 
310
301
    cmd_options = command.options()
311
302
    argsover = False
312
 
    proc_aliasarg = True # Are we processing alias_argv now?
313
 
    for proc_argv in alias_argv, argv:
314
 
        while proc_argv:
315
 
            a = proc_argv.pop(0)
316
 
            if argsover:
317
 
                args.append(a)
318
 
                continue
319
 
            elif a == '--':
320
 
                # We've received a standalone -- No more flags
321
 
                argsover = True
322
 
                continue
323
 
            if a[0] == '-':
324
 
                # option names must not be unicode
325
 
                a = str(a)
326
 
                optarg = None
327
 
                if a[1] == '-':
328
 
                    mutter("  got option %r", a)
329
 
                    if '=' in a:
330
 
                        optname, optarg = a[2:].split('=', 1)
331
 
                    else:
332
 
                        optname = a[2:]
333
 
                    if optname not in cmd_options:
334
 
                        raise BzrOptionError('unknown long option %r for'
335
 
                                             ' command %s' % 
336
 
                                             (a, command.name()))
337
 
                else:
338
 
                    shortopt = a[1:]
339
 
                    if shortopt in Option.SHORT_OPTIONS:
340
 
                        # Multi-character options must have a space to delimit
341
 
                        # their value
342
 
                        # ^^^ what does this mean? mbp 20051014
343
 
                        optname = Option.SHORT_OPTIONS[shortopt].name
344
 
                    else:
345
 
                        # Single character short options, can be chained,
346
 
                        # and have their value appended to their name
347
 
                        shortopt = a[1:2]
348
 
                        if shortopt not in Option.SHORT_OPTIONS:
349
 
                            # We didn't find the multi-character name, and we
350
 
                            # didn't find the single char name
351
 
                            raise BzrError('unknown short option %r' % a)
352
 
                        optname = Option.SHORT_OPTIONS[shortopt].name
 
303
    while argv:
 
304
        a = argv.pop(0)
 
305
        if argsover:
 
306
            args.append(a)
 
307
            continue
 
308
        elif a == '--':
 
309
            # We've received a standalone -- No more flags
 
310
            argsover = True
 
311
            continue
 
312
        if a[0] == '-':
 
313
            # option names must not be unicode
 
314
            a = str(a)
 
315
            optarg = None
 
316
            if a[1] == '-':
 
317
                mutter("  got option %r", a)
 
318
                if '=' in a:
 
319
                    optname, optarg = a[2:].split('=', 1)
 
320
                else:
 
321
                    optname = a[2:]
 
322
                if optname not in cmd_options:
 
323
                    raise BzrOptionError('unknown long option %r for command %s'
 
324
                        % (a, command.name()))
 
325
            else:
 
326
                shortopt = a[1:]
 
327
                if shortopt in Option.SHORT_OPTIONS:
 
328
                    # Multi-character options must have a space to delimit
 
329
                    # their value
 
330
                    # ^^^ what does this mean? mbp 20051014
 
331
                    optname = Option.SHORT_OPTIONS[shortopt].name
 
332
                else:
 
333
                    # Single character short options, can be chained,
 
334
                    # and have their value appended to their name
 
335
                    shortopt = a[1:2]
 
336
                    if shortopt not in Option.SHORT_OPTIONS:
 
337
                        # We didn't find the multi-character name, and we
 
338
                        # didn't find the single char name
 
339
                        raise BzrError('unknown short option %r' % a)
 
340
                    optname = Option.SHORT_OPTIONS[shortopt].name
353
341
 
354
 
                        if a[2:]:
355
 
                            # There are extra things on this option
356
 
                            # see if it is the value, or if it is another
357
 
                            # short option
358
 
                            optargfn = Option.OPTIONS[optname].type
359
 
                            if optargfn is None:
360
 
                                # This option does not take an argument, so the
361
 
                                # next entry is another short option, pack it
362
 
                                # back into the list
363
 
                                proc_argv.insert(0, '-' + a[2:])
364
 
                            else:
365
 
                                # This option takes an argument, so pack it
366
 
                                # into the array
367
 
                                optarg = a[2:]
 
342
                    if a[2:]:
 
343
                        # There are extra things on this option
 
344
                        # see if it is the value, or if it is another
 
345
                        # short option
 
346
                        optargfn = Option.OPTIONS[optname].type
 
347
                        if optargfn is None:
 
348
                            # This option does not take an argument, so the
 
349
                            # next entry is another short option, pack it back
 
350
                            # into the list
 
351
                            argv.insert(0, '-' + a[2:])
 
352
                        else:
 
353
                            # This option takes an argument, so pack it
 
354
                            # into the array
 
355
                            optarg = a[2:]
 
356
            
 
357
                if optname not in cmd_options:
 
358
                    raise BzrOptionError('unknown short option %r for command'
 
359
                        ' %s' % (shortopt, command.name()))
 
360
            if optname in opts:
 
361
                # XXX: Do we ever want to support this, e.g. for -r?
 
362
                raise BzrError('repeated option %r' % a)
368
363
                
369
 
                    if optname not in cmd_options:
370
 
                        raise BzrOptionError('unknown short option %r for'
371
 
                                             ' command %s' % 
372
 
                                             (shortopt, command.name()))
373
 
                if optname in opts:
374
 
                    # XXX: Do we ever want to support this, e.g. for -r?
375
 
                    if proc_aliasarg:
376
 
                        raise BzrError('repeated option %r' % a)
377
 
                    elif optname in alias_opts:
378
 
                        # Replace what's in the alias with what's in the real
379
 
                        # argument
380
 
                        del alias_opts[optname]
381
 
                        del opts[optname]
382
 
                        proc_argv.insert(0, a)
383
 
                        continue
 
364
            option_obj = cmd_options[optname]
 
365
            optargfn = option_obj.type
 
366
            if optargfn:
 
367
                if optarg == None:
 
368
                    if not argv:
 
369
                        raise BzrError('option %r needs an argument' % a)
384
370
                    else:
385
 
                        raise BzrError('repeated option %r' % a)
386
 
                    
387
 
                option_obj = cmd_options[optname]
388
 
                optargfn = option_obj.type
389
 
                if optargfn:
390
 
                    if optarg == None:
391
 
                        if not proc_argv:
392
 
                            raise BzrError('option %r needs an argument' % a)
393
 
                        else:
394
 
                            optarg = proc_argv.pop(0)
395
 
                    opts[optname] = optargfn(optarg)
396
 
                    if proc_aliasarg:
397
 
                        alias_opts[optname] = optargfn(optarg)
398
 
                else:
399
 
                    if optarg != None:
400
 
                        raise BzrError('option %r takes no argument' % optname)
401
 
                    opts[optname] = True
402
 
                    if proc_aliasarg:
403
 
                        alias_opts[optname] = True
 
371
                        optarg = argv.pop(0)
 
372
                opts[optname] = optargfn(optarg)
404
373
            else:
405
 
                args.append(a)
406
 
        proc_aliasarg = False # Done with alias argv
 
374
                if optarg != None:
 
375
                    raise BzrError('option %r takes no argument' % optname)
 
376
                opts[optname] = True
 
377
        else:
 
378
            args.append(a)
407
379
    return args, opts
408
380
 
409
381
 
475
447
        os.remove(pfname)
476
448
 
477
449
 
478
 
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
 
450
def apply_lsprofiled(the_callable, *args, **kwargs):
479
451
    from bzrlib.lsprof import profile
480
 
    import cPickle
481
 
    ret, stats = profile(the_callable, *args, **kwargs)
 
452
    ret,stats = profile(the_callable,*args,**kwargs)
482
453
    stats.sort()
483
 
    if filename is None:
484
 
        stats.pprint()
485
 
    else:
486
 
        stats.freeze()
487
 
        cPickle.dump(stats, open(filename, 'w'), 2)
488
 
        print 'Profile data written to %r.' % filename
 
454
    stats.pprint()
489
455
    return ret
490
456
 
491
 
 
492
 
def get_alias(cmd):
493
 
    """Return an expanded alias, or None if no alias exists"""
494
 
    import bzrlib.config
495
 
    alias = bzrlib.config.GlobalConfig().get_alias(cmd)
496
 
    if (alias):
497
 
        return alias.split(' ')
498
 
    return None
499
 
 
500
 
 
501
457
def run_bzr(argv):
502
458
    """Execute a command.
503
459
 
515
471
    --no-plugins
516
472
        Do not load plugin modules at all
517
473
 
518
 
    --no-aliases
519
 
        Do not allow aliases
520
 
 
521
474
    --builtin
522
475
        Only use builtin commands.  (Plugins are still allowed to change
523
476
        other behaviour.)
530
483
    """
531
484
    argv = [a.decode(bzrlib.user_encoding) for a in argv]
532
485
 
533
 
    opt_lsprof = opt_profile = opt_no_plugins = opt_builtin =  \
534
 
                opt_no_aliases = False
535
 
    opt_lsprof_file = None
 
486
    opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = False
536
487
 
537
488
    # --no-plugins is handled specially at a very early stage. We need
538
489
    # to load plugins before doing other command parsing so that they
539
490
    # can override commands, but this needs to happen first.
540
491
 
541
 
    argv_copy = []
542
 
    i = 0
543
 
    while i < len(argv):
544
 
        a = argv[i]
 
492
    for a in argv:
545
493
        if a == '--profile':
546
494
            opt_profile = True
547
495
        elif a == '--lsprof':
548
496
            opt_lsprof = True
549
 
        elif a == '--lsprof-file':
550
 
            opt_lsprof_file = argv[i + 1]
551
 
            i += 1
552
497
        elif a == '--no-plugins':
553
498
            opt_no_plugins = True
554
 
        elif a == '--no-aliases':
555
 
            opt_no_aliases = True
556
499
        elif a == '--builtin':
557
500
            opt_builtin = True
558
501
        elif a in ('--quiet', '-q'):
559
502
            be_quiet()
560
503
        else:
561
 
            argv_copy.append(a)
562
 
        i += 1
 
504
            continue
 
505
        argv.remove(a)
563
506
 
564
 
    argv = argv_copy
565
507
    if (not argv) or (argv[0] == '--help'):
566
508
        from bzrlib.help import help
567
509
        if len(argv) > 1:
578
520
    if not opt_no_plugins:
579
521
        from bzrlib.plugin import load_plugins
580
522
        load_plugins()
581
 
    else:
582
 
        from bzrlib.plugin import disable_plugins
583
 
        disable_plugins()
584
 
 
585
 
    alias_argv = None
586
 
 
587
 
    if not opt_no_aliases:
588
 
        alias_argv = get_alias(argv[0])
589
 
        if alias_argv:
590
 
            alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
591
 
            argv[0] = alias_argv.pop(0)
592
523
 
593
524
    cmd = str(argv.pop(0))
594
525
 
595
526
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
596
 
    if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
597
 
        run = cmd_obj.run_argv
598
 
        run_argv = [argv]
599
 
    else:
600
 
        run = cmd_obj.run_argv_aliases
601
 
        run_argv = [argv, alias_argv]
602
527
 
603
528
    try:
604
529
        if opt_lsprof:
605
 
            ret = apply_lsprofiled(opt_lsprof_file, run, *run_argv)
 
530
            ret = apply_lsprofiled(cmd_obj.run_argv, argv)
606
531
        elif opt_profile:
607
 
            ret = apply_profiled(run, *run_argv)
 
532
            ret = apply_profiled(cmd_obj.run_argv, argv)
608
533
        else:
609
 
            ret = run(*run_argv)
 
534
            ret = cmd_obj.run_argv(argv)
610
535
        return ret or 0
611
536
    finally:
612
537
        # reset, in case we may do other commands later within the same process