#!/usr/bin/python # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import difflib import os import sys import tarfile import time def taropen(fn, pos=None): r = tarfile.open(fn, "r:*") if pos is not None: r.seek(pos) return r def do_diff(ai, bi, a, b, fa, fb): # XXX handle type changes e.g., dir<->link<->file # XXX handle mode changes e.g., 0644 -> 0755 a = (a and a.readlines() or []) b = (b and b.readlines() or []) d = list(difflib.unified_diff(a, b, fa, fb, time.ctime(ai.mtime), time.ctime(bi.mtime))) if not d: return print "diff -u %s %s" % (fa, fb) sys.stdout.writelines(d) def rlc(fn, n): return "/".join(fn.split("/")[n:]) def tardiff(a, b, xa=0, xb=0): if isinstance(xa, basestring): xa = int(xa) if isinstance(xb, basestring): xb = int(xb) ta = taropen(a) tb = taropen(b) na = dict((rlc(i.name, xa), i) for i in ta) nb = dict((rlc(i.name, xb), i) for i in tb) all = set(na) | set(nb) for fn in sorted(all): if fn in na and fn in nb: do_diff( na[fn], nb[fn], ta.extractfile(na[fn]), tb.extractfile(nb[fn]), os.path.join(os.path.basename(a), na[fn].name), os.path.join(os.path.basename(b), nb[fn].name)) elif fn in ta: print "Only in %s: %s" % (a, fn) else: print "Only in %s: %s" % (b, fn) if __name__ == '__main__': tardiff(*sys.argv[1:])