forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pythonProcess.ts
104 lines (92 loc) · 4.38 KB
/
pythonProcess.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { PythonExecInfo } from '../../pythonEnvironments/exec';
import { ErrorUtils } from '../errors/errorUtils';
import { ModuleNotInstalledError } from '../errors/moduleNotInstalledError';
import * as internalPython from './internal/python';
import { ExecutionResult, IProcessService, IPythonEnvironment, ObservableExecutionResult, SpawnOptions } from './types';
class PythonProcessService {
constructor(
// This is the externally defined functionality used by the class.
private readonly deps: {
// from PythonEnvironment:
isModuleInstalled(moduleName: string): Promise<boolean>;
getExecutionInfo(pythonArgs?: string[]): PythonExecInfo;
getExecutionObservableInfo(pythonArgs?: string[]): PythonExecInfo;
// from ProcessService:
exec(file: string, args: string[], options: SpawnOptions): Promise<ExecutionResult<string>>;
execObservable(file: string, args: string[], options: SpawnOptions): ObservableExecutionResult<string>;
},
) {}
public execObservable(args: string[], options: SpawnOptions): ObservableExecutionResult<string> {
const opts: SpawnOptions = { ...options };
const executable = this.deps.getExecutionObservableInfo(args);
return this.deps.execObservable(executable.command, executable.args, opts);
}
public execModuleObservable(
moduleName: string,
moduleArgs: string[],
options: SpawnOptions,
): ObservableExecutionResult<string> {
const args = internalPython.execModule(moduleName, moduleArgs);
const opts: SpawnOptions = { ...options };
const executable = this.deps.getExecutionObservableInfo(args);
return this.deps.execObservable(executable.command, executable.args, opts);
}
public async exec(args: string[], options: SpawnOptions): Promise<ExecutionResult<string>> {
const opts: SpawnOptions = { ...options };
const executable = this.deps.getExecutionInfo(args);
return this.deps.exec(executable.command, executable.args, opts);
}
public async execModule(
moduleName: string,
moduleArgs: string[],
options: SpawnOptions,
): Promise<ExecutionResult<string>> {
const args = internalPython.execModule(moduleName, moduleArgs);
const opts: SpawnOptions = { ...options };
const executable = this.deps.getExecutionInfo(args);
const result = await this.deps.exec(executable.command, executable.args, opts);
// If a module is not installed we'll have something in stderr.
if (moduleName && ErrorUtils.outputHasModuleNotInstalledError(moduleName, result.stderr)) {
const isInstalled = await this.deps.isModuleInstalled(moduleName);
if (!isInstalled) {
throw new ModuleNotInstalledError(moduleName);
}
}
return result;
}
public async execForLinter(
moduleName: string,
args: string[],
options: SpawnOptions,
): Promise<ExecutionResult<string>> {
const opts: SpawnOptions = { ...options };
const executable = this.deps.getExecutionInfo(args);
const result = await this.deps.exec(executable.command, executable.args, opts);
// If a module is not installed we'll have something in stderr.
if (moduleName && ErrorUtils.outputHasModuleNotInstalledError(moduleName, result.stderr)) {
const isInstalled = await this.deps.isModuleInstalled(moduleName);
if (!isInstalled) {
throw new ModuleNotInstalledError(moduleName);
}
}
return result;
}
}
export function createPythonProcessService(
procs: IProcessService,
// from PythonEnvironment:
env: IPythonEnvironment,
) {
const deps = {
// from PythonService:
isModuleInstalled: async (m: string) => env.isModuleInstalled(m),
getExecutionInfo: (a?: string[]) => env.getExecutionInfo(a),
getExecutionObservableInfo: (a?: string[]) => env.getExecutionObservableInfo(a),
// from ProcessService:
exec: async (f: string, a: string[], o: SpawnOptions) => procs.exec(f, a, o),
execObservable: (f: string, a: string[], o: SpawnOptions) => procs.execObservable(f, a, o),
};
return new PythonProcessService(deps);
}