Skip to content

Commit e32d716

Browse files
Factor out internal.scripts.vscode_datascience_helpers.*().
1 parent 95a09a9 commit e32d716

File tree

5 files changed

+106
-64
lines changed

5 files changed

+106
-64
lines changed

src/client/common/process/internal.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,87 @@ export namespace scripts {
363363
return [args, parse];
364364
}
365365
}
366+
367+
//============================
368+
// vscode_datascience_helpers/
369+
370+
export namespace vscode_datascience_helpers {
371+
const _SCRIPTS_DIR = path.join(SCRIPTS_DIR, 'vscode_datascience_helpers');
372+
373+
type JupyterServerInfo = {
374+
base_url: string;
375+
notebook_dir: string;
376+
hostname: string;
377+
password: boolean;
378+
pid: number;
379+
port: number;
380+
secure: boolean;
381+
token: string;
382+
url: string;
383+
};
384+
385+
//============================
386+
// getJupyterVariableDataFrameInfo.py
387+
388+
export function getJupyterVariableDataFrameInfo(): string[] {
389+
const script = path.join(_SCRIPTS_DIR, 'getJupyterVariableDataFrameInfo.py');
390+
// There is no script-specific output to parse, so we do not return a function.
391+
return [script];
392+
}
393+
394+
//============================
395+
// getJupyterVariableDataFrameRows.py
396+
397+
export function getJupyterVariableDataFrameRows(): string[] {
398+
const script = path.join(_SCRIPTS_DIR, 'getJupyterVariableDataFrameRows.py');
399+
// There is no script-specific output to parse, so we do not return a function.
400+
return [script];
401+
}
402+
403+
//============================
404+
// getServerInfo.py
405+
406+
export function getServerInfo(): [string[], (out: string) => JupyterServerInfo[]] {
407+
const script = path.join(_SCRIPTS_DIR, 'getServerInfo.py');
408+
const args = [script];
409+
410+
function parse(out: string): JupyterServerInfo[] {
411+
return JSON.parse(out.trim());
412+
}
413+
414+
return [args, parse];
415+
}
416+
417+
//============================
418+
// getJupyterKernels.py
419+
420+
export function getJupyterKernels(): string[] {
421+
const script = path.join(_SCRIPTS_DIR, 'getJupyterKernels.py');
422+
// There is no script-specific output to parse, so we do not return a function.
423+
return [script];
424+
}
425+
426+
//============================
427+
// getJupyterKernelspecVersion.py
428+
429+
export function getJupyterKernelspecVersion(): string[] {
430+
const script = path.join(_SCRIPTS_DIR, 'getJupyterKernelspecVersion.py');
431+
// For now we do not worry about parsing the output here.
432+
return [script];
433+
}
434+
435+
//============================
436+
// jupyter_nbInstalled.py
437+
438+
export function jupyter_nbInstalled(): [string[], (out: string) => boolean] {
439+
const script = path.join(_SCRIPTS_DIR, 'jupyter_nbInstalled.py');
440+
const args = [script];
441+
442+
function parse(out: string): boolean {
443+
return out.toLowerCase().includes('available');
444+
}
445+
446+
return [args, parse];
447+
}
448+
}
366449
}

