Change configure Jupyter server steps from async to sync (#13937)

* change config steps to sync

* fix tests

* use pathexistsSync

* remove pathExistsSync call

* address PR comments
This commit is contained in:
Lucy Zhang
2021-01-20 15:45:54 -08:00
committed by GitHub
parent 58d4dda1e8
commit 068649cba4
5 changed files with 29 additions and 33 deletions

View File

@@ -22,13 +22,13 @@ export function getLivyUrl(serverName: string, port: string): string {
return this.getKnoxUrl(serverName, port) + '/default/livy/v1/'; return this.getKnoxUrl(serverName, port) + '/default/livy/v1/';
} }
export async function mkDir(dirPath: string, outputChannel?: vscode.OutputChannel): Promise<void> { export async function ensureDir(dirPath: string, outputChannel?: vscode.OutputChannel): Promise<void> {
if (!await fs.pathExists(dirPath)) { outputChannel?.appendLine(localize('ensureDirOutputMsg', "... Ensuring {0} exists", dirPath));
if (outputChannel) { await fs.ensureDir(dirPath);
outputChannel.appendLine(localize('mkdirOutputMsg', "... Creating {0}", dirPath)); }
} export function ensureDirSync(dirPath: string, outputChannel?: vscode.OutputChannel): void {
await fs.ensureDir(dirPath); outputChannel?.appendLine(localize('ensureDirOutputMsg', "... Ensuring {0} exists", dirPath));
} fs.ensureDirSync(dirPath);
} }
export function getErrorMessage(error: Error | string): string { export function getErrorMessage(error: Error | string): string {

View File

@@ -259,7 +259,7 @@ export class JupyterSession implements nb.ISession {
public async configureKernel(): Promise<void> { public async configureKernel(): Promise<void> {
let sparkmagicConfDir = path.join(utils.getUserHome(), '.sparkmagic'); let sparkmagicConfDir = path.join(utils.getUserHome(), '.sparkmagic');
await utils.mkDir(sparkmagicConfDir); await utils.ensureDir(sparkmagicConfDir);
// Default to localhost in config file. // Default to localhost in config file.
let creds: ICredentials = { let creds: ICredentials = {

View File

@@ -142,34 +142,34 @@ export class PerFolderServerInstance implements IServerInstance {
} }
private async configureJupyter(): Promise<void> { private async configureJupyter(): Promise<void> {
await this.createInstanceFolders(); this.createInstanceFolders();
let resourcesFolder = path.join(this.options.install.extensionPath, 'resources', constants.jupyterConfigRootFolder); let resourcesFolder = path.join(this.options.install.extensionPath, 'resources', constants.jupyterConfigRootFolder);
await this.copyInstanceConfig(resourcesFolder); this.copyInstanceConfig(resourcesFolder);
await this.CopyCustomJs(resourcesFolder); this.copyCustomJs(resourcesFolder);
await this.copyKernelsToSystemJupyterDirs(); await this.copyKernelsToSystemJupyterDirs();
} }
private async createInstanceFolders(): Promise<void> { private createInstanceFolders(): void {
this.baseDir = path.join(this.getSystemJupyterHomeDir(), 'instances', `${UUID.generateUuid()}`); this.baseDir = path.join(this.getSystemJupyterHomeDir(), 'instances', `${UUID.generateUuid()}`);
this.instanceConfigRoot = path.join(this.baseDir, 'config'); this.instanceConfigRoot = path.join(this.baseDir, 'config');
this.instanceDataRoot = path.join(this.baseDir, 'data'); this.instanceDataRoot = path.join(this.baseDir, 'data');
await utils.mkDir(this.baseDir, this.options.install.outputChannel); utils.ensureDirSync(this.baseDir, this.options.install.outputChannel);
await utils.mkDir(this.instanceConfigRoot, this.options.install.outputChannel); utils.ensureDirSync(this.instanceConfigRoot, this.options.install.outputChannel);
await utils.mkDir(this.instanceDataRoot, this.options.install.outputChannel); utils.ensureDirSync(this.instanceDataRoot, this.options.install.outputChannel);
} }
private async copyInstanceConfig(resourcesFolder: string): Promise<void> { private copyInstanceConfig(resourcesFolder: string): void {
let configSource = path.join(resourcesFolder, NotebookConfigFilename); let configSource = path.join(resourcesFolder, NotebookConfigFilename);
let configDest = path.join(this.instanceConfigRoot, NotebookConfigFilename); let configDest = path.join(this.instanceConfigRoot, NotebookConfigFilename);
await fs.copy(configSource, configDest); fs.copySync(configSource, configDest);
} }
private async CopyCustomJs(resourcesFolder: string): Promise<void> { private copyCustomJs(resourcesFolder: string): void {
let customPath = path.join(this.instanceConfigRoot, 'custom'); let customPath = path.join(this.instanceConfigRoot, 'custom');
await utils.mkDir(customPath, this.options.install.outputChannel); utils.ensureDirSync(customPath, this.options.install.outputChannel);
let customSource = path.join(resourcesFolder, CustomJsFilename); let customSource = path.join(resourcesFolder, CustomJsFilename);
let customDest = path.join(customPath, CustomJsFilename); let customDest = path.join(customPath, CustomJsFilename);
await fs.copy(customSource, customDest); fs.copySync(customSource, customDest);
} }
private async copyKernelsToSystemJupyterDirs(): Promise<void> { private async copyKernelsToSystemJupyterDirs(): Promise<void> {
@@ -180,10 +180,8 @@ export class PerFolderServerInstance implements IServerInstance {
kernelsExtensionSource = path.join(this.options.install.extensionPath, 'kernels'); kernelsExtensionSource = path.join(this.options.install.extensionPath, 'kernels');
} }
this._systemJupyterDir = path.join(this.getSystemJupyterHomeDir(), 'kernels'); this._systemJupyterDir = path.join(this.getSystemJupyterHomeDir(), 'kernels');
if (!(await utils.exists(this._systemJupyterDir))) { utils.ensureDirSync(this._systemJupyterDir, this.options.install.outputChannel);
await utils.mkDir(this._systemJupyterDir, this.options.install.outputChannel); fs.copySync(kernelsExtensionSource, this._systemJupyterDir);
}
await fs.copy(kernelsExtensionSource, this._systemJupyterDir);
if (this.options.install.runningOnSaw) { if (this.options.install.runningOnSaw) {
await this.options.install.updateKernelSpecPaths(this._systemJupyterDir); await this.options.install.updateKernelSpecPaths(this._systemJupyterDir);
} }

View File

@@ -28,10 +28,10 @@ describe('Utils Tests', function () {
should(utils.getLivyUrl(host, port)).endWith('/gateway/default/livy/v1/'); should(utils.getLivyUrl(host, port)).endWith('/gateway/default/livy/v1/');
}); });
it('mkDir', async () => { it('ensureDir', async () => {
const dirPath = path.join(os.tmpdir(), uuid.v4()); const dirPath = path.join(os.tmpdir(), uuid.v4());
await should(fs.stat(dirPath)).be.rejected(); await should(fs.stat(dirPath)).be.rejected();
await utils.mkDir(dirPath, new MockOutputChannel()); await utils.ensureDir(dirPath, new MockOutputChannel());
should.exist(await fs.stat(dirPath), `Folder ${dirPath} did not exist after creation`); should.exist(await fs.stat(dirPath), `Folder ${dirPath} did not exist after creation`);
}); });

View File

@@ -55,17 +55,15 @@ describe('Jupyter server instance', function (): void {
it('Should create config and data directories on configure', async function (): Promise<void> { it('Should create config and data directories on configure', async function (): Promise<void> {
// Given a server instance // Given a server instance
let mkdirStub = sinon.stub(utils,'mkDir').withArgs(sinon.match.any,sinon.match.any).returns(Promise.resolve()); let ensureDirSyncStub = sinon.stub(utils,'ensureDirSync').withArgs(sinon.match.any,sinon.match.any).returns();
let copyStub = sinon.stub(fs,'copy').returns(); let copyStub = sinon.stub(fs,'copySync').returns();
let pathStub = sinon.stub(utils,'exists').withArgs(sinon.match.any).returns(Promise.resolve(false));
// When I run configure // When I run configure
await serverInstance.configure(); await serverInstance.configure();
// Then I expect a folder to have been created with config and data subdirs // Then I expect a folder to have been created with config and data subdirs
sinon.assert.callCount(mkdirStub,5); sinon.assert.callCount(ensureDirSyncStub,5);
sinon.assert.callCount(copyStub,3); sinon.assert.callCount(copyStub,3);
sinon.assert.callCount(pathStub,1);
}); });
it('Should have URI info after start', async function (): Promise<void> { it('Should have URI info after start', async function (): Promise<void> {
@@ -152,8 +150,8 @@ describe('Jupyter server instance', function (): void {
it('Should remove directory on close', async function (): Promise<void> { it('Should remove directory on close', async function (): Promise<void> {
// Given configure and startup are done // Given configure and startup are done
sinon.stub(utils,'mkDir').withArgs(sinon.match.any,sinon.match.any).returns(Promise.resolve()); sinon.stub(utils,'ensureDirSync').withArgs(sinon.match.any,sinon.match.any).returns();
sinon.stub(fs,'copy').returns(); sinon.stub(fs,'copySync').returns();
let process = setupSpawn({ let process = setupSpawn({
sdtout: (listener: (msg: string) => void) => { }, sdtout: (listener: (msg: string) => void) => { },