|
| 1 | +/* eslint-disable class-methods-use-this */ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License. |
| 4 | + |
| 5 | +import { inject, injectable } from 'inversify'; |
| 6 | +import { ICondaService, IComponentAdapter } from '../../interpreter/contracts'; |
| 7 | +import { IServiceContainer } from '../../ioc/types'; |
| 8 | +import { ModuleInstallerType } from '../../pythonEnvironments/info'; |
| 9 | +import { ExecutionInfo, IConfigurationService, Product } from '../types'; |
| 10 | +import { isResource } from '../utils/misc'; |
| 11 | +import { ModuleInstaller, translateProductToModule } from './moduleInstaller'; |
| 12 | +import { InterpreterUri, ModuleInstallFlags } from './types'; |
| 13 | + |
| 14 | +/** |
| 15 | + * A Python module installer for a conda environment. |
| 16 | + */ |
| 17 | +@injectable() |
| 18 | +export class CondaInstaller extends ModuleInstaller { |
| 19 | + public _isCondaAvailable: boolean | undefined; |
| 20 | + |
| 21 | + // Unfortunately inversify requires the number of args in constructor to be explictly |
| 22 | + // specified as more than its base class. So we need the constructor. |
| 23 | + // eslint-disable-next-line @typescript-eslint/no-useless-constructor |
| 24 | + constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) { |
| 25 | + super(serviceContainer); |
| 26 | + } |
| 27 | + |
| 28 | + public get name(): string { |
| 29 | + return 'Conda'; |
| 30 | + } |
| 31 | + |
| 32 | + public get displayName(): string { |
| 33 | + return 'Conda'; |
| 34 | + } |
| 35 | + |
| 36 | + public get type(): ModuleInstallerType { |
| 37 | + return ModuleInstallerType.Conda; |
| 38 | + } |
| 39 | + |
| 40 | + public get priority(): number { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Checks whether we can use Conda as module installer for a given resource. |
| 46 | + * We need to perform two checks: |
| 47 | + * 1. Ensure we have conda. |
| 48 | + * 2. Check if the current environment is a conda environment. |
| 49 | + * @param {InterpreterUri} [resource=] Resource used to identify the workspace. |
| 50 | + * @returns {Promise<boolean>} Whether conda is supported as a module installer or not. |
| 51 | + */ |
| 52 | + public async isSupported(resource?: InterpreterUri): Promise<boolean> { |
| 53 | + if (this._isCondaAvailable === false) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + const condaLocator = this.serviceContainer.get<ICondaService>(ICondaService); |
| 57 | + this._isCondaAvailable = await condaLocator.isCondaAvailable(); |
| 58 | + if (!this._isCondaAvailable) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + // Now we need to check if the current environment is a conda environment or not. |
| 62 | + return this.isCurrentEnvironmentACondaEnvironment(resource); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Return the commandline args needed to install the module. |
| 67 | + */ |
| 68 | + protected async getExecutionInfo( |
| 69 | + moduleName: string, |
| 70 | + resource?: InterpreterUri, |
| 71 | + flags: ModuleInstallFlags = 0, |
| 72 | + ): Promise<ExecutionInfo> { |
| 73 | + const condaService = this.serviceContainer.get<ICondaService>(ICondaService); |
| 74 | + const condaFile = await condaService.getCondaFile(); |
| 75 | + |
| 76 | + const pythonPath = isResource(resource) |
| 77 | + ? this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(resource).pythonPath |
| 78 | + : resource.path; |
| 79 | + const condaLocatorService = this.serviceContainer.get<IComponentAdapter>(IComponentAdapter); |
| 80 | + const info = await condaLocatorService.getCondaEnvironment(pythonPath); |
| 81 | + const args = [flags & ModuleInstallFlags.upgrade ? 'update' : 'install']; |
| 82 | + |
| 83 | + // Found that using conda-forge is best at packages like tensorboard & ipykernel which seem to get updated first on conda-forge |
| 84 | + // https://github.com/microsoft/vscode-jupyter/issues/7787 & https://github.com/microsoft/vscode-python/issues/17628 |
| 85 | + // Do this just for the datascience packages. |
| 86 | + if ( |
| 87 | + [ |
| 88 | + Product.tensorboard, |
| 89 | + Product.ipykernel, |
| 90 | + Product.pandas, |
| 91 | + Product.nbconvert, |
| 92 | + Product.jupyter, |
| 93 | + Product.notebook, |
| 94 | + ] |
| 95 | + .map(translateProductToModule) |
| 96 | + .includes(moduleName) |
| 97 | + ) { |
| 98 | + args.push('-c', 'conda-forge'); |
| 99 | + } |
| 100 | + if (info && info.name) { |
| 101 | + // If we have the name of the conda environment, then use that. |
| 102 | + args.push('--name'); |
| 103 | + args.push(info.name.toCommandArgument()); |
| 104 | + } else if (info && info.path) { |
| 105 | + // Else provide the full path to the environment path. |
| 106 | + args.push('--prefix'); |
| 107 | + args.push(info.path.fileToCommandArgument()); |
| 108 | + } |
| 109 | + if (flags & ModuleInstallFlags.updateDependencies) { |
| 110 | + args.push('--update-deps'); |
| 111 | + } |
| 112 | + if (flags & ModuleInstallFlags.reInstall) { |
| 113 | + args.push('--force-reinstall'); |
| 114 | + } |
| 115 | + args.push(moduleName); |
| 116 | + args.push('-y'); |
| 117 | + return { |
| 118 | + args, |
| 119 | + execPath: condaFile, |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Is the provided interprter a conda environment |
| 125 | + */ |
| 126 | + private async isCurrentEnvironmentACondaEnvironment(resource?: InterpreterUri): Promise<boolean> { |
| 127 | + const condaService = this.serviceContainer.get<IComponentAdapter>(IComponentAdapter); |
| 128 | + const pythonPath = isResource(resource) |
| 129 | + ? this.serviceContainer.get<IConfigurationService>(IConfigurationService).getSettings(resource).pythonPath |
| 130 | + : resource.path; |
| 131 | + return condaService.isCondaEnvironment(pythonPath); |
| 132 | + } |
| 133 | +} |
0 commit comments