Unverified Commit d0be2194 authored by Richard Berger's avatar Richard Berger
Browse files

Refactor check-styles.py

parent f9d1a914
Loading
Loading
Loading
Loading
+141 −140
Original line number Diff line number Diff line
#!/usr/bin/env python3

from __future__ import print_function
import os, re, sys
from glob import glob
from argparse import ArgumentParser
import os, re, sys

parser = ArgumentParser(prog='check-styles.py',
                        description="Check style table completeness")

parser.add_argument("-v", "--verbose",
                    action='store_const',
                    const=True, default=False,
                    action='store_true',
                    help="Enable verbose output")

parser.add_argument("-d", "--doc",
@@ -20,21 +18,29 @@ parser.add_argument("-s", "--src",

args = parser.parse_args()
verbose = args.verbose
src = args.src
doc = args.doc
src_dir = args.src
doc_dir = args.doc

LAMMPS_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))

if not src_dir:
    src_dir = os.path.join(LAMMPS_DIR , 'src')

if not doc_dir:
    doc_dir = os.path.join(LAMMPS_DIR, 'doc', 'src')

if not args.src or not args.doc:
if not src_dir or not doc_dir:
  parser.print_help()
  sys.exit(1)

if not os.path.isdir(src):
    sys.exit("LAMMPS source path %s does not exist" % src)
if not os.path.isdir(src_dir):
    sys.exit(f"LAMMPS source path {src_dir} does not exist")

if not os.path.isdir(doc):
    sys.exit("LAMMPS documentation source path %s does not exist" % doc)
if not os.path.isdir(doc_dir):
    sys.exit(f"LAMMPS documentation source path {doc_dir} does not exist")

headers = glob(os.path.join(src, '*', '*.h'))
headers += glob(os.path.join(src, '*.h'))
headers = glob(os.path.join(src_dir, '*', '*.h'))
headers += glob(os.path.join(src_dir, '*.h'))

angle = {}
atom = {}
@@ -54,6 +60,7 @@ reader = {}
region = {}
total = 0

style_pattern = re.compile(r"(.+)Style\((.+),(.+)\)")
upper = re.compile("[A-Z]+")
gpu = re.compile("(.+)/gpu$")
intel = re.compile("(.+)/intel$")
@@ -63,61 +70,54 @@ omp = re.compile("(.+)/omp$")
opt = re.compile("(.+)/opt$")
removed = re.compile("(.*)Deprecated$")

def register_style(list,style,info):
    if style in list.keys():
        list[style]['gpu'] += info['gpu']
        list[style]['intel'] += info['intel']
        list[style]['kokkos'] += info['kokkos']
        list[style]['omp'] += info['omp']
        list[style]['opt'] += info['opt']
        list[style]['removed'] += info['removed']
def register_style(styles, name, info):
    if name in styles:
        for key, value in info.items():
            styles[name][key] += value
    else:
        list[style] = info
        styles[name] = info

def add_suffix(list,style):
def add_suffix(styles, name):
    suffix = ""
    if list[style]['gpu']:
    if styles[name]['gpu']:
        suffix += 'g'
    if list[style]['intel']:
    if styles[name]['intel']:
        suffix += 'i'
    if list[style]['kokkos']:
    if styles[name]['kokkos']:
        suffix += 'k'
    if list[style]['omp']:
    if styles[name]['omp']:
        suffix += 'o'
    if list[style]['opt']:
    if styles[name]['opt']:
        suffix += 't'
    if suffix:
        return style + ' (' + suffix + ')'
        return f"{name} ({suffix})"
    else:
        return style
        return name

def check_style(filename, dirname, pattern, styles, name, suffix=False, skip=set()):
    with open(os.path.join(dirname, filename)) as f:
        text = f.read()

def check_style(file,dir,pattern,list,name,suffix=False,skip=()):
    f = os.path.join(dir, file)
    fp = open(f)
    text = fp.read()
    fp.close()
    matches = re.findall(pattern, text, re.MULTILINE)

    counter = 0
    for c in list.keys():
    for c in styles:
        # known undocumented aliases we need to skip
        if c in skip: continue
        s = c
        if suffix: s = add_suffix(list,c)
        if suffix: s = add_suffix(styles, c)
        if not s in matches:
            if not list[c]['removed']:
                print("%s style entry %s" % (name,s),
                      "is missing or incomplete in %s" % file)
            if not styles[c]['removed']:
                print(f"{name} style entry {s} is missing or incomplete in {filename}")
                counter += 1
    return counter

for h in headers:
    if verbose: print("Checking ", h)
    fp = open(h)
    text = fp.read()
    fp.close()
    matches = re.findall("(.+)Style\((.+),(.+)\)",text,re.MULTILINE)
for header in headers:
    if verbose: print("Checking ", header)
    with open(header) as f:
        for line in f:
            matches = style_pattern.findall(line)
            for m in matches:

                # skip over internal styles w/o explicit documentation
                style = m[1]
                total += 1
