(interpreter: PythonEnvironment | Environment | { id: string })
| 13 | import { WrappedError } from '../errors/types'; |
| 14 | |
| 15 | export function getPythonEnvDisplayName(interpreter: PythonEnvironment | Environment | { id: string }) { |
| 16 | const env = getCachedEnvironment(interpreter); |
| 17 | if (isCI) { |
| 18 | logger.ci(`Python Env Info for ${JSON.stringify(interpreter)} is ${JSON.stringify(env)}`); |
| 19 | } |
| 20 | if (env) { |
| 21 | const versionParts: string[] = []; |
| 22 | if (typeof env.version?.major === 'number') { |
| 23 | versionParts.push(env.version.major.toString()); |
| 24 | if (typeof env.version.minor === 'number') { |
| 25 | versionParts.push(env.version.minor.toString()); |
| 26 | if (typeof env.version.micro === 'number') { |
| 27 | versionParts.push(env.version.micro.toString()); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | const version = versionParts.length ? versionParts.join('.') : ''; |
| 32 | const envName = env.environment?.name || (env.environment ? basename(env.environment?.folderUri) : ''); |
| 33 | const nameWithVersion = version ? `Python ${version}` : 'Python'; |
| 34 | if (isCondaEnvironmentWithoutPython(interpreter) && envName) { |
| 35 | return envName; |
| 36 | } |
| 37 | if (envName) { |
| 38 | return `${envName} (${nameWithVersion})`; |
| 39 | } |
| 40 | return nameWithVersion; |
| 41 | } |
| 42 | if (Object.keys(interpreter).length === 1 && interpreter.id) { |
| 43 | return interpreter.id; |
| 44 | } |
| 45 | |
| 46 | const pythonVersion = getTelemetrySafeVersion(getCachedVersion(interpreter) || '').trim(); |
| 47 | // If this is a conda environment without Python, then don't display `Python` in it. |
| 48 | const isCondaEnvWithoutPython = isCondaEnvironmentWithoutPython(interpreter); |
| 49 | const nameWithVersion = pythonVersion ? `Python ${pythonVersion}` : 'Python'; |
| 50 | const envName = getPythonEnvironmentName(interpreter as PythonEnvironment); |
| 51 | if (isCondaEnvWithoutPython && envName) { |
| 52 | return envName; |
| 53 | } |
| 54 | const details: string[] = []; |
| 55 | if (envName) { |
| 56 | details.push(envName); |
| 57 | } |
| 58 | const envType = getEnvironmentType(interpreter); |
| 59 | if (envType && envType !== EnvironmentType.Unknown) { |
| 60 | details.push(envType); |
| 61 | } |
| 62 | return [nameWithVersion, details.length ? `(${details.join(': ')})` : ''].join(' ').trim(); |
| 63 | } |
| 64 | |
| 65 | export function getPythonEnvironmentName(pythonEnv: { id: string }) { |
| 66 | // Sometimes Python extension doesn't detect conda environments correctly (e.g. conda env create without a name). |
no test coverage detected