src/client/datascience/jupyter/interpreter/jupyterCommand.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
'use strict';
44
import { SpawnOptions } from 'child_process';
55
import { inject, injectable } from 'inversify';
6-
import * as path from 'path';
76
import { traceError } from '../../../common/logger';
7+
import { scripts as internalScripts } from '../../../common/process/internal';
88
import {
99
ExecutionResult,
1010
IProcessService,
@@ -13,7 +13,6 @@ import {
1313
IPythonExecutionService,
1414
ObservableExecutionResult
1515
} from '../../../common/process/types';
16-
import { EXTENSION_ROOT_DIR } from '../../../constants';
1716
import { IEnvironmentActivationService } from '../../../interpreter/activation/types';
1817
import { IInterpreterService, PythonInterpreter } from '../../../interpreter/contracts';
1918
import { JupyterCommands, PythonDaemonModule } from '../../constants';
@@ -98,19 +97,10 @@ class InterpreterJupyterCommand implements IJupyterCommand {
9897
args.join(' ').toLowerCase().startsWith('-m jupyter notebook')) ||
9998
(moduleName.toLowerCase() === 'notebook' && args.join(' ').toLowerCase().startsWith('-m notebook'))
10099
) {
100+
const [scriptArgs, parse] = internalScripts.vscode_datascience_helpers.jupyter_nbInstalled();
101101
try {
102-
const output = await svc.exec(
103-
[
104-
path.join(
105-
EXTENSION_ROOT_DIR,
106-
'pythonFiles',
107-
'vscode_datascience_helpers',
108-
'jupyter_nbInstalled.py'
109-
)
110-
],
111-
{}
112-
);
113-
if (output.stdout.toLowerCase().includes('available')) {
102+
const proc = await svc.exec(scriptArgs, {});
103+
if (parse(proc.stdout)) {
114104
return svc;
115105
}
116106
} catch (ex) {
@@ -263,28 +253,17 @@ export class InterpreterJupyterKernelSpecCommand extends InterpreterJupyterComma
263253
interpreter,
264254
bypassCondaExecution: true
265255
});
266-
return activatedEnv.exec(
267-
[path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'vscode_datascience_helpers', 'getJupyterKernels.py')],
268-
{ ...options, throwOnStdErr: true }
269-
);
256+
const args = internalScripts.vscode_datascience_helpers.getJupyterKernels();
257+
return activatedEnv.exec(args, { ...options, throwOnStdErr: true });
270258
}
271259
private async getKernelSpecVersion(interpreter: PythonInterpreter, options: SpawnOptions) {
272260
// Try getting kernels using python script, if that fails (even if there's output in stderr) rethrow original exception.
273261
const activatedEnv = await this.pythonExecutionFactory.createActivatedEnvironment({
274262
interpreter,
275263
bypassCondaExecution: true
276264
});
277-
return activatedEnv.exec(
278-
[
279-
path.join(
280-
EXTENSION_ROOT_DIR,
281-
'pythonFiles',
282-
'vscode_datascience_helpers',
283-
'getJupyterKernelspecVersion.py'
284-
)
285-
],
286-
{ ...options, throwOnStdErr: true }
287-
);
265+
const args = internalScripts.vscode_datascience_helpers.getJupyterKernelspecVersion();
266+
return activatedEnv.exec(args, { ...options, throwOnStdErr: true });
288267
}
289268
}
290269

src/client/datascience/jupyter/interpreter/jupyterCommandInterpreterExecutionService.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import * as path from 'path';
87
import { CancellationToken } from 'vscode';
98
import { Cancellation } from '../../../common/cancellation';
109
import { traceError, traceInfo, traceWarning } from '../../../common/logger';
1110
import { IFileSystem } from '../../../common/platform/types';
11+
import { scripts as internalScripts } from '../../../common/process/internal';
1212
import { IPythonExecutionFactory, ObservableExecutionResult, SpawnOptions } from '../../../common/process/types';
1313
import { DataScience } from '../../../common/utils/localize';
1414
import { noop } from '../../../common/utils/misc';
15-
import { EXTENSION_ROOT_DIR } from '../../../constants';
1615
import { IInterpreterService, PythonInterpreter } from '../../../interpreter/contracts';
1716
import { JupyterCommands, PythonDaemonModule } from '../../constants';
1817
import { IJupyterSubCommandExecutionService } from '../../types';
@@ -102,13 +101,13 @@ export class JupyterCommandFinderInterpreterExecutionService implements IJupyter
102101

103102
// We have a small python file here that we will execute to get the server info from all running Jupyter instances
104103
const newOptions: SpawnOptions = { mergeStdOutErr: true, token: token };
105-
const file = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'vscode_datascience_helpers', 'getServerInfo.py');
106-
const serverInfoString = await daemon.exec([file], newOptions);
104+
const [args, parse] = internalScripts.vscode_datascience_helpers.getServerInfo();
105+
const serverInfoProc = await daemon.exec(args, newOptions);
107106

