forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjupyterHelpers.ts
More file actions
48 lines (42 loc) · 2.02 KB
/
Copy pathjupyterHelpers.ts
File metadata and controls
48 lines (42 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { IProcessServiceFactory } from '../../client/common/process/types';
import { IInterpreterService, PythonInterpreter } from '../../client/interpreter/contracts';
import { DataScienceIocContainer } from './dataScienceIocContainer';
export async function getNotebookCapableInterpreter(ioc: DataScienceIocContainer, processFactory: IProcessServiceFactory): Promise<PythonInterpreter | undefined> {
const is = ioc.serviceContainer.get<IInterpreterService>(IInterpreterService);
const list = await is.getInterpreters();
const procService = await processFactory.create();
if (procService) {
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < list.length; i += 1) {
const result = await procService.exec(list[i].path, ['-m', 'jupyter', 'notebook', '--version'], { env: process.env });
if (!result.stderr) {
return list[i];
}
}
}
return undefined;
}
// IP = * format is a bit different from localhost format
export function getIPConnectionInfo(output: string): string | undefined {
// String format: http://(NAME or IP):PORT/
const nameAndPortRegEx = /(https?):\/\/\(([^\s]*) or [0-9.]*\):([0-9]*)\/(?:\?token=)?([a-zA-Z0-9]*)?/;
const urlMatch = nameAndPortRegEx.exec(output);
if (urlMatch && !urlMatch[4]) {
return `${urlMatch[1]}://${urlMatch[2]}:${urlMatch[3]}/`;
} else if (urlMatch && urlMatch.length === 5) {
return `${urlMatch[1]}://${urlMatch[2]}:${urlMatch[3]}/?token=${urlMatch[4]}`;
}
// In Notebook 6.0 instead of the above format it returns a single valid web address so just return that
return getConnectionInfo(output);
}
export function getConnectionInfo(output: string): string | undefined {
const UrlPatternRegEx = /(https?:\/\/[^\s]+)/;
const urlMatch = UrlPatternRegEx.exec(output);
if (urlMatch) {
return urlMatch[0];
}
return undefined;
}