diff options
author | nicoo <nicoo@mur.at> | 2023-09-13 17:14:10 +0000 |
---|---|---|
committer | nicoo <nicoo@mur.at> | 2023-09-13 17:24:28 +0000 |
commit | 98ca06851547a243007b7665ecb5cfc6d44a19a4 (patch) | |
tree | 2b4046ad82b46b07efb9ef1fe2e44e18044e96ba | |
parent | a0b1f164af944c6a23e01f390476c6bd277fbca2 (diff) |
sha256-to-hash.py: Improve logging
-rwxr-xr-x | maintainers/scripts/sha256-to-SRI.py | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/maintainers/scripts/sha256-to-SRI.py b/maintainers/scripts/sha256-to-SRI.py index ae9940d00b434..dcacb4c58044b 100755 --- a/maintainers/scripts/sha256-to-SRI.py +++ b/maintainers/scripts/sha256-to-SRI.py @@ -1,10 +1,14 @@ #!/usr/bin/env nix-shell -#! nix-shell -i "python3 -I" -p python3 +#! nix-shell -i "python3 -I" -p "python3.withPackages(p: with p; [ rich structlog ])" from contextlib import contextmanager from pathlib import Path +from structlog.contextvars import bound_contextvars as log_context -import logging, re +import re, structlog + + +logger = structlog.getLogger("sha256-to-SRI") nix32alphabet = "0123456789abcdfghijklmnpqrsvwxyz" @@ -78,9 +82,8 @@ def defToSRI(s: str) -> str: begin, end = m.span() match = m.string[begin:end] - logging.error( - f"Skipping '%s': an exception was raised during rewriting", - match, + logger.error( + "Skipping", exc_info = exn, ) return match @@ -124,8 +127,9 @@ def atomicFileUpdate(target: Path): def fileToSRI(p: Path): with atomicFileUpdate(p) as (og, new): - for line in og: - new.write(defToSRI(line)) + for i, line in enumerate(og): + with log_context(line=i): + new.write(defToSRI(line)) if __name__ == "__main__": @@ -133,8 +137,13 @@ if __name__ == "__main__": for arg in argv[1:]: p = Path(arg) - if not p.is_file(): - print(f"Argument '{arg}' is not a regular file's path", file=stderr) - else: - print(f"Processing '{arg}'") - fileToSRI(p) + with log_context(path=str(p)): + try: + fileToSRI(p) + except Exception as exn: + logger.error( + "Unhandled exception, skipping file!", + exc_info = exn, + ) + else: + logger.info("Finished processing file") |