|
3 | 3 |
|
4 | 4 | import * as fsapi from 'fs-extra';
|
5 | 5 | import * as path from 'path';
|
6 |
| -import { traceWarning } from '../../../../common/logger'; |
7 |
| -import { getEnvironmentVariable } from '../../../../common/utils/platform'; |
8 |
| -import { isWindowsPythonExe } from '../../../common/windowsUtils'; |
9 |
| - |
10 |
| -/** |
11 |
| - * Gets path to the Windows Apps directory. |
12 |
| - * @returns {string} : Returns path to the Windows Apps directory under |
13 |
| - * `%LOCALAPPDATA%/Microsoft/WindowsApps`. |
14 |
| - */ |
15 |
| -export function getWindowsStoreAppsRoot(): string { |
16 |
| - const localAppData = getEnvironmentVariable('LOCALAPPDATA') || ''; |
17 |
| - return path.join(localAppData, 'Microsoft', 'WindowsApps'); |
18 |
| -} |
19 |
| - |
20 |
| -/** |
21 |
| - * Checks if a given path is under the forbidden windows store directory. |
22 |
| - * @param {string} interpreterPath : Absolute path to the python interpreter. |
23 |
| - * @returns {boolean} : Returns true if `interpreterPath` is under |
24 |
| - * `%ProgramFiles%/WindowsApps`. |
25 |
| - */ |
26 |
| -export function isForbiddenStorePath(interpreterPath:string):boolean { |
27 |
| - const programFilesStorePath = path |
28 |
| - .join(getEnvironmentVariable('ProgramFiles') || 'Program Files', 'WindowsApps') |
29 |
| - .normalize() |
30 |
| - .toUpperCase(); |
31 |
| - return path.normalize(interpreterPath).toUpperCase().includes(programFilesStorePath); |
32 |
| -} |
33 |
| - |
34 |
| -/** |
35 |
| - * Checks if the given interpreter belongs to Windows Store Python environment. |
36 |
| - * @param interpreterPath: Absolute path to any python interpreter. |
37 |
| - * |
38 |
| - * Remarks: |
39 |
| - * 1. Checking if the path includes `Microsoft\WindowsApps`, `Program Files\WindowsApps`, is |
40 |
| - * NOT enough. In WSL, `/mnt/c/users/user/AppData/Local/Microsoft/WindowsApps` is available as a search |
41 |
| - * path. It is possible to get a false positive for that path. So the comparison should check if the |
42 |
| - * absolute path to 'WindowsApps' directory is present in the given interpreter path. The WSL path to |
43 |
| - * 'WindowsApps' is not a valid path to access, Windows Store Python. |
44 |
| - * |
45 |
| - * 2. 'startsWith' comparison may not be right, user can provide '\\?\C:\users\' style long paths in windows. |
46 |
| - * |
47 |
| - * 3. A limitation of the checks here is that they don't handle 8.3 style windows paths. |
48 |
| - * For example, |
49 |
| - * `C:\Users\USER\AppData\Local\MICROS~1\WINDOW~1\PYTHON~2.EXE` |
50 |
| - * is the shortened form of |
51 |
| - * `C:\Users\USER\AppData\Local\Microsoft\WindowsApps\python3.7.exe` |
52 |
| - * |
53 |
| - * The correct way to compare these would be to always convert given paths to long path (or to short path). |
54 |
| - * For either approach to work correctly you need actual file to exist, and accessible from the user's |
55 |
| - * account. |
56 |
| - * |
57 |
| - * To convert to short path without using N-API in node would be to use this command. This is very expensive: |
58 |
| - * `> cmd /c for %A in ("C:\Users\USER\AppData\Local\Microsoft\WindowsApps\python3.7.exe") do @echo %~sA` |
59 |
| - * The above command will print out this: |
60 |
| - * `C:\Users\USER\AppData\Local\MICROS~1\WINDOW~1\PYTHON~2.EXE` |
61 |
| - * |
62 |
| - * If we go down the N-API route, use node-ffi and either call GetShortPathNameW or GetLongPathNameW from, |
63 |
| - * Kernel32 to convert between the two path variants. |
64 |
| - * |
65 |
| - */ |
66 |
| -export async function isWindowsStoreEnvironment(interpreterPath: string): Promise<boolean> { |
67 |
| - const pythonPathToCompare = path.normalize(interpreterPath).toUpperCase(); |
68 |
| - const localAppDataStorePath = path |
69 |
| - .normalize(getWindowsStoreAppsRoot()) |
70 |
| - .toUpperCase(); |
71 |
| - if (pythonPathToCompare.includes(localAppDataStorePath)) { |
72 |
| - return true; |
73 |
| - } |
74 |
| - |
75 |
| - // Program Files store path is a forbidden path. Only admins and system has access this path. |
76 |
| - // We should never have to look at this path or even execute python from this path. |
77 |
| - if (isForbiddenStorePath(pythonPathToCompare)) { |
78 |
| - traceWarning('isWindowsStoreEnvironment called with Program Files store path.'); |
79 |
| - return true; |
80 |
| - } |
81 |
| - return false; |
82 |
| -} |
| 6 | +import { Event, EventEmitter } from 'vscode'; |
| 7 | +import { Architecture } from '../../../../common/utils/platform'; |
| 8 | +import { |
| 9 | + PythonEnvInfo, PythonEnvKind, PythonReleaseLevel, PythonVersion, |
| 10 | +} from '../../../base/info'; |
| 11 | +import { parseVersion } from '../../../base/info/pythonVersion'; |
| 12 | +import { ILocator, IPythonEnvsIterator } from '../../../base/locator'; |
| 13 | +import { PythonEnvsChangedEvent } from '../../../base/watcher'; |
| 14 | +import { getFileInfo } from '../../../common/externalDependencies'; |
| 15 | +import { getWindowsStoreAppsRoot, isWindowsPythonExe, isWindowsStoreEnvironment } from '../../../common/windowsUtils'; |
| 16 | +import { IEnvironmentInfoService } from '../../../info/environmentInfoService'; |
83 | 17 |
|
84 | 18 | /**
|
85 | 19 | * Gets paths to the Python executable under Windows Store apps.
|
@@ -107,5 +41,77 @@ export async function getWindowsStorePythonExes(): Promise<string[]> {
|
107 | 41 | .filter(isWindowsPythonExe);
|
108 | 42 | }
|
109 | 43 |
|
110 |
| -// tslint:disable-next-line: no-suspicious-comment |
111 |
| -// TODO: The above APIs will be consumed by the Windows Store locator class when we have it. |
| 44 | +export class WindowsStoreLocator implements ILocator { |
| 45 | + private readonly kind:PythonEnvKind = PythonEnvKind.WindowsStore; |
| 46 | + |
| 47 | + private readonly eventEmitter = new EventEmitter<PythonEnvsChangedEvent>(); |
| 48 | + |
| 49 | + public constructor(private readonly envService:IEnvironmentInfoService) { } |
| 50 | + |
| 51 | + public iterEnvs(): IPythonEnvsIterator { |
| 52 | + const buildEnvInfo = (exe:string) => this.buildEnvInfo(exe); |
| 53 | + const iterator = async function* () { |
| 54 | + const exes = await getWindowsStorePythonExes(); |
| 55 | + yield* exes.map(buildEnvInfo); |
| 56 | + }; |
| 57 | + return iterator(); |
| 58 | + } |
| 59 | + |
| 60 | + public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> { |
| 61 | + const executablePath = typeof env === 'string' ? env : env.executable.filename; |
| 62 | + if (isWindowsStoreEnvironment(executablePath)) { |
| 63 | + const interpreterInfo = await this.envService.getEnvironmentInfo(executablePath); |
| 64 | + if (interpreterInfo) { |
| 65 | + const data = await getFileInfo(executablePath); |
| 66 | + interpreterInfo.executable = { |
| 67 | + ...interpreterInfo.executable, |
| 68 | + ...data, |
| 69 | + }; |
| 70 | + return Promise.resolve({ |
| 71 | + id: '', |
| 72 | + name: '', |
| 73 | + location: '', |
| 74 | + kind: this.kind, |
| 75 | + executable: interpreterInfo.executable, |
| 76 | + version: interpreterInfo.version, |
| 77 | + arch: interpreterInfo.arch, |
| 78 | + distro: { org: 'Microsoft' }, |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + |
| 85 | + public get onChanged(): Event<PythonEnvsChangedEvent> { |
| 86 | + return this.eventEmitter.event; |
| 87 | + } |
| 88 | + |
| 89 | + private async buildEnvInfo(exe:string): Promise<PythonEnvInfo> { |
| 90 | + let version:PythonVersion; |
| 91 | + try { |
| 92 | + version = parseVersion(path.basename(exe)); |
| 93 | + } catch (e) { |
| 94 | + version = { |
| 95 | + major: 3, |
| 96 | + minor: -1, |
| 97 | + micro: -1, |
| 98 | + release: { level: PythonReleaseLevel.Unknown, serial: -1 }, |
| 99 | + sysVersion: undefined, |
| 100 | + }; |
| 101 | + } |
| 102 | + return { |
| 103 | + id: '', |
| 104 | + name: '', |
| 105 | + location: '', |
| 106 | + kind: this.kind, |
| 107 | + executable: { |
| 108 | + filename: exe, |
| 109 | + sysPrefix: '', |
| 110 | + ...(await getFileInfo(exe)), |
| 111 | + }, |
| 112 | + version, |
| 113 | + arch: Architecture.x64, |
| 114 | + distro: { org: 'Microsoft' }, |
| 115 | + }; |
| 116 | + } |
| 117 | +} |
0 commit comments