X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=python%2Fcumulus%2Frebuild_database.py;h=3f391d884f9c4024189fa49249bfecb36f76991a;hb=fae5b159f9526d9903121fea3d5da7ba4b4ba73b;hp=39bf7c11941479d2ced24c0a97281faf7e32beef;hpb=ac4811d6590281d656431c2c9d8d22137fe5276f;p=cumulus.git diff --git a/python/cumulus/rebuild_database.py b/python/cumulus/rebuild_database.py index 39bf7c1..3f391d8 100755 --- a/python/cumulus/rebuild_database.py +++ b/python/cumulus/rebuild_database.py @@ -26,6 +26,8 @@ the local database. This can be used to recover from a local database loss, given data from a previous backup. """ +from __future__ import division, print_function, unicode_literals + import base64 import hashlib import itertools @@ -113,7 +115,7 @@ class Chunker(object): def compute_breaks(self, buf): breaks = [0] signature = self.window_init() - for i in xrange(len(buf)): + for i in range(len(buf)): self.window_update(signature, ord(buf[i])) block_len = i - breaks[-1] + 1 if ((signature[0] % self.TARGET_CHUNK_SIZE == self.BREAKMARK_VALUE @@ -163,9 +165,9 @@ class Chunker(object): n -= i position = 0 - for next_start, (size, digest) in sorted(signatures.iteritems()): + for next_start, (size, digest) in sorted(signatures.items()): if next_start < position: - print "Warning: overlapping signatures, ignoring" + print("Warning: overlapping signatures, ignoring") continue skip(next_start - position) records.append(struct.pack(">H", size) + digest) @@ -177,7 +179,7 @@ class Chunker(object): """Loads signatures from the binary format stored in the database.""" entry_size = 2 + self.hash_size if len(signatures) % entry_size != 0: - print "Warning: Invalid signatures to load" + print("Warning: Invalid signatures to load") return {} null_digest = "\x00" * self.hash_size @@ -250,11 +252,11 @@ class DatabaseRebuilder(object): if metadata.items.type not in ("-", "f"): continue try: path = os.path.join(reference_path, metadata.items.name) - print "Path:", path + print("Path:", path) # TODO: Check file size for early abort if different self.rebuild_file(open(path), metadata) except IOError as e: - print e + print(e) pass # Ignore the file self.database.commit() @@ -301,6 +303,9 @@ class DatabaseRebuilder(object): buf = fp.read(length) verifier.update(buf) + # Zero blocks get no checksums, so skip further processing on them. + if object is None: continue + if exact: csum = cumulus.ChecksumCreator(CHECKSUM_ALGORITHM) csum.update(buf) @@ -320,10 +325,10 @@ class DatabaseRebuilder(object): subblock[k] = self.chunker.dump_signatures(subblock[k]) self.store_checksums(checksums, subblock) else: - print "Checksum mismatch" + print("Checksum mismatch") def store_checksums(self, block_checksums, subblock_signatures): - for (segment, object), (size, checksum) in block_checksums.iteritems(): + for (segment, object), (size, checksum) in block_checksums.items(): segmentid = self.segment_to_id(segment) self.cursor.execute( """insert or ignore into block_index(segmentid, object) @@ -437,8 +442,8 @@ if __name__ == "__main__": os.path.relpath(f, topdir)) if metadata: for (k, v) in sorted(metadata.items()): - print "%s: %s" % (k, v) - print + print("%s: %s" % (k, v)) + print() sys.exit(0) # Sample code to rebuild the segments table from metadata--needs to be