import zipfile
import difflib
import os.path
import sys
def mydiff(fromarc, toarc, n=3):
to_zip = zipfile.ZipFile(toarc)
from_zip = zipfile.ZipFile(fromarc)
fromto = []
for t in to_zip.namelist():
name,ext = os.path.splitext(t)
s = t
if ext == ".cpp":
s = name + ".c"
for f in from_zip.namelist():
if f.endswith(s):
fromto.append([t,f])
for ft in fromto:
tofile,fromfile = ft
with to_zip.open(tofile, 'U') as f:
tolines = f.readlines()
with from_zip.open(fromfile, 'U') as f:
fromlines = f.readlines()
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, n=n)
sys.stdout.writelines(diff)
if __name__ == '__main__':
fromarc = "AN11321.zip"
toarc = "pms-CMSIS-DAP.zip"
mydiff(fromarc, toarc, n=3)