forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
122 lines (117 loc) · 5.03 KB
/
index.ts
File metadata and controls
122 lines (117 loc) · 5.03 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
import { inject, injectable } from 'inversify';
import { Disposable, Event, EventEmitter, Uri } from 'vscode';
import { traceDecorators } from '../../common/logger';
import { IPlatformService } from '../../common/platform/types';
import { IDisposableRegistry } from '../../common/types';
import { createDeferred, Deferred } from '../../common/utils/async';
import { OSType } from '../../common/utils/platform';
import { IServiceContainer } from '../../ioc/types';
import {
CONDA_ENV_FILE_SERVICE,
CONDA_ENV_SERVICE,
CURRENT_PATH_SERVICE,
GLOBAL_VIRTUAL_ENV_SERVICE,
IInterpreterLocatorHelper,
IInterpreterLocatorService,
KNOWN_PATH_SERVICE,
PIPENV_SERVICE,
PythonInterpreter,
WINDOWS_REGISTRY_SERVICE,
WORKSPACE_VIRTUAL_ENV_SERVICE
} from '../contracts';
import { InterpreterFilter } from './services/interpreterFilter';
import { IInterpreterFilter } from './types';
// tslint:disable-next-line:no-require-imports no-var-requires
const flatten = require('lodash/flatten') as typeof import('lodash/flatten');
/**
* Facilitates locating Python interpreters.
*/
@injectable()
export class PythonInterpreterLocatorService implements IInterpreterLocatorService {
private readonly disposables: Disposable[] = [];
private readonly platform: IPlatformService;
private readonly interpreterLocatorHelper: IInterpreterLocatorHelper;
private readonly _hasInterpreters: Deferred<boolean>;
constructor(
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(InterpreterFilter) private readonly interpreterFilter: IInterpreterFilter
) {
this._hasInterpreters = createDeferred<boolean>();
serviceContainer.get<Disposable[]>(IDisposableRegistry).push(this);
this.platform = serviceContainer.get<IPlatformService>(IPlatformService);
this.interpreterLocatorHelper = serviceContainer.get<IInterpreterLocatorHelper>(IInterpreterLocatorHelper);
}
/**
* This class should never emit events when we're locating.
* The events will be fired by the indivitual locators retrieved in `getLocators`.
*
* @readonly
* @type {Event<Promise<PythonInterpreter[]>>}
* @memberof PythonInterpreterLocatorService
*/
public get onLocating(): Event<Promise<PythonInterpreter[]>> {
return new EventEmitter<Promise<PythonInterpreter[]>>().event;
}
public get hasInterpreters(): Promise<boolean> {
return this._hasInterpreters.completed ? this._hasInterpreters.promise : Promise.resolve(false);
}
/**
* Release any held resources.
*
* Called by VS Code to indicate it is done with the resource.
*/
public dispose() {
this.disposables.forEach(disposable => disposable.dispose());
}
/**
* Return the list of known Python interpreters.
*
* The optional resource arg may control where locators look for
* interpreters.
*/
@traceDecorators.verbose('Get Interpreters')
public async getInterpreters(resource?: Uri): Promise<PythonInterpreter[]> {
const locators = this.getLocators();
const promises = locators.map(async provider => provider.getInterpreters(resource));
locators.forEach(locator => {
locator.hasInterpreters
.then(found => {
if (found) {
this._hasInterpreters.resolve(true);
}
})
.ignoreErrors();
});
const listOfInterpreters = await Promise.all(promises);
const items = flatten(listOfInterpreters)
.filter(item => !!item)
.map(item => item!)
.filter(item => !this.interpreterFilter.isHiddenInterpreter(item));
this._hasInterpreters.resolve(items.length > 0);
return this.interpreterLocatorHelper.mergeInterpreters(items);
}
/**
* Return the list of applicable interpreter locators.
*
* The locators are pulled from the registry.
*/
private getLocators(): IInterpreterLocatorService[] {
// The order of the services is important.
// The order is important because the data sources at the bottom of the list do not contain all,
// the information about the interpreters (e.g. type, environment name, etc).
// This way, the items returned from the top of the list will win, when we combine the items returned.
const keys: [string, OSType | undefined][] = [
[WINDOWS_REGISTRY_SERVICE, OSType.Windows],
[CONDA_ENV_SERVICE, undefined],
[CONDA_ENV_FILE_SERVICE, undefined],
[PIPENV_SERVICE, undefined],
[GLOBAL_VIRTUAL_ENV_SERVICE, undefined],
[WORKSPACE_VIRTUAL_ENV_SERVICE, undefined],
[KNOWN_PATH_SERVICE, undefined],
[CURRENT_PATH_SERVICE, undefined]
];
return keys
.filter(item => item[1] === undefined || item[1] === this.platform.osType)
.map(item => this.serviceContainer.get<IInterpreterLocatorService>(IInterpreterLocatorService, item[0]));
}
}