Skip to content

Commit

Permalink
Checksum fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaurin committed Oct 20, 2023
1 parent 722dd7b commit 722c429
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 12 additions & 33 deletions checksum_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,12 @@
3. iterates all files in the directory to checksum the total
4. Writes the checksum as <basename_of_7z>.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]:
Expand All @@ -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()
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 100

0 comments on commit 722c429

Please sign in to comment.