Skip to content

Commit 9ffb6d3

Browse files
author
Kartik Raj
committed
Change worker to return interpreter information instead
1 parent d31f823 commit 9ffb6d3

File tree

3 files changed

+30
-63
lines changed

3 files changed

+30
-63
lines changed

src/client/pythonEnvironments/base/info/interpreter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Architecture } from '../../../common/utils/platform';
77
import { copyPythonExecInfo, PythonExecInfo } from '../../exec';
88
import { parseVersion } from './pythonVersion';
99

10-
type PythonEnvInformation = {
10+
export type InterpreterInformation = {
1111
arch: Architecture;
1212
executable: {
1313
filename: string;
@@ -26,7 +26,7 @@ type PythonEnvInformation = {
2626
* @param python - the path to the Python executable
2727
* @param raw - the information returned by the `interpreterInfo.py` script
2828
*/
29-
export function extractPythonEnvInfo(python: string, raw: PythonEnvInfo): PythonEnvInformation {
29+
export function extractPythonEnvInfo(python: string, raw: PythonEnvInfo): InterpreterInformation {
3030
const rawVersion = `${raw.versionInfo.slice(0, 3).join('.')}-${raw.versionInfo[3]}`;
3131
const version = parseVersion(rawVersion);
3232
version.sysVersion = raw.sysVersion;
@@ -64,7 +64,7 @@ export async function getInterpreterInfo(
6464
python: PythonExecInfo,
6565
shellExec: ShellExecFunc,
6666
logger?: Logger,
67-
): Promise<PythonEnvInformation | undefined> {
67+
): Promise<InterpreterInformation | undefined> {
6868
const [args, parse] = getInterpreterInfoCommand();
6969
const info = copyPythonExecInfo(python, args);
7070
const argv = [info.command, ...info.args];

src/client/pythonEnvironments/info/environmentInfoService.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import { injectable } from 'inversify';
55
import { createDeferred, Deferred } from '../../common/utils/async';
66
import { createWorkerPool, IWorkerPool, QueuePosition } from '../../common/utils/workerPool';
7-
import { PythonEnvInfo } from '../base/info';
8-
import { getInterpreterInfo } from '../base/info/interpreter';
7+
import { getInterpreterInfo, InterpreterInformation } from '../base/info/interpreter';
98
import { shellExecute } from '../common/externalDependencies';
109
import { buildPythonExecInfo } from '../exec';
1110

@@ -17,26 +16,17 @@ export enum EnvironmentInfoServiceQueuePriority {
1716
export const IEnvironmentInfoService = Symbol('IEnvironmentInfoService');
1817
export interface IEnvironmentInfoService {
1918
getEnvironmentInfo(
20-
environment: PythonEnvInfo,
19+
interpreterPath: string,
2120
priority?: EnvironmentInfoServiceQueuePriority
22-
): Promise<PythonEnvInfo | undefined>;
21+
): Promise<InterpreterInformation | undefined>;
2322
}
2423

25-
async function buildEnvironmentInfo(environment: PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
26-
const interpreterInfo = await getInterpreterInfo(
27-
buildPythonExecInfo(environment.executable.filename),
28-
shellExecute,
29-
);
24+
async function buildEnvironmentInfo(interpreterPath: string): Promise<InterpreterInformation | undefined> {
25+
const interpreterInfo = await getInterpreterInfo(buildPythonExecInfo(interpreterPath), shellExecute);
3026
if (interpreterInfo === undefined || interpreterInfo.version === undefined) {
3127
return undefined;
3228
}
33-
// Deep copy into a new object
34-
const resolvedEnv = JSON.parse(JSON.stringify(environment)) as PythonEnvInfo;
35-
resolvedEnv.version = interpreterInfo.version;
36-
resolvedEnv.executable.filename = interpreterInfo.executable.filename;
37-
resolvedEnv.executable.sysPrefix = interpreterInfo.executable.sysPrefix;
38-
resolvedEnv.arch = interpreterInfo.arch;
39-
return resolvedEnv;
29+
return interpreterInfo;
4030
}
4131

4232
@injectable()
@@ -45,29 +35,31 @@ export class EnvironmentInfoService implements IEnvironmentInfoService {
4535
// path again and again in a given session. This information will likely not change in a given
4636
// session. There are definitely cases where this will change. But a simple reload should address
4737
// those.
48-
private readonly cache: Map<string, Deferred<PythonEnvInfo>> = new Map<string, Deferred<PythonEnvInfo>>();
38+
private readonly cache: Map<string, Deferred<InterpreterInformation>> = new Map<
39+
string,
40+
Deferred<InterpreterInformation>
41+
>();
4942

50-
private readonly workerPool: IWorkerPool<PythonEnvInfo, PythonEnvInfo | undefined>;
43+
private readonly workerPool: IWorkerPool<string, InterpreterInformation | undefined>;
5144

5245
public constructor() {
53-
this.workerPool = createWorkerPool<PythonEnvInfo, PythonEnvInfo | undefined>(buildEnvironmentInfo);
46+
this.workerPool = createWorkerPool<string, InterpreterInformation | undefined>(buildEnvironmentInfo);
5447
}
5548

5649
public async getEnvironmentInfo(
57-
environment: PythonEnvInfo,
50+
interpreterPath: string,
5851
priority?: EnvironmentInfoServiceQueuePriority,
59-
): Promise<PythonEnvInfo | undefined> {
60-
const interpreterPath = environment.executable.filename;
52+
): Promise<InterpreterInformation | undefined> {
6153
const result = this.cache.get(interpreterPath);
6254
if (result !== undefined) {
6355
// Another call for this environment has already been made, return its result
6456
return result.promise;
6557
}
66-
const deferred = createDeferred<PythonEnvInfo>();
58+
const deferred = createDeferred<InterpreterInformation>();
6759
this.cache.set(interpreterPath, deferred);
6860
return (priority === EnvironmentInfoServiceQueuePriority.High
69-
? this.workerPool.addToQueue(environment, QueuePosition.Front)
70-
: this.workerPool.addToQueue(environment, QueuePosition.Back)
61+
? this.workerPool.addToQueue(interpreterPath, QueuePosition.Front)
62+
: this.workerPool.addToQueue(interpreterPath, QueuePosition.Back)
7163
).then((r) => {
7264
if (r !== undefined) {
7365
deferred.resolve(r);

src/test/pythonEnvironments/info/environmentInfoService.functional.test.ts

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as sinon from 'sinon';
88
import { ImportMock } from 'ts-mock-imports';
99
import { ExecutionResult } from '../../../client/common/process/types';
1010
import { Architecture } from '../../../client/common/utils/platform';
11-
import { PythonEnvInfo, PythonEnvKind } from '../../../client/pythonEnvironments/base/info';
11+
import { InterpreterInformation } from '../../../client/pythonEnvironments/base/info/interpreter';
1212
import { parseVersion } from '../../../client/pythonEnvironments/base/info/pythonVersion';
1313
import * as ExternalDep from '../../../client/pythonEnvironments/common/externalDependencies';
1414
import {
@@ -19,39 +19,16 @@ import {
1919
suite('Environment Info Service', () => {
2020
let stubShellExec: sinon.SinonStub;
2121

22-
function createEnvInfo(executable: string): PythonEnvInfo {
22+
function createExpectedEnvInfo(executable: string): InterpreterInformation {
2323
return {
24-
id: '',
25-
kind: PythonEnvKind.Unknown,
26-
version: parseVersion('0.0.0'),
27-
name: '',
28-
location: '',
29-
arch: Architecture.x64,
30-
executable: {
31-
filename: executable,
32-
sysPrefix: '',
33-
mtime: -1,
34-
ctime: -1,
35-
},
36-
distro: { org: '' },
37-
};
38-
}
39-
40-
function createExpectedEnvInfo(executable: string): PythonEnvInfo {
41-
return {
42-
id: '',
43-
kind: PythonEnvKind.Unknown,
4424
version: parseVersion('3.8.3-final'),
45-
name: '',
46-
location: '',
4725
arch: Architecture.x64,
4826
executable: {
4927
filename: executable,
5028
sysPrefix: 'path',
5129
mtime: -1,
5230
ctime: -1,
5331
},
54-
distro: { org: '' },
5532
};
5633
}
5734

@@ -72,16 +49,14 @@ suite('Environment Info Service', () => {
7249
});
7350
test('Add items to queue and get results', async () => {
7451
const envService = new EnvironmentInfoService();
75-
const promises: Promise<PythonEnvInfo | undefined>[] = [];
76-
const expected: PythonEnvInfo[] = [];
52+
const promises: Promise<InterpreterInformation | undefined>[] = [];
53+
const expected: InterpreterInformation[] = [];
7754
for (let i = 0; i < 10; i = i + 1) {
7855
const path = `any-path${i}`;
7956
if (i < 5) {
80-
promises.push(envService.getEnvironmentInfo(createEnvInfo(path)));
57+
promises.push(envService.getEnvironmentInfo(path));
8158
} else {
82-
promises.push(
83-
envService.getEnvironmentInfo(createEnvInfo(path), EnvironmentInfoServiceQueuePriority.High),
84-
);
59+
promises.push(envService.getEnvironmentInfo(path, EnvironmentInfoServiceQueuePriority.High));
8560
}
8661
expected.push(createExpectedEnvInfo(path));
8762
}
@@ -97,17 +72,17 @@ suite('Environment Info Service', () => {
9772

9873
test('Add same item to queue', async () => {
9974
const envService = new EnvironmentInfoService();
100-
const promises: Promise<PythonEnvInfo | undefined>[] = [];
101-
const expected: PythonEnvInfo[] = [];
75+
const promises: Promise<InterpreterInformation | undefined>[] = [];
76+
const expected: InterpreterInformation[] = [];
10277

10378
const path = 'any-path';
10479
// Clear call counts
10580
stubShellExec.resetHistory();
10681
// Evaluate once so the result is cached.
107-
await envService.getEnvironmentInfo(createEnvInfo(path));
82+
await envService.getEnvironmentInfo(path);
10883

10984
for (let i = 0; i < 10; i = i + 1) {
110-
promises.push(envService.getEnvironmentInfo(createEnvInfo(path)));
85+
promises.push(envService.getEnvironmentInfo(path));
11186
expected.push(createExpectedEnvInfo(path));
11287
}
11388

0 commit comments

Comments
 (0)