108107
let serverInfos: JupyterServerInfo[];
109108
try {
110109
// Parse out our results, return undefined if we can't suss it out
111-
serverInfos = JSON.parse(serverInfoString.stdout.trim()) as JupyterServerInfo[];
110+
serverInfos = parse(serverInfoProc.stdout) as JupyterServerInfo[];
112111
} catch (err) {
113112
traceWarning('Failed to parse JSON when getting server info out from getServerInfo.py', err);
114113
return;

src/client/datascience/jupyter/interpreter/jupyterInterpreterSubCommandExecutionService.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
'use strict';
55

66
import { inject, injectable, named } from 'inversify';
7-
import * as path from 'path';
87
import { CancellationToken } from 'vscode';
98
import { Cancellation } from '../../../common/cancellation';
109
import { traceError, traceInfo, traceWarning } from '../../../common/logger';
1110
import { IFileSystem } from '../../../common/platform/types';
11+
import { scripts as internalScripts } from '../../../common/process/internal';
1212
import { IPythonExecutionFactory, ObservableExecutionResult, SpawnOptions } from '../../../common/process/types';
1313
import { IOutputChannel, IPathUtils, Product } from '../../../common/types';
1414
import { DataScience } from '../../../common/utils/localize';
1515
import { noop } from '../../../common/utils/misc';
16-
import { EXTENSION_ROOT_DIR } from '../../../constants';
1716
import { IInterpreterService, PythonInterpreter } from '../../../interpreter/contracts';
1817
import { sendTelemetryEvent } from '../../../telemetry';
1918
import { JUPYTER_OUTPUT_CHANNEL, PythonDaemonModule, Telemetry } from '../../constants';
@@ -126,13 +125,13 @@ export class JupyterInterpreterSubCommandExecutionService
126125

127126
// We have a small python file here that we will execute to get the server info from all running Jupyter instances
128127
const newOptions: SpawnOptions = { mergeStdOutErr: true, token: token };
129-
const file = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'vscode_datascience_helpers', 'getServerInfo.py');
130-
const serverInfoString = await daemon.exec([file], newOptions);
128+
const [args, parse] = internalScripts.vscode_datascience_helpers.getServerInfo();
129+
const serverInfoProc = await daemon.exec(args, newOptions);
131130

132131
let serverInfos: JupyterServerInfo[];
133132
try {
134133
// Parse out our results, return undefined if we can't suss it out
135-
serverInfos = JSON.parse(serverInfoString.stdout.trim()) as JupyterServerInfo[];
134+
serverInfos = parse(serverInfoProc.stdout) as JupyterServerInfo[];
136135
} catch (err) {
137136
traceWarning('Failed to parse JSON when getting server info out from getServerInfo.py', err);
138137
return;
@@ -197,18 +196,9 @@ export class JupyterInterpreterSubCommandExecutionService
197196
return '';
198197
});
199198
// Possible we cannot import ipykernel for some reason. (use as backup option).
199+
const args = internalScripts.vscode_datascience_helpers.getJupyterKernels();
200200
const stdoutFromFileExecPromise = daemon
201-
.exec(
202-
[
203-
path.join(
204-
EXTENSION_ROOT_DIR,
205-
'pythonFiles',
206-
'vscode_datascience_helpers',
207-
'getJupyterKernels.py'
208-
)
209-
],
210-
spawnOptions
211-
)
201+
.exec(args, spawnOptions)
212202
.then((output) => output.stdout)
213203
.catch((fileEx) => {
214204
traceError('Failed to list kernels from getJupyterKernels.py', fileEx);

src/client/datascience/jupyter/jupyterVariables.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
import { nbformat } from '@jupyterlab/coreutils';
55
import { JSONObject } from '@phosphor/coreutils';
66
import { inject, injectable } from 'inversify';
7-
import * as path from 'path';
87
import stripAnsi from 'strip-ansi';
98
import * as uuid from 'uuid/v4';
109

1110
import { Uri } from 'vscode';
1211
import { PYTHON_LANGUAGE } from '../../common/constants';
1312
import { traceError } from '../../common/logger';
1413
import { IFileSystem } from '../../common/platform/types';
14+
import { scripts as internalScripts } from '../../common/process/internal';
1515
import { IConfigurationService } from '../../common/types';
1616
import * as localize from '../../common/utils/localize';
17-
import { EXTENSION_ROOT_DIR } from '../../constants';
1817
import { Identifiers, Settings } from '../constants';
1918
import {
2019
ICell,
@@ -94,20 +93,12 @@ export class JupyterVariables implements IJupyterVariables {
9493
// Private methods
9594
// Load our python files for fetching variables
9695
private async loadVariableFiles(): Promise<void> {
97-
let file = path.join(
98-
EXTENSION_ROOT_DIR,
99-
'pythonFiles',
100-
'vscode_datascience_helpers',
101-
'getJupyterVariableDataFrameInfo.py'
102-
);
96+
let args = internalScripts.vscode_datascience_helpers.getJupyterVariableDataFrameInfo();
97+
let file = args[0];
10398
this.fetchDataFrameInfoScript = await this.fileSystem.readFile(file);
10499

105-
file = path.join(
106-
EXTENSION_ROOT_DIR,
107-
'pythonFiles',
108-
'vscode_datascience_helpers',
109-
'getJupyterVariableDataFrameRows.py'
110-
);
100+
args = internalScripts.vscode_datascience_helpers.getJupyterVariableDataFrameInfo();
101+
file = args[0];
111102
this.fetchDataFrameRowsScript = await this.fileSystem.readFile(file);
112103

113104
this.filesLoaded = true;

0 commit comments

Comments
 (0)