Skip to content

Commit

Permalink
Simplify locator
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Sep 30, 2020
1 parent c7ec7ea commit b8c98a8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

import * as fsapi from 'fs-extra';
import * as path from 'path';
import { Event, EventEmitter } from 'vscode';
import { traceWarning } from '../../../../common/logger';
import { Architecture, getEnvironmentVariable } from '../../../../common/utils/platform';
import {
PythonEnvInfo, PythonEnvKind, PythonReleaseLevel, PythonVersion,
} from '../../../base/info';
import { parseVersion } from '../../../base/info/pythonVersion';
import { ILocator, IPythonEnvsIterator } from '../../../base/locator';
import { PythonEnvsChangedEvent } from '../../../base/watcher';
import { PythonEnvsWatcher } from '../../../base/watcher';
import { getFileInfo } from '../../../common/externalDependencies';
import { isWindowsPythonExe } from '../../../common/windowsUtils';
import { IEnvironmentInfoService } from '../../../info/environmentInfoService';

/**
* Gets path to the Windows Apps directory.
Expand Down Expand Up @@ -116,13 +114,9 @@ export async function getWindowsStorePythonExes(): Promise<string[]> {
.filter(isWindowsPythonExe);
}

export class WindowsStoreLocator implements ILocator {
export class WindowsStoreLocator extends PythonEnvsWatcher implements ILocator {
private readonly kind:PythonEnvKind = PythonEnvKind.WindowsStore;

private readonly eventEmitter = new EventEmitter<PythonEnvsChangedEvent>();

public constructor(private readonly envService:IEnvironmentInfoService) { }

public iterEnvs(): IPythonEnvsIterator {
const buildEnvInfo = (exe:string) => this.buildEnvInfo(exe);
const iterator = async function* () {
Expand All @@ -135,31 +129,11 @@ export class WindowsStoreLocator implements ILocator {
public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
const executablePath = typeof env === 'string' ? env : env.executable.filename;
if (await isWindowsStoreEnvironment(executablePath)) {
const interpreterInfo = await this.envService.getEnvironmentInfo(executablePath);
if (interpreterInfo) {
const data = await getFileInfo(executablePath);
interpreterInfo.executable = {
...interpreterInfo.executable,
...data,
};
return Promise.resolve({
name: '',
location: '',
kind: this.kind,
executable: interpreterInfo.executable,
version: interpreterInfo.version,
arch: interpreterInfo.arch,
distro: { org: 'Microsoft' },
});
}
return this.buildEnvInfo(executablePath);
}
return undefined;
}

public get onChanged(): Event<PythonEnvsChangedEvent> {
return this.eventEmitter.event;
}

private async buildEnvInfo(exe:string): Promise<PythonEnvInfo> {
let version:PythonVersion;
try {
Expand All @@ -169,7 +143,7 @@ export class WindowsStoreLocator implements ILocator {
major: 3,
minor: -1,
micro: -1,
release: { level: PythonReleaseLevel.Unknown, serial: -1 },
release: { level: PythonReleaseLevel.Final, serial: -1 },
sysVersion: undefined,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { InterpreterInformation } from '../../../../client/pythonEnvironments/ba
import { parseVersion } from '../../../../client/pythonEnvironments/base/info/pythonVersion';
import * as externalDep from '../../../../client/pythonEnvironments/common/externalDependencies';
import { getWindowsStorePythonExes, WindowsStoreLocator } from '../../../../client/pythonEnvironments/discovery/locators/services/windowsStoreLocator';
import { EnvironmentInfoService } from '../../../../client/pythonEnvironments/info/environmentInfoService';
import { getEnvs } from '../../base/common';
import { TEST_LAYOUT_ROOT } from '../../common/commonTestConstants';

Expand Down Expand Up @@ -95,7 +94,7 @@ suite('Windows Store', () => {
major: 3,
minor: -1,
micro: -1,
release: { level: PythonReleaseLevel.Unknown, serial: -1 },
release: { level: PythonReleaseLevel.Final, serial: -1 },
sysVersion,
};
}
Expand Down Expand Up @@ -167,8 +166,7 @@ suite('Windows Store', () => {
return undefined;
});

const envService = new EnvironmentInfoService();
const locator = new WindowsStoreLocator(envService);
const locator = new WindowsStoreLocator();
const iterator = locator.iterEnvs();
const actualEnvs = (await getEnvs(iterator))
.sort((a, b) => a.executable.filename.localeCompare(b.executable.filename));
Expand All @@ -187,11 +185,10 @@ suite('Windows Store', () => {
location: '',
kind: PythonEnvKind.WindowsStore,
distro: { org: 'Microsoft' },
...createExpectedInterpreterInfo(python38path, python383data.sysVersion, python383data.sysPrefix, '3.8.3'),
...createExpectedInterpreterInfo(python38path),
};

const envService = new EnvironmentInfoService();
const locator = new WindowsStoreLocator(envService);
const locator = new WindowsStoreLocator();
const actual = await locator.resolveEnv(python38path);

assertEnvEqual(actual, expected);
Expand All @@ -205,7 +202,7 @@ suite('Windows Store', () => {
location: '',
kind: PythonEnvKind.WindowsStore,
distro: { org: 'Microsoft' },
...createExpectedInterpreterInfo(python38path, python383data.sysVersion, python383data.sysPrefix, '3.8.3'),
...createExpectedInterpreterInfo(python38path),
};

// Partially filled in env info object
Expand All @@ -225,12 +222,11 @@ suite('Windows Store', () => {
major: 3,
minor: -1,
micro: -1,
release: { level: PythonReleaseLevel.Unknown, serial: -1 },
release: { level: PythonReleaseLevel.Final, serial: -1 },
},
};

const envService = new EnvironmentInfoService();
const locator = new WindowsStoreLocator(envService);
const locator = new WindowsStoreLocator();
const actual = await locator.resolveEnv(input);

assertEnvEqual(actual, expected);
Expand All @@ -239,8 +235,16 @@ suite('Windows Store', () => {
// Use a non store root path
const python38path = path.join(testLocalAppData, 'python3.8.exe');

const envService = new EnvironmentInfoService();
const locator = new WindowsStoreLocator(envService);
const locator = new WindowsStoreLocator();
const actual = await locator.resolveEnv(python38path);

assert.deepStrictEqual(actual, undefined);
});
test('resolveEnv(string): forbidden path', async () => {
// Use a non store root path
const python38path = path.join(testLocalAppData, 'Program Files', 'WindowsApps', 'python3.8.exe');

const locator = new WindowsStoreLocator();
const actual = await locator.resolveEnv(python38path);

assert.deepStrictEqual(actual, undefined);
Expand Down

0 comments on commit b8c98a8

Please sign in to comment.