Bug 1717051: Always populate Mach pths r=firefox-build-system-reviewers,glandium

The previous behaviour was to:
* Never add a `pthfile` to the Mach virtualenv, and
* Always add Mach's paths to the `sys.path` when Mach initializes

However, this meant that `pip` would needlessly install packages
that already exist in the vendored environment.

Tweak `pth` behaviour so that `pip` behaves more efficiently.

Differential Revision: https://phabricator.services.mozilla.com/D120402
This commit is contained in:
Mitchell Hentges
2021-11-01 21:34:03 +00:00
parent 12d69b4d0a
commit cfc828417f
3 changed files with 68 additions and 40 deletions

View File

@@ -186,17 +186,28 @@ def _activate_python_environment(topsrcdir, state_dir):
)
]
from mach.virtualenv import VirtualenvManager
mach_virtualenv = VirtualenvManager(
topsrcdir,
os.path.join(state_dir, "_virtualenvs"),
"mach",
populate_local_paths=False,
from mach.virtualenv import (
MozVirtualenvMetadata,
MozVirtualenvMetadataOutOfDateError,
VirtualenvManager,
)
requirements = mach_virtualenv.requirements()
try:
mach_virtualenv = VirtualenvManager(
topsrcdir,
os.path.join(state_dir, "_virtualenvs"),
"mach",
)
active_metadata = MozVirtualenvMetadata.from_runtime()
is_mach_virtualenv = (
active_metadata and active_metadata.virtualenv_name == "mach"
)
except MozVirtualenvMetadataOutOfDateError as e:
print(e)
print('This should be resolved by running "./mach create-mach-environment".')
sys.exit(1)
requirements = mach_virtualenv.requirements()
if os.environ.get("MACH_USE_SYSTEM_PYTHON") or os.environ.get("MOZ_AUTOMATION"):
env_var = (
"MOZ_AUTOMATION"
@@ -249,12 +260,12 @@ def _activate_python_environment(topsrcdir, state_dir):
# (optional) dependencies are not installed.
_scrub_system_site_packages()
elif sys.prefix == sys.base_prefix:
# We're in an environment where we normally use the Mach virtualenv,
# but we're running a "nativecmd" such as "create-mach-environment".
# Remove global site packages from sys.path to improve isolation accordingly.
_scrub_system_site_packages()
else:
sys.path[0:0] = [
os.path.join(topsrcdir, pth.path)
for pth in requirements.pth_requirements
+ requirements.vendored_requirements
]
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
# corrupted by ad-hoc pip installs. Since the Mach virtualenv is unlikely
@@ -267,11 +278,17 @@ def _activate_python_environment(topsrcdir, state_dir):
'"./mach create-mach-environment"'
)
sys.exit(1)
else:
# We're in an environment where we normally *would* use the Mach virtualenv,
# but we're running a "nativecmd" such as "create-mach-environment".
# 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] = [
os.path.join(topsrcdir, pth.path)
for pth in requirements.pth_requirements
+ requirements.vendored_requirements
]
def initialize(topsrcdir):