mach: recreate .venv when .python-version changes (#34712)

The new images published in servo/ci-runners#12 should have `uv`
installed already and the initial build of servo triggered during the
base image construction will force the installation of the Python
version mentioned at the time of the image construction (3.12). When
.python-version changes, we can no longer use the .venv baked into the
image and must recreate the it to avoid activating the environment.

Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Mukilan Thiyagarajan 2024-12-20 15:25:21 +05:30 committed by GitHub
parent 50c9c72778
commit adfee3daa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 46 additions and 46 deletions

View file

@ -89,7 +89,7 @@ def _process_exec(args, cwd):
sys.exit(1)
def install_virtual_env_requirements(project_path: str, virtualenv_path: str):
def install_virtual_env_requirements(project_path: str, marker_path: str):
requirements_paths = [
os.path.join(project_path, "python", "requirements.txt"),
os.path.join(project_path, WPT_TOOLS_PATH, "requirements_tests.txt",),
@ -102,7 +102,6 @@ def install_virtual_env_requirements(project_path: str, virtualenv_path: str):
requirements_hasher.update(file.read())
try:
marker_path = os.path.join(virtualenv_path, "requirements.sha256")
with open(marker_path, 'r') as marker_file:
marker_hash = marker_file.read()
except FileNotFoundError:
@ -123,15 +122,19 @@ def install_virtual_env_requirements(project_path: str, virtualenv_path: str):
def _activate_virtualenv(topdir):
virtualenv_path = os.path.join(topdir, ".venv")
with open(".python-version", "r") as python_version_file:
required_python_version = python_version_file.read().strip()
marker_path = os.path.join(virtualenv_path, f"requirements.{required_python_version}.sha256")
if os.environ.get("VIRTUAL_ENV") != virtualenv_path:
if not os.path.exists(virtualenv_path):
if not os.path.exists(marker_path):
print(" * Setting up virtual environment...")
_process_exec(["uv", "venv"], cwd=topdir)
script_dir = "Scripts" if _is_windows() else "bin"
runpy.run_path(os.path.join(virtualenv_path, script_dir, 'activate_this.py'))
install_virtual_env_requirements(topdir, virtualenv_path)
install_virtual_env_requirements(topdir, marker_path)
# Turn off warnings about deprecated syntax in our indirect dependencies.
# TODO: Find a better approach for doing this.