Created
April 8, 2017 22:22
-
-
Save KingCprey/090c2f5d1588583def53906168e8631a to your computer and use it in GitHub Desktop.
Python Script to compare directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os,hashlib | |
dir_orig="yo momma" | |
dir_new="another one" | |
def splitPath(p):return p.split(os.sep) | |
def mapDirs(dp,topdown=True): | |
alldirs={"dirs":{}} | |
for root,dirs,files in os.walk(dp,topdown=topdown): | |
for d in dirs: | |
path=splitPath(os.path.join(root,d)) | |
m=alldirs | |
for i,p in enumerate(path): | |
if not p in m["dirs"]:m["dirs"][p]={"dirs":{}} | |
m=m["dirs"][p] | |
return alldirs | |
def mapDirsFiles(dp,topdown=True): | |
allstuff={"dirs":{},"files":[]} | |
for root,dirs,files in os.walk(dp,topdown=topdown): | |
for d in dirs: | |
path=splitPath(os.path.join(root,d)) | |
m=allstuff | |
for i,p in enumerate(path): | |
if not p in m["dirs"]:m["dirs"][p]={"dirs":{},"files":[]} | |
m=m["dirs"][p] | |
dirpath=splitPath(root) | |
dirm=allstuff | |
for dp2 in dirpath: | |
dirm=dirm["dirs"][dp2] | |
for f in files: | |
dirm["files"].append(f) | |
return allstuff | |
def walkMap(m): | |
pass | |
def removeRoot(path,root):return path.replace(root,"") | |
def mapDirs(*paths): | |
obj={} | |
for p in paths: | |
p_abspath=os.path.abspath(p) | |
obj[p_abspath]={"f":[],"d":[]} | |
for root,dirs,files in os.walk(p_abspath): | |
r=root.replace(p_abspath,"") | |
if len(r)==0:r="\\" | |
for f in files: | |
obj[p_abspath]["f"].append(os.path.join(r,f)) | |
for d in files: | |
obj[p_abspath]["d"].append(os.path.join(r,d)) | |
return obj | |
def getmd5(path,buffsize=4096): | |
h=hashlib.md5() | |
with open(path, "rb") as inp: | |
for byt in iter(lambda: inp.read(buffsize), b""): | |
h.update(byt) | |
return h.hexdigest() | |
def compareDirs(a,b): | |
a_abspath=os.path.abspath(a) | |
b_abspath=os.path.abspath(b) | |
dirs=mapDirs(a_abspath,b_abspath) | |
output="" | |
for bf in dirs[b_abspath]["f"]: | |
if not bf in dirs[a_abspath]["f"]: | |
output+="+ {0}\r\n".format(bf) | |
else: | |
a_md5=getmd5(os.path.join(a_abspath,bf.strip("\\"))) | |
b_md5=getmd5(os.path.join(b_abspath,bf.strip("\\"))) | |
if not a_md5==b_md5:output+="!= {0} {1} {2}\r\n".format(bf,a_md5,b_md5) | |
for af in dirs[a_abspath]["f"]: | |
if not af in dirs[b_abspath]["f"]: | |
output+="- {0}\r\n".format(af) | |
return output | |
output=compareDirs(dir_orig,dir_new) | |
with open("compared.txt",'w')as out: | |
out.write(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment