Skip to content

Commit

Permalink
Refactor do_verify for readability
Browse files Browse the repository at this point in the history
Extract size/checksum computation logic into separate functions for
gzipped and non-gzipped files.
  • Loading branch information
mgedmin committed Nov 5, 2013
1 parent 1bcacfe commit cb45088
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/ZODB/scripts/repozo.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,38 +672,41 @@ def do_verify(options):
log("Verifying %s", filename)
try:
if filename.endswith('fsz'):
fp = gzip.open(filename, 'rb')
actual_sum, size = get_checksum_and_size_of_gzipped_file(filename, options.quick)
when_uncompressed = ' (when uncompressed)'
else:
fp = open(filename, 'rb')
actual_sum, size = get_checksum_and_size_of_file(filename, options.quick)
when_uncompressed = ''
except IOError:
error("%s is missing", filename)
continue
try:
fp.seek(0, 2)
except ValueError:
# can't seek in gzipped files
if options.quick:
size = file_size(fp)
actual_sum = None
else:
actual_sum, size = checksum_and_size(fp)
else:
size = fp.tell()
if options.quick or size != expected_size:
actual_sum = None
else:
fp.seek(0)
actual_sum = checksum(fp, size)
if size != expected_size:
error("%s is %d bytes%s, should be %d bytes", filename,
size, when_uncompressed, expected_size)
elif not options.quick:
if actual_sum != sum:
error("%s has checksum %s%s instead of %s", filename,
actual_sum, when_uncompressed, sum)
fp.close()


def get_checksum_and_size_of_gzipped_file(filename, quick):
with gzip.open(filename, 'rb') as fp:
if quick:
return None, file_size(fp)
else:
return checksum_and_size(fp)


def get_checksum_and_size_of_file(filename, quick):
with open(filename, 'rb') as fp:
fp.seek(0, 2)
actual_size = fp.tell()
if quick:
actual_sum = None
else:
fp.seek(0)
actual_sum = checksum(fp, actual_size)
return actual_sum, actual_size


def main(argv=None):
Expand Down

0 comments on commit cb45088

Please sign in to comment.