Skip to content

Instantly share code, notes, and snippets.

@obsti8383
Last active November 16, 2019 16:49
Show Gist options
  • Save obsti8383/e7b2a791f7a5c6d7cae8c3a7dc251d5a to your computer and use it in GitHub Desktop.
Save obsti8383/e7b2a791f7a5c6d7cae8c3a7dc251d5a to your computer and use it in GitHub Desktop.
Compare two md5sum files (needs to be same filename format) and prints out which file have been changed/removed/added (example: python3 md5_compare.py oldmd5s.md5 newmd5s.md5))
#!/usr/bin/python3
import sys
def prRed(output): print("\033[91m{}\033[00m".format(output))
if len(sys.argv) < 3:
print("Not enough arguments. Need two files to compare (old first, new last).")
exit(1)
if len(sys.argv) > 3:
print("Too many arguments. Need two files to compare only (old first, new last).")
exit(1)
oldfilename = sys.argv[1]
newfilename = sys.argv[2]
oldmd5file = open(oldfilename)
oldmd5lines = oldmd5file.readlines()
oldmd5 = {}
for line in oldmd5lines:
line = line.strip('\n').strip()
if line.startswith("#"):
print("is#")
continue
if line=="":
print("empty")
continue
splitline = line.split(" ")
oldmd5[splitline[1]]=splitline[0]
newmd5file = open(newfilename)
newmd5lines = newmd5file.readlines()
newmd5 = {}
for line in newmd5lines:
line = line.strip('\n').strip()
if line.startswith("#"):
print("is#")
continue
if line=="":
print("empty")
continue
splitline = line.split(" ")
newmd5[splitline[1]]=splitline[0]
statValid = 0
statNew = 0
statChanged = 0
statRemoved = 0
for fn in newmd5:
if not oldmd5.get(fn):
print(fn+": new")
statNew += 1
continue
if oldmd5[fn] == newmd5[fn]:
#print(fn+": valid")
statValid += 1
oldmd5.pop(fn)
continue
if oldmd5[fn] != newmd5[fn]:
if sys.stdout.isatty():
prRed(fn+": changed")
else:
print(fn+": changed")
statChanged += 1
oldmd5.pop(fn)
continue
print(fn+": unhandled")
for fn in oldmd5:
print(fn+": removed")
statRemoved += 1
print("\nStatistics:\n- Valid: "+str(statValid)+"\n- New: "+str(statNew)+"\n- Removed: "+str(statRemoved)+"\n- Changed: "+str(statChanged))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment