-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashrc-utils
204 lines (178 loc) · 5.16 KB
/
bashrc-utils
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
##########
# globals
# only define if not yet present
[ -z "$COURSEDIR" ] && COURSEDIR=$HOME/git/flotpython-course/
[ -z "$TOOLSDIR" ] && TOOLSDIR=$HOME/git/flotpython-tools/
declare -a NORMALIZE_OPTIONS
NORMALIZE_OPTIONS=(--author "Thierry Parmentelat" --author "Arnaud Legout" --version 3.0 --logo-path media/both-logos-small-alpha.png)
##########
# derived
# where to store computed stuff like executed notebooks and tex conversions
PDFWORKDIR=$TOOLSDIR/pdf/work
function jupytext-stem() {
local arg="$1"; shift
local stem="$arg"
stem=$(basename "$stem" .ipynb)
stem=$(basename "$stem" .py)
stem=$(basename "$stem" .md)
echo $stem
}
##########
function notebooks() {
if [[ -z "$@" ]]; then
weeks=$(seq 9)
else
weeks="$@"
fi
for week in $weeks; do
notebooks-week $week
done
}
function notebook-names() {
notebooks "$@" | sed -e 's|w[0-9]/||'
}
function notebooks-week() {
local week=$1
(cd $COURSEDIR; git ls-files w${week}/w*-[cx][0-9]*.{ipynb,py,md})
}
##########
function normalize() {
(cd $COURSEDIR;
local subjects=$(notebooks "$@")
echo Running nbnorm.py on $(wc -w <<< $subjects) notebooks
$TOOLSDIR/tools/nbnorm.py "${NORMALIZE_OPTIONS[@]}" $subjects
)
}
########## spot notebooks based on a text fragment
function search() {
local nb_files=$(notebooks | xargs grep -l "$@" | wc -l)
nb_files=$(echo $nb_files)
case "$nb_files" in
1)
singlefile=$(notebooks | xargs grep -l "$@")
echo $singlefile
;;
0)
echo "NO MATCH found with" "$@"
;;
*)
echo "MULTI $nb_matches MATCHES"
notebooks | xargs grep "$@"
;;
esac
}
function edit() {
local buffer="/tmp/$$"
search "$@" > $buffer
grep -q MATCH $buffer && { cat $buffer; rm $buffer; return; }
local file=$(cat $buffer)
rm $buffer
echo Opening $file
macnb-open $file
}
########## pdf generation
# run step 1 (nbcustomexec) lazily
# execute unless a .ipynb exists in work/ and is more recent than notebook
function execute-all() {
if [[ -z "$@" ]]; then
focus=$(notebooks)
else
focus="$@"
fi
mkdir -p work
for symlink in data media; do
ln -sf $COURSEDIR/$symlink work
done
for nb in $focus; do
local fullnb=$COURSEDIR/$nb
# support for 3 possible notebook extensions
local stem=$(jupytext-stem $nb)
executed="$PDFWORKDIR/$stem.ipynb"
[ $executed -nt $fullnb ] && echo $executed OK || nbcustomexec.py -e $PDFWORKDIR -d $PDFWORKDIR -v $fullnb || {
echo failure with $nb -- exiting; rm -f $executed; return 1
}
done
}
# run step 2 on one entry unconditionally
# we can't pipe nbconvert to stdout b/c it prevents it
# from storing figure files and other artefacts
# also:
# somehow includesvg receives an extraneous .png extension
function convert-one() {
local executed=$(basename $1); shift
local stem=$(jupytext-stem $executed)
local tex=$stem.tex
local textmp=$stem.tmp.tex; shift
echo jupyter nbconvert --to latex $executed
(cd $PDFWORKDIR; \
jupyter nbconvert --to latex $executed \
&& $TOOLSDIR/pdf/striplatex.py --name $stem < $tex > $tex.tmp \
&& mv $tex.tmp $tex \
&& sed -i.svg -e 's|\(includesvg{.*\)\.png|\1|' $tex \
|| { echo failure with target $tex -- exiting; rm -f $tex; return 1; } \
)
}
# run step2 (nbconvert --to latex) lazily
# if a .tex file exists in executing/ it is deemed OK
function convert-all() {
if [[ -z "$@" ]]; then
focus=$(notebook-names)
else
focus="$@"
fi
for name in $focus; do
local stem=$(jupytext-stem $name)
local executed=$PDFWORKDIR/$stem.ipynb
local tex=$PDFWORKDIR/$stem.tex
[ $tex -nt $executed ] && echo $tex OK || convert-one $executed
done
}
# run latex
function latex-current() {
(cd $PDFWORKDIR;
ln -sf ../Python.tex;
xelatex --shell-escape Python;
)
}
function prepare-all() {
execute-all
convert-all
}
# the pdf for a given week
# does e.g. Python-w6.pdf
# can be called with a second argument to specify the number of times
# latex should be run
function week() {
week=$1; shift
[ -z "$week" ] && { echo Usage: week-all week; return 1; }
count=$1; shift
[ -z "$count" ] && count=2
# that's where it should go
cd $TOOLSDIR/pdf
#
scopecontents.py -f $week -t $week
for i in $(seq $count); do latex-current; done
mv work/Python.pdf Python-w${week}.pdf
}
# does e.g. Python-w1-to-w6.pdf
function week-one-to() {
week=$1; shift
[ -z "$week" ] && { echo Usage: week-all week; return 1; }
count=$1; shift
[ -z "$count" ] && count=2
# that's where it should go
cd $TOOLSDIR/pdf
#
[ $week == 1 ] && { echo nothing to do for week=$week; return; }
scopecontents.py -f 1 -t $week
for i in $(seq $count); do latex-current; done
mv work/Python.pdf Python-w1-to-w${week}.pdf
}
function redo-all-pdfs() {
local upto=$1; shift
[ -z "$upto" ] && upto=9
for week in $(seq $upto); do
week $week
done
week-one-to $upto
}