-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbuild_rst.py
50 lines (42 loc) · 1.48 KB
/
build_rst.py
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
import glob
from pathlib import Path
import subprocess
import shutil
_root = Path(__file__).parent.resolve()
_docs = _root / "docs"
notebooks = glob.glob('source/analyses/*/*.ipynb')
notebooks += glob.glob('source/data/*/*.ipynb')
# Remove 'workbook' files
notebooks = [Path(nbf) for nbf in notebooks if '_Workbook' not in nbf]
figures = glob.glob('source/analyses/*/*.png')
figures += glob.glob('source/data/*/*.png')
figures = [Path(file) for file in figures]
for infile in notebooks:
# convert notebook in its local directory
path = _root / infile.parent
nbfile = infile.name
rstfile = infile.with_suffix(".rst").name
files = infile.with_suffix("").name + '_files'
call_str = "jupyter nbconvert --to rst %(nbfile)s" % {'nbfile': nbfile}
subprocess.call(call_str, cwd=path, shell=True)
# move the .rst file
movefiles = path / rstfile
# pretty dependent on directory structure
dest = _docs / Path(*infile.parent.parts[1:])
# ! mv $movefiles $dest
if not dest.exists():
dest.mkdir()
shutil.move(movefiles, dest / rstfile)
# move supporting files, if there are any
movedir = path / files
if movedir.exists():
destdir = dest / files
if destdir.exists():
# !rm - r $destdir
shutil.rmtree(destdir)
# !mv $movedir $dest
shutil.move(str(movedir), str(dest))
for infile in figures:
# Copy extra figures
dest = _docs / Path(*infile.parts[1:])
shutil.copy(infile, dest)