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