forked from Azure/azureml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.py
298 lines (258 loc) · 7.65 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
# imports
import os
import json
import glob
import argparse
# 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()
# constants, variables, parameters, etc.
with open("prefix.md", "r") as f:
prefix = f.read()
with open("suffix.md", "r") as f:
suffix = f.read()
tutorial_table = """
**Tutorials**
path|status|notebooks|description
-|-|-|-
"""
notebook_table = """
**Notebooks**
path|description
-|-
"""
train_table = """
**Train**
path|compute|environment|description
-|-|-|-
"""
deploy_table = """
**Deploy**
path|compute|description
-|-|-
"""
ws = "default"
rg = "azureml-examples"
mn = "${{matrix.notebook}}"
mw = "${{matrix.workflow}}"
cr = "${{secrets.AZ_AE_CREDS}}"
kernelspec = {"display_name": "Python 3.8", "language": "python", "name": "python3.8"}
# process tutorials/*
tutorials = sorted(glob.glob("tutorials/*"))
for tutorial in tutorials:
# get list of notebooks
nbs = sorted(
[nb.split("/")[-1] for nb in glob.glob(f"{tutorial}/*.ipynb")]
) # TODO: fix for Windows
nbs = [f"[{nb}]({tutorial}/{nb})" for nb in nbs] # TODO: fix for Windows
nbs = "<br>".join(nbs)
# get the tutorial name and initials
name = tutorial.split("/")[-1] # TODO: fix for Windows
initials = "".join([word[0][0] for word in name.split("-")])
# build entries for tutorial table
status = f"[![{name}](https://github.com/Azure/azureml-examples/workflows/run-tutorial-{initials}/badge.svg)](https://github.com/Azure/azureml-examples/actions?query=workflow%3Arun-tutorial-{initials})"
desc = "*no description*"
try:
with open(f"{tutorial}/README.md", "r") as f:
for line in f.readlines():
if "description: " in str(line):
desc = line.split(": ")[-1].strip()
break
except:
pass
# add row to tutorial table
tutorial_table += f"[{name}]({tutorial})|{status}|{nbs}|{desc}\n"
# process notebooks/*
notebooks = sorted(glob.glob("notebooks/*.ipynb"))
# create `run-workflows` workflow yaml file
workflow = f"""name: run-notebooks
on:
schedule:
- cron: "0 0/2 * * *"
push:
branches:
- main
paths:
- "notebooks/**"
pull_request:
branches:
- main
paths:
- "notebooks/**"
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
notebook: {notebooks}
steps:
- name: check out repo
uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with:
python-version: "3.8"
- name: pip install
run: pip install -r requirements.txt
- name: azure login
uses: azure/login@v1
with:
creds: {cr}
- name: install azmlcli
run: az extension add -s https://azurecliext.blob.core.windows.net/release/azure_cli_ml-1.15.0-py3-none-any.whl -y
- name: attach to workspace
run: az ml folder attach -w {ws} -g {rg}
- name: run notebook
run: papermill {mn} out.ipynb -k python
"""
# write `run-notebooks` workflow yaml file
print("writing workflow file...")
with open(f".github/workflows/run-notebooks.yml", "w") as f:
f.write(workflow)
# create notebook_table
for nb in notebooks:
# read in notebook
with open(nb, "r") as f:
data = json.load(f)
# read in the description
try:
if "description: " in str(data["cells"][0]["source"]):
desc = (
str(data["cells"][0]["source"])
.split("description: ")[-1]
.replace("']", "")
.strip()
)
except:
desc = "*no description*"
# build tables
notebook_table += f"[{nb}]({nb})|{desc}\n"
# process code/azureml/*
workflows = sorted(glob.glob("workflows/**/*/job.py", recursive=True))
# create `run-workflows` workflow yaml file
workflow = f"""name: run-workflows
on:
schedule:
- cron: "0 0/2 * * *"
push:
branches:
- main
paths:
- "workflows/**"
pull_request:
branches:
- main
paths:
- "workflows/**"
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
workflow: {workflows}
steps:
- name: check out repo
uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with:
python-version: "3.8"
- name: pip install
run: pip install -r requirements.txt
- name: azure login
uses: azure/login@v1
with:
creds: {cr}
- name: install azmlcli
run: az extension add -s https://azurecliext.blob.core.windows.net/release/azure_cli_ml-1.15.0-py3-none-any.whl -y
- name: attach to workspace
run: az ml folder attach -w {ws} -g {rg}
- name: run workflow
run: python {mw}
"""
# write `run-workflows` workflow yaml file
print("writing workflow file...")
with open(f".github/workflows/run-workflows.yml", "w") as f:
f.write(workflow)
# create example tables
for wf in workflows:
# read in example
with open(wf, "r") as f:
data = f.read()
# read in the description
try:
desc = data.split("\n")[0].split(": ")[-1].strip()
except:
desc = "*no description*"
# build tables
if "train" in wf:
# parse for compute target
if "cpu-cluster" in data:
compute = "AML - CPU"
elif "gpu-cluster" in data or "gpu-K80" in data or "gpu-V100" in data:
compute = "AML - GPU"
else:
compute = "unknown"
# parse for environment type
if "Environment.from_pip_requirements" in data:
environment = "pip"
elif "Environment.from_conda_specification" in data:
environment = "conda"
elif "env.docker.base_dockerfile" in data:
environment = "docker"
elif "mlproject" in wf:
environment = "mlproject"
else:
environment = "unknown"
train_table += f"[{wf}]({wf})|{compute}|{environment}|{desc}\n"
elif "deploy" in wf:
if "aci-cpu" in wf:
compute = "ACI - CPU"
elif "aks-cpu" in wf:
compute = "AKS - CPU"
elif "aks-gpu" in wf:
compute = "AKS - GPU"
elif "local" in wf:
compute = "local"
else:
compute = "unknown"
deploy_table += f"[{wf}]({wf})|{compute}|{desc}\n"
# glob all notebooks
notebooks = sorted(glob.glob("**/**/*.ipynb", recursive=True))
# process all notebooks and rewrite
for nb in notebooks:
# read in notebook
with open(nb, "r") as f:
data = json.load(f)
# update metadata
data["metadata"]["kernelspec"] = kernelspec
# write notebook
with open(nb, "w") as f:
json.dump(data, f, indent=1)
# run code formatter on .py files
os.system("black .")
# run code formatter on .ipynb files
os.system("black-nb --clear-output .")
# read in README.md for comparison
with open("README.md", "r") as f:
before = f.read()
# write README.md file
print("writing README.md...")
with open("README.md", "w") as f:
f.write(
prefix + tutorial_table + notebook_table + train_table + deploy_table + suffix
)
# read in README.md for comparison
with open("README.md", "r") as f:
after = f.read()
# check if README.md file matches before and after
if args.check_readme and before != after:
print("README.md file did not match...")
exit(2)