forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
134 lines (128 loc) · 5.47 KB
/
helpers.ts
File metadata and controls
134 lines (128 loc) · 5.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
import { inject, injectable } from 'inversify';
import { compare } from 'semver';
import { ConfigurationTarget } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../common/application/types';
import { traceError } from '../common/logger';
import { InterpreterInfomation, IPythonExecutionFactory } from '../common/process/types';
import { IPersistentStateFactory, Resource } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { IInterpreterHelper, InterpreterType, PythonInterpreter, WorkspacePythonPath } from './contracts';
import { InterpeterHashProviderFactory } from './locators/services/hashProviderFactory';
import { IInterpreterHashProviderFactory } from './locators/types';
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 persistentFactory: IPersistentStateFactory;
constructor(
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(InterpeterHashProviderFactory) private readonly hashProviderFactory: IInterpreterHashProviderFactory
) {
this.persistentFactory = this.serviceContainer.get<IPersistentStateFactory>(IPersistentStateFactory);
}
public getActiveWorkspaceUri(resource: Resource): WorkspacePythonPath | undefined {
const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
if (!workspaceService.hasWorkspaceFolders) {
return;
}
if (Array.isArray(workspaceService.workspaceFolders) && workspaceService.workspaceFolders.length === 1) {
return { folderUri: workspaceService.workspaceFolders[0].uri, configTarget: ConfigurationTarget.Workspace };
}
if (resource) {
const workspaceFolder = workspaceService.getWorkspaceFolder(resource);
if (workspaceFolder) {
return { configTarget: ConfigurationTarget.WorkspaceFolder, folderUri: workspaceFolder.uri };
}
}
const documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager);
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>> {
const fileHash = await this.hashProviderFactory
.create({ pythonPath })
.then(provider => provider.getInterpreterHash(pythonPath))
.catch(ex => {
traceError(`Failed to create File hash for interpreter ${pythonPath}`, ex);
return '';
});
const store = this.persistentFactory.createGlobalPersistentState<CachedPythonInterpreter>(
`${pythonPath}.v3`,
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) {
traceError(`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 '';
}
}
}
public getBestInterpreter(interpreters?: PythonInterpreter[]): PythonInterpreter | undefined {
if (!Array.isArray(interpreters) || interpreters.length === 0) {
return;
}
if (interpreters.length === 1) {
return interpreters[0];
}
const sorted = interpreters.slice();
sorted.sort((a, b) => (a.version && b.version ? compare(a.version.raw, b.version.raw) : 0));
return sorted[sorted.length - 1];
}
}