findpresent.py (1623B)
1 #!/usr/bin/python3 2 3 import os 4 import sys 5 from hashlib import sha256 6 7 def get_file_hash(fi): 8 with open(fi, 'rb') as f: 9 return sha256(f.read()).hexdigest() 10 11 ### Generate hash for all files 12 def get_hash_dict(): 13 hash_to_filename = {} 14 for rootdir, _, files in os.walk('.'): 15 try: 16 #use grep on the result instead? 17 #files = [f for f in files if filefilter.lower() in f.lower()] 18 for fi in files: 19 fpath = os.path.join(rootdir, fi) 20 fhash = get_file_hash(fpath) 21 if fhash not in hash_to_filename: 22 hash_to_filename[fhash] = fi 23 except Exception as e : 24 print(e) 25 return hash_to_filename 26 27 def check_hashes(in_file): 28 hashes_to_find = {} 29 lines = open(in_file).read().split('\n') 30 for line in lines: 31 if ' ' not in line: 32 continue 33 fhash, fname = line.split(' ') 34 hashes_to_find[fhash] = fname 35 hashes_found = get_hash_dict() 36 num_found = 0 37 for fhash in hashes_to_find: 38 if fhash not in hashes_found: 39 print(hashes_to_find[fhash], f'not found ({fhash})') 40 else: 41 num_found += 1 42 print(num_found, 'files found') 43 44 if len(sys.argv) < 2: 45 print('Use: findpresent.py list | check') 46 exit(1) 47 if sys.argv[1] == 'list': 48 hash_to_filename = get_hash_dict() 49 for fhash in hash_to_filename: 50 print(fhash, hash_to_filename[fhash]) 51 if sys.argv[1] == 'check': 52 if len(sys.argv) < 3: 53 print('Use: findpresent.py check <checksum file>') 54 exit(1) 55 check_hashes(sys.argv[2])