Skip to content

Commit f433764

Browse files
committed
execute error catch
1 parent 0dd479c commit f433764

1 file changed

Lines changed: 50 additions & 18 deletions

File tree

scripts/execute-convert.py

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,73 @@
1-
import os
1+
import subprocess
22
import sys
33
import glob
44

55
slug = sys.argv[1]
66

7+
8+
def run_command(command, verbose=0):
9+
try:
10+
result = subprocess.run(command, check=True, capture_output=True, text=True)
11+
except (
12+
subprocess.CalledProcessError
13+
) as e: # Catching the error if the command fails
14+
print(f"Error building book: {e.stderr}")
15+
sys.exit(1)
16+
if verbose > 0:
17+
print(f"Output: {result.stdout}")
18+
return result
19+
20+
721
book = False
822
notebooks = glob.glob("*.ipynb")
923
if len(notebooks) > 1:
1024
print(
11-
"More than one .ipynb notebook found --> assuming this is a book rather than a single notebook"
25+
"More than one .ipynb notebook found --> assuming this is a book"
1226
)
1327
book = True
1428

1529
if book is True:
1630
# build book
17-
command = "jb build . --config _config.yml --toc _toc.yml"
18-
os.system(command)
31+
command = ["jb", "build", ".", "--config", "_config.yml", "--toc", "_toc.yml"]
32+
run_command(command)
1933
# build pdf
20-
command = "jb build . --config _config.yml --toc _toc.yml --builder pdfhtml"
21-
os.system(command)
34+
command = [
35+
"jb",
36+
"build",
37+
".",
38+
"--config",
39+
"_config.yml",
40+
"--toc",
41+
"_toc.yml",
42+
"--builder",
43+
"pdfhtml",
44+
]
45+
run_command(command)
2246

2347
# copy build outputs to 'html' dir
24-
command = "cp -r '_build/html' html"
25-
os.system(command)
26-
command = "cp '_build/pdf/book.pdf' html"
27-
os.system(command)
48+
command = ["cp", "-r", "_build/html", "html"]
49+
run_command(command)
50+
command = ["cp", "_build/pdf/book.pdf", "html"]
51+
run_command(command)
2852

2953
else:
3054
# build single notebook
31-
command = f"jb build {slug}.ipynb --config _config.yml"
32-
os.system(command)
55+
command = ["jb", "build", f"{slug}.ipynb", "--config", "_config.yml"]
56+
run_command(command)
3357
# build pdf
34-
command = f"jb build {slug}.ipynb --config _config.yml --builder pdfhtml"
35-
os.system(command)
58+
command = [
59+
"jb",
60+
"build",
61+
f"{slug}.ipynb",
62+
"--config",
63+
"_config.yml",
64+
"--builder",
65+
"pdfhtml",
66+
]
67+
run_command(command)
3668

3769
# copy build outputs to 'html' dir
38-
command = f"cp -r '_build/_page/{slug}/html' html"
39-
os.system(command)
40-
command = f"cp '_build/_page/{slug}/pdf/{slug}.pdf' html"
41-
os.system(command)
70+
command = ["cp", "-r", f"_build/_page/{slug}/html", "html"]
71+
run_command(command)
72+
command = ["cp", f"_build/_page/{slug}/pdf/{slug}.pdf", "html"]
73+
run_command(command)

0 commit comments

Comments
 (0)