From 5352d93fae187da6978c6279a051c260c862ef9c Mon Sep 17 00:00:00 2001 From: tke Date: Fri, 22 Mar 2024 14:28:54 +0100 Subject: [PATCH] Added Libarchive-Version tarsums --- tools/libarchivesum.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tools/libarchivesum.py diff --git a/tools/libarchivesum.py b/tools/libarchivesum.py new file mode 100644 index 0000000..b8bb348 --- /dev/null +++ b/tools/libarchivesum.py @@ -0,0 +1,34 @@ +import hashlib +import argparse +import sys + +try: + import libarchive +except ImportError: + sys.stderr.write('The libarchive package is missing. Please install libarchive-c first.\n\nsudo apt install python3-libarchive-c\n\n') + sys.exit(1) + +parser = argparse.ArgumentParser() +parser.add_argument('infile', nargs="+") +parser.add_argument('-c', '--hashtype', default="md5", choices=hashlib.algorithms_available) +args = parser.parse_args() + +def hash_files_in_archive(archive_path): + try: + with libarchive.file_reader(archive_path) as archive: + for entry in archive: + hasher = hashlib.new(args.hashtype) + for block in entry.get_blocks(): + hasher.update(block) + print(f'{hasher.hexdigest()} {entry.pathname}') + except AttributeError: + sys.stderr.write("It seems you're using a version of libarchive that doesn't have the 'file_reader' attribute. " + "This script requires 'python-libarchive-c'. Please ensure it's installed.\n") + sys.exit(2) + +# Example usage: +for infile in args.infile: + try: + hash_files_in_archive(infile) + except libarchive.exception.ArchiveError as e: + sys.stderr.write(f'Failed to open archive file {infile}: {e}\n')