@@ -202,44 +202,45 @@ print("""Parsed style names w/o suffixes from C++ tree in %s:
   Reader styles:    %3d    Region styles:    %3d
----------------------------------------------------
Total number of styles (including suffixes): %d""" \
      % (src, len(angle), len(atom), len(body), len(bond),   \
      % (src_dir, len(angle), len(atom), len(body), len(bond),   \
       len(command), len(compute), len(dihedral), len(dump), \
       len(fix), len(improper), len(integrate), len(kspace), \
       len(minimize), len(pair), len(reader), len(region), total))


counter = 0

counter += check_style('Commands_all.rst',doc,":doc:`(.+) <.+>`",
counter += check_style('Commands_all.rst', doc_dir, ":doc:`(.+) <.+>`",
                       command,'Command',suffix=False)
counter += check_style('Commands_compute.rst',doc,":doc:`(.+) <compute.+>`",
counter += check_style('Commands_compute.rst', doc_dir, ":doc:`(.+) <compute.+>`",
                       compute,'Compute',suffix=True)
counter += check_style('compute.rst',doc,":doc:`(.+) <compute.+>` -",
counter += check_style('compute.rst', doc_dir, ":doc:`(.+) <compute.+>` -",
                       compute,'Compute',suffix=False)
counter += check_style('Commands_fix.rst',doc,":doc:`(.+) <fix.+>`",
                       fix,'Fix',skip=('python'),suffix=True)
counter += check_style('fix.rst',doc,":doc:`(.+) <fix.+>` -",
                       fix,'Fix',skip=('python'),suffix=False)
counter += check_style('Commands_pair.rst',doc,":doc:`(.+) <pair.+>`",
counter += check_style('Commands_fix.rst', doc_dir, ":doc:`(.+) <fix.+>`",
                       fix,'Fix',skip=('python', 'NEIGH_HISTORY/omp'),suffix=True)
counter += check_style('fix.rst', doc_dir, ":doc:`(.+) <fix.+>` -",
                       fix,'Fix',skip=('python', 'NEIGH_HISTORY/omp'),suffix=False)
counter += check_style('Commands_pair.rst', doc_dir, ":doc:`(.+) <pair.+>`",
                       pair,'Pair',skip=('meam','lj/sf'),suffix=True)
counter += check_style('pair_style.rst',doc,":doc:`(.+) <pair.+>` -",
counter += check_style('pair_style.rst', doc_dir, ":doc:`(.+) <pair.+>` -",
                       pair,'Pair',skip=('meam','lj/sf'),suffix=False)
counter += check_style('Commands_bond.rst',doc,":doc:`(.+) <bond.+>`",
counter += check_style('Commands_bond.rst', doc_dir, ":doc:`(.+) <bond.+>`",
                       bond,'Bond',suffix=True)
counter += check_style('bond_style.rst',doc,":doc:`(.+) <bond.+>` -",
counter += check_style('bond_style.rst', doc_dir, ":doc:`(.+) <bond.+>` -",
                       bond,'Bond',suffix=False)
counter += check_style('Commands_bond.rst',doc,":doc:`(.+) <angle.+>`",
counter += check_style('Commands_bond.rst', doc_dir, ":doc:`(.+) <angle.+>`",
                       angle,'Angle',suffix=True)
counter += check_style('angle_style.rst',doc,":doc:`(.+) <angle.+>` -",
counter += check_style('angle_style.rst', doc_dir, ":doc:`(.+) <angle.+>` -",
                       angle,'Angle',suffix=False)
counter += check_style('Commands_bond.rst',doc,":doc:`(.+) <dihedral.+>`",
counter += check_style('Commands_bond.rst', doc_dir, ":doc:`(.+) <dihedral.+>`",
                       dihedral,'Dihedral',suffix=True)
counter += check_style('dihedral_style.rst',doc,":doc:`(.+) <dihedral.+>` -",
counter += check_style('dihedral_style.rst', doc_dir, ":doc:`(.+) <dihedral.+>` -",
                       dihedral,'Dihedral',suffix=False)
counter += check_style('Commands_bond.rst',doc,":doc:`(.+) <improper.+>`",
counter += check_style('Commands_bond.rst', doc_dir, ":doc:`(.+) <improper.+>`",
                       improper,'Improper',suffix=True)
counter += check_style('improper_style.rst',doc,":doc:`(.+) <improper.+>` -",
counter += check_style('improper_style.rst', doc_dir, ":doc:`(.+) <improper.+>` -",
                       improper,'Improper',suffix=False)
counter += check_style('Commands_kspace.rst',doc,":doc:`(.+) <kspace_style>`",
counter += check_style('Commands_kspace.rst', doc_dir, ":doc:`(.+) <kspace_style>`",
                       kspace,'KSpace',suffix=True)

if counter: