forked from DonJayamanne/vscode-python-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvironmentInfoService.ts
More file actions
102 lines (89 loc) · 3.77 KB
/
Copy pathenvironmentInfoService.ts
File metadata and controls
102 lines (89 loc) · 3.77 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { traceVerbose } from '../../../common/logger';
import { IDisposableRegistry } from '../../../common/types';
import { createDeferred, Deferred } from '../../../common/utils/async';
import { createRunningWorkerPool, IWorkerPool, QueuePosition } from '../../../common/utils/workerPool';
import { getInterpreterInfo, InterpreterInformation } from './interpreter';
import { buildPythonExecInfo } from '../../exec';
export enum EnvironmentInfoServiceQueuePriority {
Default,
High,
}
export interface IEnvironmentInfoService {
getEnvironmentInfo(
interpreterPath: string,
priority?: EnvironmentInfoServiceQueuePriority,
): Promise<InterpreterInformation | undefined>;
isInfoProvided(interpreterPath: string): boolean;
}
async function buildEnvironmentInfo(interpreterPath: string): Promise<InterpreterInformation | undefined> {
const interpreterInfo = await getInterpreterInfo(buildPythonExecInfo(interpreterPath)).catch((reason) => {
traceVerbose(reason);
return undefined;
});
if (interpreterInfo === undefined || interpreterInfo.version === undefined) {
return undefined;
}
return interpreterInfo;
}
class EnvironmentInfoService implements IEnvironmentInfoService {
// Caching environment here in-memory. This is so that we don't have to run this on the same
// path again and again in a given session. This information will likely not change in a given
// session. There are definitely cases where this will change. But a simple reload should address
// those.
private readonly cache: Map<string, Deferred<InterpreterInformation>> = new Map<
string,
Deferred<InterpreterInformation>
>();
private workerPool?: IWorkerPool<string, InterpreterInformation | undefined>;
public dispose(): void {
if (this.workerPool !== undefined) {
this.workerPool.stop();
this.workerPool = undefined;
}
}
public async getEnvironmentInfo(
interpreterPath: string,
priority?: EnvironmentInfoServiceQueuePriority,
): Promise<InterpreterInformation | undefined> {
const result = this.cache.get(interpreterPath);
if (result !== undefined) {
// Another call for this environment has already been made, return its result
return result.promise;
}
if (this.workerPool === undefined) {
this.workerPool = createRunningWorkerPool<string, InterpreterInformation | undefined>(buildEnvironmentInfo);
}
const deferred = createDeferred<InterpreterInformation>();
this.cache.set(interpreterPath, deferred);
return (priority === EnvironmentInfoServiceQueuePriority.High
? this.workerPool.addToQueue(interpreterPath, QueuePosition.Front)
: this.workerPool.addToQueue(interpreterPath, QueuePosition.Back)
).then((r) => {
deferred.resolve(r);
if (r === undefined) {
this.cache.delete(interpreterPath);
}
return r;
});
}
public isInfoProvided(interpreterPath: string): boolean {
const result = this.cache.get(interpreterPath);
return !!(result && result.completed);
}
}
let envInfoService: IEnvironmentInfoService | undefined;
export function getEnvironmentInfoService(disposables?: IDisposableRegistry): IEnvironmentInfoService {
if (envInfoService === undefined) {
const service = new EnvironmentInfoService();
disposables?.push({
dispose: () => {
service.dispose();
envInfoService = undefined;
},
});
envInfoService = service;
}
return envInfoService;
}