-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathRunCommands.ts
More file actions
255 lines (236 loc) · 9.26 KB
/
Copy pathRunCommands.ts
File metadata and controls
255 lines (236 loc) · 9.26 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import {commands, debug, ExtensionContext, Uri, window} from "vscode";
import {ConnectionManager} from "../configuration/ConnectionManager";
import {FileUtils} from "../utils";
import {LocalUri} from "../sync/SyncDestination";
import {DatabricksPythonDebugConfiguration} from "./DatabricksDebugConfigurationProvider";
import {MsPythonExtensionWrapper} from "../language/MsPythonExtensionWrapper";
import path from "path";
import {FeatureManager, FeatureState} from "../feature-manager/FeatureManager";
import {
escapeExecutableForTerminal,
escapePathArgument,
} from "../utils/shellUtils";
import {CustomWhenContext} from "../vscode-objs/CustomWhenContext";
import {WorkspaceFolderManager} from "../vscode-objs/WorkspaceFolderManager";
import {Events, Telemetry} from "../telemetry";
/**
* Run related commands
*/
export class RunCommands {
constructor(
private connection: ConnectionManager,
private readonly workspaceFolderManager: WorkspaceFolderManager,
private readonly pythonExtension: MsPythonExtensionWrapper,
private readonly featureManager: FeatureManager,
private readonly context: ExtensionContext,
private readonly customWhenContext: CustomWhenContext,
private readonly telemetry: Telemetry
) {
this.context.subscriptions.push(
window.onDidChangeActiveTextEditor(async () =>
this.updateRunAsWorkflowContext()
)
);
this.updateRunAsWorkflowContext();
}
async updateRunAsWorkflowContext() {
const uri = window.activeTextEditor?.document.uri;
if (
uri &&
uri.scheme === "file" &&
((await FileUtils.isNotebook(new LocalUri(uri))) ||
uri.fsPath.endsWith(".py"))
) {
this.customWhenContext.setShowRunAsWorkflow(true);
} else {
this.customWhenContext.setShowRunAsWorkflow(false);
}
}
/**
* Run a Python file using the command execution API
*/
runEditorContentsCommand() {
return async (resource?: Uri) => {
const targetResource = this.getTargetResource(resource);
if (targetResource) {
if (await FileUtils.isNotebook(new LocalUri(targetResource))) {
await window.showErrorMessage(
'Use "Run File as Workflow on Databricks" for running notebooks'
);
return;
}
if (this.connection.state === "CONNECTING") {
await this.connection.waitForConnect();
}
await debug.startDebugging(
undefined,
{
type: "databricks",
name: "Upload and Run File on Databricks",
request: "launch",
program: targetResource.fsPath,
},
{noDebug: true}
);
}
};
}
/**
* Run a Python file or notebook as a workflow on the connected cluster
*/
runEditorContentsAsWorkflowCommand() {
return async (resource?: Uri) => {
const targetResource = this.getTargetResource(resource);
if (targetResource) {
if (this.connection.state === "CONNECTING") {
await this.connection.waitForConnect();
}
if (this.connection.syncDestinationMapper === undefined) {
throw new Error(
"No sync destination found. Maybe the databricks.yml is misconfgured."
);
}
await debug.startDebugging(
undefined,
{
type: "databricks-workflow",
name: "Run File on Databricks as Workflow",
request: "launch",
program: targetResource.fsPath,
},
{noDebug: true}
);
}
};
}
private async checkDbconnectEnabled() {
let featureState = await this.featureManager.isEnabled(
"environment.dependencies"
);
if (
featureState.available &&
// Re-checks block on an established connection, so don't force
// one while disconnected.
this.connection.state === "CONNECTED" &&
(await this.isPythonEnvironmentStale(featureState))
) {
// The Python extension doesn't always notify us about interpreter
// changes, so the cached state can refer to a previously selected
// interpreter. Re-check before launching anything with it.
featureState = await this.featureManager.isEnabled(
"environment.dependencies",
true
);
}
if (featureState.available) {
return true;
}
// Run the setup flow, then re-check: a successful setup should let the
// launch proceed instead of aborting and making the user re-trigger.
await commands.executeCommand("databricks.environment.setup");
return (await this.featureManager.isEnabled("environment.dependencies"))
.available;
}
private async isPythonEnvironmentStale(featureState: FeatureState) {
// For an accepted checkPythonEnvironment step the message holds
// the path of the verified python executable.
const verifiedExecutable = featureState.steps.get(
"checkPythonEnvironment"
)?.message;
const currentExecutable =
await this.pythonExtension.getPythonExecutable();
return (
verifiedExecutable !== undefined &&
verifiedExecutable !== currentExecutable
);
}
private getTargetResource(resource?: Uri): Uri | undefined {
if (!resource && window.activeTextEditor) {
return window.activeTextEditor.document.uri;
}
return resource;
}
/**
* Shared preflight for the run and debug Databricks Connect launchers:
* verifies the environment, resolves the target file, and resolves the
* python interpreter. Run and debug must use the *same* interpreter, so it
* is resolved here in one place. Returns undefined (after surfacing an
* error message) when any step fails.
*/
private async resolveDbconnectLaunch(
resource?: Uri
): Promise<{targetResource: Uri; executable: string} | undefined> {
if (!(await this.checkDbconnectEnabled())) {
return undefined;
}
const targetResource = this.getTargetResource(resource);
if (!targetResource) {
window.showErrorMessage("Open a file to run on Databricks");
return undefined;
}
const executable = await this.pythonExtension.getPythonExecutable();
if (!executable) {
window.showErrorMessage("No python executable found");
return undefined;
}
return {targetResource, executable};
}
createDbconnectDebugConfig(
targetResource: Uri,
executable: string
): DatabricksPythonDebugConfiguration {
return {
program: targetResource.fsPath,
type: "debugpy",
name: "Databricks Connect",
request: "launch",
databricks: true,
console: "integratedTerminal",
env: {...process.env},
// Pin debugpy to the interpreter we have verified. Without this
// debugpy falls back to the Python extension's selected
// interpreter for the workspace folder, which can differ from the
// environment used by "run" and by the verification checks.
python: executable,
};
}
async debugFileUsingDbconnect(resource?: Uri) {
const launch = await this.resolveDbconnectLaunch(resource);
if (!launch) {
return;
}
const config = this.createDbconnectDebugConfig(
launch.targetResource,
launch.executable
);
await debug.startDebugging(
this.workspaceFolderManager.activeWorkspaceFolder,
config
);
this.telemetry.recordEvent(Events.DBCONNECT_RUN, {
launchType: "debug",
computeType: this.connection.serverless ? "serverless" : "cluster",
});
}
async runFileUsingDbconnect(resource?: Uri) {
const launch = await this.resolveDbconnectLaunch(resource);
if (!launch) {
return;
}
const {targetResource, executable} = launch;
const terminal = window.activeTerminal ?? window.createTerminal();
const bootstrapPath = this.context.asAbsolutePath(
path.join("resources", "python", "dbconnect-bootstrap.py")
);
terminal.show();
terminal.sendText(
`${escapeExecutableForTerminal(executable)} ${escapePathArgument(
bootstrapPath
)} ${escapePathArgument(targetResource.fsPath)}`
);
this.telemetry.recordEvent(Events.DBCONNECT_RUN, {
launchType: "run",
computeType: this.connection.serverless ? "serverless" : "cluster",
});
}
}