Bug 985141 - Allow mach commands to specify what virtualenv they should use. r=mhentges,dmajor

Now you can pass the `virtualenv_name` kwarg to the `Command` decorator which will configure the `_virtualenv_manager` accordingly.

Differential Revision: https://phabricator.services.mozilla.com/D86256
This commit is contained in:
Ricky Stewart 2020-08-13 22:19:44 +00:00
Родитель fd115ffc27
Коммит 2eae41178a
6 изменённых файлов: 21 добавлений и 20 удалений

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

@ -13,11 +13,8 @@ from mozbuild.base import MachCommandBase
class _MachCommand(object):
"""Container for mach command metadata.
"""Container for mach command metadata."""
Mach commands contain lots of attributes. This class exists to capture them
in a sane way so tuples, etc aren't used instead.
"""
__slots__ = (
# Content from decorator arguments to define the command.
'name',
@ -28,6 +25,7 @@ class _MachCommand(object):
'_parser',
'arguments',
'argument_group_names',
'virtualenv_name',
# By default, subcommands will be sorted. If this is set to
# 'declaration', they will be left in declaration order.
@ -56,7 +54,7 @@ class _MachCommand(object):
def __init__(self, name=None, subcommand=None, category=None,
description=None, conditions=None, parser=None,
order=None):
order=None, virtualenv_name=None):
self.name = name
self.subcommand = subcommand
self.category = category
@ -65,6 +63,7 @@ class _MachCommand(object):
self._parser = parser
self.arguments = []
self.argument_group_names = []
self.virtualenv_name = virtualenv_name
self.order = order
self.cls = None

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

@ -264,7 +264,7 @@ class CommandAction(argparse.Action):
# out for the current context or not. Condition functions can be
# applied to the command's decorator.
if handler.conditions:
instance = handler.cls(self._context)
instance = handler.cls(self._context, handler.virtualenv_name)
is_filtered = False
for c in handler.conditions:

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

@ -72,7 +72,7 @@ class MachRegistrar(object):
prerun(context, handler, args=kwargs)
context.handler = handler
return cls(context)
return cls(context, handler.virtualenv_name)
@classmethod
def _fail_conditions(_, handler, instance):

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

@ -106,7 +106,7 @@ class MozbuildObject(ProcessExecutionMixin):
"""
def __init__(self, topsrcdir, settings, log_manager, topobjdir=None,
mozconfig=MozconfigLoader.AUTODETECT):
mozconfig=MozconfigLoader.AUTODETECT, virtualenv_name=None):
"""Create a new Mozbuild object instance.
Instances are bound to a source directory, a ConfigSettings instance,
@ -123,6 +123,8 @@ class MozbuildObject(ProcessExecutionMixin):
self._topobjdir = mozpath.normsep(topobjdir) if topobjdir else topobjdir
self._mozconfig = mozconfig
self._config_environment = None
self._virtualenv_name = virtualenv_name or (
'init_py3' if six.PY3 else 'init')
self._virtualenv_manager = None
@classmethod
@ -275,12 +277,10 @@ class MozbuildObject(ProcessExecutionMixin):
from .virtualenv import VirtualenvManager
if self._virtualenv_manager is None:
name = "init"
if six.PY3:
name += "_py3"
self._virtualenv_manager = VirtualenvManager(
self.topsrcdir,
os.path.join(self.topobjdir, '_virtualenvs', name),
os.path.join(self.topobjdir, '_virtualenvs',
self._virtualenv_name),
sys.stdout,
os.path.join(self.topsrcdir, 'build', 'virtualenv_packages.txt')
)
@ -874,7 +874,7 @@ class MachCommandBase(MozbuildObject):
without having to change everything that inherits from it.
"""
def __init__(self, context):
def __init__(self, context, virtualenv_name):
# Attempt to discover topobjdir through environment detection, as it is
# more reliable than mozconfig when cwd is inside an objdir.
topsrcdir = context.topdir
@ -915,8 +915,10 @@ class MachCommandBase(MozbuildObject):
print(e)
sys.exit(1)
MozbuildObject.__init__(self, topsrcdir, context.settings,
context.log_manager, topobjdir=topobjdir)
MozbuildObject.__init__(
self, topsrcdir, context.settings,
context.log_manager, topobjdir=topobjdir,
virtualenv_name=virtualenv_name)
self._mach_context = context

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

@ -2415,7 +2415,7 @@ class StaticAnalysis(MachCommandBase):
def _get_config_environment(self):
ran_configure = False
config = None
builder = Build(self._mach_context)
builder = Build(self._mach_context, None)
try:
config = self.config_environment
@ -2475,7 +2475,7 @@ class StaticAnalysis(MachCommandBase):
"Looks like a clang compilation database has not been "
"created yet, creating it now..."
)
builder = Build(self._mach_context)
builder = Build(self._mach_context, None)
rc = builder.build_backend(["CompileDB"], verbose=verbose)
if rc != 0:
return rc
@ -2486,7 +2486,7 @@ class StaticAnalysis(MachCommandBase):
def on_line(line):
self.log(logging.INFO, "build_output", {"line": line}, "{line}")
builder = Build(self._mach_context)
builder = Build(self._mach_context, None)
# First install what we can through install manifests.
rc = builder._run_make(
directory=self.topobjdir,

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

@ -212,7 +212,7 @@ class TestMozbuildObject(unittest.TestCase):
context.log_manager = None
context.detect_virtualenv_mozinfo = False
o = MachCommandBase(context)
o = MachCommandBase(context, None)
self.assertEqual(o.topobjdir, mozpath.normsep(topobjdir))
self.assertEqual(o.topsrcdir, mozpath.normsep(topsrcdir))
@ -282,7 +282,7 @@ class TestMozbuildObject(unittest.TestCase):
sys.stdout = StringIO()
try:
with self.assertRaises(SystemExit):
MachCommandBase(context)
MachCommandBase(context, None)
self.assertTrue(sys.stdout.getvalue().startswith(
'Ambiguous object directory detected.'))