Skip to content

Commit 24b37dc

Browse files
Merge 8e26286 into 52bf1ed
2 parents 52bf1ed + 8e26286 commit 24b37dc

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

devDocs/buildSystemNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Version numbers for dependencies should be used to lock in a version.
1818
The virtual environment is recreated if it is outdated, either due to:
1919
- Python version.
2020
- `pip` requirements.
21+
- It was originally created at a different location to where it is now.
2122

2223
The user is consulted before modifying / removing a virtual environment that can't be identified
2324
as having been created by NVDA's build system.

venvUtils/ensureVenv.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@
2121
venv_python_version_path: str = os.path.join(venv_path, "python_version")
2222

2323

24+
def verifyExistingVenvPath():
25+
"""
26+
Verifies that the Python virtual environment at c{VENV_PATH} was actually created at that location,
27+
and not just copied or moved there.
28+
"""
29+
# Activate the Python virtual environment and capture the content of the VIRTUAL_ENV environment variable.
30+
existingPath = subprocess.check_output(
31+
[
32+
os.path.join(venv_path, "scripts", "activate.bat"),
33+
"&&",
34+
"call", "echo", "%VIRTUAL_ENV%",
35+
],
36+
shell=True,
37+
).decode('oem').rstrip()
38+
existingPath = os.path.normpath(existingPath)
39+
expectedPath = os.path.normpath(venv_path)
40+
if existingPath != expectedPath:
41+
print(f"Python virtual environment originally created at {existingPath},")
42+
print(f"Then moved or copied to {expectedPath}.")
43+
return False
44+
return True
45+
46+
2447
def askYesNoQuestion(message: str) -> bool:
2548
"""
2649
Displays the given message to the user and accepts y or n as input.
@@ -101,6 +124,16 @@ def ensureVenvAndRequirements():
101124
if not os.path.exists(venv_path):
102125
print("Virtual environment does not exist.")
103126
return createVenvAndPopulate()
127+
if not verifyExistingVenvPath():
128+
if askYesNoQuestion(
129+
"A Python virtual environment cannot be activated from a different location "
130+
"to where it was originally created.\n"
131+
"Should the virtual environment be recreated with the updated location?"
132+
):
133+
return createVenvAndPopulate()
134+
else:
135+
print("Aborting")
136+
sys.exit(1)
104137
if (
105138
not os.path.exists(venv_python_version_path)
106139
or not os.path.exists(venv_orig_requirements_path)

0 commit comments

Comments
 (0)