Bug 1577501 - [mach] Make sure ./mach help <command> uses the correct Python, r=glandium

Some commands use external argument parsers, so invoking |mach help <command>| will import
external modules (which may only be Python 2 compatible).

This makes sure that we detect the actual subcommand we're generating help for
and use the proper Python.

A much simpler solution would have been to run |mach help| with Python 2 all
the time. However, as we convert things to Python 3 this would have meant that
Python 3 only code would blow up. This would have forced us to continue
supporting Python 2, even for Python 3-only commands.

Differential Revision: https://phabricator.services.mozilla.com/D43989

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andrew Halberstadt 2019-09-03 00:39:22 +00:00
Родитель 5bc93ccd37
Коммит 23aca97aa6
3 изменённых файлов: 22 добавлений и 11 удалений

9
mach
Просмотреть файл

@ -138,8 +138,15 @@ run_py() {
}
first_arg=$1
if [ "$first_arg" = "help" ]; then
# When running `./mach help <command>`, the correct Python for <command>
# needs to be used.
first_arg=$2
fi
if [ -z "$first_arg" ]; then
run_py python3
# User ran `./mach` or `./mach help`, use Python 3.
run_py python3 "$@"
fi
case "${first_arg}" in

Просмотреть файл

@ -108,7 +108,7 @@ class CommandAction(argparse.Action):
if command == 'help':
if args and args[0] not in ['-h', '--help']:
# Make sure args[0] is indeed a command.
self._handle_command_help(parser, args[0], args)
self._handle_command_help(parser, args[0], args, namespace.print_command)
else:
self._handle_main_help(parser, namespace.verbose)
sys.exit(0)
@ -118,10 +118,10 @@ class CommandAction(argparse.Action):
# -- is in command arguments
if '-h' in args[:args.index('--')] or '--help' in args[:args.index('--')]:
# Honor -h or --help only if it appears before --
self._handle_command_help(parser, command, args)
self._handle_command_help(parser, command, args, namespace.print_command)
sys.exit(0)
else:
self._handle_command_help(parser, command, args)
self._handle_command_help(parser, command, args, namespace.print_command)
sys.exit(0)
else:
raise NoCommandError()
@ -137,6 +137,12 @@ class CommandAction(argparse.Action):
# Try to find similar commands, may raise UnknownCommandError.
command = self._suggest_command(command)
# This is used by the `mach` driver to find the command name amidst
# global arguments.
if namespace.print_command:
print(command)
sys.exit(0)
handler = self._mach_registrar.command_handlers.get(command)
usage = '%(prog)s [global arguments] ' + command + \
@ -304,12 +310,16 @@ class CommandAction(argparse.Action):
group = extra_groups[group_name]
group.add_argument(*arg[0], **arg[1])
def _handle_command_help(self, parser, command, args):
def _handle_command_help(self, parser, command, args, print_command):
handler = self._mach_registrar.command_handlers.get(command)
if not handler:
raise UnknownCommandError(command, 'query')
if print_command:
print(command)
sys.exit(0)
if handler.subcommand_handlers:
self._handle_subcommand_help(parser, handler, args)
return

Просмотреть файл

@ -434,12 +434,6 @@ To see more help for a specific command, run:
handler = getattr(args, 'mach_handler')
# This is used by the `mach` driver to find the command name amidst
# global arguments.
if args.print_command:
print(handler.name)
sys.exit(0)
# Add JSON logging to a file if requested.
if args.logfile:
self.log_manager.add_json_handler(args.logfile)