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
111 lines (102 loc) · 3.15 KB
/
index.ts
File metadata and controls
111 lines (102 loc) · 3.15 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as semver from 'semver';
import { Architecture } from '../../common/utils/platform';
import { PythonVersion } from './pythonVersion';
/**
* The supported Python environment types.
*/
export enum InterpreterType {
Unknown = 'Unknown',
Conda = 'Conda',
VirtualEnv = 'VirtualEnv',
Pipenv = 'PipEnv',
Pyenv = 'Pyenv',
Venv = 'Venv',
WindowsStore = 'WindowsStore'
}
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 PythonInterpreter = InterpreterInformation & {
companyDisplayName?: string;
displayName?: string;
type: InterpreterType;
envName?: string;
envPath?: string;
cachedEntry?: boolean;
};
/**
* Convert the Python environment type to a user-facing name.
*/
export function getInterpreterTypeName(interpreterType: InterpreterType) {
switch (interpreterType) {
case InterpreterType.Conda: {
return 'conda';
}
case InterpreterType.Pipenv: {
return 'pipenv';
}
case InterpreterType.Pyenv: {
return 'pyenv';
}
case InterpreterType.Venv: {
return 'venv';
}
case InterpreterType.VirtualEnv: {
return 'virtualenv';
}
default: {
return '';
}
}
}
/**
* Build a version-sorted list from the given one, with lowest first.
*/
export function sortInterpreters(interpreters: PythonInterpreter[]): PythonInterpreter[] {
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;
}