|
| 1 | +"use strict"; |
| 2 | +import * as _ from 'lodash'; |
| 3 | +import { fixInterpreterPath, fixInterpreterDisplayName } from './helpers'; |
| 4 | +import { IInterpreterLocatorService, PythonInterpreter } from '../contracts'; |
| 5 | +import { InterpreterVersionService } from '../interpreterVersion'; |
| 6 | +import { IS_WINDOWS, Is_64Bit, arePathsSame, areBasePathsSame } from '../../common/utils'; |
| 7 | +import { RegistryImplementation } from '../../common/registry'; |
| 8 | +import { CondaEnvService } from './services/condaEnvService'; |
| 9 | +import { VirtualEnvService, getKnownSearchPathsForVirtualEnvs } from './services/virtualEnvService'; |
| 10 | +import { KnownPathsService, getKnownSearchPathsForInterpreters } from './services/KnownPathsService'; |
| 11 | +import { CurrentPathService } from './services/currentPathService'; |
| 12 | +import { WindowsRegistryService } from './services/windowsRegistryService'; |
| 13 | +import { VirtualEnvironmentManager } from '../virtualEnvs'; |
| 14 | +import { CondaEnvFileService, getEnvironmentsFile as getCondaEnvFile } from './services/condaEnvFileService'; |
| 15 | + |
| 16 | +export class PythonInterpreterLocatorService implements IInterpreterLocatorService { |
| 17 | + private interpreters: PythonInterpreter[] = []; |
| 18 | + private locators: IInterpreterLocatorService[] = []; |
| 19 | + constructor(private virtualEnvMgr: VirtualEnvironmentManager) { |
| 20 | + const versionService = new InterpreterVersionService(); |
| 21 | + // The order of the services is important. |
| 22 | + if (IS_WINDOWS) { |
| 23 | + const windowsRegistryProvider = new WindowsRegistryService(new RegistryImplementation(), Is_64Bit); |
| 24 | + this.locators.push(windowsRegistryProvider); |
| 25 | + this.locators.push(new CondaEnvService(windowsRegistryProvider)); |
| 26 | + } |
| 27 | + else { |
| 28 | + this.locators.push(new CondaEnvService()); |
| 29 | + } |
| 30 | + // Supplements the above list of conda environments. |
| 31 | + this.locators.push(new CondaEnvFileService(getCondaEnvFile(), versionService)); |
| 32 | + this.locators.push(new VirtualEnvService(getKnownSearchPathsForVirtualEnvs(), this.virtualEnvMgr, versionService)); |
| 33 | + |
| 34 | + if (!IS_WINDOWS) { |
| 35 | + // This must be last, it is possible we have paths returned here that are already returned |
| 36 | + // in one of the above lists. |
| 37 | + this.locators.push(new KnownPathsService(getKnownSearchPathsForInterpreters(), versionService)); |
| 38 | + } |
| 39 | + // This must be last, it is possible we have paths returned here that are already returned |
| 40 | + // in one of the above lists. |
| 41 | + this.locators.push(new CurrentPathService(this.virtualEnvMgr, versionService)); |
| 42 | + } |
| 43 | + public async getInterpreters() { |
| 44 | + if (this.interpreters.length > 0) { |
| 45 | + return this.interpreters; |
| 46 | + } |
| 47 | + const promises = this.locators.map(provider => provider.getInterpreters()); |
| 48 | + return Promise.all(promises) |
| 49 | + .then(interpreters => _.flatten(interpreters)) |
| 50 | + .then(items => items.map(fixInterpreterDisplayName)) |
| 51 | + .then(items => items.map(fixInterpreterPath)) |
| 52 | + .then(items => items.reduce<PythonInterpreter[]>((accumulator, current) => { |
| 53 | + if (accumulator.findIndex(item => arePathsSame(item.path, current.path)) === -1 && |
| 54 | + accumulator.findIndex(item => areBasePathsSame(item.path, current.path)) === -1) { |
| 55 | + accumulator.push(current); |
| 56 | + } |
| 57 | + return accumulator; |
| 58 | + }, [])) |
| 59 | + .then(interpreters => this.interpreters = interpreters); |
| 60 | + } |
| 61 | +} |
0 commit comments