Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-16 08:46:34

0001 """Exercise uv installs with view-only packages and an inherited native package."""
0002 
0003 import os
0004 from pathlib import Path
0005 import shutil
0006 import subprocess
0007 import sys
0008 import zipfile
0009 
0010 import pytest
0011 
0012 SETUP = Path(__file__).with_name("setup.sh")
0013 
0014 
0015 def run(*args, env=None):
0016     return subprocess.check_output(args, env=env, text=True).strip()
0017 
0018 
0019 def site_packages(python):
0020     return Path(
0021         run(str(python), "-c", "import sysconfig; print(sysconfig.get_path('purelib'))")
0022     )
0023 
0024 
0025 @pytest.mark.parametrize("full_install", ["false", "true"])
0026 def test_packages_visible_in_view_but_not_venv(tmp_path, full_install):
0027     uv = shutil.which("uv")
0028     if uv is None:
0029         pytest.skip("uv is required")
0030     wheels = tmp_path / "wheels"
0031     wheels.mkdir()
0032     packages = [
0033         ("pyyaml", "yaml", "1.0"),
0034         ("jinja2", "jinja2", "1.0"),
0035         ("histcmp", "histcmp", "0.10.0"),
0036         ("matplotlib", "matplotlib", "1.0"),
0037         ("pytest_md_report", "pytest_md_report", "1.0"),
0038     ]
0039     for name, module, version in packages:
0040         info = f"{name}-{version}.dist-info"
0041         with zipfile.ZipFile(
0042             wheels / f"{name}-{version}-py3-none-any.whl", "w"
0043         ) as wheel:
0044             wheel.writestr(f"{module}.py", "fixture = True\n")
0045             wheel.writestr(
0046                 f"{info}/METADATA",
0047                 f"Metadata-Version: 2.1\nName: {name}\nVersion: {version}\n"
0048                 "Requires-Dist: acts-native-stack\n",
0049             )
0050             wheel.writestr(
0051                 f"{info}/WHEEL",
0052                 "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
0053             )
0054             wheel.writestr(f"{info}/RECORD", "")
0055     env = {
0056         **os.environ,
0057         "UV_CACHE_DIR": str(tmp_path / "cache"),
0058         "UV_OFFLINE": "1",
0059         "UV_NO_INDEX": "1",
0060         "UV_FIND_LINKS": str(wheels),
0061     }
0062     view = tmp_path / "view"
0063     venv = tmp_path / "venv"
0064     run(sys.executable, "-m", "venv", "--without-pip", str(view))
0065     view_python = view / "bin/python3"
0066     run(
0067         uv,
0068         "pip",
0069         "install",
0070         "--python",
0071         str(view_python),
0072         "--no-deps",
0073         "pyyaml",
0074         "jinja2",
0075         env=env,
0076     )
0077     run(str(view_python), "-m", "venv", "--system-site-packages", str(venv))
0078     python = venv / "bin/python3"
0079     # A nested venv loses the view's site-packages, reproducing the CI failure.
0080     if (
0081         run(
0082             str(python),
0083             "-c",
0084             "import importlib.util; print(importlib.util.find_spec('yaml'))",
0085         )
0086         != "None"
0087     ):
0088         pytest.skip("base Python already supplies PyYAML")
0089     inherited = tmp_path / "native"
0090     inherited.mkdir()
0091     (inherited / "acts_native_stack.py").write_text("native = True\n")
0092     metadata = inherited / "acts_native_stack-1.0.dist-info"
0093     metadata.mkdir()
0094     (metadata / "METADATA").write_text(
0095         "Metadata-Version: 2.1\nName: acts-native-stack\nVersion: 1.0\n"
0096     )
0097     for interpreter in (view_python, python):
0098         (site_packages(interpreter) / "native.pth").write_text(str(inherited) + "\n")
0099     requirements = tmp_path / "Python/Examples/tests/requirements.txt"
0100     requirements.parent.mkdir(parents=True)
0101     requirements.write_text("acts-native-stack==1.0\n")
0102     script_dir = tmp_path / "CI/dependencies"
0103     script_dir.mkdir(parents=True)
0104     section = SETUP.read_text().split(
0105         'start_section "Prepare python environment"\n', 1
0106     )[1]
0107     section = section.split('checkpoint "Python environment prepared"', 1)[0]
0108     run(
0109         "bash",
0110         "-euc",
0111         'retry_transient() { "$@"; }\n' + section,
0112         env={
0113             **env,
0114             "view_dir": str(view),
0115             "venv_dir": str(venv),
0116             "SCRIPT_DIR": str(script_dir),
0117             "full_install": full_install,
0118         },
0119     )
0120     run(
0121         str(python), "-c", "import yaml, jinja2; assert yaml.fixture and jinja2.fixture"
0122     )
0123     assert run(
0124         str(python), "-c", "import acts_native_stack; print(acts_native_stack.__file__)"
0125     ) == str(inherited / "acts_native_stack.py")
0126     assert not list(site_packages(python).glob("acts_native_stack*"))
0127     if full_install == "true":
0128         run(
0129             str(python),
0130             "-c",
0131             "import histcmp, matplotlib, pytest_md_report; assert histcmp.fixture and matplotlib.fixture and pytest_md_report.fixture",
0132         )