-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpublish_fornax.py
More file actions
65 lines (52 loc) · 1.97 KB
/
Copy pathpublish_fornax.py
File metadata and controls
65 lines (52 loc) · 1.97 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
"""
Script to handle Fornax branch deployment with CI
"""
import glob
from pathlib import Path
import shutil
import yaml
FORNAX_NOTEBOOK_BASEPATH = "mast_notebooks"
FORNAX_MANIFEST = "fornax-manifest.yml"
FORNAX_REQUIREMENTS = "requirements_mast_tutorials.txt"
FORNAX_METADATA_YAML = "notebook_metadata.yml"
def main():
"""
Do Fornax branch formatting.
"""
with open(FORNAX_MANIFEST, "r", encoding="utf-8") as f:
manifest = yaml.safe_load(f)
# Fornax-preferred renaming:
Path(FORNAX_NOTEBOOK_BASEPATH).mkdir(parents=True, exist_ok=True)
# Copy & rename requirements:
shutil.copy(
manifest["global-requirements"][0]["requirements-file"],
f"{FORNAX_NOTEBOOK_BASEPATH}/{FORNAX_REQUIREMENTS}"
)
# Copy notebooks over & create reformatted metadata dict:
nbs_metadata = []
for nb in manifest["notebooks"]:
# Copy files:
orig_path = '/'.join(nb['file'].split('/')[:-1])
new_path = orig_path.replace("notebooks/", f"{FORNAX_NOTEBOOK_BASEPATH}/")
shutil.copytree(orig_path, new_path, dirs_exist_ok=True)
# Reformatted metadata:
metadict = {}
for key in ["title", "file", "section", "subsection", "description"]:
val = nb.get(key, None)
if val is not None:
metadict[key] = val
# Update filename:
metadict[key] = metadict[key].replace("notebooks/", f"{FORNAX_NOTEBOOK_BASEPATH}/")
nbs_metadata.append(metadict)
# Remove the individual file reqiurements.txt files:
for file in glob.glob(
f"{FORNAX_NOTEBOOK_BASEPATH}/**/requirements.txt", recursive=True
):
# print(file)
Path(file).unlink(missing_ok=True)
# Write out a stripped down yml file with metadata:
# Title, section, subsection, file
with open(f"{FORNAX_METADATA_YAML}", 'w', encoding="utf-8") as f:
yaml.dump(nbs_metadata, f, default_flow_style=False, sort_keys=False)
if __name__ == "__main__":
main()