forked from Azure/azureml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.py
371 lines (315 loc) · 12 KB
/
readme.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
# imports
import os
import json
import glob
import argparse
# define functions
def main(args):
# get list of tutorials
tutorials = sorted(glob.glob("tutorials/*", recursive=False))
# get list of notebooks
notebooks = sorted(glob.glob("notebooks/**/*.ipynb", recursive=True))
# get list of workflows
workflows = sorted(glob.glob("workflows/**/*job*.py", recursive=True))
# get a list of ALL notebooks, including tutorials and experimental
all_notebooks = sorted(glob.glob("**/*.ipynb", recursive=True))
# get list of experimental tutorials
experimental = sorted(glob.glob("experimental/*", recursive=False))
# make all notebooks consistent
modify_notebooks(all_notebooks)
# format code
format_code()
# write workflows
write_workflows(notebooks, workflows)
# read existing README.md
with open("README.md", "r") as f:
readme_before = f.read()
# write README.md
write_readme(tutorials, notebooks, workflows, experimental)
# read modified README.md
with open("README.md", "r") as f:
readme_after = f.read()
# check if readme matches
if args.check_readme:
if not check_readme(readme_before, readme_after):
print("README.md file did not match...")
exit(2)
def write_readme(tutorials, notebooks, workflows, experimental):
# read in prefix.md and suffix.md
with open("prefix.md", "r") as f:
prefix = f.read()
with open("suffix.md", "r") as f:
suffix = f.read()
# define markdown tables
tutorial_table = "\n**Tutorials** ([tutorials](tutorials))\n\npath|status|notebooks|description\n-|-|-|-\n"
notebook_table = (
"\n**Notebooks** ([notebooks](notebooks))\n\npath|status|description\n-|-|-\n"
)
train_table = "\n**Train** ([workflows/train](workflows/train))\n\npath|status|description\n-|-|-\n"
deploy_table = "\n**Deploy** ([workflows/deploy](workflows/deploy))\n\npath|status|description\n-|-|-\n"
experimental_table = "\n**Experimental tutorials** ([experimental](experimental))\n\npath|status|notebooks|description|why experimental?\n-|-|-|-|-\n"
# process tutorials
for tutorial in tutorials + experimental:
# get list of notebooks
nbs = sorted([nb for nb in glob.glob(f"{tutorial}/**/*.ipynb", recursive=True)])
notebook_names = [nb.split("/")[-1].replace(".ipynb", "") for nb in nbs]
nbs = [f"[{nb.split('/')[-1]}]({nb})" for nb in nbs]
nbs = "<br>".join(nbs)
# get tutorial name
name = tutorial.split("/")[-1]
# build entries for tutorial table
if os.path.exists(f"../../.github/workflows/python-sdk-tutorial-{name}.yml"):
# we can have a single GitHub workflow for handling all notebooks within this tutorial folder
status = f"[![{name}](https://github.com/Azure/azureml-examples/workflows/python-sdk-tutorial-{name}/badge.svg?branch=main)](https://github.com/Azure/azureml-examples/actions/workflows/python-sdk-tutorial-{name}.yml)"
else:
# or, we could have dedicated workflows for each individual notebook contained within this tutorial folder
statuses = [
f"[![{name}](https://github.com/Azure/azureml-examples/workflows/{name}/badge.svg?branch=main)](https://github.com/Azure/azureml-examples/actions/workflows/python-sdk-tutorial-{name}.yml)"
for name in notebook_names
]
status = "<br>".join(statuses)
description = "*no description*"
try:
with open(f"{tutorial}/README.md", "r") as f:
for line in f.readlines():
if "description: " in str(line):
description = line.split(": ")[-1].strip()
break
except:
pass
# additional logic for experimental tutorials
if "experimental" in tutorial:
reason = "*unknown*"
try:
with open(f"{tutorial}/README.md", "r") as f:
for line in f.readlines():
if "experimental: " in str(line):
reason = line.split(": ")[-1].strip()
break
except:
pass
# add row to experimental tutorial table
row = f"[{name}]({tutorial})|{status}|{nbs}|{description}|{reason}\n"
experimental_table += row
else:
# add row to tutorial table
row = f"[{name}]({tutorial})|{status}|{nbs}|{description}\n"
tutorial_table += row
# process notebooks
for notebook in notebooks:
# get notebook name
name = notebook.split("/")[-1].replace(".ipynb", "")
# read in notebook
with open(notebook, "r") as f:
data = json.load(f)
# build entries for notebook table
status = f"[![{name}](https://github.com/Azure/azureml-examples/workflows/python-sdk-notebook-{name}/badge.svg?branch=main)](https://github.com/Azure/azureml-examples/actions/workflows/python-sdk-notebook-{name}.yml)"
description = "*no description*"
try:
if "description: " in str(data["cells"][0]["source"]):
description = (
str(data["cells"][0]["source"])
.split("description: ")[-1]
.replace("']", "")
.strip()
)
except:
pass
# add row to notebook table
row = f"[{name}.ipynb]({notebook})|{status}|{description}\n"
notebook_table += row
# process workflows
for workflow in workflows:
# get the workflow scenario, tool, project, and name
scenario = workflow.split("/")[1]
tool = workflow.split("/")[2]
project = workflow.split("/")[3]
name = workflow.split("/")[4].replace(".py", "")
# read in workflow
with open(workflow, "r") as f:
data = f.read()
# build entires for workflow tables
status = f"[![{scenario}-{tool}-{project}-{name}](https://github.com/Azure/azureml-examples/workflows/python-sdk-{scenario}-{tool}-{project}-{name}/badge.svg?branch=main)](https://github.com/Azure/azureml-examples/actions/workflows/python-sdk-{scenario}-{tool}-{project}-{name}.yml)"
description = "*no description*"
try:
description = data.split("\n")[0].split(": ")[-1].strip()
except:
pass
# add row to workflow table
row = f"[{tool}/{project}/{name}.py]({workflow})|{status}|{description}\n"
if scenario == "train":
train_table += row
elif scenario == "deploy":
deploy_table += row
else:
print("new scenario! modifications needed...")
exit(3)
# write README.md
print("writing README.md...")
with open("README.md", "w") as f:
f.write(
prefix
+ tutorial_table
+ notebook_table
+ train_table
+ deploy_table
+ experimental_table
+ suffix
)
def write_workflows(notebooks, workflows):
# process notebooks
for notebook in notebooks:
# get notebook name
name = notebook.split("/")[-1].replace(".ipynb", "")
# write workflow file
write_notebook_workflow(notebook, name)
# process workflows
for workflow in workflows:
# get the workflow scenario, tool, project, and name
scenario = workflow.split("/")[1]
tool = workflow.split("/")[2]
project = workflow.split("/")[3]
name = workflow.split("/")[4].replace(".py", "")
# write workflow file
write_python_workflow(workflow, scenario, tool, project, name)
def check_readme(before, after):
return before == after
def modify_notebooks(notebooks):
# setup variables
kernelspec3_8 = {
"display_name": "Python 3.8 - AzureML",
"language": "python",
"name": "python38-azureml",
}
kernelspec3_6 = {
"display_name": "Python 3.6 - AzureML",
"language": "python",
"name": "python3-azureml",
}
# for each notebooks
for notebook in notebooks:
# read in notebook
with open(notebook, "r") as f:
data = json.load(f)
# update metadata
if "automl-with-azureml" in notebook:
data["metadata"]["kernelspec"] = kernelspec3_6
else:
data["metadata"]["kernelspec"] = kernelspec3_8
# write notebook
with open(notebook, "w") as f:
json.dump(data, f, indent=1)
def format_code():
# run code formatter on .py files
os.system("black .")
# run code formatter on .ipynb files
os.system("black-nb --clear-output .")
def write_notebook_workflow(notebook, name):
creds = "${{secrets.AZ_AE_CREDS}}"
workflow_yaml = f"""name: python-sdk-notebook-{name}
on:
schedule:
- cron: "0 */8 * * *"
pull_request:
branches:
- main
paths:
- v1/python-sdk/{notebook}
- .github/workflows/python-sdk-notebook-{name}.yml
- v1/python-sdk/requirements.txt
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: check out repo
uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with:
python-version: "3.8"
- name: Run Install packages
run: |
chmod +x ./v1/scripts/install-packages.sh
./v1/scripts/install-packages.sh
shell: bash
- name: pip install
run: pip install -r v1/python-sdk/requirements.txt
- name: azure login
uses: azure/login@v1
with:
creds: {creds}
- name: Run update-azure-extensions
run: |
chmod +x ./v1/scripts/update-azure-extensions.sh
./v1/scripts/update-azure-extensions.sh
shell: bash
- name: attach to workspace
run: az ml folder attach -w main-python-sdk -g azureml-examples-rg
- name: run notebook
run: papermill v1/python-sdk/{notebook} out.ipynb -k python\n"""
# write workflow
with open(f"../../.github/workflows/python-sdk-notebook-{name}.yml", "w") as f:
f.write(workflow_yaml)
def write_python_workflow(workflow, scenario, tool, project, name):
creds = "${{secrets.AZ_AE_CREDS}}"
workflow_yaml = f"""name: python-sdk-{scenario}-{tool}-{project}-{name}
on:
schedule:
- cron: "0 */8 * * *"
pull_request:
branches:
- main
paths:
- v1/python-sdk/workflows/{scenario}/{tool}/{project}/**
- .github/workflows/python-sdk-{scenario}-{tool}-{project}-{name}.yml
- v1/python-sdk/requirements.txt
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: check out repo
uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with:
python-version: "3.8"
- name: Run Install packages
run: |
chmod +x ./v1/scripts/install-packages.sh
./v1/scripts/install-packages.sh
shell: bash
- name: pip install
run: pip install -r v1/python-sdk/requirements.txt
- name: azure login
uses: azure/login@v1
with:
creds: {creds}
- name: Run update-azure-extensions
run: |
chmod +x ./v1/scripts/update-azure-extensions.sh
./v1/scripts/update-azure-extensions.sh
shell: bash
- name: attach to workspace
run: az ml folder attach -w main-python-sdk -g azureml-examples-rg
- name: run workflow
run: python v1/python-sdk/{workflow}\n"""
# write workflow
with open(
f"../../.github/workflows/python-sdk-{scenario}-{tool}-{project}-{name}.yml",
"w",
) as f:
f.write(workflow_yaml)
# run functions
if __name__ == "__main__":
# issue #146
if "posix" not in os.name:
print(
"windows is not supported, see issue #146 (https://github.com/Azure/azureml-examples/issues/146)"
)
exit(1)
# setup argparse
parser = argparse.ArgumentParser()
parser.add_argument("--check-readme", type=bool, default=False)
args = parser.parse_args()
# call main
main(args)