forked from Azure/azureml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.py
274 lines (229 loc) · 7.62 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
# imports
import os
import json
import glob
import argparse
# define functions
def main(args):
# get list of notebooks
notebooks = sorted(glob.glob("**/*.ipynb", recursive=True))
# make all notebooks consistent
modify_notebooks(notebooks)
# get list of directories
notebook_dirs = sorted(glob.glob("*/", recursive=True))
# write workflows
write_workflows(notebook_dirs)
# read existing README.md
with open("README.md", "r") as f:
readme_before = f.read()
# write README.md
write_readme(notebook_dirs)
# 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)
# format code
format_code()
def format_code():
# TODO - update here
pass
# os.system("black .")
# os.system("black-nb --clear-output .")
def write_readme(notebook_dirs):
# 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
notebook_table = (
"\n**Notebooks**\n\ndirectory|notebooks|status|description\n-|-|-|-\n"
)
# process notebooks
for notebook_dir in notebook_dirs:
# get list of notebooks
notebooks = sorted(glob.glob(f"{notebook_dir}/*.ipynb"))
notebooks = [notebook.split("/")[-1] for notebook in notebooks]
# get notebook name
name = notebook_dir.strip("/")
# build entries for notebook table
status = f"[![{name}](https://github.com/Azure/azureml-examples/workflows/notebooks-{name}/badge.svg?branch=main)](https://github.com/Azure/azureml-examples/actions/workflows/notebooks-{name}.yml)"
# read description if given in README
description = "*no description*"
try:
with open(f"{notebook_dir}/README.md", "r") as f:
for line in f.readlines():
if "description: " in str(line):
description = line.split(": ")[-1].strip()
break
except:
pass
# add row to notebook table
row = f"[{name}]({name})|{'<br>'.join(notebooks)}|{status}|{description}\n"
notebook_table += row
# write README.md
print("writing README.md...")
with open("README.md", "w") as f:
f.write(prefix + notebook_table + suffix)
def write_workflows(notebook_dirs):
# process notebooks
for notebook_dir in notebook_dirs:
# read is_parallel if given in README
is_parallel = False
try:
with open(f"{notebook_dir}/README.md", "r") as f:
for line in f.readlines():
if "is_parallel: " in str(line):
is_parallel = bool(line.split(": ")[-1].strip())
break
except:
pass
# write workflow file
if is_parallel:
write_notebook_workflow_parallel(notebook_dir)
else:
write_notebook_workflow_sequential(notebook_dir)
def check_readme(before, after):
return before == after
def modify_notebooks(notebooks):
# setup variables
kernelspec = {
"display_name": "Python 3.8 - AzureML",
"language": "python",
"name": "python38-azureml",
}
# for each notebooks
for notebook in notebooks:
# read in notebook
with open(notebook, "r") as f:
data = json.load(f)
# update metadata
data["metadata"]["kernelspec"] = kernelspec
# write notebook
with open(notebook, "w") as f:
json.dump(data, f, indent=1)
def write_notebook_workflow_sequential(notebook_dir):
notebook_dir = notebook_dir.strip("/")
notebooks = sorted(glob.glob(f"{notebook_dir}/*.ipynb"))
notebooks = [notebook.split("/")[-1] for notebook in notebooks]
creds = "${{secrets.AZ_CREDS}}"
workflow_yaml = f"""name: notebooks-{notebook_dir}
on:
schedule:
- cron: "0 */8 * * *"
pull_request:
branches:
- main
paths:
- v1/notebooks/{notebook_dir}/**
- .github/workflows/notebooks-{notebook_dir}.yml
- v1/notebooks/dev-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 notebook reqs
run: pip install -r v1/notebooks/dev-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"""
for notebook in notebooks:
workflow_yaml += f"""
- name: run {notebook}
run: papermill {notebook} - -k python
working-directory: v1/notebooks/{notebook_dir}\n"""
# write workflow
with open(f"../../.github/workflows/notebooks-{notebook_dir}.yml", "w") as f:
f.write(workflow_yaml)
def write_notebook_workflow_parallel(notebook_dir):
notebook_dir = notebook_dir.strip("/")
notebooks = sorted(glob.glob(f"{notebook_dir}/*.ipynb"))
notebooks = [notebook.split("/")[-1] for notebook in notebooks]
matrix_notebook = "${{matrix.notebook}}"
creds = "${{secrets.AZ_CREDS}}"
workflow_yaml = f"""name: notebooks-{notebook_dir}
on:
schedule:
- cron: "0 */8 * * *"
pull_request:
branches:
- main
paths:
- v1/notebooks/{notebook_dir}/**
- .github/workflows/notebooks-{notebook_dir}.yml
- v1/notebooks/dev-requirements.txt
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
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: Run Install packages
run: |
chmod +x ./v1/scripts/install-packages.sh
./v1/scripts/install-packages.sh
shell: bash
- name: pip install notebook reqs
run: pip install -r v1/notebooks/dev-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
- name: run {matrix_notebook}
run: papermill {matrix_notebook} - -k python
working-directory: v1/notebooks/{notebook_dir}\n"""
# write workflow
with open(f"../../.github/workflows/notebooks-{notebook_dir}.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)