Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added environments resolver #14019

Merged
merged 6 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/pythonEnvironments/base/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type PythonLocatorQuery = BasicPythonLocatorQuery & {
searchLocations?: Uri[];
};

export type QueryForEvent<E> = E extends PythonEnvsChangedEvent ? PythonLocatorQuery : BasicPythonLocatorQuery;
type QueryForEvent<E> = E extends PythonEnvsChangedEvent ? PythonLocatorQuery : BasicPythonLocatorQuery;

/**
* A single Python environment locator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { traceVerbose } from '../../common/logger';
import { createDeferred } from '../../common/utils/async';
import { areSameEnvironment, PythonEnvInfo, PythonEnvKind } from '../base/info';
import {
ILocator, IPythonEnvsIterator, PythonEnvUpdatedEvent, QueryForEvent,
ILocator, IPythonEnvsIterator, PythonEnvUpdatedEvent, PythonLocatorQuery,
} from '../base/locator';
import { PythonEnvsChangedEvent } from '../base/watcher';

Expand Down Expand Up @@ -47,7 +47,7 @@ export class PythonEnvsReducer implements ILocator {
return this.parentLocator.resolveEnv(environment);
}

public iterEnvs(query?: QueryForEvent<PythonEnvsChangedEvent>): IPythonEnvsIterator {
public iterEnvs(query?: PythonLocatorQuery): IPythonEnvsIterator {
const didUpdate = new EventEmitter<PythonEnvUpdatedEvent | null>();
const incomingIterator = this.parentLocator.iterEnvs(query);
const iterator: IPythonEnvsIterator = iterEnvsIterator(incomingIterator, didUpdate);
Expand Down
133 changes: 133 additions & 0 deletions src/client/pythonEnvironments/collection/environmentsResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
karrtikr marked this conversation as resolved.
Show resolved Hide resolved
// Licensed under the MIT License.

import { cloneDeep } from 'lodash';
import { Event, EventEmitter } from 'vscode';
import { traceVerbose } from '../../common/logger';
import { areSameEnvironment, PythonEnvInfo } from '../base/info';
import { InterpreterInformation } from '../base/info/interpreter';
import {
ILocator, IPythonEnvsIterator, PythonEnvUpdatedEvent, PythonLocatorQuery,
} from '../base/locator';
import { PythonEnvsChangedEvent } from '../base/watcher';
import { IEnvironmentInfoService } from '../info/environmentInfoService';

export class PythonEnvsResolver implements ILocator {
karrtikr marked this conversation as resolved.
Show resolved Hide resolved
public get onChanged(): Event<PythonEnvsChangedEvent> {
return this.parentLocator.onChanged;
}

constructor(
private readonly parentLocator: ILocator,
private readonly environmentInfoService: IEnvironmentInfoService,
ericsnowcurrently marked this conversation as resolved.
Show resolved Hide resolved
) {}

public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
const environment = await this.parentLocator.resolveEnv(env);
if (!environment) {
return undefined;
}
const interpreterInfo = await this.environmentInfoService.getEnvironmentInfo(environment.executable.filename);
if (!interpreterInfo) {
return undefined;
}
return getResolvedEnv(interpreterInfo, environment);
}

public iterEnvs(query?: PythonLocatorQuery): IPythonEnvsIterator {
const didUpdate = new EventEmitter<PythonEnvUpdatedEvent | null>();
const incomingIterator = this.parentLocator.iterEnvs(query);
const iterator: IPythonEnvsIterator = this.iterEnvsIterator(incomingIterator, didUpdate);
iterator.onUpdated = didUpdate.event;
return iterator;
}

private async* iterEnvsIterator(
iterator: IPythonEnvsIterator,
didUpdate: EventEmitter<PythonEnvUpdatedEvent | null>,
): AsyncIterator<PythonEnvInfo, void> {
const state = {
done: false,
pending: 0,
};
const seen: PythonEnvInfo[] = [];

if (iterator.onUpdated !== undefined) {
iterator.onUpdated((event) => {
if (event === null) {
state.done = true;
checkIfFinishedAndNotify(state, didUpdate);
} else {
const oldIndex = seen.findIndex((s) => areSameEnvironment(s, event.old));
karrtikr marked this conversation as resolved.
Show resolved Hide resolved
if (oldIndex !== -1) {
seen[oldIndex] = event.new;
state.pending += 1;
this.resolveInBackground(oldIndex, state, didUpdate, seen).ignoreErrors();
} else {
// This implies a problem in a downstream locator
traceVerbose(`Expected already iterated env in resolver, got ${event.old}`);
}
}
});
}

let result = await iterator.next();
while (!result.done) {
const currEnv = result.value;
// Resolver only expects unique environments, so store & yield as-is.
karrtikr marked this conversation as resolved.
Show resolved Hide resolved
seen.push(currEnv);
yield currEnv;
state.pending += 1;
this.resolveInBackground(seen.indexOf(currEnv), state, didUpdate, seen).ignoreErrors();
// eslint-disable-next-line no-await-in-loop
result = await iterator.next();
}
if (iterator.onUpdated === undefined) {
state.done = true;
checkIfFinishedAndNotify(state, didUpdate);
}
}

private async resolveInBackground(
envIndex: number,
state: { done: boolean; pending: number },
didUpdate: EventEmitter<PythonEnvUpdatedEvent | null>,
seen: PythonEnvInfo[],
) {
const interpreterInfo = await this.environmentInfoService.getEnvironmentInfo(
seen[envIndex].executable.filename,
);
if (interpreterInfo) {
const resolvedEnv = getResolvedEnv(interpreterInfo, seen[envIndex]);
didUpdate.fire({ old: seen[envIndex], new: resolvedEnv });
seen[envIndex] = resolvedEnv;
}
state.pending -= 1;
checkIfFinishedAndNotify(state, didUpdate);
}
}

/**
* When all info from incoming iterator has been received and all background calls finishes, notify that we're done
* @param state Carries the current state of progress
* @param didUpdate Used to notify when finished
*/
function checkIfFinishedAndNotify(
state: { done: boolean; pending: number },
didUpdate: EventEmitter<PythonEnvUpdatedEvent | null>,
) {
if (state.done && state.pending === 0) {
didUpdate.fire(null);
didUpdate.dispose();
}
}

function getResolvedEnv(interpreterInfo: InterpreterInformation, environment: PythonEnvInfo) {
// Deep copy into a new object
const resolvedEnv = cloneDeep(environment);
resolvedEnv.version = interpreterInfo.version;
resolvedEnv.executable.filename = interpreterInfo.executable.filename;
resolvedEnv.executable.sysPrefix = interpreterInfo.executable.sysPrefix;
resolvedEnv.arch = interpreterInfo.arch;
return resolvedEnv;
}
karrtikr marked this conversation as resolved.
Show resolved Hide resolved
Loading