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
126 lines (115 loc) · 4.29 KB
/
utils.ts
File metadata and controls
126 lines (115 loc) · 4.29 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
'use strict';
// tslint:disable: no-any one-line no-suspicious-comment prefer-template prefer-const no-unnecessary-callback-wrapper no-function-expression no-string-literal no-control-regex no-shadowed-variable
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { Position, Range, TextDocument } from 'vscode';
export const IS_WINDOWS = /^win/.test(process.platform);
export const Is_64Bit = os.arch() === 'x64';
export const PATH_VARIABLE_NAME = IS_WINDOWS ? 'Path' : 'PATH';
export function fsExistsAsync(filePath: string): Promise<boolean> {
return new Promise<boolean>(resolve => {
fs.exists(filePath, exists => {
return resolve(exists);
});
});
}
export function fsReaddirAsync(root: string): Promise<string[]> {
return new Promise<string[]>(resolve => {
// Now look for Interpreters in this directory
fs.readdir(root, (err, subDirs) => {
if (err) {
return resolve([]);
}
resolve(subDirs.map(subDir => path.join(root, subDir)));
});
});
}
export async function getPathFromPythonCommand(pythonPath: string): Promise<string> {
return await new Promise<string>((resolve, reject) => {
child_process.execFile(pythonPath, ['-c', 'import sys;print(sys.executable)'], (_, stdout) => {
if (stdout) {
const lines = stdout.split(/\r?\n/g).map(line => line.trim()).filter(line => line.length > 0);
resolve(lines.length > 0 ? lines[0] : '');
} else {
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: string[] = [];
files.forEach(name => {
const fullPath = path.join(rootDir, name);
try {
if (fs.statSync(fullPath).isDirectory()) {
subDirs.push(fullPath);
}
}
// tslint:disable-next-line:no-empty
catch (ex) { }
});
resolve(subDirs);
});
});
}
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: Position;
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;
}
export function arePathsSame(path1: string, path2: string) {
path1 = path.normalize(path1);
path2 = path.normalize(path2);
if (IS_WINDOWS) {
return path1.toUpperCase() === path2.toUpperCase();
} else {
return path1 === path2;
}
}