|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import { CancellationToken } from 'vscode'; |
| 8 | +import { IApplicationShell } from '../../../common/application/types'; |
| 9 | +import { createPromiseFromCancellation, wrapCancellationTokens } from '../../../common/cancellation'; |
| 10 | +import { ProductNames } from '../../../common/installer/productNames'; |
| 11 | +import { IInstaller, InstallerResponse, Product } from '../../../common/types'; |
| 12 | +import { Common, DataScience } from '../../../common/utils/localize'; |
| 13 | +import { PythonInterpreter } from '../../../interpreter/contracts'; |
| 14 | +import { IKernelDependencyService, KernelInterpreterDependencyResponse } from '../../types'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Responsible for managing dependencies of a Python interpreter required to run as a Jupyter Kernel. |
| 18 | + * If required modules aren't installed, will prompt user to install them. |
| 19 | + */ |
| 20 | +@injectable() |
| 21 | +export class KernelDependencyService implements IKernelDependencyService { |
| 22 | + constructor( |
| 23 | + @inject(IApplicationShell) private readonly appShell: IApplicationShell, |
| 24 | + @inject(IInstaller) private readonly installer: IInstaller |
| 25 | + ) {} |
| 26 | + /** |
| 27 | + * Configures the python interpreter to ensure it can run a Jupyter Kernel by installing any missing dependencies. |
| 28 | + * If user opts not to install they can opt to select another interpreter. |
| 29 | + */ |
| 30 | + public async installMissingDependencies( |
| 31 | + interpreter: PythonInterpreter, |
| 32 | + token?: CancellationToken |
| 33 | + ): Promise<KernelInterpreterDependencyResponse> { |
| 34 | + if (await this.areDependenciesInstalled(interpreter, token)) { |
| 35 | + return KernelInterpreterDependencyResponse.ok; |
| 36 | + } |
| 37 | + |
| 38 | + const promptCancellationPromise = createPromiseFromCancellation({ |
| 39 | + cancelAction: 'resolve', |
| 40 | + defaultValue: undefined, |
| 41 | + token |
| 42 | + }); |
| 43 | + const message = DataScience.libraryRequiredToLaunchJupyterKernelNotInstalledInterpreter().format( |
| 44 | + interpreter.displayName || interpreter.envName || interpreter.path, |
| 45 | + ProductNames.get(Product.ipykernel)! |
| 46 | + ); |
| 47 | + const installerToken = wrapCancellationTokens(token); |
| 48 | + const selection = await Promise.race([ |
| 49 | + this.appShell.showErrorMessage(message, Common.ok(), Common.cancel()), |
| 50 | + promptCancellationPromise |
| 51 | + ]); |
| 52 | + if (installerToken.isCancellationRequested) { |
| 53 | + return KernelInterpreterDependencyResponse.cancel; |
| 54 | + } |
| 55 | + |
| 56 | + if (selection === Common.ok()) { |
| 57 | + const cancellatonPromise = createPromiseFromCancellation({ |
| 58 | + cancelAction: 'resolve', |
| 59 | + defaultValue: InstallerResponse.Ignore, |
| 60 | + token |
| 61 | + }); |
| 62 | + // Always pass a cancellation token to `install`, to ensure it waits until the module is installed. |
| 63 | + const response = await Promise.race([ |
| 64 | + this.installer.install(Product.ipykernel, interpreter, installerToken), |
| 65 | + cancellatonPromise |
| 66 | + ]); |
| 67 | + if (response === InstallerResponse.Installed) { |
| 68 | + return KernelInterpreterDependencyResponse.ok; |
| 69 | + } |
| 70 | + } |
| 71 | + return KernelInterpreterDependencyResponse.cancel; |
| 72 | + } |
| 73 | + public areDependenciesInstalled(interpreter: PythonInterpreter, _token?: CancellationToken): Promise<boolean> { |
| 74 | + return this.installer.isInstalled(Product.ipykernel, interpreter).then((installed) => installed === true); |
| 75 | + } |
| 76 | +} |
0 commit comments