forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.ts
More file actions
112 lines (85 loc) · 3.27 KB
/
python.ts
File metadata and controls
112 lines (85 loc) · 3.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// "python" contains functions corresponding to the various ways that
// the extension invokes a Python executable internally. Each function
// takes arguments relevant to the specific use case. However, each
// always *returns* a list of strings for the commandline arguments that
// should be used when invoking the Python executable for the specific
// use case, whether through spawn/exec or a terminal.
//
// Where relevant (nearly always), the function also returns a "parse"
// function that may be used to deserialize the stdout of the command
// into the corresponding object or objects. "parse()" takes a single
// string as the stdout text and returns the relevant data.
export function execCode(code: string): string[] {
let args = ['-c', code];
// "code" isn't specific enough to know how to parse it,
// so we only return the args.
return args;
}
export function execModule(name: string, moduleArgs: string[]): string[] {
const args = ['-m', name, ...moduleArgs];
// "code" isn't specific enough to know how to parse it,
// so we only return the args.
return args;
}
export function getVersion(): [string[], (out: string) => string] {
const args = ['--version'];
function parse(out: string): string {
return out.trim();
}
return [args, parse];
}
export function getSysPrefix(): [string[], (out: string) => string] {
const args = ['-c', 'import sys;print(sys.prefix)'];
function parse(out: string): string {
return out.trim();
}
return [args, parse];
}
export function getExecutable(): [string[], (out: string) => string] {
const args = ['-c', 'import sys;print(sys.executable)'];
function parse(out: string): string {
return out.trim();
}
return [args, parse];
}
export function getSitePackages(): [string[], (out: string) => string] {
// On windows we also need the libs path (second item will
// return c:\xxx\lib\site-packages). This is returned by
// the following: get_python_lib
const args = ['-c', 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'];
function parse(out: string): string {
return out.trim();
}
return [args, parse];
}
export function getUserSitePackages(): [string[], (out: string) => string] {
const args = ['site', '--user-site'];
function parse(out: string): string {
return out.trim();
}
return [args, parse];
}
export function isValid(): [string[], (out: string) => boolean] {
const args = ['-c', 'print(1234)'];
function parse(out: string): boolean {
return out.startsWith('1234');
}
return [args, parse];
}
export function isModuleInstalled(name: string): [string[], (out: string) => boolean] {
const args = ['-c', `import ${name}`];
function parse(_out: string): boolean {
// If the command did not fail then the module is installed.
return true;
}
return [args, parse];
}
export function getModuleVersion(name: string): [string[], (out: string) => string] {
const args = ['-c', `import ${name}; print(${name}.__version__)`];
function parse(out: string): string {
return out.trim();
}
return [args, parse];
}