mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add generate-hashes
to tools pkg
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
parent
3f7b5cc317
commit
74666bb08b
1 changed files with 44 additions and 0 deletions
44
tools/pkg.py
44
tools/pkg.py
|
@ -5,6 +5,8 @@ These commands are used to build Salt packages.
|
|||
from __future__ import annotations
|
||||
|
||||
import fnmatch
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
|
@ -176,3 +178,45 @@ def pre_archive_cleanup(ctx: Context, cleanup_path: str, pkg: bool = False):
|
|||
except FileNotFoundError:
|
||||
pass
|
||||
break
|
||||
|
||||
|
||||
@pkg.command(
|
||||
name="generate-hashes",
|
||||
arguments={
|
||||
"files": {
|
||||
"help": "The files to generate the hashes for.",
|
||||
"nargs": "*",
|
||||
},
|
||||
},
|
||||
)
|
||||
def generate_hashes(ctx: Context, files: list[pathlib.Path]):
|
||||
"""
|
||||
Generate "blake2b", "sha512" and "sha3_512" hashes for the passed files.
|
||||
"""
|
||||
for fpath in files:
|
||||
ctx.info(f"* Processing {fpath} ...")
|
||||
hashes = {}
|
||||
for hash_name in ("blake2b", "sha512", "sha3_512"):
|
||||
ctx.info(f" * Calculating {hash_name} ...")
|
||||
with fpath.open("rb") as rfh:
|
||||
try:
|
||||
digest = hashlib.file_digest(rfh, hash_name) # type: ignore[attr-defined]
|
||||
except AttributeError:
|
||||
# Python < 3.11
|
||||
buf = bytearray(2**18) # Reusable buffer to reduce allocations.
|
||||
view = memoryview(buf)
|
||||
digest = getattr(hashlib, hash_name)()
|
||||
while True:
|
||||
size = rfh.readinto(buf)
|
||||
if size == 0:
|
||||
break # EOF
|
||||
digest.update(view[:size])
|
||||
digest_file_path = fpath.parent / f"{fpath.name}.{hash_name}"
|
||||
hexdigest = digest.hexdigest()
|
||||
ctx.info(f" * Writing {digest_file_path} ...")
|
||||
digest_file_path.write_text(digest.hexdigest())
|
||||
hashes[hash_name] = hexdigest
|
||||
hashes_json_path = fpath.parent / f"{fpath.name}.json"
|
||||
ctx.info(f" * Writing {hashes_json_path} ...")
|
||||
hashes_json_path.write_text(json.dumps(hashes))
|
||||
ctx.info("Done")
|
||||
|
|
Loading…
Add table
Reference in a new issue