Skip to content

Commit 61db6dd

Browse files
committed
Fix venv creation to detect corrupted venvs.
1 parent 2e737af commit 61db6dd

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

CHANGES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Release Notes
22

3-
## 0.32.2
3+
## 0.31.3
4+
5+
Fix venv creation to detect corrupted venvs (via system Python upgrades or un-installs).
6+
7+
## 0.31.2
48

59
Fix venv creation to handle `#!/bin/sh` re-director script re-writes. These are Python scripts with
610
`#!/bin/sh` re-director headers that work around shebang-length limitations.

dev_cmd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Copyright 2024 John Sirois.
22
# Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4-
__version__ = "0.31.2"
4+
__version__ = "0.31.3"

dev_cmd/model.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os
77
import re
8+
import sys
89
from dataclasses import dataclass, field
910
from enum import Enum
1011
from pathlib import PurePath
@@ -146,6 +147,11 @@ def update_path(self, env: MutableMapping[str, str]) -> None:
146147
path = env.pop("PATH", None)
147148
env["PATH"] = (self.bin_path + os.pathsep + path) if path else self.bin_path
148149

150+
def is_valid(self) -> bool:
151+
if not os.path.isfile(self.python):
152+
return False
153+
return "win32" == sys.platform or os.access(self.python, os.R_OK | os.X_OK)
154+
149155

150156
@dataclass(frozen=True)
151157
class VenvConfig:

dev_cmd/venv.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,21 +412,27 @@ def _extra_requirements_args() -> Iterator[list[str]]:
412412
with layout_file.open() as in_fp:
413413
data = json.load(in_fp)
414414

415+
def rebuild():
416+
print(
417+
color.yellow(f"Venv for --python {python} at {venv_dir} is out of date, rebuilding."),
418+
file=sys.stderr,
419+
)
420+
shutil.rmtree(venv_dir)
421+
return ensure(venv_config=venv_config, python_config=python_config, rebuild_if_needed=False)
422+
415423
print(
416424
color.color(f"Using venv at {venv_dir} for {env_description}.", fg="gray"), file=sys.stderr
417425
)
418426
try:
419-
return Venv(
427+
venv = Venv(
420428
dir=venv_dir.as_posix(),
421429
python=data["python"],
422430
marker_environment=data["marker-environment"],
423431
)
432+
if not venv.is_valid():
433+
return rebuild()
434+
return venv
424435
except KeyError:
425436
if not rebuild_if_needed:
426437
raise
427-
print(
428-
color.yellow(f"Venv for --python {python} at {venv_dir} is out of date, rebuilding."),
429-
file=sys.stderr,
430-
)
431-
shutil.rmtree(venv_dir)
432-
return ensure(venv_config=venv_config, python_config=python_config, rebuild_if_needed=False)
438+
return rebuild()

0 commit comments

Comments
 (0)