6.16.2.3. Posix Implementation

The following implementation utilises the os.popen and os.system function to dispatch requests to the operating system diff and patch commands, and is intended to work on posix compliant systems. Well, it works on my Linux box :-)
Start python section to interscript/utilities/diff.py[1 /1 ]
     1: #line 75 "diff.ipk"
     2: import os
     3: import tempfile
     4: import string
     5: import re
     6: 
     7: def compare_files(o,m):
     8:   cmd = 'diff -q '+o+' '+m
     9:   f = os.popen(cmd,'r')
    10:   output = f.read()
    11:   result = f.close()
    12:   return len(output)==0
    13: 
    14: def compare_code_files(o,m,**kwds):
    15:   # slack implementation
    16:   return compare_files(o,m)
    17: 
    18: def diff_files(o,m,patch=None, context=10):
    19:   cmd = 'diff -C'+str(context)+' '+o+' '+m
    20:   f = os.popen(cmd,'r')
    21:   output = f.read()
    22:   result = f.close()
    23:   if patch:
    24:     f = open(patch,'w')
    25:     f.write(output)
    26:     f.close()
    27:   return output
    28: 
    29: def diff_strings(o,m,context=0):
    30:   foname = tempfile.mktemp()
    31:   fmname = tempfile.mktemp()
    32:   fo = open(foname,'w')
    33:   fm = open(fmname,'w')
    34:   fo.write(o)
    35:   fm.write(m)
    36:   fo.close()
    37:   fm.close()
    38:   result = diff_files(foname, fmname,context=context)
    39:   os.unlink(foname)
    40:   os.unlink(fmname)
    41:   return result
    42: 
    43: def diff_lines(o,m,context=0):
    44:   os = string.join(o,'\n')+'\n'
    45:   om = string.join(m,'\n')+'\n'
    46:   result = diff_strings(os,om,context=context)
    47:   del os
    48:   del om
    49:   data = string.split(result,'\n')[:-1]
    50:   cs = data[0][0]
    51:   cm = data[1][0]
    52:   sep = data[2]
    53:   lth = len(data)
    54:   sections = []
    55:   for i in range(2,lth):
    56:     if data[i] == sep:
    57:       sections.append([])
    58:     else:
    59:       sections[-1].append(data[i])
    60:   del data
    61:   del lth
    62:   del sep
    63: 
    64:   for i in range(len(sections)):
    65:     section = sections[i]
    66:     sections[i] = []
    67:     for j in range(len(section)):
    68:       line = section[j]
    69:       code = line[0]+line[1]
    70:       if code == cs*2 or code == cm*2:
    71:         k = 0
    72:         first = 0
    73:         count = 0
    74:         while line[k] not in '0123456789': k = k + 1
    75:         while line[k] in '0123456789':
    76:           first = first * 10 +ord(line[k])-ord('0')
    77:           k = k + 1
    78:         first = first - 1
    79:         sections[i].append([[first,0]])
    80:       else:
    81:         lineno = first + count
    82:         count = count + 1
    83:         sections[i][-1][0][1] = count
    84:         sections[i][-1].append(('%3d'%(lineno+1))+':'+line)
    85:   return sections
    86: 
    87: def patch_file(o,diff,m):
    88:   cmd = 'patch -s -c -o ' + m + ' ' + o + ' -'
    89:   print cmd
    90:   f = os.popen(cmd,'w')
    91:   f.write(diff)
    92:   result = f.close()
    93: 
    94: def posix_patch(o,diff,m):
    95:   patch_file(o,diff,m)
    96: 
End python section to interscript/utilities/diff.py[1]