summaryrefslogtreecommitdiff
path: root/rdiff-backup/rdiff_backup/myrdiff.py
blob: 48485f7ea4044ec7482e07b768658bacb065f5e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python

"""Like rdiff, but written in python and uses librsync module.

Useful for benchmarking and testing of librsync and _librsync.

"""

import librsync, sys
blocksize = 32768

def makesig(inpath, outpath):
	"""Write a signature of inpath at outpath"""
	sf = librsync.SigFile(open(inpath, "rb"))
	fout = open(outpath, "wb")
	while 1:
		buf = sf.read(blocksize)
		if not buf: break
		fout.write(buf)
	assert not sf.close()
	assert not fout.close()

def makedelta(sigpath, newpath, deltapath):
	"""Write delta at deltapath using signature at sigpath"""
	df = librsync.DeltaFile(open(sigpath, "rb"), open(newpath, "rb"))
	fout = open(deltapath, "wb")
	while 1:
		buf = df.read(blocksize)
		if not buf: break
		fout.write(buf)
	assert not df.close()
	assert not fout.close()

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()

if sys.argv[1] == "signature":
	makesig(sys.argv[2], sys.argv[3])
elif sys.argv[1] == "delta":
	makedelta(sys.argv[2], sys.argv[3], sys.argv[4])
elif sys.argv[1] == "patch":
	makepatch(sys.argv[2], sys.argv[3], sys.argv[4])
else: assert 0, "Bad mode argument %s" % (sys.argv[1],)