From 722c42974e3d4835ca90db2b1cd5801384641d4e Mon Sep 17 00:00:00 2001 From: Kaurin <2141359+Kaurin@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:06:21 +0100 Subject: [PATCH] Checksum fix --- .vscode/settings.json | 4 ++-- checksum_creator.py | 45 ++++++++++++------------------------------- setup.cfg | 2 ++ 3 files changed, 16 insertions(+), 35 deletions(-) create mode 100644 setup.cfg diff --git a/.vscode/settings.json b/.vscode/settings.json index 4197d4c..5520504 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "python.analysis.typeCheckingMode": "basic", - "python.linting.flake8Enabled": false, + "python.linting.flake8Enabled": true, "python.linting.enabled": true, - "python.linting.pylintEnabled": true + "python.linting.pylintEnabled": false } \ No newline at end of file diff --git a/checksum_creator.py b/checksum_creator.py index d82ec30..1f1eafc 100644 --- a/checksum_creator.py +++ b/checksum_creator.py @@ -5,35 +5,12 @@ 3. iterates all files in the directory to checksum the total 4. Writes the checksum as .sha256sum ''' +import subprocess import hashlib from pathlib import Path, PurePath import os from inspect import getsourcefile -from tempfile import TemporaryDirectory from locale import getpreferredencoding -import py7zr - - -def checksum_list_of_files(filenames: list[str]) -> str: - ''' - Calculate the sha256 sum of multiple files - Stolen from: - https://stackoverflow.com/questions/34807537/generating-one-md5-sha1-checksum-of-multiple-files-in-python - ''' - hashtmp = hashlib.sha256() - for filename in filenames: - try: - hashtmp.update(Path(filename).read_bytes()) - except IsADirectoryError: - pass - return hashtmp.hexdigest() - -def extract_7z(filename: str, output_dir: str): - ''' - Extracts a 7z file - ''' - with py7zr.SevenZipFile(filename, mode='r') as archive: - archive.extractall(path=output_dir) def get_files_in_dir(directory: str) -> list[str]: @@ -46,27 +23,29 @@ def get_files_in_dir(directory: str) -> list[str]: return all_files - def main(): '''main loop''' - script_dir = os.path.dirname(getsourcefile(lambda:0)) # type: ignore + script_dir = os.path.dirname(getsourcefile(lambda: 0)) # type: ignore staging_dir = os.path.join(script_dir, "Staging") compressed_dir = os.path.join(script_dir, "Compressed") checksums_dir = os.path.join(staging_dir, "Checksums") - list_of_7z_files = [arch_file for arch_file in get_files_in_dir(compressed_dir) \ - if arch_file.endswith(".7z")] + list_of_7z_files = [arch_file for arch_file in get_files_in_dir(compressed_dir) + if arch_file.endswith(".7z")] for file_7z in list_of_7z_files: - with TemporaryDirectory() as tmpdirname: - extract_7z(file_7z, tmpdirname) - chksum = checksum_list_of_files(get_files_in_dir(tmpdirname)) + # Py7zr library doesn't offer a simple way to get the complete extracted + # bytestream like the 7z utility. + z_subprocess = subprocess.run(["7z", "e", "-so", file_7z], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + check=True, + text=False) + chksum = hashlib.sha256(z_subprocess.stdout).hexdigest() dest_chksumfile = os.path.join(checksums_dir, - PurePath(file_7z).stem + ".sha256sum") + PurePath(file_7z).stem + ".sha256sum") print(f"Writing {dest_chksumfile}") Path(Path(dest_chksumfile).resolve().parents[0]).mkdir(parents=True, exist_ok=True) with open(dest_chksumfile, "w", encoding=getpreferredencoding()) as file_final: file_final.write(chksum) - if __name__ == '__main__': main() diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..51b50a0 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 100 \ No newline at end of file