|
21 | 21 | venv_python_version_path: str = os.path.join(venv_path, "python_version") |
22 | 22 |
|
23 | 23 |
|
| 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 environement 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('utf8').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 | + |
24 | 47 | def askYesNoQuestion(message: str) -> bool: |
25 | 48 | """ |
26 | 49 | Displays the given message to the user and accepts y or n as input. |
@@ -101,6 +124,16 @@ def ensureVenvAndRequirements(): |
101 | 124 | if not os.path.exists(venv_path): |
102 | 125 | print("Virtual environment does not exist.") |
103 | 126 | return createVenvAndPopulate() |
| 127 | + if not verifyExistingVenvPath(): |
| 128 | + if askYesNoQuestion( |
| 129 | + "A Python virtual environement 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) |
104 | 137 | if ( |
105 | 138 | not os.path.exists(venv_python_version_path) |
106 | 139 | or not os.path.exists(venv_orig_requirements_path) |
|
0 commit comments