2003-02-03 11:08:42 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2004-03-03 13:42:08 -04:00
|
|
|
Synopsis: %(prog)s [-h|-b|-g|-r|-a|-d] [ picklefile ] dbfile
|
2003-02-03 11:08:42 -04:00
|
|
|
|
|
|
|
Read the given picklefile as a series of key/value pairs and write to a new
|
2003-06-19 22:01:19 -03:00
|
|
|
database. If the database already exists, any contents are deleted. The
|
2004-03-03 13:42:08 -04:00
|
|
|
optional flags indicate the type of the output database:
|
2003-02-03 11:08:42 -04:00
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
-a - open using dbm (open any supported format)
|
2004-03-03 13:42:08 -04:00
|
|
|
-b - open as bsddb btree file
|
2008-05-26 07:29:35 -03:00
|
|
|
-d - open as dbm.ndbm file
|
|
|
|
-g - open as dbm.gnu file
|
2004-03-03 13:42:08 -04:00
|
|
|
-h - open as bsddb hash file
|
|
|
|
-r - open as bsddb recno file
|
|
|
|
|
|
|
|
The default is hash. If a pickle file is named it is opened for read
|
|
|
|
access. If no pickle file is named, the pickle input is read from standard
|
|
|
|
input.
|
|
|
|
|
|
|
|
Note that recno databases can only contain integer keys, so you can't dump a
|
2003-03-06 20:47:40 -04:00
|
|
|
hash or btree database using db2pickle.py and reconstitute it to a recno
|
2004-03-03 13:42:08 -04:00
|
|
|
database with %(prog)s unless your keys are integers.
|
|
|
|
|
2003-02-03 11:08:42 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import getopt
|
|
|
|
try:
|
|
|
|
import bsddb
|
|
|
|
except ImportError:
|
|
|
|
bsddb = None
|
|
|
|
try:
|
2008-05-26 07:29:35 -03:00
|
|
|
import dbm.ndbm as dbm
|
2003-02-03 11:08:42 -04:00
|
|
|
except ImportError:
|
|
|
|
dbm = None
|
2003-10-28 12:17:54 -04:00
|
|
|
try:
|
2008-05-26 07:29:35 -03:00
|
|
|
import dbm.gnu as gdbm
|
2003-10-28 12:17:54 -04:00
|
|
|
except ImportError:
|
|
|
|
gdbm = None
|
2003-02-03 11:08:42 -04:00
|
|
|
try:
|
2009-11-08 20:45:40 -04:00
|
|
|
import dbm.ndbm as anydbm
|
2003-02-03 11:08:42 -04:00
|
|
|
except ImportError:
|
|
|
|
anydbm = None
|
|
|
|
import sys
|
|
|
|
try:
|
2009-11-08 20:45:40 -04:00
|
|
|
import pickle as pickle
|
2003-02-03 11:08:42 -04:00
|
|
|
except ImportError:
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
prog = sys.argv[0]
|
|
|
|
|
|
|
|
def usage():
|
2003-04-24 13:02:54 -03:00
|
|
|
sys.stderr.write(__doc__ % globals())
|
2003-02-03 11:08:42 -04:00
|
|
|
|
|
|
|
def main(args):
|
|
|
|
try:
|
2003-10-28 12:17:54 -04:00
|
|
|
opts, args = getopt.getopt(args, "hbrdag",
|
|
|
|
["hash", "btree", "recno", "dbm", "anydbm",
|
|
|
|
"gdbm"])
|
2003-02-03 11:08:42 -04:00
|
|
|
except getopt.error:
|
|
|
|
usage()
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if len(args) == 0 or len(args) > 2:
|
|
|
|
usage()
|
|
|
|
return 1
|
|
|
|
elif len(args) == 1:
|
|
|
|
pfile = sys.stdin
|
|
|
|
dbfile = args[0]
|
2004-03-03 13:42:08 -04:00
|
|
|
else:
|
2003-02-03 11:08:42 -04:00
|
|
|
try:
|
2004-03-03 13:42:08 -04:00
|
|
|
pfile = open(args[0], 'rb')
|
2003-02-03 11:08:42 -04:00
|
|
|
except IOError:
|
2004-03-03 13:42:08 -04:00
|
|
|
sys.stderr.write("Unable to open %s\n" % args[0])
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
2004-03-03 13:42:08 -04:00
|
|
|
dbfile = args[1]
|
2003-02-03 11:08:42 -04:00
|
|
|
|
|
|
|
dbopen = None
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt in ("-h", "--hash"):
|
|
|
|
try:
|
|
|
|
dbopen = bsddb.hashopen
|
|
|
|
except AttributeError:
|
2003-02-03 11:28:23 -04:00
|
|
|
sys.stderr.write("bsddb module unavailable.\n")
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
|
|
|
elif opt in ("-b", "--btree"):
|
|
|
|
try:
|
|
|
|
dbopen = bsddb.btopen
|
|
|
|
except AttributeError:
|
2003-02-03 11:28:23 -04:00
|
|
|
sys.stderr.write("bsddb module unavailable.\n")
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
|
|
|
elif opt in ("-r", "--recno"):
|
|
|
|
try:
|
|
|
|
dbopen = bsddb.rnopen
|
|
|
|
except AttributeError:
|
2003-02-03 11:28:23 -04:00
|
|
|
sys.stderr.write("bsddb module unavailable.\n")
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
|
|
|
elif opt in ("-a", "--anydbm"):
|
|
|
|
try:
|
|
|
|
dbopen = anydbm.open
|
|
|
|
except AttributeError:
|
2008-05-26 07:29:35 -03:00
|
|
|
sys.stderr.write("dbm module unavailable.\n")
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
2003-10-28 12:17:54 -04:00
|
|
|
elif opt in ("-g", "--gdbm"):
|
|
|
|
try:
|
|
|
|
dbopen = gdbm.open
|
|
|
|
except AttributeError:
|
2008-05-26 07:29:35 -03:00
|
|
|
sys.stderr.write("dbm.gnu module unavailable.\n")
|
2003-10-28 12:17:54 -04:00
|
|
|
return 1
|
2003-02-03 11:08:42 -04:00
|
|
|
elif opt in ("-d", "--dbm"):
|
|
|
|
try:
|
|
|
|
dbopen = dbm.open
|
|
|
|
except AttributeError:
|
2008-05-26 07:29:35 -03:00
|
|
|
sys.stderr.write("dbm.ndbm module unavailable.\n")
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
|
|
|
if dbopen is None:
|
|
|
|
if bsddb is None:
|
2003-02-03 11:28:23 -04:00
|
|
|
sys.stderr.write("bsddb module unavailable - ")
|
|
|
|
sys.stderr.write("must specify dbtype.\n")
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
dbopen = bsddb.hashopen
|
|
|
|
|
|
|
|
try:
|
|
|
|
db = dbopen(dbfile, 'c')
|
|
|
|
except bsddb.error:
|
2003-02-03 11:28:23 -04:00
|
|
|
sys.stderr.write("Unable to open %s. " % dbfile)
|
|
|
|
sys.stderr.write("Check for format or version mismatch.\n")
|
2003-02-03 11:08:42 -04:00
|
|
|
return 1
|
|
|
|
else:
|
2008-05-16 12:23:30 -03:00
|
|
|
for k in list(db.keys()):
|
2003-02-03 11:08:42 -04:00
|
|
|
del db[k]
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
(key, val) = pickle.load(pfile)
|
|
|
|
except EOFError:
|
|
|
|
break
|
|
|
|
db[key] = val
|
|
|
|
|
|
|
|
db.close()
|
|
|
|
pfile.close()
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main(sys.argv[1:]))
|