2013-11-15 22:35:32 -08:00
|
|
|
#!/usr/bin/env python
|
2013-10-30 17:54:47 -07:00
|
|
|
# vim:set sw=4 et:
|
|
|
|
#
|
|
|
|
|
|
|
|
"""
|
|
|
|
Dump contents of database to stdout. Database can be any file that the anydbm
|
|
|
|
module can read. Included with warcprox because it's useful for inspecting a
|
2013-11-01 19:04:48 -07:00
|
|
|
deduplication database or a playback index database, but it is a generic tool.
|
2013-10-30 17:54:47 -07:00
|
|
|
"""
|
|
|
|
|
2013-12-07 00:27:59 -08:00
|
|
|
try:
|
|
|
|
import dbm
|
2013-12-20 13:43:49 -08:00
|
|
|
from dbm import ndbm
|
2013-12-07 00:27:59 -08:00
|
|
|
whichdb = dbm.whichdb
|
2013-12-20 13:43:49 -08:00
|
|
|
|
2013-12-07 00:27:59 -08:00
|
|
|
except:
|
|
|
|
import anydbm
|
|
|
|
dbm = anydbm
|
|
|
|
from whichdb import whichdb
|
|
|
|
|
2013-10-30 17:54:47 -07:00
|
|
|
import sys
|
2013-12-07 00:27:59 -08:00
|
|
|
import os.path
|
2013-10-30 17:54:47 -07:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
sys.stderr.write("usage: {} DBM_FILE\n".format(sys.argv[0]))
|
|
|
|
exit(1)
|
|
|
|
|
2013-12-18 21:16:52 -08:00
|
|
|
filename = sys.argv[1]
|
|
|
|
which = whichdb(filename)
|
|
|
|
|
|
|
|
# if which returns none and the file does not exist, print usage line
|
|
|
|
if which == None and not os.path.exists(sys.argv[1]):
|
2013-12-07 00:27:59 -08:00
|
|
|
sys.stderr.write('No such file {}\n\n'.format(sys.argv[1]))
|
|
|
|
sys.stderr.write("usage: {} DBM_FILE\n".format(sys.argv[0]))
|
|
|
|
exit(1)
|
|
|
|
|
2013-12-18 21:16:52 -08:00
|
|
|
# covers case where an ndbm is checked with its extension & identified incorrectly
|
|
|
|
elif 'bsd' in which:
|
|
|
|
correct_file = filename.split(".db")[0]
|
|
|
|
correct_which = whichdb(correct_file)
|
|
|
|
if correct_which == ('dbm' or 'dbm.ndbm'):
|
|
|
|
filename = correct_file
|
|
|
|
which = correct_which
|
|
|
|
|
|
|
|
elif which == '':
|
|
|
|
sys.stderr.write("{} is an unrecognized database type\n".format(sys.argv[1]))
|
2013-12-20 13:47:41 -08:00
|
|
|
sys.stderr.write("Try the file again by removing the extension\n")
|
2013-12-18 21:16:52 -08:00
|
|
|
exit(1)
|
|
|
|
|
2013-12-20 13:47:41 -08:00
|
|
|
try:
|
|
|
|
out = sys.stdout.buffer
|
2013-11-05 18:39:51 -08:00
|
|
|
|
2013-12-20 13:47:41 -08:00
|
|
|
except AttributeError:
|
|
|
|
out = sys.stdout
|
|
|
|
|
|
|
|
out.write(filename.encode('UTF-8') + b' is a ' + which.encode('UTF-8') + b' db\n')
|
2013-10-30 17:54:47 -07:00
|
|
|
|
2013-12-20 13:47:41 -08:00
|
|
|
db = dbm.open(filename, 'r')
|
2013-11-05 18:39:51 -08:00
|
|
|
for key in db.keys():
|
2013-12-20 13:47:41 -08:00
|
|
|
out.write(key + b":" + db[key] + b"\n")
|