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

More Venv Fixes + Dependency Fail Detect on Python #231

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/check_python_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# from https://stackoverflow.com/questions/16294819/check-if-my-python-has-all-required-packages
import sys
import pkg_resources
import python_constants as CONSTANTS

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

# here, if a dependency is not met, a DistributionNotFound or VersionConflict
# exception is thrown.
pkg_resources.require(dependencies)
def check_for_dependencies():
with open(f"{sys.path[0]}/requirements.txt") as f:
dependencies = [x.strip() for x in f.readlines()]

# here, if a dependency is not met, a DistributionNotFound or VersionConflict
# exception is caught and replaced with a new exception with a clearer description.
try:
pkg_resources.require(dependencies)
except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict) as e:
raise Exception(CONSTANTS.DEPEND_ERR)


if __name__ == "__main__":
check_for_dependencies()
3 changes: 3 additions & 0 deletions src/debug_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import traceback
from pathlib import Path
import python_constants as CONSTANTS
import check_python_dependencies

# will propagate errors if dependencies aren't sufficient
check_python_dependencies.check_for_dependencies()

# Insert absolute path to Adafruit library into sys.path
abs_path_to_parent_dir = os.path.dirname(os.path.abspath(__file__))
Expand Down
14 changes: 8 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ export async function activate(context: vscode.ExtensionContext) {

const installDependencies: vscode.Disposable = vscode.commands.registerCommand(
"deviceSimulatorExpress.common.installDependencies",
() => {
utils.setupEnv(context, true);
async () => {
pythonExecutableName = await utils.setupEnv(context, true);
telemetryAI.trackFeatureUsage(
TelemetryEventName.COMMAND_INSTALL_EXTENSION_DEPENDENCIES
);
Expand Down Expand Up @@ -1027,11 +1027,13 @@ export async function activate(context: vscode.ExtensionContext) {
}
});

const configsChanged = vscode.workspace.onDidChangeConfiguration(() => {
if (utils.checkConfig(CONFIG.CONFIG_ENV_ON_SWITCH)) {
utils.setupEnv(context);
const configsChanged = vscode.workspace.onDidChangeConfiguration(
async () => {
if (utils.checkConfig(CONFIG.CONFIG_ENV_ON_SWITCH)) {
pythonExecutableName = await utils.setupEnv(context);
}
}
});
);

context.subscriptions.push(
installDependencies,
Expand Down
22 changes: 13 additions & 9 deletions src/extension_utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,9 @@ export const setupEnv = async (
let pythonExecutableName = originalPythonExecutableName;

if (!(await areDependenciesInstalled(context, pythonExecutableName))) {
const pythonExecutableNameVenv = await getPythonVenv(context);
// environment needs to install dependencies
if (!(await checkIfVenv(context, pythonExecutableName))) {
const pythonExecutableNameVenv = await getPythonVenv(context);
if (await hasVenv(context)) {
// venv in extention exists with wrong dependencies
if (
Expand All @@ -562,22 +562,26 @@ export const setupEnv = async (
pythonExecutableNameVenv,
pythonExecutableName
);
} else {
pythonExecutableName = pythonExecutableNameVenv;
}
} else {
pythonExecutableName = await promptInstallVenv(
context,
originalPythonExecutableName
);
}

if (pythonExecutableName === pythonExecutableNameVenv) {
vscode.window.showInformationMessage(
CONSTANTS.INFO.UPDATED_TO_EXTENSION_VENV
);
vscode.workspace
.getConfiguration()
.update(CONFIG.PYTHON_PATH, pythonExecutableName);
}
}
if (pythonExecutableName === pythonExecutableNameVenv) {
vscode.window.showInformationMessage(
CONSTANTS.INFO.UPDATED_TO_EXTENSION_VENV
);
vscode.workspace
.getConfiguration()
.update(CONFIG.PYTHON_PATH, pythonExecutableName);
} else if (pythonExecutableName === originalPythonExecutableName) {
if (pythonExecutableName === originalPythonExecutableName) {
// going with original interpreter, either because
// already in venv or error in creating custom venv
if (checkConfig(CONFIG.SHOW_DEPENDENCY_INSTALL)) {
Expand Down
4 changes: 4 additions & 0 deletions src/process_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import traceback
import python_constants as CONSTANTS
from pathlib import Path
import check_python_dependencies

# will propagate errors if dependencies aren't sufficient
check_python_dependencies.check_for_dependencies()

read_val = ""
threads = []
Expand Down
2 changes: 2 additions & 0 deletions src/python_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

CPX_DRIVE_NAME = "CIRCUITPY"

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".'

DEVICE_NOT_IMPLEMENTED_ERROR = "Device not implemented."

ENABLE_TELEMETRY = "enable_telemetry"
Expand Down