maintainers/scripts/sha-to-sri: accept directories as input

Doesn't skip files passed as CLI arguments based on their name anymore,
since bulk changes can now be done without resorting to `xargs` or equivalent.
This commit is contained in:
nicoo 2024-09-13 08:00:53 +00:00
parent 915799a2b9
commit bf6b5f7f85

View File

@ -7,7 +7,7 @@ from pathlib import Path
from structlog.contextvars import bound_contextvars as log_context from structlog.contextvars import bound_contextvars as log_context
from typing import ClassVar, List, Tuple from typing import ClassVar, List, Tuple
import hashlib, re, structlog import hashlib, logging, re, structlog
logger = structlog.getLogger("sha-to-SRI") logger = structlog.getLogger("sha-to-SRI")
@ -208,24 +208,20 @@ if __name__ == "__main__":
logger.info("Starting!") logger.info("Starting!")
for arg in argv[1:]: def handleFile(p: Path, skipLevel = logging.INFO):
p = Path(arg) with log_context(file = str(p)):
with log_context(path = str(p)):
try: try:
if p.name == "yarn.nix" or p.name.find("generated") != -1:
logger.warning("File looks autogenerated, skipping!")
continue
with p.open() as f: with p.open() as f:
for line in f: for line in f:
if line.strip(): if line.strip():
break break
if _SKIP_RE.search(line): if _SKIP_RE.search(line):
logger.warning("File looks autogenerated, skipping!") logger.log(skipLevel, "File looks autogenerated, skipping!")
continue return
fileToSRI(p) fileToSRI(p)
except Exception as exn: except Exception as exn:
logger.error( logger.error(
"Unhandled exception, skipping file!", "Unhandled exception, skipping file!",
@ -233,3 +229,19 @@ if __name__ == "__main__":
) )
else: else:
logger.info("Finished processing file") logger.info("Finished processing file")
for arg in argv[1:]:
p = Path(arg)
with log_context(arg = arg):
if p.is_file():
handleFile(p, skipLevel = logging.WARNING)
elif p.is_dir():
logger.info("Recursing into directory")
for q in p.glob("**/*.nix"):
if q.is_file():
if q.name == "yarn.nix" or q.name.find("generated") != -1:
logger.info("File looks autogenerated, skipping!")
continue
handleFile(q)