#!/bin/sh # $Id; diffa.sh $ # # Author: Tomi Ollila -- too ät iki piste fi # # Copyright (c) 2008 Tomi Ollila # All rights reserved # # Created: Fri Jan 18 17:38:53 EET 2008 too # Last modified: Thu May 15 16:54:42 EEST 2008 too LC_ALL=C LANG=C; export LC_ALL LANG echon () { echo "$@" | tr -d \\012; } echo2 () { echo "$@" >&2; } die () { echo2 "$@"; exit 1; } case $3 in '') die Usage: $0 linewidth archive1 archive2 ;; esac case $1 in [1-9][0-9]|[1-9][0-9][0-9]) ;; *) die Linewidth "'"$1"'" not good ;; esac test -f $2 || die $2: not a file test -f $3 || die $3: not a file test $2 -ef $3 && die Both archives are the same td=`mktemp -d _diffa.XXXXXX` trap "rm -rf $td" 0 r () { echo2 + "$@"; "$@"; } x_it () { case $1 in *.tar.gz) r tar -C $td -zxf $1 ;; *.tar.bz2) r tar -C $td -jxf $1 ;; *) die $1: unknown file format ;; esac } x_it $2 case `ls -1A $td | wc` in *' '1' '*) ;; *) ls -lA $td; echo2; die $2 has more than 1 subdirectory ;; esac sd1=`ls -1A $td` x_it $3 case `ls -1A $td | wc` in *' '2' '*) ;; *' '1' '*) echo2; die $3 extracted to same directory as $2 ;; *) ls -lA $td; echo2; die $3 has more than 1 subdirectory ;; esac sd2=`ls -1A $td | sed "\\|$sd1|d"` #echo $sd1, $sd2; exit 0 (cd $td; diff -ru --brief "$sd1" "$sd2") | python -c ' import sys, os, re try: from difflib import HtmlDiff except: print >> sys.stderr, "\nThis program requires python 2.4 or newer.\n" sys.exit(1) os.chdir("'"$td"'") def only(where, what): print "
Only in %s: %s
" % (where, what) def diff(file1, file2): lines1 = ( i.rstrip().expandtabs(8) for i in file(file1) ) lines2 = ( i.rstrip().expandtabs(8) for i in file(file2) ) print "" print HtmlDiff(8, '"$1"').make_table(lines1, lines2, file1, file2, True, 3) print "
" only_re = re.compile("^Only in\s+(\S+):\s+(\S+)") diff_re = re.compile("^Files\s+(\S+)\s+and\s+(\S+)") common_re = re.compile("^Common subdirectories:") #for line in HtmlDiff(8, 80).make_file("", "").split("\n"): # print line # if line.find("") >= 0: # break for line in sys.stdin: m = diff_re.search(line) if m: diff(m.group(1), m.group(2)) continue m = only_re.search(line) if m: only(m.group(1), m.group(2)) continue m = common_re.search(line) if m: continue print "XXX", line print "\n