Skip to content

Commit

Permalink
#57 fix tutorial gen script paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Feb 23, 2024
1 parent ec27c7f commit be59762
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
8 changes: 7 additions & 1 deletion infra/CreateMarkdownTutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import os
import sys
import re
import subprocess

# This had better match GenerateHowTo.py!
HOWTO_TAG = "HOW_TO_TAG"
Expand Down Expand Up @@ -217,7 +218,12 @@ def ConvertTutorialToMarkdownText(test_file_path, test_file, other_files, revisi
ugly_file_name = os.path.splitext(os.path.basename(test_file_path))[0]
nice_file_name = re.sub(regex, " ", ugly_file_name)

link_path = f"https://github.com/Chaste/PyChaste/blob/develop/test/tutorials/{ugly_file_name}.py"
root_dir = (
subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).decode("ascii").strip()
)
link_path = os.path.relpath(os.path.abspath(test_file_path), root_dir)
link_path = f"https://github.com/Chaste/PyChaste/blob/develop/{link_path}"

page_header = f"""
---
title : "{nice_file_name}"
Expand Down
65 changes: 40 additions & 25 deletions infra/GenerateWikiPages.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
This script converts Python tutorials to markdown and jupyter notebook formats for use on the
PyChaste website.
"""

import argparse
import fnmatch
import ntpath
Expand All @@ -56,43 +57,57 @@
)
args = parser.parse_args()

# Get the repository root directory
root_dir = (
subprocess.check_output(["git", "rev-parse", "--show-toplevel"])
.decode("ascii")
.strip()
)

# Find all the tutorial files.
tutorial_files = []
for root, dirs, files in os.walk("../test"):
for root, dirs, files in os.walk(os.path.join(root_dir, "test")):
for file in files:
if fnmatch.fnmatch(file, "Test*LiteratePaper*") or fnmatch.fnmatch(
file, "Test*Tutorial*"
if fnmatch.fnmatch(file, "Test*LiteratePaper*.py") or fnmatch.fnmatch(
file, "Test*Tutorial*.py"
):
if not fnmatch.fnmatch(file, "*.pyc"):
tutorial_files.append([root, file])
tutorial_files.append((root, file))

# Get git revision
revision = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('ascii').strip()
# Get the git revision
revision = (
subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()
)

if args.format == "markdown":
# Generate the markdown for each
for eachFile in tutorial_files:
outfile = (
"../doc/tutorials/"
+ os.path.splitext(ntpath.basename(eachFile[1]))[0]
+ ".md"
# Generate a markdown file for each tutorial
for tutorial_file in tutorial_files:
input_filepath = os.path.join(tutorial_file[0], tutorial_file[1])

output_filename = (
os.path.splitext(os.path.basename(tutorial_file[1]))[0] + ".md"
)
inputfile = eachFile[0] + "/" + eachFile[1]
launch_string = f"../infra/CreateMarkdownTutorial.py {inputfile} {outfile} --revision {revision}"
output_filepath = os.path.join(
root_dir, "doc", "tutorials", output_filename
)

launch_string = f"{root_dir}/infra/CreateMarkdownTutorial.py {input_filepath} {output_filepath} --revision {revision}"
os.system(launch_string)

elif args.format == "jupyter":
# Generate the jupyter notebooks for each
for eachFile in tutorial_files:
outfile = (
"../doc/tutorials/"
+ os.path.splitext(ntpath.basename(eachFile[1]))[0]
+ ".ipynb"
# Generate a jupyter notebook for each tutorial
for tutorial_file in tutorial_files:
input_filepath = os.path.join(tutorial_file[0], tutorial_file[1])

output_filename = (
os.path.splitext(os.path.basename(tutorial_file[1]))[0] + ".ipynb"
)
inputfile = eachFile[0] + "/" + eachFile[1]
launch_string = (
f"../infra/CreateJupyterNotebookTutorial.py {inputfile} {outfile}"
output_filepath = os.path.join(
root_dir, "doc", "tutorials", output_filename
)

launch_string = f"{root_dir}/infra/CreateJupyterNotebookTutorial.py {input_filepath} {output_filepath}"
os.system(launch_string)

subprocess.call(f"jupyter nbconvert --to notebook {outfile}", shell=True)
subprocess.call(
f"jupyter nbconvert --to notebook {output_filepath}", shell=True
)

0 comments on commit be59762

Please sign in to comment.