forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalDebugClient.ts
More file actions
203 lines (186 loc) · 8.48 KB
/
Copy pathLocalDebugClient.ts
File metadata and controls
203 lines (186 loc) · 8.48 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {BaseDebugServer} from '../DebugServers/BaseDebugServer';
import {LocalDebugServer} from '../DebugServers/LocalDebugServer';
import {IPythonProcess, IPythonThread, IDebugServer} from '../Common/Contracts';
import {DebugSession, OutputEvent} from 'vscode-debugadapter';
import * as path from 'path';
import * as child_process from 'child_process';
import {LaunchRequestArguments} from '../Common/Contracts';
import {DebugClient, DebugType} from './DebugClient';
import * as fs from 'fs';
import {open} from '../../common/open';
let fsExtra = require("fs-extra");
let tmp = require("tmp");
let prependFile = require('prepend-file');
var LineByLineReader = require('line-by-line');
const PTVS_FILES = ["visualstudio_ipython_repl.py", "visualstudio_py_debugger.py",
"visualstudio_py_launcher.py", "visualstudio_py_repl.py", "visualstudio_py_util.py"];
export class LocalDebugClient extends DebugClient {
protected args: LaunchRequestArguments;
constructor(args: any, debugSession: DebugSession) {
super(args, debugSession);
this.args = args;
}
private pyProc: child_process.ChildProcess;
private pythonProcess: IPythonProcess;
private debugServer: BaseDebugServer;
public CreateDebugServer(pythonProcess: IPythonProcess): BaseDebugServer {
this.pythonProcess = pythonProcess;
this.debugServer = new LocalDebugServer(this.debugSession, this.pythonProcess);
return this.debugServer;
}
public get DebugType(): DebugType {
return DebugType.Local;
}
public Stop() {
if (this.debugServer) {
this.debugServer.Stop()
this.debugServer = null;
}
if (this.pyProc) {
try { this.pyProc.send("EXIT"); }
catch (ex) { }
try { this.pyProc.stdin.write("EXIT"); }
catch (ex) { }
try { this.pyProc.disconnect(); }
catch (ex) { }
this.pyProc = null;
}
}
private getPTVSToolsFilePath(): Promise<string> {
var currentFileName = module.filename;
return new Promise<String>((resolve, reject) => {
tmp.dir((error, tmpDir) => {
if (error) { return reject(error); }
var ptVSToolsPath = path.join(path.dirname(currentFileName), "..", "..", "..", "..", "pythonFiles", "PythonTools");
var promises = PTVS_FILES.map(ptvsFile=> {
return new Promise((copyResolve, copyReject) => {
var sourceFile = path.join(ptVSToolsPath, ptvsFile);
var targetFile = path.join(tmpDir, ptvsFile);
fsExtra.copy(sourceFile, targetFile, copyError=> {
if (copyError) { return copyReject(copyError); }
copyResolve(targetFile);
});
});
});
Promise.all(promises).then(() => {
resolve(path.join(tmpDir, "visualstudio_py_launcher.py"));
}, reject);
});
});
}
private displayError(error) {
if (!error) { return; }
var errorMsg = typeof error === "string" ? error : ((error.message && error.message.length > 0) ? error.message : "");
if (errorMsg.length > 0) {
this.debugSession.sendEvent(new OutputEvent(errorMsg + "\n", "stderr"));
console.error(errorMsg);
}
}
private getShebangLines(program: string): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
var lr = new LineByLineReader(program);
var lineNumber = 0;
var shebangLines: string[] = [];
lr.on('error', err=> {
resolve(shebangLines);
});
lr.on('line', (line: string) => {
lineNumber++;
var trimmedLine = line.trim();
if (trimmedLine.startsWith("#")) {
shebangLines.push(line);
}
if (lineNumber >= 2) {
//Ensure we always have two lines, even if no shebangLines
//This way if ever we get lines numbers in errors for the python file, we have a consistency
while (shebangLines.length <= 2) {
shebangLines.push("#");
}
resolve(shebangLines);
lr.close();
}
});
lr.on('end', function() {
//Ensure we always have two lines, even if no shebangLines
//This way if ever we get lines numbers in errors for the python file, we have a consistency
while (shebangLines.length <= 2) {
shebangLines.push("#");
}
resolve(shebangLines);
});
});
}
private prependShebangToPTVSFile(ptVSToolsFilePath: string, program: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.getShebangLines(program).then(lines=> {
var linesToPrepend = lines.join('\n') + '\n';
prependFile(ptVSToolsFilePath, linesToPrepend, error=> {
if (error) { reject(error); }
else { resolve(ptVSToolsFilePath); }
})
}, reject);
});
}
public LaunchApplicationToDebug(dbgServer: IDebugServer): Promise<any> {
return new Promise<any>((resolve, reject) => {
var fileDir = path.dirname(this.args.program);
var processCwd = fileDir;
var fileNameWithoutPath = path.basename(this.args.program);
var pythonPath = "python";
if (typeof this.args.pythonPath === "string" && this.args.pythonPath.trim().length > 0) {
pythonPath = this.args.pythonPath;
}
var environmentVariables = this.args.env ? this.args.env : {};
//GUID is hardcoded for now, will have to be fixed
var currentFileName = module.filename;
//var ptVSToolsFilePath = path.join(path.dirname(currentFileName), "..", "..", "..", "..", "pythonFiles", "PythonTools", "visualstudio_py_launcher.py");
this.getPTVSToolsFilePath().then((ptVSToolsFilePath) => {
return this.prependShebangToPTVSFile(ptVSToolsFilePath, this.args.program);
}, error=> {
this.displayError(error);
reject(error);
}).then((ptVSToolsFilePath) => {
var launcherArgs = this.buildLauncherArguments();
var args = [ptVSToolsFilePath, fileDir, dbgServer.port.toString(), "34806ad9-833a-4524-8cd6-18ca4aa74f14"].concat(launcherArgs);
console.log(pythonPath + " " + args.join(" "));
if (this.args.externalConsole === true) {
open({ wait: false, app: [pythonPath].concat(args), cwd: processCwd, env: environmentVariables }).then(proc=> {
this.pyProc = proc;
resolve();
}, error=> {
if (!this.debugServer && this.debugServer.IsRunning) {
return;
}
this.displayError(error);
});
return;
}
this.pyProc = child_process.spawn(pythonPath, args, { cwd: processCwd, env: environmentVariables });
this.pyProc.on("error", error=> {
if (!this.debugServer && this.debugServer.IsRunning) {
return;
}
this.displayError(error);
});
this.pyProc.on("stderr", error=> {
if (!this.debugServer && this.debugServer.IsRunning) {
return;
}
this.displayError(error);
});
resolve();
}, error=> {
this.displayError(error);
reject(error);
});
});
}
protected buildLauncherArguments(): string[] {
var vsDebugOptions = "WaitOnAbnormalExit, WaitOnNormalExit, RedirectOutput";
if (Array.isArray(this.args.debugOptions)) {
vsDebugOptions = this.args.debugOptions.join(", ");
}
var programArgs = Array.isArray(this.args.args) && this.args.args.length > 0 ? this.args.args : [];
return [vsDebugOptions, this.args.program].concat(programArgs);
}
}