forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
96 lines (91 loc) · 4.18 KB
/
helpers.ts
File metadata and controls
96 lines (91 loc) · 4.18 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
import { inject, injectable } from 'inversify';
import { ConfigurationTarget } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../common/application/types';
import { IFileSystem } from '../common/platform/types';
import { InterpreterInfomation, IPythonExecutionFactory } from '../common/process/types';
import { IPersistentStateFactory } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { IInterpreterHelper, InterpreterType, PythonInterpreter, WorkspacePythonPath } from './contracts';
const EXPITY_DURATION = 24 * 60 * 60 * 1000;
type CachedPythonInterpreter = Partial<PythonInterpreter> & { fileHash: string };
export function getFirstNonEmptyLineFromMultilineString(stdout: string) {
if (!stdout) {
return '';
}
const lines = stdout.split(/\r?\n/g).map(line => line.trim()).filter(line => line.length > 0);
return lines.length > 0 ? lines[0] : '';
}
@injectable()
export class InterpreterHelper implements IInterpreterHelper {
private readonly fs: IFileSystem;
private readonly persistentFactory: IPersistentStateFactory;
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
this.persistentFactory = this.serviceContainer.get<IPersistentStateFactory>(IPersistentStateFactory);
this.fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
}
public getActiveWorkspaceUri(): WorkspacePythonPath | undefined {
const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager);
if (!workspaceService.hasWorkspaceFolders) {
return;
}
if (Array.isArray(workspaceService.workspaceFolders) && workspaceService.workspaceFolders.length === 1) {
return { folderUri: workspaceService.workspaceFolders[0].uri, configTarget: ConfigurationTarget.Workspace };
}
if (documentManager.activeTextEditor) {
const workspaceFolder = workspaceService.getWorkspaceFolder(documentManager.activeTextEditor.document.uri);
if (workspaceFolder) {
return { configTarget: ConfigurationTarget.WorkspaceFolder, folderUri: workspaceFolder.uri };
}
}
}
public async getInterpreterInformation(pythonPath: string): Promise<undefined | Partial<PythonInterpreter>> {
let fileHash = await this.fs.getFileHash(pythonPath).catch(() => '');
fileHash = fileHash ? fileHash : '';
const store = this.persistentFactory.createGlobalPersistentState<CachedPythonInterpreter>(`${pythonPath}.v1`, undefined, EXPITY_DURATION);
if (store.value && fileHash && store.value.fileHash === fileHash) {
return store.value;
}
const processService = await this.serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory).create({ pythonPath });
try {
const info = await processService.getInterpreterInformation().catch<InterpreterInfomation | undefined>(() => undefined);
if (!info) {
return;
}
const details = {
...(info),
fileHash
};
await store.updateValue(details);
return details;
} catch (ex) {
console.error(`Failed to get interpreter information for '${pythonPath}'`, ex);
return;
}
}
public isMacDefaultPythonPath(pythonPath: string) {
return pythonPath === 'python' || pythonPath === '/usr/bin/python';
}
public getInterpreterTypeDisplayName(interpreterType: InterpreterType) {
switch (interpreterType) {
case InterpreterType.Conda: {
return 'conda';
}
case InterpreterType.PipEnv: {
return 'pipenv';
}
case InterpreterType.Pyenv: {
return 'pyenv';
}
case InterpreterType.Venv: {
return 'venv';
}
case InterpreterType.VirtualEnv: {
return 'virtualenv';
}
default: {
return '';
}
}
}
}