forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trust-doc-nbs
executable file
·47 lines (36 loc) · 1.65 KB
/
trust-doc-nbs
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
#!/usr/bin/env python3
# This script signs all notebooks in the examples directory as trusted
# It's is used as a git post-merge hook
import nbformat.sign, sys, os, os.path
from pathlib import Path
# make sure we are under the root of the project
cur_dir = Path(".").resolve().name
if (cur_dir == "tools"): os.chdir("..")
# Be almost invisible when run as a git hook, to keep the `git pull`
# output familiar, yet telling the user that his system is busy
# running the script.
# We know we run under git when this script is invoked as
# .git/hooks/post-merge. When it is invoked as tools/trust-doc-nbs we
# can be more verbose.
stealth = True if "post-merge" in __file__ else False
end = "\r" if stealth else "\n"
def trust_nbs(dirname):
print(f"Attention! Signing notebooks under {dirname} as *trusted*", end=end)
path = Path(dirname)
fname_last_checked = path/".last_checked"
last_checked = os.path.getmtime(fname_last_checked) if fname_last_checked.exists() else None
for fname in path.glob("*.ipynb"):
# speed up things by skipping the if-not-signed test if the nb hasn't changed since the last signing attempt
if last_checked:
last_changed = os.path.getmtime(fname)
if last_changed < last_checked: continue
with open(fname) as f:
nb = nbformat.read(f, as_version=4)
if not nbformat.sign.NotebookNotary().check_signature(nb):
#print(f"unsigned {fname}")
nbformat.sign.NotebookNotary().sign(nb)
fname_last_checked.touch(exist_ok=True)
trust_nbs('examples')
trust_nbs('docs_src')
# clear the temporary output
if stealth: sys.stdout.write("\033[K")