gallery_server_client_s190js/diff_js_css_html.py
2026-10-26 11:00:14 +01:00

86 lines
2 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
import hashlib
import argparse
import sys
parser = argparse.ArgumentParser(
description="Confronta i file JS, CSS e HTML della cartella corrente "
"con un'altra cartella specificata come parametro."
)
parser.add_argument(
"altra_cartella",
help="Percorso della cartella da confrontare con quella corrente"
)
args = parser.parse_args()
old = Path(".").resolve()
new = Path(args.altra_cartella).expanduser().resolve()
if not old.is_dir():
sys.exit(f"ERRORE: cartella corrente non valida: {old}")
if not new.is_dir():
sys.exit(f"ERRORE: cartella inesistente: {new}")
exts = {".js", ".mjs", ".cjs", ".jsx", ".css", ".html", ".htm"}
excluded = {
"node_modules", ".git", ".venv", ".venv-clustering",
"var", "native", "coverage", "dist", "build"
}
def collect(root):
result = {}
for p in root.rglob("*"):
if not p.is_file() or p.suffix.lower() not in exts:
continue
rel = p.relative_to(root)
if any(part in excluded or part.startswith("backup-")
for part in rel.parts):
continue
result[str(rel)] = p
return result
a = collect(old)
b = collect(new)
added = sorted(b.keys() - a.keys())
removed = sorted(a.keys() - b.keys())
common = sorted(a.keys() & b.keys())
modified = [
f for f in common
if hashlib.sha256(a[f].read_bytes()).digest()
!= hashlib.sha256(b[f].read_bytes()).digest()
]
print(f"\nCARTELLA CORRENTE: {old}")
print(f"ALTRA CARTELLA: {new}")
print(f"\nAGGIUNTI nella cartella corrente: {len(removed)}")
for f in removed:
print(" +", f)
print(f"\nASSENTI dalla cartella corrente: {len(added)}")
for f in added:
print(" -", f)
print(f"\nMODIFICATI: {len(modified)}")
for f in modified:
print(" *", f)
Path("/tmp/s184js-modificati.txt").write_text(
"\n".join(modified) + ("\n" if modified else ""),
encoding="utf-8"
)
print("\nElenco dei modificati salvato in /tmp/s184js-modificati.txt")