Has anybody used cx_bsdiff?

News Reader news at nportal.net
Tue Oct 25 17:04:26 CEST 2005


Hello,

I am trying to use cx_bsdiff 
(http://starship.python.net/crew/atuining/cx_bsdiff/index.html) to to 
make a diff and patch some files.  It appears that I can make the diff, 
but when I apply the patch, the result is not the same.

As far as I understand the documentation, I am using this correctly. 
Can anyone help me find what is wrong in the code below?

Thanks.  Please reply to the list.

NR.

=========================================================

import bsdiff, bz2
import cPickle as pickle
from filecmp import dircmp


def diffdir(comparator, basepath=''):
     return [os.path.join(basepath, fname)
             for fname in comparator.diff_files]


def descend(comparator, basepath=''):
     print 'Examining "%s"...' % basepath
     yield diffdir(comparator, basepath)
     for subdir in comparator.subdirs:
         subname = os.path.join(basepath, subdir)
         subcmp = comparator.subdirs[subdir]
         for subresult in descend(subcmp, subname):
             yield subresult


def compare_all(orig_dir, fix_dir):
     patch = {}
     comparator = dircmp(orig_dir, fix_dir, [])
     for diff_list in descend(comparator):
         for diff_file in diff_list:
             orig_fname = os.path.join(orig_dir, diff_file)
             fix_fname = os.path.join(fix_dir, diff_file)
             print 'Making diff of %s...' % diff_file
             orig_data = open(orig_fname, 'rb').read()
             fix_data = open(fix_fname, 'rb').read()
             dl = len(fix_data)
             ctrl, dblock, xblock = bsdiff.Diff(orig_data, fix_data)
             patch[diff_file] = (dl, ctrl, dblock, xblock)
     print 'Done.'
     return patch


def fix_all(tofix_dir, patch):
     for fname in patch:
         orig_fname = os.path.join(tofix_dir, fname)
         print 'Fixing %s...' % orig_fname
         datafile = open(orig_fname, 'rb')
         orig_data = datafile.read()
         datafile.close()
         fix_data = bsdiff.Patch(orig_data, *patch[fname])
         datafile = open('%s-fixed' % orig_fname, 'wb')
         datafile.write(fix_data)
         datafile.close()
         os.remove(orig_fname)
         os.rename('%s-fixed' % orig_fname, orig_fname)


def save(object, filename, protocol=-1):
     compressed = bz2.BZ2File(filename, 'wb')
     compressed.write(pickle.dumps(object, protocol))
     compressed.close()


def load(filename):
     compressed = bz2.BZ2File(filename, 'rb')
     object = pickle.loads(compressed.read())
     compressed.close()
     return object


def make_patch():
     orig_dir = raw_input('Original Directory: ')
     fix_dir = raw_input('Fixed Directory: ')
     savepatch = raw_input('Save result? (y/n): ')
     if savepatch == 'y':
         patchname = raw_input('Patch file name: ')
     else: savepatch = ''
     patch = compare_all(orig_dir, fix_dir)
     if savepatch:
         save(patch, patchname)




More information about the Python-list mailing list