#!/usr/bin/env python import sqlite3, sys, fnmatch, os def verify_database_file(path): connection = None sys.stdout.write('Checking database @ "{0}".\n'.format(path)) try: connection = sqlite3.connect(path, isolation_level="EXCLUSIVE") except Exception, e: sys.stderr.write('Unable to connect to database.\n') sys.stderr.write(e.message) else: sys.stdout.write('Connected to database.\n') try: cursor = connection.cursor() cursor.execute('''pragma integrity_check;''') cursor.close() except Exception, e: sys.stderr.write('Integrity check failed for "{0}".\n'.format(path)) else: sys.stdout.write('Integrity check passed.\n') try: cursor = connection.cursor() cursor.execute('''vacuum;''') cursor.close() except Exception, e: sys.stderr.write('Vaccum operation failed on "{0}".\n'.format(path)) else: sys.stdout.write('Vaccum operation completed.\n') finally: if connection: connection.close(); sys.stdout.write('"{0}" closed.\n'.format(path)) sys.stdout.write('\n') def find_files(directory, pattern): for root, dirs, files in os.walk(directory): for basename in files: if fnmatch.fnmatch(basename, pattern): filename = os.path.join(root, basename) yield filename if __name__ == '__main__': HOME = os.path.expanduser("~") ROOTS = [ '.local/share', '.cache', '.pki', '.rcc', '.config' ] for root in ROOTS: top = '{0}/{1}'.format(HOME, root) if os.path.exists(top): for filename in find_files(top, '*.db'): verify_database_file(filename)