blob: 5cd550cfdf6073892e023891efbc4ddcdaf9377f [file] [log] [blame]
Skip Montanaro9db49c82003-02-03 15:08:42 +00001#!/usr/bin/env python
2
3"""
4Synopsis: %(prog)s [-h|-b|-r|-a|-d] dbfile [ picklefile ]
5
6Read the given picklefile as a series of key/value pairs and write to a new
7bsddb database. If the database already exists, any contents are deleted.
8The optional flags indicate the type of the database (bsddb hash, bsddb
9btree, bsddb recno, anydbm, dbm). The default is hash. If a pickle file is
10named it is opened for read access. If no pickle file is named, the pickle
11input is read from standard input.
12
13Note that recno databases can only contain numeric keys, so you can't dump a
Skip Montanaroea7598e2003-03-07 00:47:40 +000014hash or btree database using db2pickle.py and reconstitute it to a recno
Skip Montanaro9db49c82003-02-03 15:08:42 +000015database with %(prog)s.
16"""
17
18import getopt
19try:
20 import bsddb
21except ImportError:
22 bsddb = None
23try:
24 import dbm
25except ImportError:
26 dbm = None
27try:
28 import anydbm
29except ImportError:
30 anydbm = None
31import sys
32try:
33 import cPickle as pickle
34except ImportError:
35 import pickle
36
37prog = sys.argv[0]
38
39def usage():
Skip Montanaro7c1274d2003-02-03 15:28:23 +000040 sys.stderr.write(__doc__ % globals())
Skip Montanaro9db49c82003-02-03 15:08:42 +000041
42def main(args):
43 try:
44 opts, args = getopt.getopt(args, "hbrda",
45 ["hash", "btree", "recno", "dbm", "anydbm"])
46 except getopt.error:
47 usage()
48 return 1
49
50 if len(args) == 0 or len(args) > 2:
51 usage()
52 return 1
53 elif len(args) == 1:
54 dbfile = args[0]
55 pfile = sys.stdin
56 else:
57 dbfile = args[0]
58 try:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000059 pfile = open(args[1], 'rb')
Skip Montanaro9db49c82003-02-03 15:08:42 +000060 except IOError:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000061 sys.stderr.write("Unable to open %s\n" % args[1])
Skip Montanaro9db49c82003-02-03 15:08:42 +000062 return 1
63
64 dbopen = None
65 for opt, arg in opts:
66 if opt in ("-h", "--hash"):
67 try:
68 dbopen = bsddb.hashopen
69 except AttributeError:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000070 sys.stderr.write("bsddb module unavailable.\n")
Skip Montanaro9db49c82003-02-03 15:08:42 +000071 return 1
72 elif opt in ("-b", "--btree"):
73 try:
74 dbopen = bsddb.btopen
75 except AttributeError:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000076 sys.stderr.write("bsddb module unavailable.\n")
Skip Montanaro9db49c82003-02-03 15:08:42 +000077 return 1
78 elif opt in ("-r", "--recno"):
79 try:
80 dbopen = bsddb.rnopen
81 except AttributeError:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000082 sys.stderr.write("bsddb module unavailable.\n")
Skip Montanaro9db49c82003-02-03 15:08:42 +000083 return 1
84 elif opt in ("-a", "--anydbm"):
85 try:
86 dbopen = anydbm.open
87 except AttributeError:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000088 sys.stderr.write("anydbm module unavailable.\n")
Skip Montanaro9db49c82003-02-03 15:08:42 +000089 return 1
90 elif opt in ("-d", "--dbm"):
91 try:
92 dbopen = dbm.open
93 except AttributeError:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000094 sys.stderr.write("dbm module unavailable.\n")
Skip Montanaro9db49c82003-02-03 15:08:42 +000095 return 1
96 if dbopen is None:
97 if bsddb is None:
Skip Montanaro7c1274d2003-02-03 15:28:23 +000098 sys.stderr.write("bsddb module unavailable - ")
99 sys.stderr.write("must specify dbtype.\n")
Skip Montanaro9db49c82003-02-03 15:08:42 +0000100 return 1
101 else:
102 dbopen = bsddb.hashopen
103
104 try:
105 db = dbopen(dbfile, 'c')
106 except bsddb.error:
Skip Montanaro7c1274d2003-02-03 15:28:23 +0000107 sys.stderr.write("Unable to open %s. " % dbfile)
108 sys.stderr.write("Check for format or version mismatch.\n")
Skip Montanaro9db49c82003-02-03 15:08:42 +0000109 return 1
110 else:
111 for k in db.keys():
112 del db[k]
113
114 while 1:
115 try:
116 (key, val) = pickle.load(pfile)
117 except EOFError:
118 break
119 db[key] = val
120
121 db.close()
122 pfile.close()
123
124 return 0
125
126if __name__ == "__main__":
127 sys.exit(main(sys.argv[1:]))