forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindowsUtils.ts
More file actions
159 lines (141 loc) · 5.38 KB
/
Copy pathwindowsUtils.ts
File metadata and controls
159 lines (141 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { uniqBy } from 'lodash';
import * as path from 'path';
import { isTestExecution } from '../../common/constants';
import { traceError, traceVerbose } from '../../logging';
import {
HKCU,
HKLM,
IRegistryKey,
IRegistryValue,
readRegistryKeys,
readRegistryValues,
REG_SZ,
} from './windowsRegistry';
/* eslint-disable global-require */
/**
* Determine if the given filename looks like the simplest Python executable.
*/
export function matchBasicPythonBinFilename(filename: string): boolean {
return path.basename(filename).toLowerCase() === 'python.exe';
}
/**
* Checks if a given path ends with python*.exe
* @param {string} interpreterPath : Path to python interpreter.
* @returns {boolean} : Returns true if the path matches pattern for windows python executable.
*/
export function matchPythonBinFilename(filename: string): boolean {
/**
* This Reg-ex matches following file names:
* python.exe
* python3.exe
* python38.exe
* python3.8.exe
*/
const windowsPythonExes = /^python(\d+(.\d+)?)?\.exe$/;
return windowsPythonExes.test(path.basename(filename));
}
export interface IRegistryInterpreterData {
interpreterPath: string;
versionStr?: string;
sysVersionStr?: string;
bitnessStr?: string;
companyDisplayName?: string;
distroOrgName?: string;
}
async function getInterpreterDataFromKey(
{ arch, hive, key }: IRegistryKey,
distroOrgName: string,
useWorkerThreads: boolean,
): Promise<IRegistryInterpreterData | undefined> {
const result: IRegistryInterpreterData = {
interpreterPath: '',
distroOrgName,
};
const values: IRegistryValue[] = await readRegistryValues({ arch, hive, key }, useWorkerThreads);
for (const value of values) {
switch (value.name) {
case 'SysArchitecture':
result.bitnessStr = value.value;
break;
case 'SysVersion':
result.sysVersionStr = value.value;
break;
case 'Version':
result.versionStr = value.value;
break;
case 'DisplayName':
result.companyDisplayName = value.value;
break;
default:
break;
}
}
const subKeys: IRegistryKey[] = await readRegistryKeys({ arch, hive, key }, useWorkerThreads);
const subKey = subKeys.map((s) => s.key).find((s) => s.endsWith('InstallPath'));
if (subKey) {
const subKeyValues: IRegistryValue[] = await readRegistryValues({ arch, hive, key: subKey }, useWorkerThreads);
const value = subKeyValues.find((v) => v.name === 'ExecutablePath');
if (value) {
result.interpreterPath = value.value;
if (value.type !== REG_SZ) {
traceVerbose(`Registry interpreter path type [${value.type}]: ${value.value}`);
}
}
}
if (result.interpreterPath.length > 0) {
return result;
}
return undefined;
}
export async function getInterpreterDataFromRegistry(
arch: string,
hive: string,
key: string,
useWorkerThreads: boolean,
): Promise<IRegistryInterpreterData[]> {
const subKeys = await readRegistryKeys({ arch, hive, key }, useWorkerThreads);
const distroOrgName = key.substr(key.lastIndexOf('\\') + 1);
const allData = await Promise.all(
subKeys.map((subKey) => getInterpreterDataFromKey(subKey, distroOrgName, useWorkerThreads)),
);
return (allData.filter((data) => data !== undefined) || []) as IRegistryInterpreterData[];
}
let registryInterpretersCache: IRegistryInterpreterData[] | undefined;
/**
* Returns windows registry interpreters from memory, returns undefined if memory is empty.
* getRegistryInterpreters() must be called prior to this to populate memory.
*/
export function getRegistryInterpretersSync(): IRegistryInterpreterData[] | undefined {
return !isTestExecution() ? registryInterpretersCache : undefined;
}
let registryInterpretersPromise: Promise<IRegistryInterpreterData[]> | undefined;
export async function getRegistryInterpreters(): Promise<IRegistryInterpreterData[]> {
if (!isTestExecution() && registryInterpretersPromise !== undefined) {
return registryInterpretersPromise;
}
registryInterpretersPromise = getRegistryInterpretersImpl();
return registryInterpretersPromise;
}
async function getRegistryInterpretersImpl(useWorkerThreads = false): Promise<IRegistryInterpreterData[]> {
let registryData: IRegistryInterpreterData[] = [];
for (const arch of ['x64', 'x86']) {
for (const hive of [HKLM, HKCU]) {
const root = '\\SOFTWARE\\Python';
let keys: string[] = [];
try {
keys = (await readRegistryKeys({ arch, hive, key: root }, useWorkerThreads)).map((k) => k.key);
} catch (ex) {
traceError(`Failed to access Registry: ${arch}\\${hive}\\${root}`, ex);
}
for (const key of keys) {
registryData = registryData.concat(
await getInterpreterDataFromRegistry(arch, hive, key, useWorkerThreads),
);
}
}
}
registryInterpretersCache = uniqBy(registryData, (r: IRegistryInterpreterData) => r.interpreterPath);
return registryInterpretersCache;
}