Python ships with a difflib(3) module which is used in the pdtest(1)
utility. The Differ.compare() routine is rather expensive, especially
when the set of input text becomes "large" (a few thousand lines). Use
the less expensive unified_diff() routine instead which does not suffer
from that cost. From the sigrok-test perspective the resulting data is
as usable.
from getopt import getopt
from tempfile import mkstemp
from subprocess import Popen, PIPE
from getopt import getopt
from tempfile import mkstemp
from subprocess import Popen, PIPE
-from difflib import Differ
+from difflib import unified_diff
from hashlib import md5
from shutil import copy
from hashlib import md5
from shutil import copy
def diff_text(f1, f2):
t1 = open(f1).readlines()
t2 = open(f2).readlines()
def diff_text(f1, f2):
t1 = open(f1).readlines()
t2 = open(f2).readlines()
- diff = []
- d = Differ()
- for line in d.compare(t1, t2):
- if line[:2] in ('- ', '+ '):
- diff.append(line.strip())
-
+ diff = list(unified_diff(t1, t2))
+ diff = diff[2:] # Strip two from/to filename lines with "+++"/"---".
+ diff = [d.strip() for d in diff if d[0] in ('+', '-')]