diff --git a/build/mach_initialize.py b/build/mach_initialize.py index c47292f5739d..2c4669d0bbf6 100644 --- a/build/mach_initialize.py +++ b/build/mach_initialize.py @@ -260,11 +260,7 @@ def _activate_python_environment(topsrcdir, state_dir): # (optional) dependencies are not installed. _scrub_system_site_packages() - sys.path[0:0] = [ - os.path.join(topsrcdir, pth.path) - for pth in requirements.pth_requirements - + requirements.vendored_requirements - ] + sys.path[0:0] = requirements.pths_as_absolute(topsrcdir) elif is_mach_virtualenv: # We're running in the Mach virtualenv - check that it's up-to-date. # Note that the "pip package check" exists to ensure that a virtualenv isn't @@ -284,11 +280,7 @@ def _activate_python_environment(topsrcdir, state_dir): # Remove global site packages from sys.path to improve isolation accordingly. _scrub_system_site_packages() - sys.path[0:0] = [ - os.path.join(topsrcdir, pth.path) - for pth in requirements.pth_requirements - + requirements.vendored_requirements - ] + sys.path[0:0] = requirements.pths_as_absolute(topsrcdir) def initialize(topsrcdir): diff --git a/python/mach/mach/requirements.py b/python/mach/mach/requirements.py index 2be2d5f1cd2d..adfab41451d7 100644 --- a/python/mach/mach/requirements.py +++ b/python/mach/mach/requirements.py @@ -82,6 +82,14 @@ class MachEnvRequirements: self.pypi_optional_requirements = [] self.vendored_requirements = [] + def pths_as_absolute(self, topsrcdir): + return sorted( + [ + os.path.normcase(os.path.join(topsrcdir, pth.path)) + for pth in (self.pth_requirements + self.vendored_requirements) + ] + ) + def validate_environment_packages(self, pip_command): result = EnvironmentPackageValidationResult() if not self.pypi_requirements and not self.pypi_optional_requirements: diff --git a/python/mach/mach/virtualenv.py b/python/mach/mach/virtualenv.py index 0ef14b98ec7d..8ad24a29a775 100644 --- a/python/mach/mach/virtualenv.py +++ b/python/mach/mach/virtualenv.py @@ -204,26 +204,11 @@ class VirtualenvManager(VirtualenvHelper): with open( os.path.join(self._site_packages_dir(), PTH_FILENAME) ) as file: - pth_lines = file.read().strip().split("\n") + current_paths = file.read().strip().split("\n") except FileNotFoundError: return False - current_paths = [ - os.path.normcase( - os.path.abspath(os.path.join(self._site_packages_dir(), path)) - ) - for path in pth_lines - ] - - required_paths = [ - os.path.normcase( - os.path.abspath(os.path.join(self.topsrcdir, pth.path)) - ) - for pth in env_requirements.pth_requirements - + env_requirements.vendored_requirements - ] - - if current_paths != required_paths: + if current_paths != env_requirements.pths_as_absolute(self.topsrcdir): return False if not skip_pip_package_check: @@ -310,17 +295,9 @@ class VirtualenvManager(VirtualenvHelper): self.create() env_requirements = self.requirements() site_packages_dir = self._site_packages_dir() + pthfile_lines = self.requirements().pths_as_absolute(self.topsrcdir) with open(os.path.join(site_packages_dir, PTH_FILENAME), "a") as f: - for pth_requirement in ( - env_requirements.pth_requirements - + env_requirements.vendored_requirements - ): - path = os.path.join(self.topsrcdir, pth_requirement.path) - # This path is relative to the .pth file. Using a - # relative path allows the srcdir/objdir combination - # to be moved around (as long as the paths relative to - # each other remain the same). - f.write("{}\n".format(os.path.relpath(path, site_packages_dir))) + f.write("\n".join(pthfile_lines)) old_env_variables = {} try: diff --git a/python/mach_commands.py b/python/mach_commands.py index f3293a2cc44a..4f07506468e8 100644 --- a/python/mach_commands.py +++ b/python/mach_commands.py @@ -80,9 +80,7 @@ def python( ) append_env["PYTHONPATH"] = os.pathsep.join( - os.path.join(command_context.topsrcdir, pth.path) - for pth in requirements.pth_requirements - + requirements.vendored_requirements + requirements.pths_as_absolute(command_context.topsrcdir) ) else: command_context.virtualenv_manager.ensure()