Skip to content

Commit db46298

Browse files
committed
refactor: unify function
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 540cdbd commit db46298

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

nox/virtualenv.py

+21-24
Original file line numberDiff line numberDiff line change
@@ -349,21 +349,27 @@ def _clean_location(self) -> bool:
349349
shutil.rmtree(self.location)
350350
return True
351351

352+
def _read_pyvenv_cfg(self) -> dict[str, str] | None:
353+
"""Read a pyvenv.cfg file into dict, returns None if missing."""
354+
path = os.path.join(self.location, "pyvenv.cfg")
355+
with contextlib.suppress(FileNotFoundError), open(path) as fp:
356+
parts = (x.partition("=") for x in fp if "=" in x)
357+
return {k.strip(): v.strip() for k, _, v in parts}
358+
return None
359+
352360
def _check_reused_environment_type(self) -> bool:
353361
"""Check if reused environment type is the same or equivalent."""
354-
try:
355-
with open(os.path.join(self.location, "pyvenv.cfg")) as fp:
356-
parts = (x.partition("=") for x in fp if "=" in x)
357-
config = {k.strip(): v.strip() for k, _, v in parts}
358-
if "uv" in config or "gourgeist" in config:
359-
old_env = "uv"
360-
elif "virtualenv" in config:
361-
old_env = "virtualenv"
362-
else:
363-
old_env = "venv"
364-
except FileNotFoundError: # pragma: no cover
365-
# virtualenv < 20.0 does not create pyvenv.cfg
362+
363+
config = self._read_pyvenv_cfg()
364+
# virtualenv < 20.0 does not create pyvenv.cfg
365+
if config is None:
366366
old_env = "virtualenv"
367+
elif "uv" in config or "gourgeist" in config:
368+
old_env = "uv"
369+
elif "virtualenv" in config:
370+
old_env = "virtualenv"
371+
else:
372+
old_env = "venv"
367373

368374
# Can't detect mamba separately, but shouldn't matter
369375
if os.path.isdir(os.path.join(self.location, "conda-meta")):
@@ -395,7 +401,9 @@ def _check_reused_environment_interpreter(self) -> bool:
395401
if not os.environ.get("NOX_ENABLE_STALENESS_CHECK", ""):
396402
return True
397403

398-
original = self._read_base_prefix_from_pyvenv_cfg()
404+
config = self._read_pyvenv_cfg() or {}
405+
original = config.get("base-prefix", None)
406+
399407
program = (
400408
"import sys; sys.stdout.write(getattr(sys, 'real_prefix', sys.base_prefix))"
401409
)
@@ -417,17 +425,6 @@ def _check_reused_environment_interpreter(self) -> bool:
417425
and os.path.samefile(original, created)
418426
)
419427

420-
def _read_base_prefix_from_pyvenv_cfg(self) -> str | None:
421-
"""Return the base-prefix entry from pyvenv.cfg, if present."""
422-
path = os.path.join(self.location, "pyvenv.cfg")
423-
if os.path.isfile(path):
424-
with open(path) as io:
425-
for line in io:
426-
key, _, value = line.partition("=")
427-
if key.strip() == "base-prefix":
428-
return value.strip()
429-
return None
430-
431428
@property
432429
def _resolved_interpreter(self) -> str:
433430
"""Return the interpreter, appropriately resolved for the platform.

0 commit comments

Comments
 (0)