96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Write checksum metadata for a Nexus release tarball."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import hashlib
|
||
|
|
import json
|
||
|
|
import tarfile
|
||
|
|
from datetime import datetime, timezone
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
|
||
|
|
def sha256_file(path: Path) -> str:
|
||
|
|
h = hashlib.sha256()
|
||
|
|
with path.open("rb") as fh:
|
||
|
|
for chunk in iter(lambda: fh.read(1024 * 1024), b""):
|
||
|
|
h.update(chunk)
|
||
|
|
return h.hexdigest()
|
||
|
|
|
||
|
|
|
||
|
|
def tar_entry_count(path: Path) -> int:
|
||
|
|
with tarfile.open(path, "r:gz") as tf:
|
||
|
|
return len(tf.getmembers())
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> int:
|
||
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
||
|
|
parser.add_argument("tarball", type=Path)
|
||
|
|
parser.add_argument("--json-output", required=True, type=Path)
|
||
|
|
parser.add_argument("--markdown-output", required=True, type=Path)
|
||
|
|
parser.add_argument(
|
||
|
|
"--sha256-output",
|
||
|
|
type=Path,
|
||
|
|
help="Optional checksum file path. Defaults to '<tarball>.sha256'.",
|
||
|
|
)
|
||
|
|
args = parser.parse_args()
|
||
|
|
|
||
|
|
tarball = args.tarball.resolve()
|
||
|
|
stat = tarball.stat()
|
||
|
|
metadata = {
|
||
|
|
"created_at_utc": datetime.now(timezone.utc).isoformat(),
|
||
|
|
"tarball": str(tarball),
|
||
|
|
"filename": tarball.name,
|
||
|
|
"size_bytes": stat.st_size,
|
||
|
|
"size_mb": round(stat.st_size / 1024 / 1024, 2),
|
||
|
|
"sha256": sha256_file(tarball),
|
||
|
|
"entries": tar_entry_count(tarball),
|
||
|
|
}
|
||
|
|
|
||
|
|
args.json_output.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
args.json_output.write_text(json.dumps(metadata, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||
|
|
|
||
|
|
sha256_output = args.sha256_output or Path(str(tarball) + ".sha256")
|
||
|
|
sha256_output.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
sha256_output.write_text(
|
||
|
|
f"{metadata['sha256']} {metadata['filename']}\n",
|
||
|
|
encoding="utf-8",
|
||
|
|
newline="\n",
|
||
|
|
)
|
||
|
|
|
||
|
|
lines = [
|
||
|
|
"# Nexus release artifact metadata",
|
||
|
|
"",
|
||
|
|
f"- File: `{metadata['filename']}`",
|
||
|
|
f"- Size: `{metadata['size_mb']} MB`",
|
||
|
|
f"- Entries: `{metadata['entries']}`",
|
||
|
|
f"- SHA256: `{metadata['sha256']}`",
|
||
|
|
f"- Created UTC: `{metadata['created_at_utc']}`",
|
||
|
|
"",
|
||
|
|
"## Verify after upload",
|
||
|
|
"",
|
||
|
|
"Linux:",
|
||
|
|
"",
|
||
|
|
"```bash",
|
||
|
|
f"sha256sum {metadata['filename']}",
|
||
|
|
"```",
|
||
|
|
"",
|
||
|
|
"PowerShell:",
|
||
|
|
"",
|
||
|
|
"```powershell",
|
||
|
|
f"Get-FileHash .\\{metadata['filename']} -Algorithm SHA256",
|
||
|
|
"```",
|
||
|
|
]
|
||
|
|
args.markdown_output.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||
|
|
|
||
|
|
print(args.json_output)
|
||
|
|
print(args.markdown_output)
|
||
|
|
print(sha256_output)
|
||
|
|
print(f"sha256={metadata['sha256']}")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
raise SystemExit(main())
|