forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
343 lines (312 loc) · 13.2 KB
/
utils.ts
File metadata and controls
343 lines (312 loc) · 13.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/// <reference path="../../../node_modules/@types/node/index.d.ts" />
/// <reference path="../../../node_modules/vscode/vscode.d.ts" />
'use strict';
// TODO: Cleanup this place
// Add options for execPythonFile
import * as path from 'path';
import * as fs from 'fs';
import * as child_process from 'child_process';
import * as settings from './configSettings';
import { CancellationToken, TextDocument, Range } from 'vscode';
import { isNotInstalledError } from './helpers';
import { mergeEnvVariables, parseEnvFile } from './envFileParser';
export const IS_WINDOWS = /^win/.test(process.platform);
export const PATH_VARIABLE_NAME = IS_WINDOWS ? 'Path' : 'PATH';
const PathValidity: Map<string, boolean> = new Map<string, boolean>();
export function validatePath(filePath: string): Promise<string> {
if (filePath.length === 0) {
return Promise.resolve('');
}
if (PathValidity.has(filePath)) {
return Promise.resolve(PathValidity.get(filePath) ? filePath : '');
}
return new Promise<string>(resolve => {
fs.exists(filePath, exists => {
PathValidity.set(filePath, exists);
return resolve(exists ? filePath : '');
});
});
}
export function fsExistsAsync(filePath: string): Promise<boolean> {
return new Promise<boolean>(resolve => {
fs.exists(filePath, exists => {
PathValidity.set(filePath, exists);
return resolve(exists);
});
});
}
let pythonInterpretterDirectory: string = null;
let previouslyIdentifiedPythonPath: string = null;
let customEnvVariables: any = null;
// If config settings change then clear env variables that we have cached
// Remember, the path to the python interpreter can change, hence we need to re-set the paths
settings.PythonSettings.getInstance().on('change', function () {
customEnvVariables = null;
});
export function getPythonInterpreterDirectory(): Promise<string> {
// If we already have it and the python path hasn't changed, yay
if (pythonInterpretterDirectory && previouslyIdentifiedPythonPath === settings.PythonSettings.getInstance().pythonPath) {
return Promise.resolve(pythonInterpretterDirectory);
}
let pythonFileName = settings.PythonSettings.getInstance().pythonPath;
// Check if we have the path
if (path.basename(pythonFileName) === pythonFileName) {
// No path provided, however we can get it by using sys.executableFile
return getPathFromPythonCommand(["-c", "import sys;print(sys.executable)"])
.then(pythonExecutablePath => pythonInterpretterDirectory = path.dirname(pythonExecutablePath))
.catch(() => pythonInterpretterDirectory = '');
}
return new Promise<string>(resolve => {
// If we can execute the python, then get the path from the fully qualified name
child_process.execFile(pythonFileName, ['-c', 'print(1234)'], (error, stdout, stderr) => {
// Yes this is a valid python path
if (stdout.startsWith('1234')) {
previouslyIdentifiedPythonPath = path.dirname(pythonFileName);
}
else {
previouslyIdentifiedPythonPath = '';
}
// No idea, didn't work, hence don't reject, but return empty path
resolve(previouslyIdentifiedPythonPath);
});
});
}
export function getFullyQualifiedPythonInterpreterPath(): Promise<string> {
return getPythonInterpreterDirectory()
.then(pyPath => path.join(pyPath, path.basename(settings.PythonSettings.getInstance().pythonPath)));
}
export function getPathFromPythonCommand(args: string[]): Promise<string> {
return execPythonFile(settings.PythonSettings.getInstance().pythonPath, args, __dirname).then(stdout => {
if (stdout.length === 0) {
return "";
}
let lines = stdout.split(/\r?\n/g).filter(line => line.length > 0);
return validatePath(lines[0]);
}).catch(() => {
return "";
});
}
export function execPythonFile(file: string, args: string[], cwd: string, includeErrorAsResponse: boolean = false, stdOut: (line: string) => void = null, token?: CancellationToken): Promise<string> {
// If running the python file, then always revert to execFileInternal
// Cuz python interpreter is always a file and we can and will always run it using child_process.execFile()
if (file === settings.PythonSettings.getInstance().pythonPath) {
if (stdOut) {
return spawnFileInternal(file, args, { cwd }, includeErrorAsResponse, stdOut, token);
}
return execFileInternal(file, args, { cwd: cwd }, includeErrorAsResponse, token);
}
return getPythonInterpreterDirectory().then(pyPath => {
// We don't have a path
if (pyPath.length === 0) {
if (stdOut) {
return spawnFileInternal(file, args, { cwd }, includeErrorAsResponse, stdOut, token);
}
return execFileInternal(file, args, { cwd: cwd }, includeErrorAsResponse, token);
}
if (customEnvVariables === null) {
customEnvVariables = getCustomEnvVars();
customEnvVariables = customEnvVariables ? customEnvVariables : {};
// Ensure to include the path of the current python
let newPath = '';
let currentPath = typeof customEnvVariables[PATH_VARIABLE_NAME] === 'string' ? customEnvVariables[PATH_VARIABLE_NAME] : process.env[PATH_VARIABLE_NAME];
if (IS_WINDOWS) {
newPath = pyPath + '\\' + path.delimiter + path.join(pyPath, 'Scripts\\') + path.delimiter + currentPath;
// This needs to be done for windows
process.env[PATH_VARIABLE_NAME] = newPath;
}
else {
newPath = pyPath + path.delimiter + currentPath;
}
customEnvVariables = mergeEnvVariables(customEnvVariables, process.env);
customEnvVariables[PATH_VARIABLE_NAME] = newPath;
}
if (stdOut) {
return spawnFileInternal(file, args, { cwd, env: customEnvVariables }, includeErrorAsResponse, stdOut, token);
}
return execFileInternal(file, args, { cwd, env: customEnvVariables }, includeErrorAsResponse, token);
});
}
function handleResponse(file: string, includeErrorAsResponse: boolean, error: Error, stdout: string, stderr: string, token?: CancellationToken): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (token && token.isCancellationRequested) {
return;
}
if (isNotInstalledError(error)) {
return reject(error);
}
// pylint:
// In the case of pylint we have some messages (such as config file not found and using default etc...) being returned in stderr
// These error messages are useless when using pylint
if (includeErrorAsResponse && (stdout.length > 0 || stderr.length > 0)) {
return resolve(stdout + '\n' + stderr);
}
let hasErrors = (error && error.message.length > 0) || (stderr && stderr.length > 0);
if (hasErrors && (typeof stdout !== 'string' || stdout.length === 0)) {
let errorMsg = (error && error.message) ? error.message : (stderr && stderr.length > 0 ? stderr + '' : '');
console.error('stdout');
console.error(stdout);
console.error('stderr');
console.error(stderr);
console.error('error');
console.error(error);
console.error('Over');
return reject(errorMsg);
}
resolve(stdout + '');
});
}
function execFileInternal(file: string, args: string[], options: child_process.ExecFileOptions, includeErrorAsResponse: boolean, token?: CancellationToken): Promise<string> {
options.maxBuffer = options.maxBuffer ? options.maxBuffer : 1024 * 102400;
return new Promise<string>((resolve, reject) => {
let proc = child_process.execFile(file, args, options, (error, stdout, stderr) => {
handleResponse(file, includeErrorAsResponse, error, stdout, stderr, token).then(resolve, reject);
});
if (token && token.onCancellationRequested) {
token.onCancellationRequested(() => {
if (proc) {
proc.kill();
proc = null;
}
});
}
});
}
function spawnFileInternal(file: string, args: string[], options: child_process.ExecFileOptions, includeErrorAsResponse: boolean, stdOut: (line: string) => void, token?: CancellationToken): Promise<string> {
return new Promise<string>((resolve, reject) => {
let proc = child_process.spawn(file, args, options);
let error = '';
let exited = false;
if (token && token.onCancellationRequested) {
token.onCancellationRequested(() => {
if (!exited && proc) {
proc.kill();
proc = null;
}
});
}
proc.on('error', error => {
return reject(error);
});
proc.stdout.setEncoding('utf8');
proc.stderr.setEncoding('utf8');
proc.stdout.on('data', function (data: string) {
if (token && token.isCancellationRequested) {
return;
}
stdOut(data);
});
proc.stderr.on('data', function (data: string) {
if (token && token.isCancellationRequested) {
return;
}
if (includeErrorAsResponse) {
stdOut(data);
}
else {
error += data;
}
});
proc.on('exit', function (code) {
exited = true;
if (token && token.isCancellationRequested) {
return reject();
}
if (error.length > 0) {
return reject(error);
}
resolve();
});
});
}
function execInternal(command: string, args: string[], options: child_process.ExecFileOptions, includeErrorAsResponse: boolean): Promise<string> {
return new Promise<string>((resolve, reject) => {
child_process.exec([command].concat(args).join(' '), options, (error, stdout, stderr) => {
handleResponse(command, includeErrorAsResponse, error, stdout, stderr).then(resolve, reject);
});
});
}
export function formatErrorForLogging(error: Error | string): string {
let message: string = '';
if (typeof error === 'string') {
message = error;
}
else {
if (error.message) {
message = `Error Message: ${error.message}`;
}
if (error.name && error.message.indexOf(error.name) === -1) {
message += `, (${error.name})`;
}
const innerException = (error as any).innerException;
if (innerException && (innerException.message || innerException.name)) {
if (innerException.message) {
message += `, Inner Error Message: ${innerException.message}`;
}
if (innerException.name && innerException.message.indexOf(innerException.name) === -1) {
message += `, (${innerException.name})`;
}
}
}
return message;
}
export function getSubDirectories(rootDir: string): Promise<string[]> {
return new Promise<string[]>(resolve => {
fs.readdir(rootDir, (error, files) => {
if (error) {
return resolve([]);
}
const subDirs = [];
files.forEach(name => {
const fullPath = path.join(rootDir, name);
try {
if (fs.statSync(fullPath).isDirectory()) {
subDirs.push(fullPath);
}
}
catch (ex) {
}
});
resolve(subDirs);
});
});
}
export function getCustomEnvVars(): any {
const envFile = settings.PythonSettings.getInstance().envFile;
if (typeof envFile === 'string' &&
envFile.length > 0 &&
fs.existsSync(envFile)) {
try {
let vars = parseEnvFile(envFile);
if (vars && typeof vars === 'object' && Object.keys(vars).length > 0) {
return vars;
}
}
catch (ex) {
console.error('Failed to load env file');
console.error(ex);
return null;
}
}
return null;
}
export function getWindowsLineEndingCount(document: TextDocument, offset: Number) {
const eolPattern = new RegExp('\r\n', 'g');
const readBlock = 1024;
let count = 0;
let offsetDiff = offset.valueOf();
// In order to prevent the one-time loading of large files from taking up too much memory
for (let pos = 0; pos < offset; pos += readBlock) {
let startAt = document.positionAt(pos);
let endAt = null;
if (offsetDiff >= readBlock) {
endAt = document.positionAt(pos + readBlock);
offsetDiff = offsetDiff - readBlock;
} else {
endAt = document.positionAt(pos + offsetDiff);
}
let text = document.getText(new Range(startAt, endAt));
let cr = text.match(eolPattern);
count += cr ? cr.length : 0;
}
return count;
}