6.23.3.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:   if data == []: return []
    51:   cs = data[0][0]
    52:   cm = data[1][0]
    53:   sep = data[2]
    54:   lth = len(data)
    55:   sections = []
    56:   for i in range(2,lth):
    57:     if data[i] == sep:
    58:       sections.append([])
    59:     else:
    60:       sections[-1].append(data[i])
    61:   del data
    62:   del lth
    63:   del sep
    64: 
    65:   for i in range(len(sections)):
    66:     section = sections[i]
    67:     sections[i] = []
    68:     for j in range(len(section)):
    69:       line = section[j]
    70:       code = line[0]+line[1]
    71:       if code == cs*2 or code == cm*2:
    72:         k = 0
    73:         first = 0
    74:         count = 0
    75:         while line[k] not in '0123456789': k = k + 1
    76:         while line[k] in '0123456789':
    77:           first = first * 10 +ord(line[k])-ord('0')
    78:           k = k + 1
    79:         first = first - 1
    80:         sections[i].append([[first,0]])
    81:       else:
    82:         lineno = first + count
    83:         count = count + 1
    84:         sections[i][-1][0][1] = count
    85:         sections[i][-1].append(('%3d'%(lineno+1))+':'+line)
    86:   return sections
    87: 
    88: def patch_file(o,diff,m):
    89:   cmd = 'patch -s -c -o ' + m + ' ' + o + ' -'
    90:   print cmd
    91:   f = os.popen(cmd,'w')
    92:   f.write(diff)
    93:   result = f.close()
    94: 
    95: def posix_patch(o,diff,m):
    96:   patch_file(o,diff,m)
    97: 
End python section to interscript/utilities/diff.py[1]