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

Python code coverage for Tools/scripts/pickle2db.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a
3n/a"""
4n/aSynopsis: %(prog)s [-h|-b|-g|-r|-a|-d] [ picklefile ] dbfile
5n/a
6n/aRead the given picklefile as a series of key/value pairs and write to a new
7n/adatabase. If the database already exists, any contents are deleted. The
8n/aoptional flags indicate the type of the output database:
9n/a
10n/a -a - open using dbm (open any supported format)
11n/a -b - open as bsddb btree file
12n/a -d - open as dbm.ndbm file
13n/a -g - open as dbm.gnu file
14n/a -h - open as bsddb hash file
15n/a -r - open as bsddb recno file
16n/a
17n/aThe default is hash. If a pickle file is named it is opened for read
18n/aaccess. If no pickle file is named, the pickle input is read from standard
19n/ainput.
20n/a
21n/aNote that recno databases can only contain integer keys, so you can't dump a
22n/ahash or btree database using db2pickle.py and reconstitute it to a recno
23n/adatabase with %(prog)s unless your keys are integers.
24n/a
25n/a"""
26n/a
27n/aimport getopt
28n/atry:
29n/a import bsddb
30n/aexcept ImportError:
31n/a bsddb = None
32n/atry:
33n/a import dbm.ndbm as dbm
34n/aexcept ImportError:
35n/a dbm = None
36n/atry:
37n/a import dbm.gnu as gdbm
38n/aexcept ImportError:
39n/a gdbm = None
40n/atry:
41n/a import dbm.ndbm as anydbm
42n/aexcept ImportError:
43n/a anydbm = None
44n/aimport sys
45n/atry:
46n/a import pickle as pickle
47n/aexcept ImportError:
48n/a import pickle
49n/a
50n/aprog = sys.argv[0]
51n/a
52n/adef usage():
53n/a sys.stderr.write(__doc__ % globals())
54n/a
55n/adef main(args):
56n/a try:
57n/a opts, args = getopt.getopt(args, "hbrdag",
58n/a ["hash", "btree", "recno", "dbm", "anydbm",
59n/a "gdbm"])
60n/a except getopt.error:
61n/a usage()
62n/a return 1
63n/a
64n/a if len(args) == 0 or len(args) > 2:
65n/a usage()
66n/a return 1
67n/a elif len(args) == 1:
68n/a pfile = sys.stdin
69n/a dbfile = args[0]
70n/a else:
71n/a try:
72n/a pfile = open(args[0], 'rb')
73n/a except IOError:
74n/a sys.stderr.write("Unable to open %s\n" % args[0])
75n/a return 1
76n/a dbfile = args[1]
77n/a
78n/a dbopen = None
79n/a for opt, arg in opts:
80n/a if opt in ("-h", "--hash"):
81n/a try:
82n/a dbopen = bsddb.hashopen
83n/a except AttributeError:
84n/a sys.stderr.write("bsddb module unavailable.\n")
85n/a return 1
86n/a elif opt in ("-b", "--btree"):
87n/a try:
88n/a dbopen = bsddb.btopen
89n/a except AttributeError:
90n/a sys.stderr.write("bsddb module unavailable.\n")
91n/a return 1
92n/a elif opt in ("-r", "--recno"):
93n/a try:
94n/a dbopen = bsddb.rnopen
95n/a except AttributeError:
96n/a sys.stderr.write("bsddb module unavailable.\n")
97n/a return 1
98n/a elif opt in ("-a", "--anydbm"):
99n/a try:
100n/a dbopen = anydbm.open
101n/a except AttributeError:
102n/a sys.stderr.write("dbm module unavailable.\n")
103n/a return 1
104n/a elif opt in ("-g", "--gdbm"):
105n/a try:
106n/a dbopen = gdbm.open
107n/a except AttributeError:
108n/a sys.stderr.write("dbm.gnu module unavailable.\n")
109n/a return 1
110n/a elif opt in ("-d", "--dbm"):
111n/a try:
112n/a dbopen = dbm.open
113n/a except AttributeError:
114n/a sys.stderr.write("dbm.ndbm module unavailable.\n")
115n/a return 1
116n/a if dbopen is None:
117n/a if bsddb is None:
118n/a sys.stderr.write("bsddb module unavailable - ")
119n/a sys.stderr.write("must specify dbtype.\n")
120n/a return 1
121n/a else:
122n/a dbopen = bsddb.hashopen
123n/a
124n/a try:
125n/a db = dbopen(dbfile, 'c')
126n/a except bsddb.error:
127n/a sys.stderr.write("Unable to open %s. " % dbfile)
128n/a sys.stderr.write("Check for format or version mismatch.\n")
129n/a return 1
130n/a else:
131n/a for k in list(db.keys()):
132n/a del db[k]
133n/a
134n/a while 1:
135n/a try:
136n/a (key, val) = pickle.load(pfile)
137n/a except EOFError:
138n/a break
139n/a db[key] = val
140n/a
141n/a db.close()
142n/a pfile.close()
143n/a
144n/a return 0
145n/a
146n/aif __name__ == "__main__":
147n/a sys.exit(main(sys.argv[1:]))