|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import { extensions, Uri } from 'vscode'; |
| 6 | + |
| 7 | +import { IExtensionApi, ResolvedEnvironment } from './ms-python-api/types'; |
| 8 | +import { shellTask, spawnAsPromise } from './shell'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Get Python path from the workspace or the system |
| 12 | + * |
| 13 | + * @param resource file, folder or workspace to search for python |
| 14 | + * @returns string with path to python |
| 15 | + */ |
| 16 | +export async function getPythonPath(resource?: Uri): Promise<string> { |
| 17 | + const pythonEnv = await getPythonEnvMS(resource); |
| 18 | + if (pythonEnv) { |
| 19 | + return pythonEnv.path; |
| 20 | + } |
| 21 | + return process.platform === 'win32' ? 'python' : 'python3'; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Get path that pip installs binaries into. |
| 26 | + * Useful, for when the path is not in the PATH environment variable. |
| 27 | + * |
| 28 | + * @param resource file, folder or workspace |
| 29 | + * @returns string with path to pip |
| 30 | + */ |
| 31 | +export async function getPipBinDir(resource?: Uri): Promise<string> { |
| 32 | + const py = await getPythonPath(resource); |
| 33 | + const script = path.join(__dirname, 'scripts', 'get_pip_bin_dir.py'); |
| 34 | + const [stdout, stderr] = await spawnAsPromise(py, [script]); |
| 35 | + if (stderr) { |
| 36 | + throw new Error(stderr); |
| 37 | + } |
| 38 | + return stdout.trim(); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * A wrapper around a call to `pip` for installing external tools. |
| 43 | + * Does not explicitly check if `pip` is installed. |
| 44 | + * |
| 45 | + * @param pyPackage name of python package in PyPi |
| 46 | + */ |
| 47 | +export async function pipInstall(pyPackage: string): Promise<string> { |
| 48 | + const py = await getPythonPath(); |
| 49 | + const args = ['-m', 'pip', 'install', '--user', '--upgrade', pyPackage]; |
| 50 | + return await shellTask(py, args, `pip: ${pyPackage}`); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Get the active Python environment, if any, via the ms-python.python |
| 55 | + * extension API. |
| 56 | + * |
| 57 | + * @param resource file/folder/workspace Uri or undefined |
| 58 | + * @returns Path to the active Python environment or undefined |
| 59 | + */ |
| 60 | +export async function getPythonEnvMS( |
| 61 | + resource?: Uri | undefined |
| 62 | +): Promise<ResolvedEnvironment | undefined> { |
| 63 | + try { |
| 64 | + const extension = extensions.getExtension('ms-python.python'); |
| 65 | + if (!extension) { |
| 66 | + return undefined; // extension not installed |
| 67 | + } |
| 68 | + if (!extension.isActive) { |
| 69 | + await extension.activate(); |
| 70 | + } |
| 71 | + const pythonApi: IExtensionApi = extension.exports as IExtensionApi; |
| 72 | + const activeEnv: ResolvedEnvironment = await pythonApi.environments.resolveEnvironment( |
| 73 | + pythonApi.environments.getActiveEnvironmentPath(resource) |
| 74 | + ); |
| 75 | + if (!activeEnv) { |
| 76 | + return undefined; // no active environment, unlikely but possible |
| 77 | + } |
| 78 | + return activeEnv; |
| 79 | + } catch (error) { |
| 80 | + return undefined; |
| 81 | + } |
| 82 | +} |
0 commit comments