def makepatch(basis_path, delta_path, new_path): """Write new given basis and delta""" pf = librsync.PatchedFile(open(basis_path, "rb"), open(delta_path, "rb")) fout = open(new_path, "wb") while 1: buf = pf.read(blocksize) if not buf: break fout.write(buf) assert not pf.close() assert not fout.close()
def testPatch(self): """Test patching against Rdiff""" MakeRandomFile(self.basis.path) assert not os.system("rdiff signature %s %s" % (self.basis.path, self.sig.path)) for i in range(5): MakeRandomFile(self.new.path) assert not os.system("rdiff delta %s %s %s" % (self.sig.path, self.new.path, self.delta.path)) fp = self.new.open("rb") real_new = fp.read() fp.close() pf = librsync.PatchedFile(self.basis.open("rb"), self.delta.open("rb")) librsync_new = pf.read() pf.close() assert real_new == librsync_new, \ (len(real_new), len(librsync_new))
def testPatch(self): """Test patching against Rdiff""" MakeRandomFile(self.basis.path) self._clean_file(self.sig) self.assertEqual(os.system( b"rdiff signature %s %s" % (self.basis.path, self.sig.path)), 0) for i in range(5): MakeRandomFile(self.new.path) self._clean_file(self.delta) self.assertEqual(os.system( b"rdiff delta %s %s %s" % (self.sig.path, self.new.path, self.delta.path)), 0) fp = self.new.open("rb") real_new = fp.read() fp.close() pf = librsync.PatchedFile(self.basis.open("rb"), self.delta.open("rb")) librsync_new = pf.read() pf.close() self.assertEqual(real_new, librsync_new)
def patch_local(rp_basis, rp_delta, outrp=None, delta_compressed=None): """ Patch routine that must be run locally, writes to outrp This should be run local to rp_basis because it needs to be a real file (librsync may need to seek around in it). If outrp is None, patch rp_basis instead. The return value is the close value of the delta, so it can be used to produce hashes. """ assert rp_basis.conn is Globals.local_connection, ( "This function must run locally and not over '{conn}'.".format( conn=rp_basis.conn)) if delta_compressed: deltafile = rp_delta.open("rb", 1) else: deltafile = rp_delta.open("rb") patchfile = librsync.PatchedFile(rp_basis.open("rb"), deltafile) if outrp: return outrp.write_from_fileobj(patchfile) else: return _write_via_tempfile(patchfile, rp_basis)
def write_patched_fp(basis_fp, delta_fp, out_fp): """Write patched file to out_fp given input fps. Closes input files""" rpath.copyfileobj(librsync.PatchedFile(basis_fp, delta_fp), out_fp) basis_fp.close() delta_fp.close()