def weave_stats(weave_file, pb): from bzrlib.weavefile import read_weave wf = file(weave_file, 'rb') w = read_weave(wf) # FIXME: doesn't work on pipes weave_size = wf.tell() total = 0 vers = len(w) for i in range(vers): pb.update('checking sizes', i, vers) for origin, lineno, line in w._extract([i]): total += len(line) pb.clear() print 'versions %9d' % vers print 'weave file %9d bytes' % weave_size print 'total contents %9d bytes' % total print 'compression ratio %9.2fx' % (float(total) / float(weave_size)) if vers: avg = total/vers print 'average size %9d bytes' % avg print 'relative size %9.2fx' % (float(weave_size) / float(avg))
def weave_stats(weave_file, pb): from bzrlib.weavefile import read_weave wf = file(weave_file, 'rb') w = read_weave(wf) # FIXME: doesn't work on pipes weave_size = wf.tell() total = 0 vers = len(w) for i in range(vers): pb.update('checking sizes', i, vers) for origin, lineno, line in w._extract([i]): total += len(line) pb.clear() print 'versions %9d' % vers print 'weave file %9d bytes' % weave_size print 'total contents %9d bytes' % total print 'compression ratio %9.2fx' % (float(total) / float(weave_size)) if vers: avg = total / vers print 'average size %9d bytes' % avg print 'relative size %9.2fx' % (float(weave_size) / float(avg))
def test_written_detection(self): # Test detection of weave file corruption. # # Make sure that we can detect if a weave file has # been corrupted. This doesn't test all forms of corruption, # but it at least helps verify the data you get, is what you want. from cStringIO import StringIO w = Weave() w.add_lines('v1', [], ['hello\n']) w.add_lines('v2', ['v1'], ['hello\n', 'there\n']) tmpf = StringIO() write_weave(w, tmpf) # Because we are corrupting, we need to make sure we have the exact text self.assertEqual('# bzr weave file v5\n' 'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n' 'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n' 'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n', tmpf.getvalue()) # Change a single letter tmpf = StringIO('# bzr weave file v5\n' 'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n' 'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n' 'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n') w = read_weave(tmpf) self.assertEqual('hello\n', w.get_text('v1')) self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.check) # Change the sha checksum tmpf = StringIO('# bzr weave file v5\n' 'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n' 'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n' 'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n') w = read_weave(tmpf) self.assertEqual('hello\n', w.get_text('v1')) self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.check)
def test_written_detection(self): # Test detection of weave file corruption. # # Make sure that we can detect if a weave file has # been corrupted. This doesn't test all forms of corruption, # but it at least helps verify the data you get, is what you want. from cStringIO import StringIO w = Weave() w.add_lines('v1', [], ['hello\n']) w.add_lines('v2', ['v1'], ['hello\n', 'there\n']) tmpf = StringIO() write_weave(w, tmpf) # Because we are corrupting, we need to make sure we have the exact text self.assertEquals('# bzr weave file v5\n' 'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n' 'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n' 'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n', tmpf.getvalue()) # Change a single letter tmpf = StringIO('# bzr weave file v5\n' 'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n' 'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n' 'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n') w = read_weave(tmpf) self.assertEqual('hello\n', w.get_text('v1')) self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.check) # Change the sha checksum tmpf = StringIO('# bzr weave file v5\n' 'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n' 'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n' 'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n') w = read_weave(tmpf) self.assertEqual('hello\n', w.get_text('v1')) self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2') self.assertRaises(errors.WeaveInvalidChecksum, w.check)
def run(self, filename): from bzrlib.weavefile import read_weave from bzrlib.transport import get_transport from bzrlib import osutils vf = read_weave(file(filename, 'rb')) names = vf.versions() names.sort() print '\n'.join(names)
def run(self, weave_file, revision_a, revision_b): from bzrlib.weavefile import read_weave w = read_weave(file(weave_file, 'rb')) for state, line in w.plan_merge(revision_a, revision_b): # make sure to print every line with a newline, even if it doesn't # really have one if not line: continue if line[-1] != '\n': state += '!eol' line += '\n' if '\n' in line[:-1]: warning("line in weave contains embedded newline: %r" % line) print '%15s | %s' % (state, line),
def check_read_write(self, k): """Check the weave k can be written & re-read.""" from tempfile import TemporaryFile tf = TemporaryFile() write_weave(k, tf) tf.seek(0) k2 = read_weave(tf) if k != k2: tf.seek(0) self.log('serialized weave:') self.log(tf.read()) self.log('') self.log('parents: %s' % (k._parents == k2._parents)) self.log(' %r' % k._parents) self.log(' %r' % k2._parents) self.log('') self.fail('read/write check failed')
def readit(): return read_weave(file(argv[2], 'rb'))
def run(self, weave_file, revision_a, revision_b): from bzrlib.weavefile import read_weave w = read_weave(file(weave_file, 'rb')) p = w.plan_merge(revision_a, revision_b) sys.stdout.writelines(w.weave_merge(p))