-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare-all-notebooks.sh
executable file
·95 lines (79 loc) · 2 KB
/
prepare-all-notebooks.sh
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
#! /usr/bin/env zsh
set -e
setopt -o EXTENDED_GLOB
function with_echo()
{
echo "$@"
"$@"
}
function mkdir_and_cp()
{
dn=$(dirname "$2")
mkdir -p "$dn"
with_echo cp "$1" "$2"
}
unset PYTHONWARNINGS
ME=$(readlink -f "$0")
DIR=$(dirname "$ME")
MYDIR=$(cd "$DIR" && pwd)
if [[ "$1" = "" ]]; then
INPUTDIR="$(pwd)"
else
INPUTDIR="$1"
fi
if [[ "$2" = "" ]]; then
OUTPUTDIR="$(pwd)"
else
OUTPUTDIR="$2"
fi
for nb in $INPUTDIR/*/**/*.ipynb; do
if [[ $nb == upload* ]]; then
continue
fi
if [[ $nb == cleared* ]]; then
continue
fi
DIR="$(dirname "$nb")"
BN="$(basename "$nb")"
RELDIR="$(realpath --relative-to="$INPUTDIR" "$DIR")"
CONV_DIR="$OUTPUTDIR/upload/$RELDIR"
CONV_BASE="$CONV_DIR/${BN%.ipynb}"
CONV_PY="${CONV_BASE}.py"
CONV_HTML="${CONV_BASE}.html"
PROCESSED_IPYNB="${CONV_BASE}.ipynb"
if test -f "$DIR/.do-not-publish"; then
continue
fi
mkdir -p "$CONV_DIR"
if ! test -f "$PROCESSED_IPYNB" || test "$nb" -nt "$PROCESSED_IPYNB"; then
with_echo "$MYDIR/prepare-ipynb" remove-marks "$nb" "$PROCESSED_IPYNB"
fi
if ! test -f "$CONV_PY" || test "$nb" -nt "$CONV_PY"; then
with_echo python -m nbconvert "$PROCESSED_IPYNB" --to=python
fi
if ! test -f "$CONV_HTML" || test "$nb" -nt "$CONV_HTML"; then
with_echo python -m nbconvert "$PROCESSED_IPYNB" --to=html
fi
CONV_DIR="$OUTPUTDIR/cleared/$RELDIR"
mkdir -p "$CONV_DIR"
CONV_IPYNB="$CONV_DIR/$BN"
with_echo "$MYDIR/prepare-ipynb" clear-output clear-marked-inputs "$nb" "$CONV_IPYNB"
done
for i in $INPUTDIR/*/**/*~*ipynb~*.log~*.pyc~*\~(#q.)(#qN); do
DIR="$(dirname "$i")"
RELNAME="$(realpath --relative-to="$INPUTDIR" "$i")"
if test -f "$DIR/.do-not-publish"; then
continue
fi
if [[ "$RELNAME" == upload* ]]; then
continue
fi
if [[ "$RELNAME" == ipython-demo-tools* ]]; then
continue
fi
if [[ "$RELNAME" == cleared* ]]; then
continue
fi
with_echo mkdir_and_cp "$i" "$OUTPUTDIR/cleared/$RELNAME"
with_echo mkdir_and_cp "$i" "$OUTPUTDIR/upload/$RELNAME"
done