forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
228 lines (213 loc) · 6.82 KB
/
index.ts
File metadata and controls
228 lines (213 loc) · 6.82 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as path from 'path';
import * as semver from 'semver';
import { IFileSystem } from '../../common/platform/types';
import { Architecture } from '../../common/utils/platform';
import { areSameVersion, PythonVersion } from './pythonVersion';
/**
* The supported Python environment types.
*/
export enum EnvironmentType {
Unknown = 'Unknown',
Conda = 'Conda',
VirtualEnv = 'VirtualEnv',
Pipenv = 'PipEnv',
Pyenv = 'Pyenv',
Venv = 'Venv',
WindowsStore = 'WindowsStore',
Poetry = 'Poetry',
VirtualEnvWrapper = 'VirtualEnvWrapper',
Global = 'Global',
System = 'System'
}
type ReleaseLevel = 'alpha' | 'beta' | 'candidate' | 'final' | 'unknown';
/**
* The components of a Python version.
*
* These match the elements of `sys.version_info`.
*/
export type PythonVersionInfo = [number, number, number, ReleaseLevel];
/**
* Details about a Python runtime.
*
* @prop path - the location of the executable file
* @prop version - the runtime version
* @prop sysVersion - the raw value of `sys.version`
* @prop architecture - of the host CPU (e.g. `x86`)
* @prop sysPrefix - the environment's install root (`sys.prefix`)
* @prop pipEnvWorkspaceFolder - the pipenv root, if applicable
*/
export type InterpreterInformation = {
path: string;
version?: PythonVersion;
sysVersion?: string;
architecture: Architecture;
sysPrefix: string;
pipEnvWorkspaceFolder?: string;
};
/**
* Details about a Python environment.
*
* @prop companyDisplayName - the user-facing name of the distro publisher
* @prop displayName - the user-facing name for the environment
* @prop type - the kind of Python environment
* @prop envName - the environment's name, if applicable (else `envPath` is set)
* @prop envPath - the environment's root dir, if applicable (else `envName`)
* @prop cachedEntry - whether or not the info came from a cache
*/
// Note that "cachedEntry" is specific to the caching machinery
// and doesn't really belong here.
export type PythonEnvironment = InterpreterInformation & {
companyDisplayName?: string;
displayName?: string;
envType: EnvironmentType;
envName?: string;
envPath?: string;
cachedEntry?: boolean;
};
/**
* Python environment containing only partial info. But it will contain the environment path.
*/
export type PartialPythonEnvironment = Partial<Omit<PythonEnvironment, 'path'>> & { path: string };
/**
* Standardize the given env info.
*
* @param environment = the env info to normalize
*/
export function normalizeEnvironment(environment: PartialPythonEnvironment): void {
environment.path = path.normalize(environment.path);
}
/**
* Convert the Python environment type to a user-facing name.
*/
export function getEnvironmentTypeName(environmentType: EnvironmentType) {
switch (environmentType) {
case EnvironmentType.Conda: {
return 'conda';
}
case EnvironmentType.Pipenv: {
return 'pipenv';
}
case EnvironmentType.Pyenv: {
return 'pyenv';
}
case EnvironmentType.Venv: {
return 'venv';
}
case EnvironmentType.VirtualEnv: {
return 'virtualenv';
}
default: {
return '';
}
}
}
/**
* Determine if the given infos correspond to the same env.
*
* @param environment1 - one of the two envs to compare
* @param environment2 - one of the two envs to compare
*/
export function areSameEnvironment(
environment1: PartialPythonEnvironment | undefined,
environment2: PartialPythonEnvironment | undefined,
fs: IFileSystem,
): boolean {
if (!environment1 || !environment2) {
return false;
}
if (fs.arePathsSame(environment1.path, environment2.path)) {
return true;
}
if (!areSameVersion(environment1.version, environment2.version)) {
return false;
}
// Could be Python 3.6 with path = python.exe, and Python 3.6
// and path = python3.exe, so we check the parent directory.
if (!inSameDirectory(environment1.path, environment2.path, fs)) {
return false;
}
return true;
}
/**
* Update one env info with another.
*
* @param environment - the info to update
* @param other - the info to copy in
*/
export function updateEnvironment(environment: PartialPythonEnvironment, other: PartialPythonEnvironment): void {
// Preserve type information.
// Possible we identified environment as unknown, but a later provider has identified env type.
if (environment.envType === EnvironmentType.Unknown && other.envType && other.envType !== EnvironmentType.Unknown) {
environment.envType = other.envType;
}
const props: (keyof PythonEnvironment)[] = [
'envName',
'envPath',
'path',
'sysPrefix',
'architecture',
'sysVersion',
'version',
'pipEnvWorkspaceFolder',
];
props.forEach((prop) => {
if (!environment[prop] && other[prop]) {
// tslint:disable-next-line: no-any
(environment as any)[prop] = other[prop];
}
});
}
/**
* Combine env info for matching environments.
*
* Environments are matched by path and version.
*
* @param environments - the env infos to merge
*/
export function mergeEnvironments(
environments: PartialPythonEnvironment[],
fs: IFileSystem,
): PartialPythonEnvironment[] {
return environments.reduce<PartialPythonEnvironment[]>((accumulator, current) => {
const existingItem = accumulator.find((item) => areSameEnvironment(current, item, fs));
if (!existingItem) {
const copied: PartialPythonEnvironment = { ...current };
normalizeEnvironment(copied);
accumulator.push(copied);
} else {
updateEnvironment(existingItem, current);
}
return accumulator;
}, []);
}
/**
* Determine if the given paths are in the same directory.
*
* @param path1 - one of the two paths to compare
* @param path2 - one of the two paths to compare
*/
export function inSameDirectory(path1: string | undefined, path2: string | undefined, fs: IFileSystem): boolean {
if (!path1 || !path2) {
return false;
}
const dir1 = path.dirname(path1);
const dir2 = path.dirname(path2);
return fs.arePathsSame(dir1, dir2);
}
/**
* Build a version-sorted list from the given one, with lowest first.
*/
export function sortInterpreters(interpreters: PythonEnvironment[]): PythonEnvironment[] {
if (interpreters.length === 0) {
return [];
}
if (interpreters.length === 1) {
return [interpreters[0]];
}
const sorted = interpreters.slice();
sorted.sort((a, b) => (a.version && b.version ? semver.compare(a.version.raw, b.version.raw) : 0));
return sorted;
}