forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.ts
More file actions
67 lines (62 loc) · 2.85 KB
/
python.ts
File metadata and controls
67 lines (62 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { exec } from 'child_process';
import { debug } from './logger';
import { sleep } from './misc';
export async function isPackageInstalled(pythonPath: string, moduleName: string): Promise<boolean> {
const cmd = `${pythonPath.toCommandArgument()} -c "import ${moduleName};print('Hello World')"`;
debug(`Executing command = ${cmd}`);
return new Promise<boolean>(resolve => {
exec(cmd, (ex, stdout: string, stdErr: string) => {
if (ex || stdErr) {
debug(`Executing command = ${cmd}, error: `, ex, stdErr);
return resolve(false);
}
debug(`Executing command = ${cmd}, output: `, stdout);
resolve(stdout.trim() === 'Hello World');
});
});
}
export async function runPythonCommand(pythonPath: string, cwd: string, command: string): Promise<boolean> {
const cmd = `${pythonPath.toCommandArgument()} ${command}`;
debug(`Executing command = ${cmd}`);
return new Promise<boolean>(resolve => {
exec(cmd, { cwd }, (ex, stdout: string) => {
if (ex) {
debug(`Executing command = ${cmd}, error: `, ex);
return resolve(false);
}
debug(`Executing command = ${cmd}, output: `, stdout);
resolve(true);
});
});
}
export async function installPackage(pythonPath: string, moduleName: string): Promise<void> {
await installOrUninstallPackage(pythonPath, moduleName, true);
}
export async function uninstallModule(pythonPath: string, moduleName: string): Promise<void> {
await installOrUninstallPackage(pythonPath, moduleName, false);
}
export async function installOrUninstallPackage(pythonPath: string, moduleName: string, install: boolean = true): Promise<void> {
const installCmd = install ? 'install' : 'uninstall';
const extraArgs = install ? [] : ['-y'];
const cmd = `${pythonPath.toCommandArgument()} -m pip ${installCmd} ${moduleName} -q --disable-pip-version-check ${extraArgs.join(' ')}`;
// tslint:disable-next-line: no-unnecessary-callback-wrapper
return new Promise<void>(resolve => exec(cmd.trim(), () => resolve()));
}
export async function ensurePackageIsInstalled(pythonPath: string, moduleName: string): Promise<void> {
const installed = await isPackageInstalled(pythonPath, moduleName);
if (!installed) {
await installPackage(pythonPath, moduleName);
await sleep(1000);
}
}
export async function ensurePackageIsNotInstalled(pythonPath: string, moduleName: string): Promise<void> {
const installed = await isPackageInstalled(pythonPath, moduleName);
debug(`Module ${moduleName} is installed = ${installed}`);
if (installed) {
await uninstallModule(pythonPath, moduleName);
await sleep(1000);
}
}