forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonDebugClient.ts
More file actions
134 lines (126 loc) · 5.53 KB
/
Copy pathNonDebugClient.ts
File metadata and controls
134 lines (126 loc) · 5.53 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
import { BaseDebugServer } from "../DebugServers/BaseDebugServer";
import { NonDebugServer } from "../DebugServers/NonDebugServer";
import { IPythonProcess, IDebugServer } from "../Common/Contracts";
import { DebugSession, OutputEvent } from "vscode-debugadapter";
import { DebugProtocol } from "vscode-debugprotocol";
import * as path from "path";
import * as child_process from "child_process";
import { LaunchRequestArguments } from "../Common/Contracts";
import { DebugClient, DebugType } from "./DebugClient";
import { open } from "../../common/open";
export class NonDebugClient extends DebugClient {
protected args: LaunchRequestArguments;
constructor(args: any, debugSession: DebugSession) {
super(args, debugSession);
this.args = args;
}
private pyProc: child_process.ChildProcess;
private debugServer: BaseDebugServer;
public CreateDebugServer(pythonProcess: IPythonProcess): BaseDebugServer {
return new NonDebugServer(this.debugSession, pythonProcess);
}
public get DebugType(): DebugType {
return DebugType.RunLocal;
}
public Stop() {
if (this.debugServer) {
this.debugServer.Stop();
this.debugServer = null;
}
if (this.pyProc) {
try { this.pyProc.kill(); }
catch (ex) { }
this.pyProc = null;
}
}
public LaunchApplicationToDebug(dbgServer: IDebugServer, processErrored: (error: any) => void): Promise<any> {
return new Promise<any>((resolve, reject) => {
let fileDir = path.dirname(this.args.program);
let processCwd = fileDir;
if (typeof this.args.cwd === "string" && this.args.cwd.length > 0 && this.args.cwd !== 'null') {
processCwd = this.args.cwd;
}
let pythonPath = "python";
if (typeof this.args.pythonPath === "string" && this.args.pythonPath.trim().length > 0) {
pythonPath = this.args.pythonPath;
}
let environmentVariables = this.args.env ? this.args.env : {};
let newEnvVars = {};
if (environmentVariables) {
for (let setting in environmentVariables) {
if (!newEnvVars[setting]) {
newEnvVars[setting] = environmentVariables[setting];
}
}
for (let setting in process.env) {
if (!environmentVariables[setting]) {
environmentVariables[setting] = process.env[setting];
}
}
}
if (!environmentVariables.hasOwnProperty("PYTHONIOENCODING")) {
environmentVariables["PYTHONIOENCODING"] = "UTF-8";
newEnvVars["PYTHONIOENCODING"] = "UTF-8";
}
let launcherArgs = this.buildLauncherArguments();
let args = launcherArgs;
if (this.args.console === 'externalTerminal') {
open({ wait: false, app: [pythonPath].concat(args), cwd: processCwd, env: environmentVariables }).then(proc => {
this.pyProc = proc;
this.pyProc.on('exit', () => {
this.pyProc = null;
this.emit('exit');
});
resolve();
}, error => {
if (reject) {
reject(error);
reject = null;
}
});
return;
}
if (this.args.console === 'integratedTerminal') {
const isSudo = Array.isArray(this.args.debugOptions) && this.args.debugOptions.some(opt => opt === 'Sudo');
const command = isSudo ? 'sudo' : pythonPath;
const commandArgs = isSudo ? [pythonPath].concat(args) : args;
const termArgs: DebugProtocol.RunInTerminalRequestArguments = {
kind: 'integrated',
title: "Python Debug Console",
cwd: processCwd,
args: [command].concat(commandArgs),
env: newEnvVars as { [key: string]: string }
};
this.debugSession.runInTerminalRequest(termArgs, 5000, (response) => {
if (response.success) {
resolve();
} else {
reject(response);
}
});
return;
}
this.pyProc = child_process.spawn(pythonPath, args, { cwd: processCwd, env: environmentVariables });
this.pyProc.on("error", error => {
this.debugSession.sendEvent(new OutputEvent(error + '', "stderr"));
});
this.pyProc.stderr.setEncoding("utf8");
this.pyProc.stdout.setEncoding("utf8");
this.pyProc.stderr.on("data", (error: string) => {
this.debugSession.sendEvent(new OutputEvent(error, "stderr"));
});
this.pyProc.stdout.on("data", (d: string) => {
this.debugSession.sendEvent(new OutputEvent(d, "stdout"));
});
this.pyProc.on('exit', () => {
this.pyProc = null;
this.emit('exit');
});
resolve();
});
}
protected buildLauncherArguments(): string[] {
let programArgs = Array.isArray(this.args.args) && this.args.args.length > 0 ? this.args.args : [];
return [this.args.program].concat(programArgs);
}
}