|
1 | | -import os |
| 1 | +import subprocess |
2 | 2 | import sys |
3 | 3 | import glob |
4 | 4 |
|
5 | 5 | slug = sys.argv[1] |
6 | 6 |
|
| 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 | + |
7 | 21 | book = False |
8 | 22 | notebooks = glob.glob("*.ipynb") |
9 | 23 | if len(notebooks) > 1: |
10 | 24 | 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" |
12 | 26 | ) |
13 | 27 | book = True |
14 | 28 |
|
15 | 29 | if book is True: |
16 | 30 | # 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) |
19 | 33 | # 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) |
22 | 46 |
|
23 | 47 | # 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) |
28 | 52 |
|
29 | 53 | else: |
30 | 54 | # 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) |
33 | 57 | # 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) |
36 | 68 |
|
37 | 69 | # 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