4
Synopsis: %(prog)s [-h|-g|-b|-r|-a] dbfile [ picklefile ]
6
Convert the database file given on the command line to a pickle
7
representation. The optional flags indicate the type of the database:
9
-a - open using dbm (any supported format)
10
-b - open as bsddb btree file
12
-g - open as gdbm file
13
-h - open as bsddb hash file
14
-r - open as bsddb recno file
16
The default is hash. If a pickle file is named it is opened for write
17
access (deleting any existing data). If no pickle file is named, the pickle
18
output is written to standard output.
28
import dbm.ndbm as dbm
32
import dbm.gnu as gdbm
41
import cPickle as pickle
48
sys.stderr.write(__doc__ % globals())
52
opts, args = getopt.getopt(args, "hbrdag",
53
["hash", "btree", "recno", "dbm",
59
if len(args) == 0 or len(args) > 2:
68
pfile = open(args[1], 'wb')
70
sys.stderr.write("Unable to open %s\n" % args[1])
75
if opt in ("-h", "--hash"):
77
dbopen = bsddb.hashopen
78
except AttributeError:
79
sys.stderr.write("bsddb module unavailable.\n")
81
elif opt in ("-b", "--btree"):
84
except AttributeError:
85
sys.stderr.write("bsddb module unavailable.\n")
87
elif opt in ("-r", "--recno"):
90
except AttributeError:
91
sys.stderr.write("bsddb module unavailable.\n")
93
elif opt in ("-a", "--anydbm"):
96
except AttributeError:
97
sys.stderr.write("dbm module unavailable.\n")
99
elif opt in ("-g", "--gdbm"):
102
except AttributeError:
103
sys.stderr.write("dbm.gnu module unavailable.\n")
105
elif opt in ("-d", "--dbm"):
108
except AttributeError:
109
sys.stderr.write("dbm.ndbm module unavailable.\n")
113
sys.stderr.write("bsddb module unavailable - ")
114
sys.stderr.write("must specify dbtype.\n")
117
dbopen = bsddb.hashopen
120
db = dbopen(dbfile, 'r')
122
sys.stderr.write("Unable to open %s. " % dbfile)
123
sys.stderr.write("Check for format or version mismatch.\n")
127
pickle.dump((k, db[k]), pfile, 1==1)
134
if __name__ == "__main__":
135
sys.exit(main(sys.argv[1:]))