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

  • Committer: Jelmer Vernooij
  • Date: 2019-03-05 07:32:38 UTC
  • mto: (7290.1.21 work)
  • mto: This revision was merged to the branch mainline in revision 7311.
  • Revision ID: jelmer@jelmer.uk-20190305073238-zlqn981opwnqsmzi
Add appveyor configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
# TODO: Define arguments by objects, rather than just using names.
18
20
# Those objects can specify the expected type of the argument, which
19
21
# would help with validation and shell completion.  They could also provide
21
23
 
22
24
# TODO: Specific "examples" property on commands for consistent formatting.
23
25
 
24
 
import contextlib
25
26
import os
26
27
import sys
27
28
 
37
38
 
38
39
import breezy
39
40
from breezy import (
 
41
    cleanup,
40
42
    cmdline,
41
43
    debug,
42
44
    trace,
50
52
from .option import Option
51
53
from .plugin import disable_plugins, load_plugins, plugin_name
52
54
from . import errors, registry
53
 
 
54
 
 
55
 
class BzrOptionError(errors.CommandError):
 
55
from .sixish import (
 
56
    string_types,
 
57
    )
 
58
 
 
59
 
 
60
class BzrOptionError(errors.BzrCommandError):
56
61
 
57
62
    _fmt = "Error in command line options"
58
63
 
259
264
        names.update(cmd.aliases)
260
265
    # candidate: modified levenshtein distance against cmd_name.
261
266
    costs = {}
262
 
    import patiencediff
 
267
    from . import patiencediff
263
268
    for name in sorted(names):
264
269
        matcher = patiencediff.PatienceSequenceMatcher(None, cmd_name, name)
265
270
        distance = 0.0
298
303
        # No command found, see if this was a typo
299
304
        candidate = guess_command(cmd_name)
300
305
        if candidate is not None:
301
 
            raise errors.CommandError(
 
306
            raise errors.BzrCommandError(
302
307
                gettext('unknown command "%s". Perhaps you meant "%s"')
303
308
                % (cmd_name, candidate))
304
 
        raise errors.CommandError(gettext('unknown command "%s"')
 
309
        raise errors.BzrCommandError(gettext('unknown command "%s"')
305
310
                                     % cmd_name)
306
311
 
307
312
 
491
496
 
492
497
        Functions will be called in LIFO order.
493
498
        """
494
 
        self._exit_stack.callback(cleanup_func, *args, **kwargs)
 
499
        self._operation.add_cleanup(cleanup_func, *args, **kwargs)
495
500
 
496
501
    def cleanup_now(self):
497
502
        """Execute and empty pending cleanup functions immediately.
506
511
        as it releases all resources, this may release locks that the command
507
512
        wants to hold, so use should be done with care.
508
513
        """
509
 
        self._exit_stack.close()
510
 
 
511
 
    def enter_context(self, cm):
512
 
        return self._exit_stack.enter_context(cm)
 
514
        self._operation.cleanup_now()
513
515
 
514
516
    def _usage(self):
515
517
        """Return single-line grammar for this command.
711
713
        r = Option.STD_OPTIONS.copy()
712
714
        std_names = set(r)
713
715
        for o in self.takes_options:
714
 
            if isinstance(o, str):
 
716
            if isinstance(o, string_types):
715
717
                o = option.Option.OPTIONS[o]
716
718
            r[o.name] = o
717
719
            if o.name in std_names:
777
779
        def run(*args, **kwargs):
778
780
            for hook in Command.hooks['pre_command']:
779
781
                hook(self)
 
782
            self._operation = cleanup.OperationWithCleanups(class_run)
780
783
            try:
781
 
                with contextlib.ExitStack() as self._exit_stack:
782
 
                    return class_run(*args, **kwargs)
 
784
                return self._operation.run_simple(*args, **kwargs)
783
785
            finally:
 
786
                del self._operation
784
787
                for hook in Command.hooks['post_command']:
785
788
                    hook(self)
786
789
        self.run = run
796
799
        an exception to raise up.
797
800
 
798
801
        This method is automatically wrapped by Command.__init__ with a
799
 
        ExitStack, stored as self._exit_stack. This can be used
 
802
        cleanup operation, stored as self._operation. This can be used
800
803
        via self.add_cleanup to perform automatic cleanups at the end of
801
804
        run().
802
805
 
910
913
    try:
911
914
        options, args = parser.parse_args(args)
912
915
    except UnicodeEncodeError:
913
 
        raise errors.CommandError(
 
916
        raise errors.BzrCommandError(
914
917
            gettext('Only ASCII permitted in option names'))
915
918
 
916
919
    opts = dict((k, v) for k, v in options.__dict__.items() if
935
938
                argdict[argname + '_list'] = None
936
939
        elif ap[-1] == '+':
937
940
            if not args:
938
 
                raise errors.CommandError(gettext(
 
941
                raise errors.BzrCommandError(gettext(
939
942
                    "command {0!r} needs one or more {1}").format(
940
943
                    cmd, argname.upper()))
941
944
            else:
943
946
                args = []
944
947
        elif ap[-1] == '$':  # all but one
945
948
            if len(args) < 2:
946
 
                raise errors.CommandError(
 
949
                raise errors.BzrCommandError(
947
950
                    gettext("command {0!r} needs one or more {1}").format(
948
951
                        cmd, argname.upper()))
949
952
            argdict[argname + '_list'] = args[:-1]
952
955
            # just a plain arg
953
956
            argname = ap
954
957
            if not args:
955
 
                raise errors.CommandError(
 
958
                raise errors.BzrCommandError(
956
959
                    gettext("command {0!r} requires argument {1}").format(
957
960
                        cmd, argname.upper()))
958
961
            else:
959
962
                argdict[argname] = args.pop(0)
960
963
 
961
964
    if args:
962
 
        raise errors.CommandError(gettext(
 
965
        raise errors.BzrCommandError(gettext(
963
966
            "extra argument to command {0}: {1}").format(
964
967
            cmd, args[0]))
965
968
 
969
972
def apply_coveraged(the_callable, *args, **kwargs):
970
973
    import coverage
971
974
    cov = coverage.Coverage()
972
 
    try:
973
 
        config_file = cov.config.config_file
974
 
    except AttributeError:  # older versions of coverage
975
 
        config_file = cov.config_file
976
 
    os.environ['COVERAGE_PROCESS_START'] = config_file
 
975
    os.environ['COVERAGE_PROCESS_START'] = cov.config_file
977
976
    cov.start()
978
977
    try:
979
978
        return exception_to_return_code(the_callable, *args, **kwargs)
1155
1154
    debug.set_debug_flags_from_config()
1156
1155
 
1157
1156
    if not opt_no_plugins:
1158
 
        from breezy import config
1159
 
        c = config.GlobalConfig()
1160
 
        warn_load_problems = not c.suppress_warning('plugin_load_failure')
1161
 
        load_plugins(warn_load_problems=warn_load_problems)
 
1157
        load_plugins()
1162
1158
    else:
1163
1159
        disable_plugins()
1164
1160
 
1255
1251
 
1256
1252
def _specified_or_unicode_argv(argv):
1257
1253
    # For internal or testing use, argv can be passed.  Otherwise, get it from
1258
 
    # the process arguments.
 
1254
    # the process arguments in a unicode-safe way.
1259
1255
    if argv is None:
1260
 
        return sys.argv[1:]
 
1256
        return osutils.get_unicode_argv()
1261
1257
    new_argv = []
1262
1258
    try:
1263
1259
        # ensure all arguments are unicode strings
1264
1260
        for a in argv:
1265
 
            if not isinstance(a, str):
 
1261
            if not isinstance(a, string_types):
1266
1262
                raise ValueError('not native str or unicode: %r' % (a,))
 
1263
            if isinstance(a, bytes):
 
1264
                # For Python 2 only allow ascii native strings
 
1265
                a = a.decode('ascii')
1267
1266
            new_argv.append(a)
1268
1267
    except (ValueError, UnicodeDecodeError):
1269
1268
        raise errors.BzrError("argv should be list of unicode strings.")