From c4f649a8490e32965c9435f9f59865b2605d88f4 Mon Sep 17 00:00:00 2001 From: rajeshka Date: Mon, 19 Oct 2020 18:20:48 -0700 Subject: [PATCH] Changes to use bundled python package (#12967) * Set python path in kernel specs when running on SAW devices. * Use tab spacer for kernel json. * Update path to jupyter kernelspec. * removing the kernelspec write * Changed powershell kernel.json to use appdata folder * Addressed PR and added try catches around the code. * removed redundant try catch * removed redundant try catch * removed another try catch * removed space Co-authored-by: Cory Rivera --- .../saw-kernels/powershell/kernel.json | 10 +++++++ extensions/notebook/src/contracts/content.ts | 1 + .../src/jupyter/jupyterServerInstallation.ts | 27 +++++++++++++++++++ .../notebook/src/jupyter/serverInstance.ts | 10 ++++++- 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 extensions/notebook/saw-kernels/powershell/kernel.json diff --git a/extensions/notebook/saw-kernels/powershell/kernel.json b/extensions/notebook/saw-kernels/powershell/kernel.json new file mode 100644 index 0000000000..43c2fb3b37 --- /dev/null +++ b/extensions/notebook/saw-kernels/powershell/kernel.json @@ -0,0 +1,10 @@ +{ + "argv": [ + "{ADS_PYTHONDIR}\\python", + "{ADS_PYTHONDIR}\\Lib\\site-packages\\powershell_kernel\\__main__.py", + "-f", + "{connection_file}" + ], + "display_name": "PowerShell", + "language": "powershell" +} diff --git a/extensions/notebook/src/contracts/content.ts b/extensions/notebook/src/contracts/content.ts index f46f18195a..aae3080775 100644 --- a/extensions/notebook/src/contracts/content.ts +++ b/extensions/notebook/src/contracts/content.ts @@ -20,6 +20,7 @@ export interface IKernelInfo { name: string; language?: string; display_name?: string; + argv?: string[]; } export interface ILanguageInfo { diff --git a/extensions/notebook/src/jupyter/jupyterServerInstallation.ts b/extensions/notebook/src/jupyter/jupyterServerInstallation.ts index 6dac8dd7fd..be51a1fc1a 100644 --- a/extensions/notebook/src/jupyter/jupyterServerInstallation.ts +++ b/extensions/notebook/src/jupyter/jupyterServerInstallation.ts @@ -17,6 +17,7 @@ import * as constants from '../common/constants'; import * as utils from '../common/utils'; import { Deferred } from '../common/promise'; import { ConfigurePythonWizard } from '../dialog/configurePython/configurePythonWizard'; +import { IKernelInfo } from '../contracts/content'; const localize = nls.loadMessageBundle(); const msgInstallPkgProgress = localize('msgInstallPkgProgress', "Notebook dependencies installation is in progress"); @@ -79,6 +80,8 @@ export class JupyterServerInstallation implements IJupyterServerInstallation { private readonly _runningOnSAW: boolean; + private _kernelSpecsUpdated = false; + constructor(extensionPath: string, outputChannel: vscode.OutputChannel) { this.extensionPath = extensionPath; this.outputChannel = outputChannel; @@ -708,6 +711,30 @@ export class JupyterServerInstallation implements IJupyterServerInstallation { public getRequiredPackagesForKernel(kernelName: string): PythonPkgDetails[] { return this._requiredKernelPackages.get(kernelName) ?? []; } + + public get runningOnSaw(): boolean { + return this._runningOnSAW; + } + + public async updateKernelSpecPaths(kernelsFolder: string): Promise { + if (!this._runningOnSAW || this._kernelSpecsUpdated) { + return; + } + let fileNames = await fs.readdir(kernelsFolder); + let filePaths = fileNames.map(name => path.join(kernelsFolder, name)); + let fileStats = await Promise.all(filePaths.map(path => fs.stat(path))); + let folderPaths = filePaths.filter((value, index) => value && fileStats[index].isDirectory()); + let kernelFiles = folderPaths.map(folder => path.join(folder, 'kernel.json')); + await Promise.all(kernelFiles.map(file => this.updateKernelSpecPath(file))); + this._kernelSpecsUpdated = true; + } + + private async updateKernelSpecPath(kernelPath: string): Promise { + let fileContents = await fs.readFile(kernelPath); + let kernelSpec = JSON.parse(fileContents.toString()); + kernelSpec.argv = kernelSpec.argv?.map(arg => arg.replace('{ADS_PYTHONDIR}', this._pythonInstallationPath)); + await fs.writeFile(kernelPath, JSON.stringify(kernelSpec, undefined, '\t')); + } } export interface PythonPkgDetails { diff --git a/extensions/notebook/src/jupyter/serverInstance.ts b/extensions/notebook/src/jupyter/serverInstance.ts index 9204224fa5..fbe859dc6a 100644 --- a/extensions/notebook/src/jupyter/serverInstance.ts +++ b/extensions/notebook/src/jupyter/serverInstance.ts @@ -174,12 +174,20 @@ export class PerFolderServerInstance implements IServerInstance { } private async copyKernelsToSystemJupyterDirs(): Promise { - let kernelsExtensionSource = path.join(this.options.install.extensionPath, 'kernels'); + let kernelsExtensionSource: string; + if (this.options.install.runningOnSaw) { + kernelsExtensionSource = path.join(this.options.install.extensionPath, 'saw-kernels'); + } else { + kernelsExtensionSource = path.join(this.options.install.extensionPath, 'kernels'); + } this._systemJupyterDir = path.join(this.getSystemJupyterHomeDir(), 'kernels'); if (!(await utils.exists(this._systemJupyterDir))) { await utils.mkDir(this._systemJupyterDir, this.options.install.outputChannel); } await fs.copy(kernelsExtensionSource, this._systemJupyterDir); + if (this.options.install.runningOnSaw) { + await this.options.install.updateKernelSpecPaths(this._systemJupyterDir); + } } private getSystemJupyterHomeDir(): string {