Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit 6cf1ad6

Browse files
authored
More Venv Fixes + Dependency Fail Detect on Python (#231)
fixed venv user flow problems and added python dependency detection
1 parent 3d21ebb commit 6cf1ad6

File tree

6 files changed

+45
-20
lines changed

6 files changed

+45
-20
lines changed

src/check_python_dependencies.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# from https://stackoverflow.com/questions/16294819/check-if-my-python-has-all-required-packages
22
import sys
33
import pkg_resources
4+
import python_constants as CONSTANTS
45

5-
with open(f"{sys.path[0]}/requirements.txt") as f:
6-
dependencies = [x.strip() for x in f.readlines()]
76

8-
# here, if a dependency is not met, a DistributionNotFound or VersionConflict
9-
# exception is thrown.
10-
pkg_resources.require(dependencies)
7+
def check_for_dependencies():
8+
with open(f"{sys.path[0]}/requirements.txt") as f:
9+
dependencies = [x.strip() for x in f.readlines()]
10+
11+
# here, if a dependency is not met, a DistributionNotFound or VersionConflict
12+
# exception is caught and replaced with a new exception with a clearer description.
13+
try:
14+
pkg_resources.require(dependencies)
15+
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict) as e:
16+
raise Exception(CONSTANTS.DEPEND_ERR)
17+
18+
19+
if __name__ == "__main__":
20+
check_for_dependencies()

src/debug_user_code.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import traceback
77
from pathlib import Path
88
import python_constants as CONSTANTS
9+
import check_python_dependencies
910

11+
# will propagate errors if dependencies aren't sufficient
12+
check_python_dependencies.check_for_dependencies()
1013

1114
# Insert absolute path to Adafruit library into sys.path
1215
abs_path_to_parent_dir = os.path.dirname(os.path.abspath(__file__))

src/extension.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ export async function activate(context: vscode.ExtensionContext) {
441441

442442
const installDependencies: vscode.Disposable = vscode.commands.registerCommand(
443443
"deviceSimulatorExpress.common.installDependencies",
444-
() => {
445-
utils.setupEnv(context, true);
444+
async () => {
445+
pythonExecutableName = await utils.setupEnv(context, true);
446446
telemetryAI.trackFeatureUsage(
447447
TelemetryEventName.COMMAND_INSTALL_EXTENSION_DEPENDENCIES
448448
);
@@ -1027,11 +1027,13 @@ export async function activate(context: vscode.ExtensionContext) {
10271027
}
10281028
});
10291029

1030-
const configsChanged = vscode.workspace.onDidChangeConfiguration(() => {
1031-
if (utils.checkConfig(CONFIG.CONFIG_ENV_ON_SWITCH)) {
1032-
utils.setupEnv(context);
1030+
const configsChanged = vscode.workspace.onDidChangeConfiguration(
1031+
async () => {
1032+
if (utils.checkConfig(CONFIG.CONFIG_ENV_ON_SWITCH)) {
1033+
pythonExecutableName = await utils.setupEnv(context);
1034+
}
10331035
}
1034-
});
1036+
);
10351037

10361038
context.subscriptions.push(
10371039
installDependencies,

src/extension_utils/utils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ export const setupEnv = async (
546546
let pythonExecutableName = originalPythonExecutableName;
547547

548548
if (!(await areDependenciesInstalled(context, pythonExecutableName))) {
549-
const pythonExecutableNameVenv = await getPythonVenv(context);
550549
// environment needs to install dependencies
551550
if (!(await checkIfVenv(context, pythonExecutableName))) {
551+
const pythonExecutableNameVenv = await getPythonVenv(context);
552552
if (await hasVenv(context)) {
553553
// venv in extention exists with wrong dependencies
554554
if (
@@ -562,22 +562,26 @@ export const setupEnv = async (
562562
pythonExecutableNameVenv,
563563
pythonExecutableName
564564
);
565+
} else {
566+
pythonExecutableName = pythonExecutableNameVenv;
565567
}
566568
} else {
567569
pythonExecutableName = await promptInstallVenv(
568570
context,
569571
originalPythonExecutableName
570572
);
571573
}
574+
575+
if (pythonExecutableName === pythonExecutableNameVenv) {
576+
vscode.window.showInformationMessage(
577+
CONSTANTS.INFO.UPDATED_TO_EXTENSION_VENV
578+
);
579+
vscode.workspace
580+
.getConfiguration()
581+
.update(CONFIG.PYTHON_PATH, pythonExecutableName);
582+
}
572583
}
573-
if (pythonExecutableName === pythonExecutableNameVenv) {
574-
vscode.window.showInformationMessage(
575-
CONSTANTS.INFO.UPDATED_TO_EXTENSION_VENV
576-
);
577-
vscode.workspace
578-
.getConfiguration()
579-
.update(CONFIG.PYTHON_PATH, pythonExecutableName);
580-
} else if (pythonExecutableName === originalPythonExecutableName) {
584+
if (pythonExecutableName === originalPythonExecutableName) {
581585
// going with original interpreter, either because
582586
// already in venv or error in creating custom venv
583587
if (checkConfig(CONFIG.SHOW_DEPENDENCY_INSTALL)) {

src/process_user_code.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import traceback
1111
import python_constants as CONSTANTS
1212
from pathlib import Path
13+
import check_python_dependencies
14+
15+
# will propagate errors if dependencies aren't sufficient
16+
check_python_dependencies.check_for_dependencies()
1317

1418
read_val = ""
1519
threads = []

src/python_constants.py

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

66
CPX_DRIVE_NAME = "CIRCUITPY"
77

8+
DEPEND_ERR = 'The required dependencies aren\'t downloaded. Please use CTRL+SHIFT+P to open the command palette and select "Device Simulator Express: Install Extension Dependencies".'
9+
810
DEVICE_NOT_IMPLEMENTED_ERROR = "Device not implemented."
911

1012
ENABLE_TELEMETRY = "enable_telemetry"

0 commit comments

Comments
 (0)