#diffdiff.py



# Create a zip file containing the difference between two directories.



# Usage: python dirdiff.py newdir olddir [outputfile]



# Files files in newdir that are different in olddir or are not present



# will be zipped. If the outputfile parameter is not provided the a



# file diff.zip will be created.



#



from

import

from

import

from

import

from

import

from

import

def

chop

if

not

return

def

listfiles

for

in

for

in

yield

def

dirdiff

"w"

def

write

for

in

for

in

for

in

for

in

for

in

if

and

if

"__main__"

1

2

if

4

3

else

"diff.zip"

oswalk, statos.pathjoin, sep, splitsysargvitertoolsizipzipfileZipFile, ZIP_DEFLATED(base, path):base.endswith(sep):base = base + seppath[len(base):](path):base, dirs, fileswalk(path):files:join(base, f)(new_path, old_path, diff_file):zf = ZipFile(diff_file,(file_path):zf.write(file_path, chop(new_path, file_path), ZIP_DEFLATED)old, newizip(walk(old_path), walk(new_path)):(old_base, old_dirs, old_files) = old(new_base, new_dirs, new_files) = newset(new_dirs).difference(old_dirs):listfiles(join(new_base, d)):write(f)set(new_files).difference(old_files):write(join(new_base,f))fs = set(old_files) & set(new_files)ds = set(old_dirs) & set(new_dirs)old_dirs[:] = dsnew_dirs[:] = dsfs:file_old = join(old_base, f)file_new = join(new_base, f)(stat(file_old).st_mtime != stat(file_new).st_mtimefile(file_old).read()!=file(file_new).read()):write(join(new_base, f))zf.close()__name__ ==new = argv[old = argv[len(argv) ==out=argv[out=dirdiff(new, old, out)