Refactor stat script in justfile to Python for improved readability and maintainability

This commit is contained in:
Yifan Gao
2025-11-25 13:01:01 +08:00
parent 4e24cefa5e
commit a3d55cd5e6

View File

@@ -153,33 +153,28 @@ guard:
echo "INFO> guard checks passed" >&2 echo "INFO> guard checks passed" >&2
[doc('Summarize total IPv4/IPv6 address space per operator')] [doc('Summarize total IPv4/IPv6 address space per operator')]
[script]
stat: stat:
set -euo pipefail #!/usr/bin/env python3
cd result import re, sys
from pathlib import Path
for file in *.txt; do result_dir = Path("result")
name="${file%.*}" files = sorted(result_dir.glob("*.txt")) if result_dir.is_dir() else []
echo "${name}" if not files:
if [[ "${file}" == *6.txt ]]; then sys.exit("result/*.txt files missing")
base=48
else
base=32
fi
sum=0 mask = re.compile(r"/(\d+)")
while IFS=/ read -r _ mask; do
if [[ -z "${mask}" ]]; then def seats(path):
continue base = 48 if path.name.endswith("6.txt") else 32
fi with path.open() as fh:
if (( mask <= base )); then masks = (int(m.group(1)) for line in fh if (m := mask.search(line)))
((s=base-mask)) total = sum(1 << (base - m) for m in masks if m <= base)
((sum+=1<<s)) return path.stem, total
fi
done < "${file}" report = "\n\n".join(f"{name}\n{total}" for name, total in map(seats, files)) + "\n"
echo "${sum}" sys.stdout.write(report)
echo (result_dir / "stat").write_text(report)
done | tee stat
[doc('Publish generated results into the ip-lists branch')] [doc('Publish generated results into the ip-lists branch')]
[script] [script]