-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathconfigSettings.ts
247 lines (213 loc) · 9.43 KB
/
configSettings.ts
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
import * as child_process from 'child_process';
import { ConfigurationChangeEvent, Disposable, workspace, WorkspaceConfiguration } from 'coc.nvim';
import fs from 'fs';
import path from 'path';
import which from 'which';
import { SystemVariables } from './systemVariables';
import { IFormattingSettings, ILintingSettings, IPythonSettings, ISortImportSettings } from './types';
export class PythonSettings implements IPythonSettings {
private workspaceRoot: string;
private static pythonSettings: Map<string, PythonSettings> = new Map<string, PythonSettings>();
public linting!: ILintingSettings;
public formatting!: IFormattingSettings;
public sortImports!: ISortImportSettings;
private disposables: Disposable[] = [];
private _pythonPath = '';
private _stdLibs: string[] = [];
constructor() {
this.workspaceRoot = workspace.root ? workspace.root : __dirname;
this.initialize();
}
public static getInstance(): PythonSettings {
const workspaceFolder = workspace.workspaceFolders.length > 0 ? workspace.workspaceFolders[0] : undefined;
const workspaceFolderKey = workspaceFolder ? workspaceFolder.name : 'unknown';
if (!PythonSettings.pythonSettings.has(workspaceFolderKey)) {
const settings = new PythonSettings();
PythonSettings.pythonSettings.set(workspaceFolderKey, settings);
}
return PythonSettings.pythonSettings.get(workspaceFolderKey)!;
}
public static dispose() {
PythonSettings.pythonSettings.forEach((item) => item && item.dispose());
PythonSettings.pythonSettings.clear();
}
public dispose() {
this.disposables.forEach((disposable) => disposable && disposable.dispose());
this.disposables = [];
}
private resolvePythonFromVENV(): string | undefined {
function pythonBinFromPath(p: string): string | undefined {
const fullPath = process.platform === 'win32' ? path.join(p, 'Scripts', 'python.exe') : path.join(p, 'bin', 'python');
return fs.existsSync(fullPath) ? fullPath : undefined;
}
try {
// virtualenv
if (process.env.VIRTUAL_ENV && fs.existsSync(path.join(process.env.VIRTUAL_ENV, 'pyvenv.cfg'))) {
return pythonBinFromPath(process.env.VIRTUAL_ENV);
}
// conda
if (process.env.CONDA_PREFIX) {
return pythonBinFromPath(process.env.CONDA_PREFIX);
}
// `pyenv local` creates `.python-version`, but not `PYENV_VERSION`
let p = path.join(this.workspaceRoot, '.python-version');
if (fs.existsSync(p)) {
if (!process.env.PYENV_VERSION) {
// pyenv local can special multiple Python, use first one only
process.env.PYENV_VERSION = fs.readFileSync(p).toString().trim().split('\n')[0];
}
return;
}
// pipenv
p = path.join(this.workspaceRoot, 'Pipfile');
if (fs.existsSync(p)) {
return child_process.spawnSync('pipenv', ['--py'], { encoding: 'utf8' }).stdout.trim();
}
// poetry
p = path.join(this.workspaceRoot, 'poetry.lock');
if (fs.existsSync(p)) {
const list = child_process.spawnSync('poetry', ['env', 'list', '--full-path', '--no-ansi'], { encoding: 'utf8', cwd: this.workspaceRoot }).stdout.trim();
let info = '';
for (const item of list.split('\n')) {
if (item.includes('(Activated)')) {
info = item.replace(/\(Activated\)/, '').trim();
break;
}
info = item;
}
if (info) {
return pythonBinFromPath(info);
}
}
// pdm
p = path.join(this.workspaceRoot, '.pdm-python');
if (fs.existsSync(p)) {
return child_process.spawnSync('pdm', ['info', '--python'], { encoding: 'utf8' }).stdout.trim();
}
// virtualenv in the workspace root
const files = fs.readdirSync(this.workspaceRoot);
for (const file of files) {
const x = path.join(this.workspaceRoot, file);
if (fs.existsSync(path.join(x, 'pyvenv.cfg'))) {
return pythonBinFromPath(x);
}
}
} catch (e) {
console.error(e);
}
}
protected update(pythonSettings: WorkspaceConfiguration) {
const systemVariables: SystemVariables = new SystemVariables(this.workspaceRoot ? this.workspaceRoot : undefined);
const vp = this.resolvePythonFromVENV();
this.pythonPath = vp ? vp : systemVariables.resolve(pythonSettings.get('pythonPath') as string);
const lintingSettings = systemVariables.resolveAny(pythonSettings.get<ILintingSettings>('linting'))!;
if (this.linting) {
Object.assign<ILintingSettings, ILintingSettings>(this.linting, lintingSettings);
} else {
this.linting = lintingSettings;
}
this.linting.pylintPath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.pylintPath));
this.linting.flake8Path = this.getAbsolutePath(systemVariables.resolveAny(this.linting.flake8Path));
this.linting.pycodestylePath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.pycodestylePath));
this.linting.pyflakesPath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.pyflakesPath));
this.linting.pylamaPath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.pylamaPath));
this.linting.prospectorPath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.prospectorPath));
this.linting.pydocstylePath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.pydocstylePath));
this.linting.mypyPath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.mypyPath));
this.linting.banditPath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.banditPath));
this.linting.ruffPath = this.getAbsolutePath(systemVariables.resolveAny(this.linting.ruffPath));
const formattingSettings = systemVariables.resolveAny(pythonSettings.get<IFormattingSettings>('formatting'))!;
if (this.formatting) {
Object.assign<IFormattingSettings, IFormattingSettings>(this.formatting, formattingSettings);
} else {
this.formatting = formattingSettings;
}
this.formatting.autopep8Path = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.autopep8Path));
this.formatting.yapfPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.yapfPath));
this.formatting.ruffPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.ruffPath));
this.formatting.blackPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.blackPath));
this.formatting.pyinkPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.pyinkPath));
this.formatting.blackdPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.blackdPath));
this.formatting.darkerPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.darkerPath));
const isort = systemVariables.resolveAny(pythonSettings.get<ISortImportSettings>('sortImports'))!;
if (this.sortImports) {
Object.assign<ISortImportSettings, ISortImportSettings>(this.sortImports, isort);
} else {
this.sortImports = isort;
}
this.sortImports.path = this.getAbsolutePath(systemVariables.resolveAny(this.sortImports.path));
}
public get stdLibs(): string[] {
return this._stdLibs;
}
public get pythonPath(): string {
return this._pythonPath;
}
public set pythonPath(value: string) {
if (this._pythonPath === value) {
return;
}
try {
this._pythonPath = getPythonExecutable(value);
this._stdLibs = getStdLibs(this._pythonPath);
} catch (ex) {
this._pythonPath = value;
}
}
private getAbsolutePath(pathToCheck: string, rootDir?: string): string {
if (!rootDir) {
rootDir = this.workspaceRoot;
}
pathToCheck = workspace.expand(pathToCheck);
if (pathToCheck.indexOf(path.sep) === -1) {
return pathToCheck;
}
return path.isAbsolute(pathToCheck) ? pathToCheck : path.resolve(rootDir, pathToCheck);
}
protected initialize(): void {
this.disposables.push(
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration('python')) {
const currentConfig = workspace.getConfiguration('python', workspace.root);
this.update(currentConfig);
}
})
);
const initialConfig = workspace.getConfiguration('python', workspace.root);
if (initialConfig) {
this.update(initialConfig);
}
}
}
function getPythonExecutable(pythonPath: string): string {
pythonPath = workspace.expand(pythonPath);
// If only 'python'.
if (pythonPath === 'python' || pythonPath.indexOf(path.sep) === -1 || path.basename(pythonPath) === path.dirname(pythonPath)) {
const bin = which.sync(pythonPath, { nothrow: true });
if (bin) {
pythonPath = bin;
}
}
if (isValidPythonPath(pythonPath)) {
return pythonPath;
}
return pythonPath;
}
function getStdLibs(pythonPath: string): string[] {
try {
let args = ['-c', 'import site;print(site.getsitepackages()[0])'];
const sitePkgs = child_process.spawnSync(pythonPath, args, { encoding: 'utf8' }).stdout.trim();
args = ['-c', 'import site;print(site.getusersitepackages())'];
const userPkgs = child_process.spawnSync(pythonPath, args, { encoding: 'utf8' }).stdout.trim();
return [sitePkgs, userPkgs];
} catch (e) {
return [];
}
}
function isValidPythonPath(pythonPath: string): boolean {
try {
return child_process.spawnSync(pythonPath, ['-c', 'print(1234)'], { encoding: 'utf8' }).stdout.startsWith('1234');
} catch (ex) {
return false;
}
}