|
37 | 37 | This script converts Python tutorials to markdown and jupyter notebook formats for use on the
|
38 | 38 | PyChaste website.
|
39 | 39 | """
|
| 40 | + |
40 | 41 | import argparse
|
41 | 42 | import fnmatch
|
42 | 43 | import ntpath
|
|
56 | 57 | )
|
57 | 58 | args = parser.parse_args()
|
58 | 59 |
|
| 60 | + # Get the repository root directory |
| 61 | + root_dir = ( |
| 62 | + subprocess.check_output(["git", "rev-parse", "--show-toplevel"]) |
| 63 | + .decode("ascii") |
| 64 | + .strip() |
| 65 | + ) |
| 66 | + |
59 | 67 | # Find all the tutorial files.
|
60 | 68 | tutorial_files = []
|
61 |
| - for root, dirs, files in os.walk("../test"): |
| 69 | + for root, dirs, files in os.walk(os.path.join(root_dir, "test")): |
62 | 70 | for file in files:
|
63 |
| - if fnmatch.fnmatch(file, "Test*LiteratePaper*") or fnmatch.fnmatch( |
64 |
| - file, "Test*Tutorial*" |
| 71 | + if fnmatch.fnmatch(file, "Test*LiteratePaper*.py") or fnmatch.fnmatch( |
| 72 | + file, "Test*Tutorial*.py" |
65 | 73 | ):
|
66 |
| - if not fnmatch.fnmatch(file, "*.pyc"): |
67 |
| - tutorial_files.append([root, file]) |
| 74 | + tutorial_files.append((root, file)) |
68 | 75 |
|
69 |
| - # Get git revision |
70 |
| - revision = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('ascii').strip() |
| 76 | + # Get the git revision |
| 77 | + revision = ( |
| 78 | + subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip() |
| 79 | + ) |
71 | 80 |
|
72 | 81 | if args.format == "markdown":
|
73 |
| - # Generate the markdown for each |
74 |
| - for eachFile in tutorial_files: |
75 |
| - outfile = ( |
76 |
| - "../doc/tutorials/" |
77 |
| - + os.path.splitext(ntpath.basename(eachFile[1]))[0] |
78 |
| - + ".md" |
| 82 | + # Generate a markdown file for each tutorial |
| 83 | + for tutorial_file in tutorial_files: |
| 84 | + input_filepath = os.path.join(tutorial_file[0], tutorial_file[1]) |
| 85 | + |
| 86 | + output_filename = ( |
| 87 | + os.path.splitext(os.path.basename(tutorial_file[1]))[0] + ".md" |
79 | 88 | )
|
80 |
| - inputfile = eachFile[0] + "/" + eachFile[1] |
81 |
| - launch_string = f"../infra/CreateMarkdownTutorial.py {inputfile} {outfile} --revision {revision}" |
| 89 | + output_filepath = os.path.join( |
| 90 | + root_dir, "doc", "tutorials", output_filename |
| 91 | + ) |
| 92 | + |
| 93 | + launch_string = f"{root_dir}/infra/CreateMarkdownTutorial.py {input_filepath} {output_filepath} --revision {revision}" |
82 | 94 | os.system(launch_string)
|
83 | 95 |
|
84 | 96 | elif args.format == "jupyter":
|
85 |
| - # Generate the jupyter notebooks for each |
86 |
| - for eachFile in tutorial_files: |
87 |
| - outfile = ( |
88 |
| - "../doc/tutorials/" |
89 |
| - + os.path.splitext(ntpath.basename(eachFile[1]))[0] |
90 |
| - + ".ipynb" |
| 97 | + # Generate a jupyter notebook for each tutorial |
| 98 | + for tutorial_file in tutorial_files: |
| 99 | + input_filepath = os.path.join(tutorial_file[0], tutorial_file[1]) |
| 100 | + |
| 101 | + output_filename = ( |
| 102 | + os.path.splitext(os.path.basename(tutorial_file[1]))[0] + ".ipynb" |
91 | 103 | )
|
92 |
| - inputfile = eachFile[0] + "/" + eachFile[1] |
93 |
| - launch_string = ( |
94 |
| - f"../infra/CreateJupyterNotebookTutorial.py {inputfile} {outfile}" |
| 104 | + output_filepath = os.path.join( |
| 105 | + root_dir, "doc", "tutorials", output_filename |
95 | 106 | )
|
| 107 | + |
| 108 | + launch_string = f"{root_dir}/infra/CreateJupyterNotebookTutorial.py {input_filepath} {output_filepath}" |
96 | 109 | os.system(launch_string)
|
97 | 110 |
|
98 |
| - subprocess.call(f"jupyter nbconvert --to notebook {outfile}", shell=True) |
| 111 | + subprocess.call( |
| 112 | + f"jupyter nbconvert --to notebook {output_filepath}", shell=True |
| 113 | + ) |
0 commit comments