Update main.py

This commit is contained in:
2022-04-03 18:53:58 +07:00
committed by GitHub
parent 4fa0e035dc
commit 9ed5ecb26c

74
main.py
View File

@@ -1,61 +1,63 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
hash_chunk_size = 65536
from sys import exit from sys import exit
from re import search
from hashlib import md5 from hashlib import md5
from os.path import getsize from os.path import exists, getsize
from progress.bar import IncrementalBar from progress.bar import IncrementalBar
hash_chunk_size = 65536
hash_pattern = r"[a-zA-Z0-9]{32,}" # Паттерн поиска хэш-суммы
def check_hash_file(fn): def check_hash_file(fn):
try: if not exists(fn):
with open(fn, "r") as f: if search(hash_pattern, fn).group() is not None:
fdata = f.read() return fn
fdata = fdata.split(" *") print(" ! Hash sum not found")
return fdata[0]
except FileNotFoundError:
return fn
def check_file_exists(fn):
try:
open(fn, "rb").close()
return True
except FileNotFoundError:
print(" ! File not found")
return False return False
except: with open(fn, "r") as f:
print(" ! Unknown error") fdata = f.read()
found_hash = search(hash_pattern, fdata).group()
if found_hash is not None:
# print(" found_hash:", found_hash)
return found_hash
print(" ! Hash sum not found")
return False return False
def generate_md5(fn): def generate_md5(fn):
if check_file_exists(fn): if not exists(fn):
hash_md5 = md5() print(" ! File not found")
with open(fn, "rb") as f: return False
bar = IncrementalBar(" Comparing...", max=int(getsize(fn) / hash_chunk_size), suffix='%(percent)d%%') hash_md5 = md5()
for chunk in iter(lambda: f.read(hash_chunk_size), b""): with open(fn, "rb") as f:
hash_md5.update(chunk) bar = IncrementalBar(" Generating hash sum...", max=int(getsize(fn) / hash_chunk_size), suffix='%(percent)d%%')
bar.next() for chunk in iter(lambda: f.read(hash_chunk_size), b""):
bar.finish() hash_md5.update(chunk)
return hash_md5.hexdigest() bar.next()
return False bar.finish()
return hash_md5.hexdigest()
def input_files(): def main():
file1 = str(input("\n Input first file: ")) file1 = str(input("\n Input first file: "))
file2 = str(input(" Input hash sum: ")) file2 = str(input(" Input hash sum (or file): "))
selected_mode = bool(int(input(" Select compare mode (0 or 1)\n 0 - hash sum; 1 - second file path: ")))
# print(" selected_mode:", selected_mode)
if (file1 != "") and (file2 != ""): if (file1 != "") and (file2 != ""):
print("\n Equals?:", generate_md5(file1) == check_hash_file(file2), "\n") if (selected_mode is False and exists(file2) and not file2.endswith(".txt")) or (selected_mode is True and not exists(file2)):
input_files() # if (not exists(file2) and selected_mode is True):
print(" ! Selected invalid mode")
main()
print("\n Equals?:", generate_md5(file1) == (check_hash_file(file2) if selected_mode is False else generate_md5(file2)), "\n")
main()
if __name__ == '__main__': if __name__ == '__main__':
try: try:
input_files() main()
except KeyboardInterrupt: except KeyboardInterrupt:
if input("\n Stop comparing? (y/n): ").lower() == "y": if input("\n Stop comparing? (y/n): ").lower() == "y":
exit() exit()
input_files()