From aee10292ced25c3dc096e7f1b372581d824150d3 Mon Sep 17 00:00:00 2001 From: andreamah Date: Fri, 28 Feb 2020 22:37:41 +0000 Subject: [PATCH 1/4] introductory work for vscode online compat --- src/constants.ts | 5 ++ src/extension.ts | 18 +++--- src/extension_utils/utils.ts | 110 +++++++++++++++++++---------------- 3 files changed, 75 insertions(+), 58 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 9a0bc9529..01530417a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -471,6 +471,11 @@ export const HELPER_FILES = { DEVICE_PY: "device.py", PROCESS_USER_CODE_PY: "process_user_code.py", PYTHON_EXE: "python.exe", + PYTHON: "python", }; +export const GLOBAL_ENV_VARS = { + PYTHON: "python" +} + export default CONSTANTS; diff --git a/src/extension.ts b/src/extension.ts index b9dc563de..dd70f94a1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,6 +4,7 @@ import * as cp from "child_process"; import * as fs from "fs"; import * as open from "open"; +import * as os from "os"; import * as path from "path"; import * as vscode from "vscode"; import { @@ -12,6 +13,7 @@ import { CPX_CONFIG_FILE, DEFAULT_DEVICE, DialogResponses, + GLOBAL_ENV_VARS, HELPER_FILES, SERVER_INFO, TelemetryEventName, @@ -34,7 +36,7 @@ import { registerDefaultFontFaces } from "office-ui-fabric-react"; let currentFileAbsPath: string = ""; let currentTextDocument: vscode.TextDocument; let telemetryAI: TelemetryAI; -let pythonExecutableName: string = "python"; +let pythonExecutablePath: string = GLOBAL_ENV_VARS.PYTHON; let configFileCreated: boolean = false; let inDebugMode: boolean = false; // Notification booleans @@ -98,7 +100,7 @@ export async function activate(context: vscode.ExtensionContext) { // doesn't trigger lint errors updatePylintArgs(context); - pythonExecutableName = await utils.setupEnv(context); + pythonExecutablePath = await utils.setupEnv(context); try { utils.generateCPXConfig(); @@ -108,7 +110,7 @@ export async function activate(context: vscode.ExtensionContext) { configFileCreated = false; } - if (pythonExecutableName === "") { + if (pythonExecutablePath === "") { return; } @@ -442,7 +444,7 @@ export async function activate(context: vscode.ExtensionContext) { const installDependencies: vscode.Disposable = vscode.commands.registerCommand( "deviceSimulatorExpress.common.installDependencies", async () => { - pythonExecutableName = await utils.setupEnv(context, true); + pythonExecutablePath = await utils.setupEnv(context, true); telemetryAI.trackFeatureUsage( TelemetryEventName.COMMAND_INSTALL_EXTENSION_DEPENDENCIES ); @@ -568,7 +570,7 @@ export async function activate(context: vscode.ExtensionContext) { active_device: currentActiveDevice, }); - childProcess = cp.spawn(pythonExecutableName, [ + childProcess = cp.spawn(pythonExecutablePath, [ utils.getPathToScript( context, CONSTANTS.FILESYSTEM.OUTPUT_DIRECTORY, @@ -727,7 +729,7 @@ export async function activate(context: vscode.ExtensionContext) { CONSTANTS.INFO.FILE_SELECTED(currentFileAbsPath) ); - const deviceProcess = cp.spawn(pythonExecutableName, [ + const deviceProcess = cp.spawn(pythonExecutablePath, [ utils.getPathToScript( context, CONSTANTS.FILESYSTEM.OUTPUT_DIRECTORY, @@ -1030,7 +1032,7 @@ export async function activate(context: vscode.ExtensionContext) { const configsChanged = vscode.workspace.onDidChangeConfiguration( async () => { if (utils.checkConfig(CONFIG.CONFIG_ENV_ON_SWITCH)) { - pythonExecutableName = await utils.setupEnv(context); + pythonExecutablePath = await utils.setupEnv(context); } } ); @@ -1081,7 +1083,7 @@ const updateCurrentFileIfPython = async ( if ( currentTextDocument && utils.getActiveEditorFromPath(currentTextDocument.fileName) === - undefined + undefined ) { await vscode.window.showTextDocument( currentTextDocument, diff --git a/src/extension_utils/utils.ts b/src/extension_utils/utils.ts index b4e7b71cf..9ee437557 100644 --- a/src/extension_utils/utils.ts +++ b/src/extension_utils/utils.ts @@ -13,6 +13,7 @@ import { CONSTANTS, CPX_CONFIG_FILE, DialogResponses, + GLOBAL_ENV_VARS, HELPER_FILES, SERVER_INFO, USER_CODE_NAMES, @@ -85,7 +86,7 @@ export function tryParseJSON(jsonString: string): any | boolean { if (jsonObj && typeof jsonObj === "object") { return jsonObj; } - } catch (exception) {} + } catch (exception) { } return false; } @@ -169,11 +170,14 @@ export function generateCPXConfig(): void { fs.writeFileSync(cpxConfigFilePath, JSON.stringify(cpxJson, null, 4)); } -export const isPipInstalled = async (pythonExecutableName: string) => { +export const isPipInstalled = async (pythonExecutablePath: string) => { + try { - await executePythonCommand(pythonExecutableName, " -m pip"); + const { stdout } = await executePythonCommand(pythonExecutablePath, " -m pip"); + console.log(`uuwuu: ${stdout}`) return true; } catch (err) { + console.log(`uuwuu: ${err}`) vscode.window .showErrorMessage( CONSTANTS.ERROR.NO_PIP, @@ -220,7 +224,7 @@ export const addVisibleTextEditorCallback = ( export const filterForPythonFiles = (textEditors: vscode.TextEditor[]) => { return textEditors - .filter(editor => editor.document.languageId === "python") + .filter(editor => editor.document.languageId === GLOBAL_ENV_VARS.PYTHON) .map(editor => editor.document.fileName); }; @@ -273,7 +277,7 @@ export const getTelemetryState = () => { export const checkIfVenv = async ( context: vscode.ExtensionContext, - pythonExecutableName: string + pythonExecutablePath: string ) => { const venvCheckerPath: string = getPathToScript( context, @@ -281,22 +285,23 @@ export const checkIfVenv = async ( HELPER_FILES.CHECK_IF_VENV_PY ); const { stdout } = await executePythonCommand( - pythonExecutableName, + pythonExecutablePath, `"${venvCheckerPath}"` ); return stdout.trim() === "1"; }; export const executePythonCommand = async ( - pythonExecutableName: string, + pythonExecutablePath: string, command: string ) => { - return exec(`${createEscapedPath(pythonExecutableName)} ${command}`); + console.log(`DSE COMMAND: ${createEscapedPath(pythonExecutablePath)} ${command}`) + return exec(`${createEscapedPath(pythonExecutablePath)} ${command}`); }; -export const validatePythonVersion = async (pythonExecutableName: string) => { +export const validatePythonVersion = async (pythonExecutablePath: string) => { const { stdout } = await executePythonCommand( - pythonExecutableName, + pythonExecutablePath, "--version" ); if (stdout < VERSIONS.MIN_PY_VERSION) { @@ -333,7 +338,8 @@ export const hasVenv = async (context: vscode.ExtensionContext) => { export const promptInstallVenv = ( context: vscode.ExtensionContext, - pythonExecutable: string + pythonExecutable: string, + pythonExecutableName: string ) => { return vscode.window .showInformationMessage( @@ -343,7 +349,7 @@ export const promptInstallVenv = ( ) .then((selection: vscode.MessageItem | undefined) => { if (selection === DialogResponses.YES) { - return installPythonVenv(context, pythonExecutable); + return installPythonVenv(context, pythonExecutable, pythonExecutableName); } else { // return pythonExecutable, notifying the caller // that the user was unwilling to create venv @@ -354,19 +360,20 @@ export const promptInstallVenv = ( }); }; -export const getPythonVenv = async (context: vscode.ExtensionContext) => { +export const getPythonVenv = async (context: vscode.ExtensionContext, pythonExecutableName: string) => { const subFolder = os.platform() === "win32" ? "Scripts" : "bin"; return getPathToScript( context, path.join(CONSTANTS.FILESYSTEM.PYTHON_VENV_DIR, subFolder), - HELPER_FILES.PYTHON_EXE + pythonExecutableName ); }; export const installPythonVenv = async ( context: vscode.ExtensionContext, - pythonExecutable: string + pythonExecutable: string, + pythonExecutableName: string ) => { const pathToEnv: string = getPathToScript( context, @@ -375,7 +382,7 @@ export const installPythonVenv = async ( vscode.window.showInformationMessage(CONSTANTS.INFO.INSTALLING_PYTHON_VENV); - const pythonPath: string = await getPythonVenv(context); + const pythonPath: string = await getPythonVenv(context, pythonExecutableName); try { // make venv @@ -478,25 +485,25 @@ export const installDependenciesWrapper = async ( } return pythonPath; }; -export const getCurrentPythonExecutableName = async () => { - let originalPythonExecutableName = ""; +export const getCurrentpythonExecutablePath = async () => { + let originalpythonExecutablePath = ""; + // try to get name from interpreter try { - originalPythonExecutableName = getConfig(CONFIG.PYTHON_PATH); + originalpythonExecutablePath = getConfig(CONFIG.PYTHON_PATH); } catch (err) { - originalPythonExecutableName = "python"; + originalpythonExecutablePath = + GLOBAL_ENV_VARS.PYTHON; } if ( - originalPythonExecutableName === "python" || - originalPythonExecutableName === "" + originalpythonExecutablePath === GLOBAL_ENV_VARS.PYTHON || + originalpythonExecutablePath === "" ) { try { - const { stdout } = await exec( - 'python -c "import sys; print(sys.executable)"' - ); - originalPythonExecutableName = stdout.trim(); + const { stdout } = await executePythonCommand(GLOBAL_ENV_VARS.PYTHON, `-c "import sys; print(sys.executable)"`) + originalpythonExecutablePath = stdout.trim() } catch (err) { vscode.window .showErrorMessage( @@ -520,68 +527,71 @@ export const getCurrentPythonExecutableName = async () => { } } // fix path to be absolute - if (!path.isAbsolute(originalPythonExecutableName)) { - originalPythonExecutableName = path.join( + if (!path.isAbsolute(originalpythonExecutablePath)) { + originalpythonExecutablePath = path.join( vscode.workspace.rootPath, - originalPythonExecutableName + originalpythonExecutablePath ); } - if (!fs.existsSync(originalPythonExecutableName)) { + if (!fs.existsSync(originalpythonExecutablePath)) { await vscode.window.showErrorMessage(CONSTANTS.ERROR.BAD_PYTHON_PATH); return ""; } - if (!(await validatePythonVersion(originalPythonExecutableName))) { + if (!(await validatePythonVersion(originalpythonExecutablePath))) { return ""; } - return originalPythonExecutableName; + return originalpythonExecutablePath; }; export const setupEnv = async ( context: vscode.ExtensionContext, needsResponse: boolean = false ) => { - const originalPythonExecutableName = await getCurrentPythonExecutableName(); - let pythonExecutableName = originalPythonExecutableName; + const originalpythonExecutablePath = await getCurrentpythonExecutablePath(); + let pythonExecutablePath = originalpythonExecutablePath; + let pythonExecutableName: string = (os.platform() !== "win32") ? HELPER_FILES.PYTHON_EXE : HELPER_FILES.PYTHON; + - if (!(await areDependenciesInstalled(context, pythonExecutableName))) { + if (!(await areDependenciesInstalled(context, pythonExecutablePath))) { // environment needs to install dependencies - if (!(await checkIfVenv(context, pythonExecutableName))) { - const pythonExecutableNameVenv = await getPythonVenv(context); + if (!(await checkIfVenv(context, pythonExecutablePath))) { + const pythonExecutablePathVenv = await getPythonVenv(context, pythonExecutableName); if (await hasVenv(context)) { // venv in extention exists with wrong dependencies if ( !(await areDependenciesInstalled( context, - pythonExecutableNameVenv + pythonExecutablePathVenv )) ) { - pythonExecutableName = await installDependenciesWrapper( + pythonExecutablePath = await installDependenciesWrapper( context, - pythonExecutableNameVenv, - pythonExecutableName + pythonExecutablePathVenv, + pythonExecutablePath ); } else { - pythonExecutableName = pythonExecutableNameVenv; + pythonExecutablePath = pythonExecutablePathVenv; } } else { - pythonExecutableName = await promptInstallVenv( + pythonExecutablePath = await promptInstallVenv( context, - originalPythonExecutableName + originalpythonExecutablePath, + pythonExecutableName ); } - if (pythonExecutableName === pythonExecutableNameVenv) { + if (pythonExecutablePath === pythonExecutablePathVenv) { vscode.window.showInformationMessage( CONSTANTS.INFO.UPDATED_TO_EXTENSION_VENV ); vscode.workspace .getConfiguration() - .update(CONFIG.PYTHON_PATH, pythonExecutableName); + .update(CONFIG.PYTHON_PATH, pythonExecutablePath); } } - if (pythonExecutableName === originalPythonExecutableName) { + if (pythonExecutablePath === originalpythonExecutablePath) { // going with original interpreter, either because // already in venv or error in creating custom venv if (checkConfig(CONFIG.SHOW_DEPENDENCY_INSTALL)) { @@ -598,7 +608,7 @@ export const setupEnv = async ( if (installChoice === DialogResponses.INSTALL_NOW) { await installDependenciesWrapper( context, - pythonExecutableName + pythonExecutablePath ); } else { await vscode.window @@ -619,7 +629,7 @@ export const setupEnv = async ( ) { await installDependenciesWrapper( context, - pythonExecutableName + pythonExecutablePath ); } } @@ -635,5 +645,5 @@ export const setupEnv = async ( ); } - return pythonExecutableName; + return pythonExecutablePath; }; From 4b3596a508d6abe383dde8a263877d6cd28f0c38 Mon Sep 17 00:00:00 2001 From: andreamah Date: Fri, 28 Feb 2020 22:55:51 +0000 Subject: [PATCH 2/4] fixed small mistake --- src/extension_utils/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension_utils/utils.ts b/src/extension_utils/utils.ts index 9ee437557..f3e053d37 100644 --- a/src/extension_utils/utils.ts +++ b/src/extension_utils/utils.ts @@ -551,7 +551,7 @@ export const setupEnv = async ( ) => { const originalpythonExecutablePath = await getCurrentpythonExecutablePath(); let pythonExecutablePath = originalpythonExecutablePath; - let pythonExecutableName: string = (os.platform() !== "win32") ? HELPER_FILES.PYTHON_EXE : HELPER_FILES.PYTHON; + let pythonExecutableName: string = (os.platform() === "win32") ? HELPER_FILES.PYTHON_EXE : HELPER_FILES.PYTHON; if (!(await areDependenciesInstalled(context, pythonExecutablePath))) { From 1f87a2667b3d36852c127ddf39f8f7df6d42fa0e Mon Sep 17 00:00:00 2001 From: andreamah Date: Sat, 29 Feb 2020 00:03:54 +0000 Subject: [PATCH 3/4] removed console logs --- src/extension_utils/utils.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/extension_utils/utils.ts b/src/extension_utils/utils.ts index f3e053d37..4a18f71d9 100644 --- a/src/extension_utils/utils.ts +++ b/src/extension_utils/utils.ts @@ -174,10 +174,8 @@ export const isPipInstalled = async (pythonExecutablePath: string) => { try { const { stdout } = await executePythonCommand(pythonExecutablePath, " -m pip"); - console.log(`uuwuu: ${stdout}`) return true; } catch (err) { - console.log(`uuwuu: ${err}`) vscode.window .showErrorMessage( CONSTANTS.ERROR.NO_PIP, @@ -295,7 +293,6 @@ export const executePythonCommand = async ( pythonExecutablePath: string, command: string ) => { - console.log(`DSE COMMAND: ${createEscapedPath(pythonExecutablePath)} ${command}`) return exec(`${createEscapedPath(pythonExecutablePath)} ${command}`); }; From 3fe7c5c81fbb09c32c29de5e43be4dc2adafe546 Mon Sep 17 00:00:00 2001 From: andreamah Date: Sat, 29 Feb 2020 00:20:14 +0000 Subject: [PATCH 4/4] formatting --- src/constants.ts | 4 ++-- src/extension.ts | 2 +- src/extension_utils/utils.ts | 46 +++++++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 01530417a..7c537c122 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -475,7 +475,7 @@ export const HELPER_FILES = { }; export const GLOBAL_ENV_VARS = { - PYTHON: "python" -} + PYTHON: "python", +}; export default CONSTANTS; diff --git a/src/extension.ts b/src/extension.ts index dd70f94a1..c14532a0a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1083,7 +1083,7 @@ const updateCurrentFileIfPython = async ( if ( currentTextDocument && utils.getActiveEditorFromPath(currentTextDocument.fileName) === - undefined + undefined ) { await vscode.window.showTextDocument( currentTextDocument, diff --git a/src/extension_utils/utils.ts b/src/extension_utils/utils.ts index 4a18f71d9..ba51c6815 100644 --- a/src/extension_utils/utils.ts +++ b/src/extension_utils/utils.ts @@ -86,7 +86,7 @@ export function tryParseJSON(jsonString: string): any | boolean { if (jsonObj && typeof jsonObj === "object") { return jsonObj; } - } catch (exception) { } + } catch (exception) {} return false; } @@ -171,9 +171,11 @@ export function generateCPXConfig(): void { } export const isPipInstalled = async (pythonExecutablePath: string) => { - try { - const { stdout } = await executePythonCommand(pythonExecutablePath, " -m pip"); + const { stdout } = await executePythonCommand( + pythonExecutablePath, + " -m pip" + ); return true; } catch (err) { vscode.window @@ -346,7 +348,11 @@ export const promptInstallVenv = ( ) .then((selection: vscode.MessageItem | undefined) => { if (selection === DialogResponses.YES) { - return installPythonVenv(context, pythonExecutable, pythonExecutableName); + return installPythonVenv( + context, + pythonExecutable, + pythonExecutableName + ); } else { // return pythonExecutable, notifying the caller // that the user was unwilling to create venv @@ -357,7 +363,10 @@ export const promptInstallVenv = ( }); }; -export const getPythonVenv = async (context: vscode.ExtensionContext, pythonExecutableName: string) => { +export const getPythonVenv = async ( + context: vscode.ExtensionContext, + pythonExecutableName: string +) => { const subFolder = os.platform() === "win32" ? "Scripts" : "bin"; return getPathToScript( @@ -379,7 +388,10 @@ export const installPythonVenv = async ( vscode.window.showInformationMessage(CONSTANTS.INFO.INSTALLING_PYTHON_VENV); - const pythonPath: string = await getPythonVenv(context, pythonExecutableName); + const pythonPath: string = await getPythonVenv( + context, + pythonExecutableName + ); try { // make venv @@ -485,13 +497,11 @@ export const installDependenciesWrapper = async ( export const getCurrentpythonExecutablePath = async () => { let originalpythonExecutablePath = ""; - // try to get name from interpreter try { originalpythonExecutablePath = getConfig(CONFIG.PYTHON_PATH); } catch (err) { - originalpythonExecutablePath = - GLOBAL_ENV_VARS.PYTHON; + originalpythonExecutablePath = GLOBAL_ENV_VARS.PYTHON; } if ( @@ -499,8 +509,11 @@ export const getCurrentpythonExecutablePath = async () => { originalpythonExecutablePath === "" ) { try { - const { stdout } = await executePythonCommand(GLOBAL_ENV_VARS.PYTHON, `-c "import sys; print(sys.executable)"`) - originalpythonExecutablePath = stdout.trim() + const { stdout } = await executePythonCommand( + GLOBAL_ENV_VARS.PYTHON, + `-c "import sys; print(sys.executable)"` + ); + originalpythonExecutablePath = stdout.trim(); } catch (err) { vscode.window .showErrorMessage( @@ -548,13 +561,18 @@ export const setupEnv = async ( ) => { const originalpythonExecutablePath = await getCurrentpythonExecutablePath(); let pythonExecutablePath = originalpythonExecutablePath; - let pythonExecutableName: string = (os.platform() === "win32") ? HELPER_FILES.PYTHON_EXE : HELPER_FILES.PYTHON; - + let pythonExecutableName: string = + os.platform() === "win32" + ? HELPER_FILES.PYTHON_EXE + : HELPER_FILES.PYTHON; if (!(await areDependenciesInstalled(context, pythonExecutablePath))) { // environment needs to install dependencies if (!(await checkIfVenv(context, pythonExecutablePath))) { - const pythonExecutablePathVenv = await getPythonVenv(context, pythonExecutableName); + const pythonExecutablePathVenv = await getPythonVenv( + context, + pythonExecutableName + ); if (await hasVenv(context)) { // venv in extention exists with wrong dependencies if (