4
Synopsis: %(prog)s [-h|-b|-g|-r|-a|-d] [ picklefile ] dbfile
6
Read the given picklefile as a series of key/value pairs and write to a new
7
database. If the database already exists, any contents are deleted. The
8
optional flags indicate the type of the output database:
10
-a - open using dbm (open any supported format)
11
-b - open as bsddb btree file
12
-d - open as dbm.ndbm file
13
-g - open as dbm.gnu file
14
-h - open as bsddb hash file
15
-r - open as bsddb recno file
17
The default is hash. If a pickle file is named it is opened for read
18
access. If no pickle file is named, the pickle input is read from standard
21
Note that recno databases can only contain integer keys, so you can't dump a
22
hash or btree database using db2pickle.py and reconstitute it to a recno
23
database with %(prog)s unless your keys are integers.
33
import dbm.ndbm as dbm
37
import dbm.gnu as gdbm
41
import dbm.ndbm as anydbm
46
import pickle as pickle
53
sys.stderr.write(__doc__ % globals())
57
opts, args = getopt.getopt(args, "hbrdag",
58
["hash", "btree", "recno", "dbm", "anydbm",
64
if len(args) == 0 or len(args) > 2:
72
pfile = open(args[0], 'rb')
74
sys.stderr.write("Unable to open %s\n" % args[0])
80
if opt in ("-h", "--hash"):
82
dbopen = bsddb.hashopen
83
except AttributeError:
84
sys.stderr.write("bsddb module unavailable.\n")
86
elif opt in ("-b", "--btree"):
89
except AttributeError:
90
sys.stderr.write("bsddb module unavailable.\n")
92
elif opt in ("-r", "--recno"):
95
except AttributeError:
96
sys.stderr.write("bsddb module unavailable.\n")
98
elif opt in ("-a", "--anydbm"):
101
except AttributeError:
102
sys.stderr.write("dbm module unavailable.\n")
104
elif opt in ("-g", "--gdbm"):
107
except AttributeError:
108
sys.stderr.write("dbm.gnu module unavailable.\n")
110
elif opt in ("-d", "--dbm"):
113
except AttributeError:
114
sys.stderr.write("dbm.ndbm module unavailable.\n")
118
sys.stderr.write("bsddb module unavailable - ")
119
sys.stderr.write("must specify dbtype.\n")
122
dbopen = bsddb.hashopen
125
db = dbopen(dbfile, 'c')
127
sys.stderr.write("Unable to open %s. " % dbfile)
128
sys.stderr.write("Check for format or version mismatch.\n")
131
for k in list(db.keys()):
136
(key, val) = pickle.load(pfile)
146
if __name__ == "__main__":
147
sys.exit(main(sys.argv[1:]))