forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonV2Provider.ts
More file actions
138 lines (133 loc) · 7.12 KB
/
Copy pathpythonV2Provider.ts
File metadata and controls
138 lines (133 loc) · 7.12 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { Uri } from 'vscode';
import { IPlatformService } from '../../common/platform/types';
import { IServiceContainer } from '../../ioc/types';
import { sendTelemetryEvent } from '../../telemetry';
import { DEBUGGER } from '../../telemetry/constants';
import { DebuggerTelemetryV2 } from '../../telemetry/types';
import { AttachRequestArguments, DebugOptions, LaunchRequestArguments } from '../Common/Contracts';
import { BaseConfigurationProvider, PythonAttachDebugConfiguration, PythonLaunchDebugConfiguration } from './baseProvider';
import { IConfigurationProviderUtils } from './types';
@injectable()
export class PythonV2DebugConfigurationProvider extends BaseConfigurationProvider<LaunchRequestArguments, AttachRequestArguments> {
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) {
super('pythonExperimental', serviceContainer);
}
protected async provideLaunchDefaults(workspaceFolder: Uri | undefined, debugConfiguration: PythonLaunchDebugConfiguration<LaunchRequestArguments>): Promise<void> {
await super.provideLaunchDefaults(workspaceFolder, debugConfiguration);
const debugOptions = debugConfiguration.debugOptions!;
if (debugConfiguration.debugStdLib) {
this.debugOption(debugOptions, DebugOptions.DebugStdLib);
}
if (debugConfiguration.django) {
this.debugOption(debugOptions, DebugOptions.Django);
}
if (debugConfiguration.jinja) {
this.debugOption(debugOptions, DebugOptions.Jinja);
}
if (debugConfiguration.redirectOutput || debugConfiguration.redirectOutput === undefined) {
this.debugOption(debugOptions, DebugOptions.RedirectOutput);
}
if (debugConfiguration.sudo) {
this.debugOption(debugOptions, DebugOptions.Sudo);
}
if (this.serviceContainer.get<IPlatformService>(IPlatformService).isWindows) {
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
}
const isFlask = this.isDebuggingFlask(debugConfiguration);
if ((debugConfiguration.pyramid || isFlask)
&& debugOptions.indexOf(DebugOptions.Jinja) === -1
&& debugConfiguration.jinja !== false) {
this.debugOption(debugOptions, DebugOptions.Jinja);
}
if (debugConfiguration.pyramid) {
const utils = this.serviceContainer.get<IConfigurationProviderUtils>(IConfigurationProviderUtils);
debugConfiguration.program = (await utils.getPyramidStartupScriptFilePath(workspaceFolder))!;
}
this.sendTelemetry('launch', debugConfiguration);
}
// tslint:disable-next-line:cyclomatic-complexity
protected async provideAttachDefaults(workspaceFolder: Uri | undefined, debugConfiguration: PythonAttachDebugConfiguration<AttachRequestArguments>): Promise<void> {
await super.provideAttachDefaults(workspaceFolder, debugConfiguration);
const debugOptions = debugConfiguration.debugOptions!;
if (debugConfiguration.debugStdLib) {
this.debugOption(debugOptions, DebugOptions.DebugStdLib);
}
if (debugConfiguration.django) {
this.debugOption(debugOptions, DebugOptions.Django);
}
if (debugConfiguration.jinja) {
this.debugOption(debugOptions, DebugOptions.Jinja);
}
if (debugConfiguration.pyramid
&& debugOptions.indexOf(DebugOptions.Jinja) === -1
&& debugConfiguration.jinja !== false) {
this.debugOption(debugOptions, DebugOptions.Jinja);
}
if (debugConfiguration.redirectOutput || debugConfiguration.redirectOutput === undefined) {
this.debugOption(debugOptions, DebugOptions.RedirectOutput);
}
// We'll need paths to be fixed only in the case where local and remote hosts are the same
// I.e. only if hostName === 'localhost' or '127.0.0.1' or ''
const isLocalHost = this.isLocalHost(debugConfiguration.host);
if (this.serviceContainer.get<IPlatformService>(IPlatformService).isWindows && isLocalHost) {
this.debugOption(debugOptions, DebugOptions.FixFilePathCase);
}
if (this.serviceContainer.get<IPlatformService>(IPlatformService).isWindows) {
this.debugOption(debugOptions, DebugOptions.WindowsClient);
}
if (!debugConfiguration.pathMappings) {
debugConfiguration.pathMappings = [];
}
// This is for backwards compatibility.
if (debugConfiguration.localRoot && debugConfiguration.remoteRoot) {
debugConfiguration.pathMappings!.push({
localRoot: debugConfiguration.localRoot,
remoteRoot: debugConfiguration.remoteRoot
});
}
// If attaching to local host, then always map local root and remote roots.
if (workspaceFolder && debugConfiguration.host &&
debugConfiguration.pathMappings!.length === 0 &&
['LOCALHOST', '127.0.0.1', '::1'].indexOf(debugConfiguration.host.toUpperCase()) >= 0) {
debugConfiguration.pathMappings!.push({
localRoot: workspaceFolder.fsPath,
remoteRoot: workspaceFolder.fsPath
});
}
this.sendTelemetry('attach', debugConfiguration);
}
private debugOption(debugOptions: DebugOptions[], debugOption: DebugOptions) {
if (debugOptions.indexOf(debugOption) >= 0) {
return;
}
debugOptions.push(debugOption);
}
private isLocalHost(hostName?: string) {
const LocalHosts = ['localhost', '127.0.0.1', '::1'];
return (hostName && LocalHosts.indexOf(hostName.toLowerCase()) >= 0) ? true : false;
}
private isDebuggingFlask(debugConfiguration: PythonAttachDebugConfiguration<Partial<LaunchRequestArguments & AttachRequestArguments>>) {
return (debugConfiguration.module && debugConfiguration.module.toUpperCase() === 'FLASK') ? true : false;
}
private sendTelemetry(trigger: 'launch' | 'attach', debugConfiguration: PythonAttachDebugConfiguration<Partial<LaunchRequestArguments & AttachRequestArguments>>) {
const telemetryProps: DebuggerTelemetryV2 = {
trigger,
console: debugConfiguration.console,
hasEnvVars: typeof debugConfiguration.env === 'object' && Object.keys(debugConfiguration.env).length > 0,
django: !!debugConfiguration.django,
flask: this.isDebuggingFlask(debugConfiguration),
hasArgs: Array.isArray(debugConfiguration.args) && debugConfiguration.args.length > 0,
isLocalhost: this.isLocalHost(debugConfiguration.host),
isModule: typeof debugConfiguration.module === 'string' && debugConfiguration.module.length > 0,
isSudo: !!debugConfiguration.sudo,
jinja: !!debugConfiguration.jinja,
pyramid: !!debugConfiguration.pyramid,
stopOnEntry: !!debugConfiguration.stopOnEntry
};
sendTelemetryEvent(DEBUGGER, undefined, telemetryProps);
}
}