forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
131 lines (118 loc) · 5.89 KB
/
common.ts
File metadata and controls
131 lines (118 loc) · 5.89 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
import * as fs from 'fs-extra';
import * as path from 'path';
import { ConfigurationTarget, Uri, workspace } from 'vscode';
import { PythonSettings } from '../client/common/configSettings';
import { sleep } from './core';
import { IS_MULTI_ROOT_TEST } from './initialize';
export * from './core';
const fileInNonRootWorkspace = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'dummy.py');
export const rootWorkspaceUri = getWorkspaceRoot();
export const PYTHON_PATH = getPythonPath();
export type PythonSettingKeys = 'workspaceSymbols.enabled' | 'pythonPath' |
'linting.lintOnSave' |
'linting.enabled' | 'linting.pylintEnabled' |
'linting.flake8Enabled' | 'linting.pep8Enabled' | 'linting.pylamaEnabled' |
'linting.prospectorEnabled' | 'linting.pydocstyleEnabled' | 'linting.mypyEnabled' |
'unitTest.nosetestArgs' | 'unitTest.pyTestArgs' | 'unitTest.unittestArgs' |
'formatting.provider' | 'sortImports.args' |
'unitTest.nosetestsEnabled' | 'unitTest.pyTestEnabled' | 'unitTest.unittestEnabled' |
'envFile';
export async function updateSetting(setting: PythonSettingKeys, value: {} | undefined, resource: Uri | undefined, configTarget: ConfigurationTarget) {
const settings = workspace.getConfiguration('python', resource);
const currentValue = settings.inspect(setting);
if (currentValue !== undefined && ((configTarget === ConfigurationTarget.Global && currentValue.globalValue === value) ||
(configTarget === ConfigurationTarget.Workspace && currentValue.workspaceValue === value) ||
(configTarget === ConfigurationTarget.WorkspaceFolder && currentValue.workspaceFolderValue === value))) {
PythonSettings.dispose();
return;
}
// tslint:disable-next-line:await-promise
await settings.update(setting, value, configTarget);
await sleep(2000);
PythonSettings.dispose();
}
function getWorkspaceRoot() {
if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) {
return Uri.file(path.join(__dirname, '..', '..', 'src', 'test'));
}
if (workspace.workspaceFolders.length === 1) {
return workspace.workspaceFolders[0].uri;
}
const workspaceFolder = workspace.getWorkspaceFolder(Uri.file(fileInNonRootWorkspace));
return workspaceFolder ? workspaceFolder.uri : workspace.workspaceFolders[0].uri;
}
// tslint:disable-next-line:no-any
export function retryAsync(wrapped: Function, retryCount: number = 2) {
// tslint:disable-next-line:no-any
return async (...args: any[]) => {
return new Promise((resolve, reject) => {
// tslint:disable-next-line:no-any
const reasons: any[] = [];
const makeCall = () => {
// tslint:disable-next-line:no-unsafe-any no-any no-invalid-this
wrapped.call(this as Function, ...args)
// tslint:disable-next-line:no-unsafe-any no-any
.then(resolve, (reason: any) => {
reasons.push(reason);
if (reasons.length >= retryCount) {
reject(reasons);
} else {
// If failed once, lets wait for some time before trying again.
// tslint:disable-next-line:no-string-based-set-timeout
setTimeout(makeCall, 500);
}
});
};
makeCall();
});
};
}
async function setPythonPathInWorkspace(resource: string | Uri | undefined, config: ConfigurationTarget, pythonPath?: string) {
if (config === ConfigurationTarget.WorkspaceFolder && !IS_MULTI_ROOT_TEST) {
return;
}
const resourceUri = typeof resource === 'string' ? Uri.file(resource) : resource;
const settings = workspace.getConfiguration('python', resourceUri);
const value = settings.inspect<string>('pythonPath');
const prop: 'workspaceFolderValue' | 'workspaceValue' = config === ConfigurationTarget.Workspace ? 'workspaceValue' : 'workspaceFolderValue';
if (value && value[prop] !== pythonPath) {
await settings.update('pythonPath', pythonPath, config);
PythonSettings.dispose();
}
}
async function restoreGlobalPythonPathSetting(): Promise<void> {
// tslint:disable-next-line:no-any
const pythonConfig = workspace.getConfiguration('python', null as any as Uri);
// tslint:disable-next-line:no-non-null-assertion
const currentGlobalPythonPathSetting = pythonConfig.inspect('pythonPath')!.globalValue;
// tslint:disable-next-line:no-use-before-declare
if (globalPythonPathSetting !== currentGlobalPythonPathSetting) {
await pythonConfig.update('pythonPath', undefined, true);
}
PythonSettings.dispose();
}
export async function deleteDirectory(dir: string) {
const exists = await fs.pathExists(dir);
if (exists) {
await fs.remove(dir);
}
}
export async function deleteFile(file: string) {
const exists = await fs.pathExists(file);
if (exists) {
await fs.remove(file);
}
}
// tslint:disable-next-line:no-non-null-assertion
const globalPythonPathSetting = workspace.getConfiguration('python').inspect('pythonPath')!.globalValue;
export const clearPythonPathInWorkspaceFolder = async (resource: string | Uri) => retryAsync(setPythonPathInWorkspace)(resource, ConfigurationTarget.WorkspaceFolder);
export const setPythonPathInWorkspaceRoot = async (pythonPath: string) => retryAsync(setPythonPathInWorkspace)(undefined, ConfigurationTarget.Workspace, pythonPath);
export const resetGlobalPythonPathSetting = async () => retryAsync(restoreGlobalPythonPathSetting)();
function getPythonPath(): string {
// tslint:disable-next-line:no-unsafe-any
if (process.env.TRAVIS_PYTHON_PATH && fs.existsSync(process.env.TRAVIS_PYTHON_PATH)) {
// tslint:disable-next-line:no-unsafe-any
return process.env.TRAVIS_PYTHON_PATH;
}
return 'python';
}