Release SPHLite 0.1.0 framework
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
FORTRAN_FILE_MAX_LINES = 700
|
||||
C_FILE_MAX_LINES = 500
|
||||
FORTRAN_PROCEDURE_MAX_LINES = 140
|
||||
|
||||
|
||||
def read_exceptions(path: Path) -> set[str]:
|
||||
exceptions: set[str] = set()
|
||||
if not path.exists():
|
||||
raise SystemExit(f"exception file does not exist: {path}")
|
||||
for raw in path.read_text(encoding="utf-8").splitlines():
|
||||
line = raw.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
exceptions.add(line.split()[0].replace("\\", "/"))
|
||||
return exceptions
|
||||
|
||||
|
||||
def source_files(root: Path) -> list[Path]:
|
||||
skip_dirs = {".git", ".agents", ".codex"}
|
||||
result: list[Path] = []
|
||||
for path in root.rglob("*"):
|
||||
if not path.is_file():
|
||||
continue
|
||||
rel_path = path.relative_to(root)
|
||||
rel_parts = set(rel_path.parts)
|
||||
if rel_parts & skip_dirs:
|
||||
continue
|
||||
if rel_path.parts and rel_path.parts[0].startswith("build"):
|
||||
continue
|
||||
if path.suffix.lower() in {".f90", ".c", ".h", ".hpp"}:
|
||||
result.append(path)
|
||||
return sorted(result)
|
||||
|
||||
|
||||
def rel_key(root: Path, path: Path) -> str:
|
||||
return path.relative_to(root).as_posix()
|
||||
|
||||
|
||||
def check_file_lines(root: Path, path: Path, exceptions: set[str], errors: list[str]) -> None:
|
||||
rel = rel_key(root, path)
|
||||
lines = path.read_text(encoding="utf-8").splitlines()
|
||||
suffix = path.suffix.lower()
|
||||
if suffix in {".f90"}:
|
||||
limit = FORTRAN_FILE_MAX_LINES
|
||||
elif suffix in {".c", ".h", ".hpp"}:
|
||||
limit = C_FILE_MAX_LINES
|
||||
else:
|
||||
return
|
||||
if len(lines) > limit and rel not in exceptions:
|
||||
errors.append(f"{rel}: {len(lines)} lines exceeds limit {limit}")
|
||||
|
||||
|
||||
def check_fortran_procedures(root: Path, path: Path, exceptions: set[str], errors: list[str]) -> None:
|
||||
rel = rel_key(root, path)
|
||||
lines = path.read_text(encoding="utf-8").splitlines()
|
||||
start_re = re.compile(r"^\s*(subroutine|function|program)\s+([a-zA-Z_][a-zA-Z0-9_]*)", re.I)
|
||||
end_re = re.compile(r"^\s*end\s+(subroutine|function|program)\b", re.I)
|
||||
active_name: str | None = None
|
||||
active_start = 0
|
||||
|
||||
for index, line in enumerate(lines, start=1):
|
||||
if active_name is None:
|
||||
match = start_re.match(line)
|
||||
if match:
|
||||
active_name = match.group(2)
|
||||
active_start = index
|
||||
continue
|
||||
if end_re.match(line):
|
||||
size = index - active_start + 1
|
||||
key = f"{rel}:{active_name}"
|
||||
if size > FORTRAN_PROCEDURE_MAX_LINES and key not in exceptions:
|
||||
errors.append(
|
||||
f"{key}: {size} lines exceeds procedure limit "
|
||||
f"{FORTRAN_PROCEDURE_MAX_LINES}"
|
||||
)
|
||||
active_name = None
|
||||
|
||||
|
||||
def check_stale_exceptions(root: Path, exceptions: set[str], files: list[Path], errors: list[str]) -> None:
|
||||
file_keys = {rel_key(root, path) for path in files}
|
||||
possible_keys = set(file_keys)
|
||||
for path in files:
|
||||
if path.suffix.lower() == ".f90":
|
||||
text = path.read_text(encoding="utf-8")
|
||||
for match in re.finditer(
|
||||
r"^\s*(subroutine|function|program)\s+([a-zA-Z_][a-zA-Z0-9_]*)",
|
||||
text,
|
||||
re.I | re.M,
|
||||
):
|
||||
possible_keys.add(f"{rel_key(root, path)}:{match.group(2)}")
|
||||
for item in exceptions:
|
||||
if item not in possible_keys:
|
||||
errors.append(f"stale source size exception: {item}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--root", required=True)
|
||||
parser.add_argument("--exceptions", required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
root = Path(args.root).resolve()
|
||||
exceptions = read_exceptions(Path(args.exceptions).resolve())
|
||||
files = source_files(root)
|
||||
errors: list[str] = []
|
||||
|
||||
for path in files:
|
||||
check_file_lines(root, path, exceptions, errors)
|
||||
if path.suffix.lower() == ".f90":
|
||||
check_fortran_procedures(root, path, exceptions, errors)
|
||||
check_stale_exceptions(root, exceptions, files, errors)
|
||||
|
||||
if errors:
|
||||
for error in errors:
|
||||
print(error)
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user