diff --git a/setup.py b/setup.py index b47ad875..b82861fa 100644 --- a/setup.py +++ b/setup.py @@ -3361,6 +3361,20 @@ def control_missing_packages() -> list: if importlib.util.find_spec(m) is None] +def pip_stamp_is_ours(stamp: Path) -> bool: + """The list of installed packages was written for THIS system (#3634). A .venv on a disk that Windows and Linux both start from keeps + the stamp of the other one, which then says "already installed" about packages this Python does not have. A marker beside the stamp + names the system that wrote it; an older stamp without a marker is believed only while the packages every run needs can be imported.""" + marker = stamp.with_name(stamp.name + ".os") + try: + if marker.exists(): + return marker.read_text(encoding="utf-8").strip() == sys.platform + except OSError: + return False + import importlib.util + return all(importlib.util.find_spec(m) is not None for m in ("numpy", "jinja2", "regex", "yaml")) + + def pip_install(packages, what, extra=()): """pip install into .venv, skipped when the same list was installed before. An install from before the pinned requirements (#214) recorded bare names: those packages are kept as they are (nothing is reinstalled), and the @@ -3372,7 +3386,7 @@ def pip_install(packages, what, extra=()): ok(f"{what}: bundled with the app") return stamp = Path(sys.prefix) / ".strata-pip.json" - have = json.loads(stamp.read_text(encoding="utf-8")) if stamp.exists() else [] + have = json.loads(stamp.read_text(encoding="utf-8")) if stamp.exists() and pip_stamp_is_ours(stamp) else [] bare = {p.lower() for p in have if req_name(p) == p.lower()} need = [p for p in packages if p not in have and req_name(p) not in bare and not (bare and "==" in p and _installed(req_name(p)))] @@ -3383,6 +3397,7 @@ def pip_install(packages, what, extra=()): say(f" Installing {what} ...") run([sys.executable, "-m", "pip", "install", "--quiet", "--disable-pip-version-check", *extra, *need]) stamp.write_text(json.dumps(sorted(set(have) | set(need)), indent=0)) + stamp.with_name(stamp.name + ".os").write_text(sys.platform, encoding="utf-8") ok(f"{what} installed") diff --git a/tools/test_setup_pins.py b/tools/test_setup_pins.py index 052c9a97..f0be55ba 100644 --- a/tools/test_setup_pins.py +++ b/tools/test_setup_pins.py @@ -398,6 +398,21 @@ class Requirements(unittest.TestCase): after = json.loads((Path(d) / ".strata-pip.json").read_text()) if ran else stamp return [c for cmd in ran for c in cmd if "==" in c or c in setup.PY_PACKAGES], after + def test_a_stamp_written_by_another_system_is_not_believed(self): # #3634 + lines = setup.requirement_lines() + with tempfile.TemporaryDirectory() as d: + (Path(d) / ".strata-pip.json").write_text(json.dumps(lines)) + (Path(d) / ".strata-pip.json.os").write_text("win32" if setup.sys.platform != "win32" else "linux") + ran = [] + with mock.patch.object(setup.sys, "prefix", d), mock.patch.object(setup, "run", lambda cmd, **kw: ran.append(cmd)): + quiet(setup.pip_install, lines, "the packages") + self.assertTrue(ran) # installed again, here + self.assertEqual((Path(d) / ".strata-pip.json.os").read_text(), setup.sys.platform) + ran.clear() + with mock.patch.object(setup.sys, "prefix", d), mock.patch.object(setup, "run", lambda cmd, **kw: ran.append(cmd)): + quiet(setup.pip_install, lines, "the packages") + self.assertFalse(ran) # and then skipped as before + def test_fresh_install_gets_every_pin(self): lines = setup.requirement_lines() ran, stamp = self.pip(None, lines)