forked from DonJayamanne/vscode-python-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindowsUtils.ts
More file actions
199 lines (171 loc) · 6.08 KB
/
Copy pathwindowsUtils.ts
File metadata and controls
199 lines (171 loc) · 6.08 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// 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,
): Promise<IRegistryInterpreterData | undefined> {
const result: IRegistryInterpreterData = {
interpreterPath: '',
distroOrgName,
};
const values: IRegistryValue[] = await readRegistryValues({ arch, hive, key });
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 });
const subKey = subKeys.map((s) => s.key).find((s) => s.endsWith('InstallPath'));
if (subKey) {
const subKeyValues: IRegistryValue[] = await readRegistryValues({ arch, hive, key: subKey });
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,
): Promise<IRegistryInterpreterData[]> {
const subKeys = await readRegistryKeys({ arch, hive, key });
const distroOrgName = key.substr(key.lastIndexOf('\\') + 1);
const allData = await Promise.all(subKeys.map((subKey) => getInterpreterDataFromKey(subKey, distroOrgName)));
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(): 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 })).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));
}
}
}
registryInterpretersCache = uniqBy(registryData, (r: IRegistryInterpreterData) => r.interpreterPath);
return registryInterpretersCache;
}
class CacheMap {
private static instance: CacheMap;
private cache: Map<string, string>;
// 私有构造函数,保证不能直接实例化
private constructor() {
this.cache = new Map();
}
// 获取单例实例
public static getInstance(): CacheMap {
if (!CacheMap.instance) {
CacheMap.instance = new CacheMap();
}
return CacheMap.instance;
}
// 设置值
public set(key: string, value: string): void {
this.cache.set(key, value);
}
// 获取值
public get(key: string): string | undefined {
return this.cache.get(key);
}
// 检查缓存中是否有该 key
public has(key: string): boolean {
return this.cache.has(key);
}
// 删除指定 key
public delete(key: string): boolean {
return this.cache.delete(key);
}
// 清空缓存
public clear(): void {
this.cache.clear();
}
}
export default CacheMap;