1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | Synopsis: %(prog)s [-h|-g|-b|-r|-a] dbfile [ picklefile ] |
---|
5 | n/a | |
---|
6 | n/a | Convert the database file given on the command line to a pickle |
---|
7 | n/a | representation. The optional flags indicate the type of the database: |
---|
8 | n/a | |
---|
9 | n/a | -a - open using dbm (any supported format) |
---|
10 | n/a | -b - open as bsddb btree file |
---|
11 | n/a | -d - open as dbm file |
---|
12 | n/a | -g - open as gdbm file |
---|
13 | n/a | -h - open as bsddb hash file |
---|
14 | n/a | -r - open as bsddb recno file |
---|
15 | n/a | |
---|
16 | n/a | The default is hash. If a pickle file is named it is opened for write |
---|
17 | n/a | access (deleting any existing data). If no pickle file is named, the pickle |
---|
18 | n/a | output is written to standard output. |
---|
19 | n/a | |
---|
20 | n/a | """ |
---|
21 | n/a | |
---|
22 | n/a | import getopt |
---|
23 | n/a | try: |
---|
24 | n/a | import bsddb |
---|
25 | n/a | except ImportError: |
---|
26 | n/a | bsddb = None |
---|
27 | n/a | try: |
---|
28 | n/a | import dbm.ndbm as dbm |
---|
29 | n/a | except ImportError: |
---|
30 | n/a | dbm = None |
---|
31 | n/a | try: |
---|
32 | n/a | import dbm.gnu as gdbm |
---|
33 | n/a | except ImportError: |
---|
34 | n/a | gdbm = None |
---|
35 | n/a | try: |
---|
36 | n/a | import dbm.ndbm as anydbm |
---|
37 | n/a | except ImportError: |
---|
38 | n/a | anydbm = None |
---|
39 | n/a | import sys |
---|
40 | n/a | try: |
---|
41 | n/a | import pickle as pickle |
---|
42 | n/a | except ImportError: |
---|
43 | n/a | import pickle |
---|
44 | n/a | |
---|
45 | n/a | prog = sys.argv[0] |
---|
46 | n/a | |
---|
47 | n/a | def usage(): |
---|
48 | n/a | sys.stderr.write(__doc__ % globals()) |
---|
49 | n/a | |
---|
50 | n/a | def main(args): |
---|
51 | n/a | try: |
---|
52 | n/a | opts, args = getopt.getopt(args, "hbrdag", |
---|
53 | n/a | ["hash", "btree", "recno", "dbm", |
---|
54 | n/a | "gdbm", "anydbm"]) |
---|
55 | n/a | except getopt.error: |
---|
56 | n/a | usage() |
---|
57 | n/a | return 1 |
---|
58 | n/a | |
---|
59 | n/a | if len(args) == 0 or len(args) > 2: |
---|
60 | n/a | usage() |
---|
61 | n/a | return 1 |
---|
62 | n/a | elif len(args) == 1: |
---|
63 | n/a | dbfile = args[0] |
---|
64 | n/a | pfile = sys.stdout |
---|
65 | n/a | else: |
---|
66 | n/a | dbfile = args[0] |
---|
67 | n/a | try: |
---|
68 | n/a | pfile = open(args[1], 'wb') |
---|
69 | n/a | except IOError: |
---|
70 | n/a | sys.stderr.write("Unable to open %s\n" % args[1]) |
---|
71 | n/a | return 1 |
---|
72 | n/a | |
---|
73 | n/a | dbopen = None |
---|
74 | n/a | for opt, arg in opts: |
---|
75 | n/a | if opt in ("-h", "--hash"): |
---|
76 | n/a | try: |
---|
77 | n/a | dbopen = bsddb.hashopen |
---|
78 | n/a | except AttributeError: |
---|
79 | n/a | sys.stderr.write("bsddb module unavailable.\n") |
---|
80 | n/a | return 1 |
---|
81 | n/a | elif opt in ("-b", "--btree"): |
---|
82 | n/a | try: |
---|
83 | n/a | dbopen = bsddb.btopen |
---|
84 | n/a | except AttributeError: |
---|
85 | n/a | sys.stderr.write("bsddb module unavailable.\n") |
---|
86 | n/a | return 1 |
---|
87 | n/a | elif opt in ("-r", "--recno"): |
---|
88 | n/a | try: |
---|
89 | n/a | dbopen = bsddb.rnopen |
---|
90 | n/a | except AttributeError: |
---|
91 | n/a | sys.stderr.write("bsddb module unavailable.\n") |
---|
92 | n/a | return 1 |
---|
93 | n/a | elif opt in ("-a", "--anydbm"): |
---|
94 | n/a | try: |
---|
95 | n/a | dbopen = anydbm.open |
---|
96 | n/a | except AttributeError: |
---|
97 | n/a | sys.stderr.write("dbm module unavailable.\n") |
---|
98 | n/a | return 1 |
---|
99 | n/a | elif opt in ("-g", "--gdbm"): |
---|
100 | n/a | try: |
---|
101 | n/a | dbopen = gdbm.open |
---|
102 | n/a | except AttributeError: |
---|
103 | n/a | sys.stderr.write("dbm.gnu module unavailable.\n") |
---|
104 | n/a | return 1 |
---|
105 | n/a | elif opt in ("-d", "--dbm"): |
---|
106 | n/a | try: |
---|
107 | n/a | dbopen = dbm.open |
---|
108 | n/a | except AttributeError: |
---|
109 | n/a | sys.stderr.write("dbm.ndbm module unavailable.\n") |
---|
110 | n/a | return 1 |
---|
111 | n/a | if dbopen is None: |
---|
112 | n/a | if bsddb is None: |
---|
113 | n/a | sys.stderr.write("bsddb module unavailable - ") |
---|
114 | n/a | sys.stderr.write("must specify dbtype.\n") |
---|
115 | n/a | return 1 |
---|
116 | n/a | else: |
---|
117 | n/a | dbopen = bsddb.hashopen |
---|
118 | n/a | |
---|
119 | n/a | try: |
---|
120 | n/a | db = dbopen(dbfile, 'r') |
---|
121 | n/a | except bsddb.error: |
---|
122 | n/a | sys.stderr.write("Unable to open %s. " % dbfile) |
---|
123 | n/a | sys.stderr.write("Check for format or version mismatch.\n") |
---|
124 | n/a | return 1 |
---|
125 | n/a | |
---|
126 | n/a | for k in db.keys(): |
---|
127 | n/a | pickle.dump((k, db[k]), pfile, 1==1) |
---|
128 | n/a | |
---|
129 | n/a | db.close() |
---|
130 | n/a | pfile.close() |
---|
131 | n/a | |
---|
132 | n/a | return 0 |
---|
133 | n/a | |
---|
134 | n/a | if __name__ == "__main__": |
---|
135 | n/a | sys.exit(main(sys.argv[1:])) |
---|