From 30a3720ffb58fc1ecc4a29666304c1afddfc8b59 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Sun, 31 Dec 2017 15:50:29 +0900 Subject: [PATCH] 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 --- python/mach_commands.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/python/mach_commands.py b/python/mach_commands.py index dc8eb529837b..b3b29df0e3ef 100644 --- a/python/mach_commands.py +++ b/python/mach_commands.py @@ -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.')