| 
 | 1 | +// Copyright (c) Microsoft Corporation. All rights reserved.  | 
 | 2 | +// Licensed under the MIT License.  | 
 | 3 | + | 
 | 4 | +import * as fsapi from 'fs-extra';  | 
 | 5 | +import { toUpper, uniq } from 'lodash';  | 
 | 6 | +import * as path from 'path';  | 
 | 7 | +import { traceVerbose } from '../../../../common/logger';  | 
 | 8 | +import { chain, iterable } from '../../../../common/utils/async';  | 
 | 9 | +import {  | 
 | 10 | + getEnvironmentVariable, getOSType, getUserHomeDir, OSType,  | 
 | 11 | +} from '../../../../common/utils/platform';  | 
 | 12 | +import {  | 
 | 13 | + PythonEnvInfo, PythonEnvKind, UNKNOWN_PYTHON_VERSION,  | 
 | 14 | +} from '../../../base/info';  | 
 | 15 | +import { buildEnvInfo } from '../../../base/info/env';  | 
 | 16 | +import { ILocator, IPythonEnvsIterator } from '../../../base/locator';  | 
 | 17 | +import { PythonEnvsWatcher } from '../../../base/watcher';  | 
 | 18 | +import { findInterpretersInDir } from '../../../common/commonUtils';  | 
 | 19 | +import { getFileInfo, pathExists } from '../../../common/externalDependencies';  | 
 | 20 | +import { isVenvEnvironment, isVirtualenvEnvironment, isVirtualenvwrapperEnvironment } from './virtualEnvironmentIdentifier';  | 
 | 21 | + | 
 | 22 | +const DEFAULT_SEARCH_DEPTH = 2;  | 
 | 23 | + | 
 | 24 | +/**  | 
 | 25 | + * Gets all default virtual environment locations. This uses WORKON_HOME,  | 
 | 26 | + * and user home directory to find some known locations where global virtual  | 
 | 27 | + * environments are often created.  | 
 | 28 | + */  | 
 | 29 | +async function getGlobalVirtualEnvDirs(): Promise<string[]> {  | 
 | 30 | + const venvDirs:string[] = [];  | 
 | 31 | + | 
 | 32 | + const workOnHome = getEnvironmentVariable('WORKON_HOME');  | 
 | 33 | + if (workOnHome && await pathExists(workOnHome)) {  | 
 | 34 | + venvDirs.push(workOnHome);  | 
 | 35 | + }  | 
 | 36 | + | 
 | 37 | + const homeDir = getUserHomeDir();  | 
 | 38 | + if (homeDir && await pathExists(homeDir)) {  | 
 | 39 | + const os = getOSType();  | 
 | 40 | + let subDirs = ['Envs', 'envs', '.direnv', '.venvs', '.virtualenvs'];  | 
 | 41 | + if (os === OSType.Windows) {  | 
 | 42 | + subDirs = uniq(subDirs.map(toUpper));  | 
 | 43 | + }  | 
 | 44 | + | 
 | 45 | + (await fsapi.readdir(homeDir))  | 
 | 46 | + .filter((d) => subDirs.includes(os === OSType.Windows ? d.toUpperCase() : d))  | 
 | 47 | + .forEach((d) => venvDirs.push(path.join(homeDir, d)));  | 
 | 48 | + }  | 
 | 49 | + | 
 | 50 | + return venvDirs;  | 
 | 51 | +}  | 
 | 52 | + | 
 | 53 | +/**  | 
 | 54 | + * Gets the virtual environment kind for a given interpreter path.  | 
 | 55 | + * This only checks for environments created using venv, virtualenv,  | 
 | 56 | + * and virtualenvwrapper based environments.  | 
 | 57 | + * @param interpreterPath: Absolute path to the interpreter paths.  | 
 | 58 | + */  | 
 | 59 | +async function getVirtualEnvKind(interpreterPath:string): Promise<PythonEnvKind> {  | 
 | 60 | + if (await isVenvEnvironment(interpreterPath)) {  | 
 | 61 | + return PythonEnvKind.Venv;  | 
 | 62 | + }  | 
 | 63 | + | 
 | 64 | + if (await isVirtualenvwrapperEnvironment(interpreterPath)) {  | 
 | 65 | + return PythonEnvKind.VirtualEnvWrapper;  | 
 | 66 | + }  | 
 | 67 | + | 
 | 68 | + if (await isVirtualenvEnvironment(interpreterPath)) {  | 
 | 69 | + return PythonEnvKind.VirtualEnv;  | 
 | 70 | + }  | 
 | 71 | + | 
 | 72 | + return PythonEnvKind.Unknown;  | 
 | 73 | +}  | 
 | 74 | + | 
 | 75 | +/**  | 
 | 76 | + * Finds and resolves virtual environments created in known global locations.  | 
 | 77 | + */  | 
 | 78 | +export class GlobalVirtualEnvironmentLocator extends PythonEnvsWatcher implements ILocator {  | 
 | 79 | + private virtualEnvKinds = [  | 
 | 80 | + PythonEnvKind.Venv,  | 
 | 81 | + PythonEnvKind.VirtualEnv,  | 
 | 82 | + PythonEnvKind.VirtualEnvWrapper,  | 
 | 83 | + ];  | 
 | 84 | + | 
 | 85 | + public constructor(private readonly searchDepth?:number) {  | 
 | 86 | + super();  | 
 | 87 | + }  | 
 | 88 | + | 
 | 89 | + public iterEnvs(): IPythonEnvsIterator {  | 
 | 90 | + // Number of levels of sub-directories to recurse when looking for  | 
 | 91 | + // interpreters  | 
 | 92 | + const searchDepth = this.searchDepth ?? DEFAULT_SEARCH_DEPTH;  | 
 | 93 | + | 
 | 94 | + async function* iterator(virtualEnvKinds:PythonEnvKind[]) {  | 
 | 95 | + const envRootDirs = await getGlobalVirtualEnvDirs();  | 
 | 96 | + const envGenerators = envRootDirs.map((envRootDir) => {  | 
 | 97 | + async function* generator() {  | 
 | 98 | + traceVerbose(`Searching for global virtual envs in: ${envRootDir}`);  | 
 | 99 | + | 
 | 100 | + const envGenerator = findInterpretersInDir(envRootDir, searchDepth);  | 
 | 101 | + | 
 | 102 | + for await (const env of envGenerator) {  | 
 | 103 | + // We only care about python.exe (on windows) and python (on linux/mac)  | 
 | 104 | + // Other version like python3.exe or python3.8 are often symlinks to  | 
 | 105 | + // python.exe or python in the same directory in the case of virtual  | 
 | 106 | + // environments.  | 
 | 107 | + const name = path.basename(env).toLowerCase();  | 
 | 108 | + if (name === 'python.exe' || name === 'python') {  | 
 | 109 | + // We should extract the kind here to avoid doing is*Environment()  | 
 | 110 | + // check multiple times. Those checks are file system heavy and  | 
 | 111 | + // we can use the kind to determine this anyway.  | 
 | 112 | + const kind = await getVirtualEnvKind(env);  | 
 | 113 | + | 
 | 114 | + const timeData = await getFileInfo(env);  | 
 | 115 | + if (virtualEnvKinds.includes(kind)) {  | 
 | 116 | + traceVerbose(`Global Virtual Environment: [added] ${env}`);  | 
 | 117 | + const envInfo = buildEnvInfo({  | 
 | 118 | + kind,  | 
 | 119 | + executable: env,  | 
 | 120 | + version: UNKNOWN_PYTHON_VERSION,  | 
 | 121 | + });  | 
 | 122 | + envInfo.executable.ctime = timeData.ctime;  | 
 | 123 | + envInfo.executable.mtime = timeData.mtime;  | 
 | 124 | + yield envInfo;  | 
 | 125 | + } else {  | 
 | 126 | + traceVerbose(`Global Virtual Environment: [skipped] ${env}`);  | 
 | 127 | + }  | 
 | 128 | + } else {  | 
 | 129 | + traceVerbose(`Global Virtual Environment: [skipped] ${env}`);  | 
 | 130 | + }  | 
 | 131 | + }  | 
 | 132 | + }  | 
 | 133 | + return generator();  | 
 | 134 | + });  | 
 | 135 | + | 
 | 136 | + yield* iterable(chain(envGenerators));  | 
 | 137 | + }  | 
 | 138 | + | 
 | 139 | + return iterator(this.virtualEnvKinds);  | 
 | 140 | + }  | 
 | 141 | + | 
 | 142 | + public async resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {  | 
 | 143 | + const executablePath = typeof env === 'string' ? env : env.executable.filename;  | 
 | 144 | + if (await pathExists(executablePath)) {  | 
 | 145 | + // We should extract the kind here to avoid doing is*Environment()  | 
 | 146 | + // check multiple times. Those checks are file system heavy and  | 
 | 147 | + // we can use the kind to determine this anyway.  | 
 | 148 | + const kind = await getVirtualEnvKind(executablePath);  | 
 | 149 | + if (this.virtualEnvKinds.includes(kind)) {  | 
 | 150 | + const timeData = await getFileInfo(executablePath);  | 
 | 151 | + const envInfo = buildEnvInfo({  | 
 | 152 | + kind,  | 
 | 153 | + version: UNKNOWN_PYTHON_VERSION,  | 
 | 154 | + executable: executablePath,  | 
 | 155 | + });  | 
 | 156 | + envInfo.executable.ctime = timeData.ctime;  | 
 | 157 | + envInfo.executable.mtime = timeData.mtime;  | 
 | 158 | + return envInfo;  | 
 | 159 | + }  | 
 | 160 | + }  | 
 | 161 | + return undefined;  | 
 | 162 | + }  | 
 | 163 | +}  | 
0 commit comments