Advertisement
DeaD_EyE

compress hash example

Jul 11th, 2021
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. import hashlib
  2. import mmap
  3. import subprocess
  4. import sys
  5. from argparse import ArgumentParser
  6. from bz2 import BZ2File
  7. from gzip import GzipFile
  8. from lzma import LZMAFile
  9. from pathlib import Path
  10.  
  11.  
  12. def compressor(source: Path, target: Path, CompressClass, buffer_size=1024 ** 2):
  13.     with CompressClass(target, "w") as target, source.open("rb") as source:
  14.         buffer = bytearray(buffer_size)
  15.         view = memoryview(buffer)
  16.         while True:
  17.             size = source.readinto(buffer)
  18.             if size == 0:
  19.                 break
  20.  
  21.             target.write(view[:size])
  22.  
  23.  
  24. def hasher(algo, files):
  25.     result = []
  26.     for file in files:
  27.         with file.open("rb") as fd:
  28.             with mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ) as mm:
  29.                 dig = hashlib.new(algo, mm).hexdigest()
  30.                 result.append(f"{dig}  {file.name}")
  31.  
  32.     result = "\n".join(result)
  33.     with open(f"{files[0].stem}.{algo}", "w") as hash_file:
  34.         hash_file.write(result)
  35.  
  36.  
  37. def compress(image):
  38.     bz2 = image.with_suffix(".img.bz2")
  39.     xz = image.with_suffix(".img.xz")
  40.     gz = image.with_suffix(".img.gz")
  41.     md5 = image.with_suffix(".md5")
  42.     sha256 = image.with_suffix(".sha256")
  43.  
  44.     print(f"Komprimiere {bz2}")
  45.  
  46.     # with bz2.open("wb") as fd:
  47.     #    subprocess.run(["bzip2", "-c", "-9", "-k", str(image)], stdout=fd)
  48.  
  49.     compressor(image, bz2, BZ2File)
  50.  
  51.     print(f"Komprimiere {xz}")
  52.  
  53.     # with xz.open("wb") as fd:
  54.     #     subprocess.run(["xz", "-c", "-9", "-k", str(image)], stdout=fd)
  55.  
  56.     compressor(image, xz, LZMAFile)
  57.  
  58.     print(f"Komprimiere {gz}")
  59.     compressor(image, gz, GzipFile)
  60.  
  61.     print("Erstelle md5sum")
  62.     hasher("md5", [image, bz2, gz, xz])
  63.  
  64.     # with md5.open("wb") as fd:
  65.     #     subprocess.run(["md5sum", str(image), str(bz2), str(xz)], stdout=fd)
  66.  
  67.     print("Erstelle sha256sum")
  68.     hasher("sha256", [image, bz2, gz, xz])
  69.  
  70.     # with sha256.open("wb") as fd:
  71.     #     subprocess.run(["sha256sum", str(image), str(bz2), str(xz)], stdout=fd)
  72.  
  73.  
  74. def main(image):
  75.     if not image.exists():
  76.         raise ValueError("Image file does not exist.")
  77.     compress(image)
  78.  
  79.  
  80. def get_args():
  81.     parser = ArgumentParser()
  82.     parser.add_argument("file", type=Path, help="Image to compress")
  83.     return parser.parse_args()
  84.  
  85.  
  86. if __name__ == "__main__":
  87.     args = get_args()
  88.     main(args.file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement