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
242 lines (217 loc) · 9.47 KB
/
utils.ts
File metadata and controls
242 lines (217 loc) · 9.47 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
/// <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 os from 'os';
import * as fs from 'fs';
import * as child_process from 'child_process';
import * as settings from './configSettings';
import { CancellationToken } from 'vscode';
import { isNotInstalledError } from './helpers';
export const IS_WINDOWS = /^win/.test(process.platform);
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 : '');
});
});
}
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);
}
return new Promise<string>(resolve => {
let pythonFileName = settings.PythonSettings.getInstance().pythonPath;
// Check if we have the path
if (path.basename(pythonFileName) === pythonFileName) {
// No path provided
return 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')) {
return resolve(path.dirname(pythonFileName));
}
// No idea, didn't work, hence don't reject, but return empty path
resolve('');
});
}).then(value => {
// Cache and return
previouslyIdentifiedPythonPath = settings.PythonSettings.getInstance().pythonPath;
return pythonInterpretterDirectory = value;
}).catch(() => {
// Don't care what the error is, all we know is that this doesn't work
return pythonInterpretterDirectory = '';
});
}
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);
}
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);
}
if (customEnvVariables === null) {
// Ensure to include the path of the current python
let newPath = '';
if (IS_WINDOWS) {
newPath = pyPath + '\\' + path.delimiter + path.join(pyPath, 'Scripts\\') + path.delimiter + process.env[PATH_VARIABLE_NAME];
// This needs to be done for windows
process.env[PATH_VARIABLE_NAME] = newPath;
}
else {
newPath = pyPath + path.delimiter + process.env[PATH_VARIABLE_NAME];
}
let customSettings = <{ [key: string]: string }>{};
customSettings[PATH_VARIABLE_NAME] = newPath;
customEnvVariables = mergeEnvVariables(customSettings);
}
if (stdOut) {
return spawnFileInternal(file, args, { cwd, env: customEnvVariables }, includeErrorAsResponse, stdOut, token);
}
return execFileInternal(file, args, { cwd, env: customEnvVariables }, includeErrorAsResponse);
});
}
function handleResponse(file: string, includeErrorAsResponse: boolean, error: Error, stdout: string, stderr: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
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 + '' : '');
return reject(errorMsg);
}
resolve(stdout + '');
});
}
function execFileInternal(file: string, args: string[], options: child_process.ExecFileOptions, includeErrorAsResponse: boolean): Promise<string> {
return new Promise<string>((resolve, reject) => {
child_process.execFile(file, args, options, (error, stdout, stderr) => {
handleResponse(file, includeErrorAsResponse, error, stdout, stderr).then(resolve, reject);
});
});
}
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 mergeEnvVariables(newVariables: { [key: string]: string }): any {
for (let setting in process.env) {
if (!newVariables[setting]) {
newVariables[setting] = process.env[setting];
}
}
return newVariables;
}
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;
}