forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubenv.ts
More file actions
219 lines (204 loc) · 7.58 KB
/
Copy pathsubenv.ts
File metadata and controls
219 lines (204 loc) · 7.58 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { EnvironmentType } from '../info';
import { getPyenvTypeFinder } from './globalenv';
type ExecFunc = (cmd: string, args: string[]) => Promise<{ stdout: string }>;
type NameFinderFunc = (python: string) => Promise<string | undefined>;
type TypeFinderFunc = (python: string) => Promise<EnvironmentType | undefined>;
type ExecutableFinderFunc = (python: string) => Promise<string | undefined>;
/**
* Determine the environment name for the given Python executable.
*
* @param python - the executable to inspect
* @param finders - the functions specific to different Python environment types
*/
export async function getName(python: string, finders: NameFinderFunc[]): Promise<string | undefined> {
for (const find of finders) {
const found = await find(python);
if (found && found !== '') {
return found;
}
}
return undefined;
}
/**
* Determine the environment type for the given Python executable.
*
* @param python - the executable to inspect
* @param finders - the functions specific to different Python environment types
*/
export async function getType(python: string, finders: TypeFinderFunc[]): Promise<EnvironmentType | undefined> {
for (const find of finders) {
const found = await find(python);
if (found && found !== EnvironmentType.Unknown) {
return found;
}
}
return undefined;
}
// ======= default sets ========
/**
* Build the list of default "name finder" functions to pass to `getName()`.
*
* @param dirname - the "root" of a directory tree to search
* @param pathDirname - typically `path.dirname`
* @param pathBasename - typically `path.basename`
* @param isPipenvRoot - a function the determines if it's a pipenv dir
*/
export function getNameFinders(
dirname: string | undefined,
// <path>
pathDirname: (filename: string) => string,
pathBasename: (filename: string) => string,
// </path>
isPipenvRoot: (dir: string, python: string) => Promise<boolean>,
): NameFinderFunc[] {
return [
// Note that currently there is only one finder function in
// the list. That is only a temporary situation as we
// consolidate code under the py-envs component.
async (python) => {
if (dirname && (await isPipenvRoot(dirname, python))) {
// In pipenv, return the folder name of the root dir.
return pathBasename(dirname);
}
return pathBasename(pathDirname(pathDirname(python)));
},
];
}
/**
* Build the list of default "type finder" functions to pass to `getType()`.
*
* @param homedir - the user's home directory (e.g. `$HOME`)
* @param scripts - the names of possible activation scripts (e.g. `activate.sh`)
* @param pathSep - the path separator to use (typically `path.sep`)
* @param pathJoin - typically `path.join`
* @param pathDirname - typically `path.dirname`
* @param getCurDir - a function that returns `$CWD`
* @param isPipenvRoot - a function the determines if it's a pipenv dir
* @param getEnvVar - a function to look up a process environment variable (i,e. `process.env[name]`)
* @param fileExists - typically `fs.exists`
* @param exec - the function to use to run a command in a subprocess
*/
export function getTypeFinders(
homedir: string,
scripts: string[],
// <path>
pathSep: string,
pathJoin: (...parts: string[]) => string,
pathDirname: (filename: string) => string,
// </path>
getCurDir: () => Promise<string | undefined>,
isPipenvRoot: (dir: string, python: string) => Promise<boolean>,
getEnvVar: (name: string) => string | undefined,
fileExists: (n: string) => Promise<boolean>,
exec: ExecFunc,
): TypeFinderFunc[] {
return [
getVenvTypeFinder(pathDirname, pathJoin, fileExists),
// For now we treat pyenv as a "virtual" environment (to keep compatibility)...
getPyenvTypeFinder(homedir, pathSep, pathJoin, getEnvVar, exec),
getPipenvTypeFinder(getCurDir, isPipenvRoot),
getVirtualenvTypeFinder(scripts, pathDirname, pathJoin, fileExists),
// Lets not try to determine whether this is a conda environment or not.
];
}
// ======= venv ========
/**
* Build a "type finder" function that identifies venv environments.
*
* @param pathDirname - typically `path.dirname`
* @param pathJoin - typically `path.join`
* @param fileExists - typically `fs.exists`
*/
export function getVenvTypeFinder(
// <path>
pathDirname: (filename: string) => string,
pathJoin: (...parts: string[]) => string,
// </path>
fileExists: (n: string) => Promise<boolean>,
): TypeFinderFunc {
return async (python: string) => {
const dir = pathDirname(python);
const VENVFILES = ['pyvenv.cfg', pathJoin('..', 'pyvenv.cfg')];
const cfgFiles = VENVFILES.map((file) => pathJoin(dir, file));
for (const file of cfgFiles) {
if (await fileExists(file)) {
return EnvironmentType.Venv;
}
}
return undefined;
};
}
/**
* Build an "executable finder" function that identifies venv environments.
*
* @param basename - the venv name or names to look for
* @param pathDirname - typically `path.dirname`
* @param pathJoin - typically `path.join`
* @param fileExists - typically `fs.exists`
*/
export function getVenvExecutableFinder(
basename: string | string[],
// <path>
pathDirname: (filename: string) => string,
pathJoin: (...parts: string[]) => string,
// </path>
fileExists: (n: string) => Promise<boolean>,
): ExecutableFinderFunc {
const basenames = typeof basename === 'string' ? [basename] : basename;
return async (python: string) => {
// Generated scripts are found in the same directory as the interpreter.
const binDir = pathDirname(python);
for (const name of basenames) {
const filename = pathJoin(binDir, name);
if (await fileExists(filename)) {
return filename;
}
}
// No matches so return undefined.
return undefined;
};
}
// ======= virtualenv ========
/**
* Build a "type finder" function that identifies virtualenv environments.
*
* @param scripts - the names of possible activation scripts (e.g. `activate.sh`)
* @param pathDirname - typically `path.dirname`
* @param pathJoin - typically `path.join`
* @param fileExists - typically `fs.exists`
*/
export function getVirtualenvTypeFinder(
scripts: string[],
// <path>
pathDirname: (filename: string) => string,
pathJoin: (...parts: string[]) => string,
// </path>
fileExists: (n: string) => Promise<boolean>,
): TypeFinderFunc {
const find = getVenvExecutableFinder(scripts, pathDirname, pathJoin, fileExists);
return async (python: string) => {
const found = await find(python);
return found !== undefined ? EnvironmentType.VirtualEnv : undefined;
};
}
// ======= pipenv ========
/**
* Build a "type finder" function that identifies pipenv environments.
*
* @param getCurDir - a function that returns `$CWD`
* @param isPipenvRoot - a function the determines if it's a pipenv dir
*/
export function getPipenvTypeFinder(
getCurDir: () => Promise<string | undefined>,
isPipenvRoot: (dir: string, python: string) => Promise<boolean>,
): TypeFinderFunc {
return async (python: string) => {
const curDir = await getCurDir();
if (curDir && (await isPipenvRoot(curDir, python))) {
return EnvironmentType.Pipenv;
}
return undefined;
};
}