forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonDaemon.ts
More file actions
128 lines (123 loc) · 5.34 KB
/
pythonDaemon.ts
File metadata and controls
128 lines (123 loc) · 5.34 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { ChildProcess } from 'child_process';
import { MessageConnection, RequestType, RequestType0 } from 'vscode-jsonrpc';
import { PythonExecInfo } from '../../pythonEnvironments/exec';
import { InterpreterInformation } from '../../pythonEnvironments/info';
import { extractInterpreterInfo } from '../../pythonEnvironments/info/interpreter';
import { traceWarning } from '../logger';
import { BasePythonDaemon } from './baseDaemon';
import { PythonEnvInfo } from './internal/scripts';
import {
IPythonDaemonExecutionService,
IPythonExecutionService,
ObservableExecutionResult,
SpawnOptions
} from './types';
type ErrorResponse = { error?: string };
export class ConnectionClosedError extends Error {
constructor(public readonly message: string) {
super();
}
}
export class DaemonError extends Error {
constructor(public readonly message: string) {
super();
}
}
export class PythonDaemonExecutionService extends BasePythonDaemon implements IPythonDaemonExecutionService {
constructor(
pythonExecutionService: IPythonExecutionService,
pythonPath: string,
proc: ChildProcess,
connection: MessageConnection
) {
super(pythonExecutionService, pythonPath, proc, connection);
}
public async getInterpreterInformation(): Promise<InterpreterInformation | undefined> {
try {
this.throwIfRPCConnectionIsDead();
const request = new RequestType0<PythonEnvInfo & ErrorResponse, void, void>('get_interpreter_information');
const response = await this.sendRequestWithoutArgs(request);
if (response.error) {
throw Error(response.error);
}
return extractInterpreterInfo(this.pythonPath, response);
} catch (ex) {
traceWarning('Falling back to Python Execution Service due to failure in daemon', ex);
return this.pythonExecutionService.getInterpreterInformation();
}
}
public async getExecutablePath(): Promise<string> {
try {
this.throwIfRPCConnectionIsDead();
type ExecutablePathResponse = ErrorResponse & { path: string };
const request = new RequestType0<ExecutablePathResponse, void, void>('get_executable');
const response = await this.sendRequestWithoutArgs(request);
if (response.error) {
throw new DaemonError(response.error);
}
return response.path;
} catch (ex) {
traceWarning('Falling back to Python Execution Service due to failure in daemon', ex);
return this.pythonExecutionService.getExecutablePath();
}
}
public getExecutionInfo(pythonArgs?: string[]): PythonExecInfo {
return this.pythonExecutionService.getExecutionInfo(pythonArgs);
}
public async isModuleInstalled(moduleName: string): Promise<boolean> {
try {
this.throwIfRPCConnectionIsDead();
type ModuleInstalledResponse = ErrorResponse & { exists: boolean };
const request = new RequestType<{ module_name: string }, ModuleInstalledResponse, void, void>(
'is_module_installed'
);
const response = await this.sendRequest(request, { module_name: moduleName });
if (response.error) {
throw new DaemonError(response.error);
}
return response.exists;
} catch (ex) {
traceWarning('Falling back to Python Execution Service due to failure in daemon', ex);
return this.pythonExecutionService.isModuleInstalled(moduleName);
}
}
public execObservable(args: string[], options: SpawnOptions): ObservableExecutionResult<string> {
if (this.isAlive && this.canExecFileUsingDaemon(args, options)) {
try {
return this.execAsObservable({ fileName: args[0] }, args.slice(1), options);
} catch (ex) {
if (ex instanceof DaemonError || ex instanceof ConnectionClosedError) {
traceWarning('Falling back to Python Execution Service due to failure in daemon', ex);
return this.pythonExecutionService.execObservable(args, options);
} else {
throw ex;
}
}
} else {
return this.pythonExecutionService.execObservable(args, options);
}
}
public execModuleObservable(
moduleName: string,
args: string[],
options: SpawnOptions
): ObservableExecutionResult<string> {
if (this.isAlive && this.canExecModuleUsingDaemon(moduleName, args, options)) {
try {
return this.execAsObservable({ moduleName }, args, options);
} catch (ex) {
if (ex instanceof DaemonError || ex instanceof ConnectionClosedError) {
traceWarning('Falling back to Python Execution Service due to failure in daemon', ex);
return this.pythonExecutionService.execModuleObservable(moduleName, args, options);
} else {
throw ex;
}
}
} else {
return this.pythonExecutionService.execModuleObservable(moduleName, args, options);
}
}
}