change globbed toc script from bash to python

This commit is contained in:
Axel Kohlmeyer
2024-10-10 17:24:23 -04:00
parent 9835adcbfc
commit 7693e96828
10 changed files with 121 additions and 119 deletions

View File

@ -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

View File

@ -1,6 +0,0 @@
Angle Styles
############
.. toctree::
:maxdepth: 1

View File

@ -1,6 +0,0 @@
Bond Styles
###########
.. toctree::
:maxdepth: 1

View File

@ -1,6 +0,0 @@
Computes
########
.. toctree::
:maxdepth: 1

View File

@ -1,6 +0,0 @@
Dihedral Styles
###############
.. toctree::
:maxdepth: 1

View File

@ -1,6 +0,0 @@
Fixes
#####
.. toctree::
:maxdepth: 1

View File

@ -1,6 +0,0 @@
Improper Styles
###############
.. toctree::
:maxdepth: 1

View File

@ -1,6 +0,0 @@
Pair Styles
###########
.. toctree::
:maxdepth: 1

View File

@ -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)")

View File

@ -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 <rstdir>"
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