|
1 |
| -'use strict'; |
2 |
| - |
3 | 1 | import * as path from 'path';
|
4 | 2 |
|
5 | 3 | import { extensions, Uri } from 'vscode';
|
6 | 4 |
|
7 | 5 | import { IExtensionApi, ResolvedEnvironment } from './ms-python-api/types';
|
8 | 6 | import { shellTask, spawnAsPromise } from './shell';
|
9 | 7 |
|
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 |
| -} |
| 8 | +export class Python { |
| 9 | + public readonly path: Promise<string>; |
| 10 | + private usingMSPython = false; |
| 11 | + private pythonEnvMS: ResolvedEnvironment | undefined; |
23 | 12 |
|
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); |
| 13 | + constructor(resource?: Uri) { |
| 14 | + this.path = this.getPythonPath(resource); |
37 | 15 | }
|
38 |
| - return stdout.trim(); |
39 |
| -} |
40 | 16 |
|
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 |
| -} |
| 17 | + /** |
| 18 | + * Get the path to the active Python interpreter. |
| 19 | + * |
| 20 | + * @returns The path to the active Python interpreter. |
| 21 | + */ |
| 22 | + public async getPythonPath(resource?: Uri): Promise<string> { |
| 23 | + const pythonEnv = await this.getPythonEnvMS(resource); |
| 24 | + if (pythonEnv) { |
| 25 | + this.usingMSPython = true; |
| 26 | + return pythonEnv.path; |
| 27 | + } |
| 28 | + return process.platform === 'win32' ? 'python' : 'python3'; |
| 29 | + } |
52 | 30 |
|
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 |
| 31 | + /** |
| 32 | + * Get the path to the directory where pip installs binaries. |
| 33 | + * |
| 34 | + * @returns The path to the directory where pip installs binaries. |
| 35 | + */ |
| 36 | + public async getPipBinDir(): Promise<string> { |
| 37 | + const py = await this.path; |
| 38 | + const script = path.join(__dirname, 'scripts', 'get_pip_bin_dir.py'); |
| 39 | + const [stdout, stderr] = await spawnAsPromise(py, [script]); |
| 40 | + if (stderr) { |
| 41 | + throw new Error(stderr); |
67 | 42 | }
|
68 |
| - if (!extension.isActive) { |
69 |
| - await extension.activate(); |
| 43 | + return stdout.trim(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Install a Python package using pip. |
| 48 | + * |
| 49 | + * @param packageName The name of the package to install. |
| 50 | + * @returns The output of the pip command. |
| 51 | + */ |
| 52 | + public async pipInstall(packageName: string): Promise<string> { |
| 53 | + const py = await this.path; |
| 54 | + const args = ['-m', 'pip', 'install', '--user', '--upgrade', packageName]; |
| 55 | + return await shellTask(py, args, `pip: ${packageName}`); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the active Python environment, if any, via the ms-python.python |
| 60 | + * extension API. |
| 61 | + * |
| 62 | + * @returns The active Python environment, or undefined if there is none. |
| 63 | + */ |
| 64 | + public async getPythonEnvMS(resource?: Uri): Promise<ResolvedEnvironment | undefined> { |
| 65 | + try { |
| 66 | + const extension = extensions.getExtension('ms-python.python'); |
| 67 | + if (!extension) { |
| 68 | + return undefined; // extension not installed |
| 69 | + } |
| 70 | + if (!extension.isActive) { |
| 71 | + await extension.activate(); |
| 72 | + } |
| 73 | + const pythonApi: IExtensionApi = extension.exports as IExtensionApi; |
| 74 | + const activeEnv: ResolvedEnvironment = await pythonApi.environments.resolveEnvironment( |
| 75 | + pythonApi.environments.getActiveEnvironmentPath(resource) |
| 76 | + ); |
| 77 | + if (!activeEnv) { |
| 78 | + return undefined; // no active environment, unlikely but possible |
| 79 | + } |
| 80 | + this.pythonEnvMS = activeEnv; |
| 81 | + return activeEnv; |
| 82 | + } catch (error) { |
| 83 | + return undefined; |
70 | 84 | }
|
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 |
| 85 | + } |
| 86 | + |
| 87 | + public async isInstalled(packageName: string): Promise<boolean> { |
| 88 | + const py = await this.path; |
| 89 | + const script = path.join(__dirname, 'scripts', 'mod_in_env.py'); |
| 90 | + try { |
| 91 | + const [_, stderr] = await spawnAsPromise(py, [script, packageName]); |
| 92 | + if (stderr) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + return true; |
| 96 | + } catch (error) { |
| 97 | + return false; |
77 | 98 | }
|
78 |
| - return activeEnv; |
79 |
| - } catch (error) { |
80 |
| - return undefined; |
81 | 99 | }
|
82 | 100 | }
|
0 commit comments