ยปCore Development>Code coverage>Tools/scripts/db2pickle.py

Python code coverage for Tools/scripts/db2pickle.py

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