gh-156 Add verify command to check all files for bit rot

This commit is contained in:
Jaisen Mathai 2016-12-13 22:27:12 -08:00
parent 475fbeb6aa
commit 9ad7358198
2 changed files with 27 additions and 0 deletions

View File

@ -142,6 +142,24 @@ def _generate_db(source):
db.add_hash(db.checksum(current_file), current_file) db.add_hash(db.checksum(current_file), current_file)
db.update_hash_db() db.update_hash_db()
RESULT.write()
@click.command('verify')
def _verify():
db = Db()
for checksum, file_path in db.all():
if not os.path.isfile(file_path):
RESULT.append((file_path, True))
continue
actual_checksum = db.checksum(file_path)
if checksum == actual_checksum:
RESULT.append((file_path, True))
else:
RESULT.append((file_path, None))
RESULT.write()
def update_location(media, file_path, location_name): def update_location(media, file_path, location_name):
@ -275,6 +293,7 @@ def main():
main.add_command(_import) main.add_command(_import)
main.add_command(_update) main.add_command(_update)
main.add_command(_generate_db) main.add_command(_generate_db)
main.add_command(_verify)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -183,6 +183,14 @@ class Db(object):
return None return None
def all(self):
"""Generator to get all entries from self.hash_db
:returns tuple(string)
"""
for checksum, path in self.hash_db.items():
yield (checksum, path)
def reset_hash_db(self): def reset_hash_db(self):
self.hash_db = {} self.hash_db = {}