Bug 1427468 - Allow to run mach python without a virtualenv. r=nalexander

Sometimes, one just wants to run a one-off script with access to all
(or most) the libraries available like mozbuild, etc. but without
the weight of the whole virtualenv, which implies having an objdir
setup, etc.

One of my use cases is to run our preprocessor before the objdir is even
setup, and I'd rather not have one automatically created.

--HG--
extra : rebase_source : a6ad30a47ea8e497b274845caf7a9504b9f13282
This commit is contained in:
Mike Hommey 2017-12-31 15:50:29 +09:00
parent bd1eb0ab94
commit 30a3720ffb

View File

@ -7,6 +7,7 @@ from __future__ import absolute_import, print_function, unicode_literals
import argparse
import logging
import os
import sys
import tempfile
from concurrent.futures import (
@ -35,18 +36,29 @@ from mach.decorators import (
class MachCommands(MachCommandBase):
@Command('python', category='devenv',
description='Run Python.')
@CommandArgument('--no-virtualenv', action='store_true',
help='Do not set up a virtualenv')
@CommandArgument('args', nargs=argparse.REMAINDER)
def python(self, args):
def python(self, no_virtualenv, args):
# Avoid logging the command
self.log_manager.terminal_handler.setLevel(logging.CRITICAL)
self._activate_virtualenv()
# Note: subprocess requires native strings in os.environ on Windows.
append_env = {
b'PYTHONDONTWRITEBYTECODE': str('1'),
}
return self.run_process([self.virtualenv_manager.python_path] + args,
if no_virtualenv:
python_path = sys.executable
append_env[b'PYTHONPATH'] = os.pathsep.join(sys.path)
else:
self._activate_virtualenv()
python_path = self.virtualenv_manager.python_path
return self.run_process([python_path] + args,
pass_thru=True, # Allow user to run Python interactively.
ensure_exit_code=False, # Don't throw on non-zero exit code.
# Note: subprocess requires native strings in os.environ on Windows
append_env={b'PYTHONDONTWRITEBYTECODE': str('1')})
append_env=append_env)
@Command('python-test', category='testing',
description='Run Python unit tests with an appropriate test runner.')