Created
December 6, 2011 17:40
-
-
Save coderjo/1439113 to your computer and use it in GitHub Desktop.
whitemice's sql integrity check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sqlite3, sys, fnmatch, os, platform | |
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, patterns): | |
for root, dirs, files in os.walk(directory): | |
for basename in files: | |
for pattern in patterns: | |
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' ] | |
if "Windows" == platform.system(): | |
HOME = os.environ['APPDATA'] | |
ROOTS = [''] | |
for root in ROOTS: | |
top = unicode(os.path.join(HOME, root)) | |
if os.path.exists(top): | |
for filename in find_files(top, ['*.db', '*.sqlite']): | |
verify_database_file(filename) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment