Bug 1427468 - Allow to run mach python without a virtualenv. r=nalexander
authorMike Hommey <mh+mozilla@glandium.org>
Sun, 31 Dec 2017 15:50:29 +0900
changeset 397709 aa60e61cdc9581821fc5d6a1246cdd170b2e52ab
parent 397708 8f74ddd39f610c992426a5e03158d89430788a31
child 397710 0059514e73b5326f09351c8f05a1a7455cabf81e
push id98592
push usernerli@mozilla.com
push dateThu, 04 Jan 2018 09:56:38 +0000
treeherdermozilla-inbound@f78a83244fbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs1427468
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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.
python/mach_commands.py
--- a/python/mach_commands.py
+++ b/python/mach_commands.py
@@ -2,16 +2,17 @@
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import absolute_import, print_function, unicode_literals
 
 import argparse
 import logging
 import os
+import sys
 import tempfile
 
 from concurrent.futures import (
     ThreadPoolExecutor,
     as_completed,
     thread,
 )
 
@@ -30,28 +31,39 @@ from mach.decorators import (
     Command,
 )
 
 
 @CommandProvider
 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.')
     @CommandArgument('--verbose',
                      default=False,
                      action='store_true',
                      help='Verbose output.')
     @CommandArgument('--stop',