forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
166 lines (160 loc) · 5.42 KB
/
common.ts
File metadata and controls
166 lines (160 loc) · 5.42 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { createDeferred, flattenIterator, iterable, mapToIterator } from '../../../client/common/utils/async';
import { Architecture } from '../../../client/common/utils/platform';
import { EMPTY_VERSION, parseBasicVersionInfo } from '../../../client/common/utils/version';
import {
PythonEnvInfo,
PythonEnvKind,
PythonReleaseLevel,
PythonVersion
} from '../../../client/pythonEnvironments/base/info';
import { IPythonEnvsIterator, Locator, PythonLocatorQuery } from '../../../client/pythonEnvironments/base/locator';
import { PythonEnvsChangedEvent } from '../../../client/pythonEnvironments/base/watcher';
export function createEnv(
name: string,
versionStr: string,
kind?: PythonEnvKind,
executable?: string,
idStr?: string
): PythonEnvInfo {
if (kind === undefined) {
kind = PythonEnvKind.Unknown;
}
if (executable === undefined || executable === '') {
executable = 'python';
}
const id = idStr ? idStr : `${kind}-${name}`;
const version = parseVersion(versionStr);
return {
id,
kind,
version,
name,
location: '',
arch: Architecture.x86,
executable: {
filename: executable,
sysPrefix: '',
mtime: -1,
ctime: -1
},
distro: { org: '' }
};
}
function parseVersion(versionStr: string): PythonVersion {
const parsed = parseBasicVersionInfo<PythonVersion>(versionStr);
if (!parsed) {
if (versionStr === '') {
return EMPTY_VERSION as PythonVersion;
}
throw Error(`invalid version ${versionStr}`);
}
const { version, after } = parsed;
const match = after.match(/^(a|b|rc)(\d+)$/);
if (match) {
const [, levelStr, serialStr ] = match;
let level: PythonReleaseLevel;
if (levelStr === 'a') {
level = PythonReleaseLevel.Alpha;
} else if (levelStr === 'b') {
level = PythonReleaseLevel.Beta;
} else if (levelStr === 'rc') {
level = PythonReleaseLevel.Candidate;
} else {
throw Error('unreachable!');
}
version.release = {
level,
serial: parseInt(serialStr, 10)
};
}
return version;
}
export function createLocatedEnv(
location: string,
versionStr: string,
kind = PythonEnvKind.Unknown,
executable = 'python',
idStr?: string
): PythonEnvInfo {
if (!idStr) {
idStr = `${kind}-${location}`;
}
const env = createEnv('', versionStr, kind, executable, idStr);
env.location = location;
return env;
}
export class SimpleLocator extends Locator {
private deferred = createDeferred<void>();
constructor(
private envs: PythonEnvInfo[],
private callbacks?: {
resolve?: null | ((env: PythonEnvInfo) => Promise<PythonEnvInfo | undefined>);
before?: Promise<void>;
after?: Promise<void>;
beforeEach?(e: PythonEnvInfo): Promise<void>;
afterEach?(e: PythonEnvInfo): Promise<void>;
onQuery?(query: PythonLocatorQuery | undefined, envs: PythonEnvInfo[]): Promise<PythonEnvInfo[]>;
}
) {
super();
}
public get done(): Promise<void> {
return this.deferred.promise;
}
public fire(event: PythonEnvsChangedEvent) {
this.emitter.fire(event);
}
public iterEnvs(query?: PythonLocatorQuery): IPythonEnvsIterator {
const deferred = this.deferred;
const callbacks = this.callbacks;
let envs = this.envs;
async function* iterator() {
if (callbacks?.onQuery !== undefined) {
envs = await callbacks.onQuery(query, envs);
}
if (callbacks?.before !== undefined) {
await callbacks.before;
}
if (callbacks?.beforeEach !== undefined) {
// The results will likely come in a different order.
const mapped = mapToIterator(envs, async (env) => {
await callbacks.beforeEach!(env);
return env;
});
for await (const env of iterable(mapped)) {
yield env;
if (callbacks?.afterEach !== undefined) {
await callbacks.afterEach(env);
}
}
} else {
for (const env of envs) {
yield env;
if (callbacks?.afterEach !== undefined) {
await callbacks.afterEach(env);
}
}
}
if (callbacks?.after!== undefined) {
await callbacks.after;
}
deferred.resolve();
}
return iterator();
}
public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
const envInfo: PythonEnvInfo = typeof env === 'string' ? createEnv('', '', undefined, env) : env;
if (this.callbacks?.resolve === undefined) {
return envInfo;
} else if (this.callbacks?.resolve === null) {
return undefined;
} else {
return this.callbacks.resolve(envInfo);
}
}
}
export async function getEnvs(iterator: IPythonEnvsIterator): Promise<PythonEnvInfo[]> {
return flattenIterator(iterator);
}