-
Notifications
You must be signed in to change notification settings - Fork 12
/
debug_script_generator.py
506 lines (443 loc) · 16.2 KB
/
debug_script_generator.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import os
import json
import textwrap
from typing import List, Dict, Any, Optional, Tuple, Union
from metaflow import Step
from metaflow_extensions.netflix_ext.cmd.debug.constants import Constants
from metaflow_extensions.netflix_ext.cmd.debug.debug_utils import fetch_environment_type
from metaflow_extensions.netflix_ext.cmd.debug.debug_stub_generator import (
DebugStubGenerator,
)
class DebugScriptGenerator(object):
def __init__(self, task_pathspec: str, inspect: Union[str, bool]):
"""
Initializes the debugScriptGenerator.
Parameters
----------
task_pathspec : str
The pathspec of the task.
inspect : Union[str, bool]
The inspect flag.
"""
self.task_pathspec = task_pathspec
if isinstance(inspect, bool):
self.inspect = inspect
else:
self.inspect = True if inspect.lower() in ("true", "1") else False
self.debug_stub_generator = DebugStubGenerator(task_pathspec)
def namespace_imports(self, script: str) -> str:
"""
Appends the namespace imports to the script.
Parameters
----------
script : str
The script for the step.
Returns
-------
str
The script with the namespace imports appended.
"""
script += textwrap.dedent(
Constants.CURRENT_TASK_NAMESPACE_STUB.format(
Namespace=self.debug_stub_generator.task_namespace
)
)
return script
def append_imports(self, metaflow_root_dir: str, script: str) -> str:
"""
Appends the imports to the script.
Parameters
----------
metaflow_root_dir : str
The root directory of the Metaflow installation.
script : str
The script for the step.
Returns
-------
str
The script with the imports appended.
"""
script += textwrap.dedent(
Constants.IMPORTS_STUB.format(
MetaflowEnvEscapeDir=os.path.join(
metaflow_root_dir, "_escape_trampolines"
),
MetaflowFileName=self.debug_stub_generator.file_name.split(".")[0],
)
)
return script
def append_debug_stub_instantiation(self, script: str) -> str:
"""
Appends the debug stub generator instantiation to the script.
Parameters
----------
script : str
The script for the step.
Returns
-------
str
The script with the debug stub generator instantiation appended.
"""
script += textwrap.dedent(
Constants.DEBUG_STUB_GENERATOR_INSTANTIATION_TEMPLATE.format(
TaskPathspec=self.task_pathspec
)
)
return script
@staticmethod
def append_stubbed_classes(flow_name: str, script: str) -> str:
"""
Appends the stubs to the script.
Parameters
----------
flow_name : str
The name of the flow.
script : str
The script for the step.
Returns
-------
str
The script with the stubs appended.
"""
script += textwrap.dedent(
Constants.DEBUG_INPUT_ITEM_STUB.format(
FlowSpecClass=flow_name,
)
)
script += textwrap.dedent(
Constants.DEBUG_INPUT_STUB.format(
FlowSpecClass=flow_name,
)
)
script += textwrap.dedent(
Constants.DEBUG_LINEAR_STEP_STUB.format(
FlowSpecClass=flow_name,
)
)
script += textwrap.dedent(
Constants.DEBUG_JOIN_STEP_STUB.format(
FlowSpecClass=flow_name,
)
)
return script
def append_join_step_instantiation(self, script: str) -> str:
"""
Appends the join step instantiation to the script.
Parameters
----------
script : str
The script for the step.
Returns
-------
str
The script with the join step instantiation appended.
"""
template = (
Constants.JOIN_STUB_INSPECT_INSTANTIATION_TEMPLATE
if self.inspect
else Constants.JOIN_STUB_INSTANTIATION_TEMPLATE
)
script += textwrap.dedent(template)
return script
def append_linear_step_instantiation(self, script: str) -> str:
"""
Appends the linear step instantiation to the script.
Parameters
----------
script : str
The script for the step.
Returns
-------
str
The script with the linear step instantiation appended.
"""
template = (
Constants.LINEAR_STUB_INSPECT_INSTANTIATION_TEMPLATE
if self.inspect
else Constants.LINEAR_STUB_INSTANTIATION_TEMPLATE
)
script += textwrap.dedent(template)
return script
def append_start_step_instantiation(self, script: str) -> str:
"""
Appends the start step instantiation to the script.
Parameters
----------
script : str
The script for the step.
Returns
-------
str
The script with the start step instantiation appended.
"""
_run_pathspec = "/".join(self.task_pathspec.split("/")[:2])
prev_task = Step(
os.path.join(_run_pathspec, "_parameters"), _namespace_check=False
).task
template = (
Constants.START_STUB_INSPECT_INSTANTIATION_TEMPLATE
if self.inspect
else Constants.START_STUB_INSTANTIATION_TEMPLATE
)
script += textwrap.dedent(template.format(ParameterTask=prev_task.pathspec))
return script
def append_current_stub_instantiation(self, script: str) -> str:
"""
Appends the current stub instantiation to the script.
Parameters
----------
script : str
The script for the step.
Returns
-------
str
The script with the current stub instantiation appended.
"""
script += textwrap.dedent(
Constants.CURRENT_STUB_INSTANTIATION_TEMPLATE.format(
TaskPathspec=self.task_pathspec
)
)
return script
@staticmethod
def write_debug_script(
metaflow_root_dir: str, debug_script: str, debug_file_name: str
) -> None:
"""
Writes the debug script for the step.
Parameters
----------
metaflow_root_dir : str
The root directory of the Metaflow installation.
debug_script : str
The debug script for the step.
debug_file_name : str
The name of the debug script.
"""
debug_script_path = os.path.join(metaflow_root_dir, debug_file_name)
with open(debug_script_path, "w") as f:
f.write(debug_script)
def generate_debug_script(self, metaflow_root_dir: str, flow_name: str) -> str:
"""
Generates the debug script for the step.
Parameters
----------
metaflow_root_dir : str
The root directory of the Metaflow installation.
flow_name : str
The name of the flow.
Returns
-------
str
The debug script for the step.
"""
print("Generating debug script for task: {}".format(self.task_pathspec))
# We first write out the imports
debug_script = self.append_imports(metaflow_root_dir, "")
# We then write out the namespace imports
debug_script = self.namespace_imports(debug_script)
# We then write out the stubbed classes
debug_script = self.append_stubbed_classes(flow_name, debug_script)
# We instantiate the current stub generator
debug_script = self.append_current_stub_instantiation(debug_script)
# We instantiate the debug stub generator
debug_script = self.append_debug_stub_instantiation(debug_script)
# We then write out the stubs instantiation
if self.debug_stub_generator.step_type == "start":
debug_script = self.append_start_step_instantiation(debug_script)
elif self.debug_stub_generator.step_type == "join":
debug_script = self.append_join_step_instantiation(debug_script)
else:
debug_script = self.append_linear_step_instantiation(debug_script)
return debug_script
def generate_debug_notebook(
self,
metaflow_root_dir: str,
debug_file_name: str,
kernel_def: Optional[Tuple[str, str]] = None,
) -> Dict[str, Any]:
"""
Generates the debug notebook for the task.
Parameters
----------
metaflow_root_dir : str
The root directory of the Metaflow installation.
debug_file_name : str
The name of the debug script
kernel_def : Optional[Tuple[str, str]], optional, default None
The kernel definition containing the kernel name and the kernel display name.
Returns
-------
Dict[str, Any]
The dictionary representing the debug notebook for the task.
"""
# We first generate the notebook cells
notebook_cells = self.generate_notebook_cells(
metaflow_root_dir, debug_file_name
)
# We then generate the notebook JSON
notebook_json = self.generate_notebook_json(notebook_cells, kernel_def)
return notebook_json
@staticmethod
def write_debug_notebook(
metaflow_root_dir: str, notebook_json: Dict[str, Any]
) -> None:
"""
Writes the debug notebook for the task.
Parameters
----------
metaflow_root_dir : str
The root directory of the Metaflow installation.
notebook_json : Dict[str, Any]
The name of the debug notebook.
"""
# We then write out the notebook JSON
debug_notebook_name = "00_DEBUG_ME.ipynb"
debug_notebook_path = os.path.join(metaflow_root_dir, debug_notebook_name)
with open(debug_notebook_path, "w") as f:
json.dump(notebook_json, f, indent=4)
@staticmethod
def generate_notebook_json(
notebook_cells: List[Dict[str, Any]],
kernel_def: Optional[Tuple[str, str]] = None,
) -> Dict[str, Any]:
"""
Generates the notebook JSON for the task.
Parameters
----------
notebook_cells : List[Dict[str, Any]]
The list of notebook cells.
kernel_def : Optional[Tuple[str, str]], optional, default None
The kernel definition containing the kernel name and the kernel display name.
Returns
-------
Dict[str, Any]
The notebook JSON for the task.
"""
# Create an empty notebook
nb = {
"cells": [],
"metadata": (
{}
if kernel_def is None
else {
"kernelspec": {
"name": kernel_def[0],
"display_name": kernel_def[1],
"language": "python",
}
}
),
"nbformat": 4,
"nbformat_minor": 4,
}
# Add cells to the notebook
for cell in notebook_cells:
nb_cell = {
"cell_type": cell.get("format"),
"execution_count": None,
"metadata": {},
"outputs": [],
"source": [cell.get("content")],
}
nb["cells"].append(nb_cell)
return nb
def get_function_definition(self, metaflow_root_dir: str) -> Tuple[str, int]:
"""
Returns the function definition and the end line number for the task.
Parameters
----------
metaflow_root_dir : str
The root directory of the Metaflow installation.
Returns
-------
Tuple[str, int]
The function definition and the end line number for the task.
"""
with open(
os.path.join(metaflow_root_dir, self.debug_stub_generator.file_name), "r"
) as f:
step_started = False
function_definition = ""
is_step_end = self.debug_stub_generator.task.parent.id == "end"
start_indentation = None
for i, line in enumerate(f):
if i == self.debug_stub_generator.task_line_num:
step_started = True
start_indentation = len(line) - len(line.lstrip())
if step_started:
current_indentation = len(line) - len(line.lstrip())
if "self.next(self." in line:
function_definition += line
return function_definition, i
# Special case for the end step, as there is no next step and we need to
# return the function definition at the end of the file or when the function ends
if is_step_end:
if line.strip() and current_indentation < start_indentation:
return function_definition, i - 1
function_definition += line
# If we reach here, it means that the step is the last step in the flow
# and there is no other user code after this step
return function_definition, i
def generate_notebook_cells(
self, metaflow_root_dir: str, debug_file_name: str
) -> List[Dict[str, Any]]:
"""
Generates the notebook cells for the task.
Parameters
----------
metaflow_root_dir : str
The root directory of the Metaflow installation.
debug_file_name : str
The name of the debug notebook.
Returns
-------
str
The notebook cells for the task.
"""
# We first add a markdown cell stating that it is a debugging notebook
notebook_cells = [
{
"format": "markdown",
"content": Constants.JUPYTER_TITLE_MARKDOWN.format(
TaskPathSpec=self.task_pathspec,
DebugType=(
"Post-Execution State"
if self.inspect
else "Pre-Execution State"
),
),
}
]
# We now add a code cell that incorporates the Metaflow escape hatch
# We only add this if the user code ran in a conda environment
if fetch_environment_type(self.debug_stub_generator.task) != "default":
script = textwrap.dedent(
Constants.ESCAPE_HATCH_STUB.format(
MetaflowEnvEscapeDir=os.path.abspath(
os.path.join(metaflow_root_dir, "_escape_trampolines")
)
)
)
notebook_cells.append({"format": "code", "content": script.strip()})
# We now add a code cell that imports the debugging modules
script = "from {} import * ".format(debug_file_name.split(".")[0])
notebook_cells.append({"format": "code", "content": script})
# We now add a markdown cell that tells the user to add their own code
flow_file_name = self.debug_stub_generator.file_name
start_line_num = self.debug_stub_generator.task_line_num
function_definition, end_line_num = self.get_function_definition(
metaflow_root_dir
)
markdown_content = Constants.JUPYTER_INSTRUCTIONS_MARKDOWN.format(
FileName=flow_file_name,
StepStartLine=start_line_num,
StepEndLine=end_line_num,
FunctionDefinition=textwrap.dedent(function_definition),
)
notebook_cells.append(
{"format": "markdown", "content": textwrap.dedent(markdown_content.strip())}
)
# Add an empty cell for the user to add their own code
notebook_cells.append({"format": "code", "content": ""})
return notebook_cells