forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonDaemon.ts
More file actions
141 lines (136 loc) · 5.9 KB
/
pythonDaemon.ts
File metadata and controls
141 lines (136 loc) · 5.9 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
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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 { InterpreterInformation, PythonVersionInfo } from '../../pythonEnvironments/discovery/types';
import { traceWarning } from '../logger';
import { Architecture } from '../utils/platform';
import { parsePythonVersion } from '../utils/version';
import { BasePythonDaemon } from './baseDaemon';
import {
IPythonDaemonExecutionService,
IPythonExecutionService,
ObservableExecutionResult,
PythonExecutionInfo,
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();
type InterpreterInfoResponse = ErrorResponse & {
versionInfo: PythonVersionInfo;
sysPrefix: string;
sysVersion: string;
is64Bit: boolean;
};
const request = new RequestType0<InterpreterInfoResponse, void, void>('get_interpreter_information');
const response = await this.sendRequestWithoutArgs(request);
const versionValue =
response.versionInfo.length === 4
? `${response.versionInfo.slice(0, 3).join('.')}-${response.versionInfo[3]}`
: response.versionInfo.join('.');
return {
architecture: response.is64Bit ? Architecture.x64 : Architecture.x86,
path: this.pythonPath,
version: parsePythonVersion(versionValue),
sysVersion: response.sysVersion,
sysPrefix: response.sysPrefix
};
} 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[]): PythonExecutionInfo {
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);
}
}
}