-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathzip_guide.py
More file actions
executable file
·87 lines (70 loc) · 2.71 KB
/
zip_guide.py
File metadata and controls
executable file
·87 lines (70 loc) · 2.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
import os, argparse
from pathlib import Path
import sys
import zipfile
from export_code import generated_path, lib_name
import subprocess
import shutil
def delete_checkpoints(folderpath: Path):
for f in folderpath.rglob("*.ipynb_checkpoints"):
if f.is_dir() and f.name == ".ipynb_checkpoints":
print(f" Deleting {f.absolute()}..")
shutil.rmtree(f.absolute())
def clear_notebooks(folderpath: Path):
for f in folderpath.rglob("*.ipynb"):
if not f.is_file():
continue
command = f"jupyter nbconvert --clear-output --inplace '{f.absolute()}'"
subprocess.run(command, shell=True)
def zip_all(path, zip_file):
for f in path.iterdir():
if f.is_file():
zip_file.write(f, f.name)
if f.is_dir():
zipdir(f, zip_file)
def zipdir(path, zip_file, skip_hidden=True):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
if file.startswith(".") and skip_hidden:
continue
zip_file.write(
os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.join(path, ".."))
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(dest="language", help="Language of guide to export")
args = parser.parse_args()
language = args.language
print(
f"""
********************************************
* This script will compile and zip a guide *
* Only run this command from the root of *
* the edunn library *
********************************************
"""
)
guides_folderpath = Path("guides")
releases_folderpath = Path("releases")
guide_folderpath = guides_folderpath / language
if not guide_folderpath.exists():
sys.exit(f"Language {language} not found. Check `guides` folder for available languages.")
print(f"Language *{language}* available.")
print(f"Deleting checkpoints in {guide_folderpath}...")
delete_checkpoints(guide_folderpath)
print(f"Clearing notebooks in {guide_folderpath}...")
clear_notebooks(guide_folderpath)
zip_filepath = releases_folderpath / f"{lib_name}-{language}.zip"
if not generated_path.exists():
sys.exit(f"Code skeleton not found in {generated_path.absolute()}")
print(f"Creating zip file...")
zip_file = zipfile.ZipFile(zip_filepath, "w", zipfile.ZIP_DEFLATED)
print(f"Adding guide to zip...")
zip_all(guide_folderpath, zip_file)
print(f"Adding code to zip...")
zip_all(generated_path, zip_file)
print(f"Saving to file...")
zip_file.close()
print(f"Done: {zip_filepath}")