From 7693e9682898fb9f2d29a325d411f8d18da25141 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 10 Oct 2024 17:24:23 -0400 Subject: [PATCH] change globbed toc script from bash to python --- doc/Makefile | 3 +- doc/src/angles.rst.in | 6 -- doc/src/bonds.rst.in | 6 -- doc/src/computes.rst.in | 6 -- doc/src/dihedrals.rst.in | 6 -- doc/src/fixes.rst.in | 6 -- doc/src/impropers.rst.in | 6 -- doc/src/pairs.rst.in | 6 -- doc/utils/make-globbed-tocs.py | 120 +++++++++++++++++++++++++++++++++ doc/utils/make-globbed-tocs.sh | 75 --------------------- 10 files changed, 121 insertions(+), 119 deletions(-) delete mode 100644 doc/src/angles.rst.in delete mode 100644 doc/src/bonds.rst.in delete mode 100644 doc/src/computes.rst.in delete mode 100644 doc/src/dihedrals.rst.in delete mode 100644 doc/src/fixes.rst.in delete mode 100644 doc/src/impropers.rst.in delete mode 100644 doc/src/pairs.rst.in create mode 100644 doc/utils/make-globbed-tocs.py delete mode 100755 doc/utils/make-globbed-tocs.sh diff --git a/doc/Makefile b/doc/Makefile index dda15151a7..d26e6020a6 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -84,8 +84,7 @@ $(SPHINXCONFIG)/conf.py: $(SPHINXCONFIG)/conf.py.in -e 's,@LAMMPS_DOC_DIR@,$(BUILDDIR),g' $< > $@ globbed-tocs: - @if [ "$(HAS_BASH)" == "NO" ] ; then echo "bash was not found at $(OSHELL)! Please use: $(MAKE) SHELL=/path/to/bash" 1>&2; exit 1; fi - $(SHELL) $(BUILDDIR)/utils/make-globbed-tocs.sh $(RSTDIR) + $(PYTHON) $(BUILDDIR)/utils/make-globbed-tocs.py -d $(RSTDIR) html: xmlgen globbed-tocs $(VENV) $(SPHINXCONFIG)/conf.py $(ANCHORCHECK) $(MATHJAX) @if [ "$(HAS_BASH)" == "NO" ] ; then echo "bash was not found at $(OSHELL)! Please use: $(MAKE) SHELL=/path/to/bash" 1>&2; exit 1; fi diff --git a/doc/src/angles.rst.in b/doc/src/angles.rst.in deleted file mode 100644 index 2bf3afc768..0000000000 --- a/doc/src/angles.rst.in +++ /dev/null @@ -1,6 +0,0 @@ -Angle Styles -############ - -.. toctree:: - :maxdepth: 1 - diff --git a/doc/src/bonds.rst.in b/doc/src/bonds.rst.in deleted file mode 100644 index c66e61ace6..0000000000 --- a/doc/src/bonds.rst.in +++ /dev/null @@ -1,6 +0,0 @@ -Bond Styles -########### - -.. toctree:: - :maxdepth: 1 - diff --git a/doc/src/computes.rst.in b/doc/src/computes.rst.in deleted file mode 100644 index 9ba0e61576..0000000000 --- a/doc/src/computes.rst.in +++ /dev/null @@ -1,6 +0,0 @@ -Computes -######## - -.. toctree:: - :maxdepth: 1 - diff --git a/doc/src/dihedrals.rst.in b/doc/src/dihedrals.rst.in deleted file mode 100644 index f2818afb17..0000000000 --- a/doc/src/dihedrals.rst.in +++ /dev/null @@ -1,6 +0,0 @@ -Dihedral Styles -############### - -.. toctree:: - :maxdepth: 1 - diff --git a/doc/src/fixes.rst.in b/doc/src/fixes.rst.in deleted file mode 100644 index 825f278729..0000000000 --- a/doc/src/fixes.rst.in +++ /dev/null @@ -1,6 +0,0 @@ -Fixes -##### - -.. toctree:: - :maxdepth: 1 - diff --git a/doc/src/impropers.rst.in b/doc/src/impropers.rst.in deleted file mode 100644 index 2ca1e8cf3c..0000000000 --- a/doc/src/impropers.rst.in +++ /dev/null @@ -1,6 +0,0 @@ -Improper Styles -############### - -.. toctree:: - :maxdepth: 1 - diff --git a/doc/src/pairs.rst.in b/doc/src/pairs.rst.in deleted file mode 100644 index 7ae581382e..0000000000 --- a/doc/src/pairs.rst.in +++ /dev/null @@ -1,6 +0,0 @@ -Pair Styles -########### - -.. toctree:: - :maxdepth: 1 - diff --git a/doc/utils/make-globbed-tocs.py b/doc/utils/make-globbed-tocs.py new file mode 100644 index 0000000000..bd0062adbe --- /dev/null +++ b/doc/utils/make-globbed-tocs.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 + +""" +Script to emulate globbed toctrees with the added feature +to avoid adding files that are already included elsewhere. +""" + +import os, re +import filecmp +import tempfile +import shutil +from glob import glob +from argparse import ArgumentParser + +LAMMPS_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) + +parser = ArgumentParser(prog='make-globbed-tocs.py', + description="Create toctree files from patterns with exclusions") +parser.add_argument("-v", "--verbose", action='store_true', help="Enable verbose output") +parser.add_argument("-d", "--doc", help="Path to LAMMPS documentation sources") + +args = parser.parse_args() +verbose = args.verbose +if args.doc: + docsrc = os.path.realpath(args.doc) +else: + docsrc = os.path.join(LAMMPS_DIR, 'doc', 'src') + +if not os.path.isdir(docsrc): + sys.exit(f"LAMMPS manual source dir {docsrc} does not exist") + +def glob_tocfile(style, name, head, exclude): + + newname = None + exclude_re = re.compile(exclude) + if verbose: print("Processing style ", style) + with tempfile.NamedTemporaryFile(prefix=style + '.', delete=False) as f: + newname = f.name + if verbose: print("Temporary file: ", newname) + f.write(head.encode('utf-8')) + for doc in sorted(glob(os.path.join(docsrc, style + '_*.rst'))): + d,e = os.path.splitext(os.path.basename(doc)) + if exclude_re.match(d): + if verbose: print("skipping file ", d) + continue + if verbose: print("processing file ", d) + f.write((" " + d + "\n").encode('utf-8')) + + oldname = os.path.join(docsrc, name) + if os.path.exists(oldname) and filecmp.cmp(newname, oldname, shallow=False): + print("File ", name, " is unchanged") + os.remove(newname) + else: + print("Overwriting file ", name, " with new version") + shutil.move(newname, os.path.join(docsrc, name)) + +################################## + +pair_head = """Pair Styles +########### + +.. toctree:: + :maxdepth: 1 + +""" +glob_tocfile('pair', 'pairs.rst', pair_head, r"pair_(coeff|modify|style|write)") + +bond_head = """Bond Styles +########### + +.. toctree:: + :maxdepth: 1 + +""" +glob_tocfile('bond', 'bonds.rst', bond_head, r"bond_(coeff|modify|style|write)") + +angle_head = """Angle Styles +############ + +.. toctree:: + :maxdepth: 1 + +""" +glob_tocfile('angle', 'angles.rst', angle_head, r"angle_(coeff|modify|style|write)") + +dihedral_head = """Dihedral Styles +############### + +.. toctree:: + :maxdepth: 1 + +""" +glob_tocfile('dihedral', 'dihedrals.rst', dihedral_head, r"dihedral_(coeff|modify|style|write)") + +improper_head = """Improper Styles +############### + +.. toctree:: + :maxdepth: 1 + +""" +glob_tocfile('improper', 'impropers.rst', improper_head, r"improper_(coeff|modify|style|write)") + +compute_head = """Compute Styles +############### + +.. toctree:: + :maxdepth: 1 + +""" +glob_tocfile('compute', 'computes.rst', compute_head, r"compute_modify") + +fix_head = """Fix Styles +########## + +.. toctree:: + :maxdepth: 1 + +""" +glob_tocfile('fix', 'fixes.rst', fix_head, r"fix_modify(|_atc_commands)") diff --git a/doc/utils/make-globbed-tocs.sh b/doc/utils/make-globbed-tocs.sh deleted file mode 100755 index 29c53cacb3..0000000000 --- a/doc/utils/make-globbed-tocs.sh +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/bash -# script to emulate globbed toctrees that allows to skip files that were entered already elsewhere - -if [ $# != 1 ] -then - echo "Usage: $0 " - exit 1 -fi - -RSTDIR="$1" -TMPNAM=${RANDOM} -TMPDIR=${TMPDIR-/tmp} - -# pairs.rst -cp ${RSTDIR}/pairs.rst.in ${TMPDIR}/${TMPNAM}.pairs.rst -for f in $(echo ${RSTDIR}/pair_*.rst | sed -e "s@${RSTDIR}/@@g" -e 's@\.rst@@g' -e 's@pair_\(coeff\|modify\|style\|write\)@@g' | sort ) -do \ - echo " $f" >> ${TMPDIR}/${TMPNAM}.pairs.rst -done -cmp -s ${TMPDIR}/${TMPNAM}.pairs.rst ${RSTDIR}/pairs.rst || mv -vf ${TMPDIR}/${TMPNAM}.pairs.rst ${RSTDIR}/pairs.rst -rm -f ${TMPDIR}/${TMPNAM}.pairs.rst - -# bonds.rst -cp ${RSTDIR}/bonds.rst.in ${TMPDIR}/${TMPNAM}.bonds.rst -for f in $(echo ${RSTDIR}/bond_*.rst | sed -e "s@${RSTDIR}/@@g" -e "s@\\.rst@@g" -e 's@bond_\(coeff\|modify\|style\|write\)@@g' | sort ) -do \ - echo " $f" >> ${TMPDIR}/${TMPNAM}.bonds.rst -done -cmp -s ${TMPDIR}/${TMPNAM}.bonds.rst ${RSTDIR}/bonds.rst || mv -vf ${TMPDIR}/${TMPNAM}.bonds.rst ${RSTDIR}/bonds.rst -rm -f ${TMPDIR}/${TMPNAM}.bonds.rst - -# angles.rst -cp ${RSTDIR}/angles.rst.in ${TMPDIR}/${TMPNAM}.angles.rst -for f in $(echo ${RSTDIR}/angle_*.rst | sed -e "s@${RSTDIR}/@@g" -e "s@\\.rst@@g" -e 's@angle_\(coeff\|modify\|style\|write\)@@g' | sort ) -do \ - echo " $f" >> ${TMPDIR}/${TMPNAM}.angles.rst -done -cmp -s ${TMPDIR}/${TMPNAM}.angles.rst ${RSTDIR}/angles.rst || mv -vf ${TMPDIR}/${TMPNAM}.angles.rst ${RSTDIR}/angles.rst -rm -f ${TMPDIR}/${TMPNAM}.angles.rst - -# dihedrals.rst -cp ${RSTDIR}/dihedrals.rst.in ${TMPDIR}/${TMPNAM}.dihedrals.rst -for f in $(echo ${RSTDIR}/dihedral_*.rst | sed -e "s@${RSTDIR}/@@g" -e "s@\\.rst@@g" -e 's@dihedral_\(coeff\|modify\|style\|write\)@@g' | sort ) -do \ - echo " $f" >> ${TMPDIR}/${TMPNAM}.dihedrals.rst -done -cmp -s ${TMPDIR}/${TMPNAM}.dihedrals.rst ${RSTDIR}/dihedrals.rst || mv -vf ${TMPDIR}/${TMPNAM}.dihedrals.rst ${RSTDIR}/dihedrals.rst -rm -f ${TMPDIR}/${TMPNAM}.dihedrals.rst - -# impropers.rst -cp ${RSTDIR}/impropers.rst.in ${TMPDIR}/${TMPNAM}.impropers.rst -for f in $(echo ${RSTDIR}/improper_*.rst | sed -e "s@${RSTDIR}/@@g" -e "s@\\.rst@@g" -e 's@improper_\(coeff\|modify\|style\|write\)@@g' | sort ) -do \ - echo " $f" >> ${TMPDIR}/${TMPNAM}.impropers.rst -done -cmp -s ${TMPDIR}/${TMPNAM}.impropers.rst ${RSTDIR}/impropers.rst || mv -vf ${TMPDIR}/${TMPNAM}.impropers.rst ${RSTDIR}/impropers.rst -rm -f ${TMPDIR}/${TMPNAM}.impropers.rst - -# computes.rst -cp ${RSTDIR}/computes.rst.in ${TMPDIR}/${TMPNAM}.computes.rst -for f in $(echo ${RSTDIR}/compute_*.rst | sed -e "s@${RSTDIR}/@@g" -e "s@\\.rst@@g" -e 's@compute_modify@@g' | sort ) -do \ - echo " $f" >> ${TMPDIR}/${TMPNAM}.computes.rst -done -cmp -s ${TMPDIR}/${TMPNAM}.computes.rst ${RSTDIR}/computes.rst || mv -vf ${TMPDIR}/${TMPNAM}.computes.rst ${RSTDIR}/computes.rst -rm -f ${TMPDIR}/${TMPNAM}.computes.rst - -# fixs.rst -cp ${RSTDIR}/fixes.rst.in ${TMPDIR}/${TMPNAM}.fixes.rst -for f in $(echo ${RSTDIR}/fix_*.rst | sed -e "s@${RSTDIR}/@@g" -e "s@\\.rst@@g" -e 's@fix_\(modify\|modify_atc_commands\)@@g' | sort ) -do \ - echo " $f" >> ${TMPDIR}/${TMPNAM}.fixes.rst -done -cmp -s ${TMPDIR}/${TMPNAM}.fixes.rst ${RSTDIR}/fixes.rst || mv -vf ${TMPDIR}/${TMPNAM}.fixes.rst ${RSTDIR}/fixes.rst -rm -f ${TMPDIR}/${TMPNAM}.fixes.rst