Debian Codegraph @

utils/cfdb_convert

raw symbols SCIP
1 #!/usr/bin/env python3
2 import bsddb3.dbshelve
3 import dbm
4 import os
5 import shelve
6 import sys
7
8 auto_list = ['ImperialBank_DB', 'crossfiremail', 'crossfireboard', 'SlotMachine_file', 'PShop', 'PicDB']
9
10 def need_convert(path):
11 try:
12 shelve.open(path, flag='r')
13 return False
14 except dbm.error:
15 return True
16
17 def main():
18 if len(sys.argv) <= 1:
19 print("usage: cfdb_convert FILES")
20 print(" cfdb_convert --auto")
21 print("Convert Python 2 databases to Python 3 databases")
22
23 if len(sys.argv) >= 2:
24 if sys.argv[1] == '--auto':
25 files = auto_list
26 else:
27 files = sys.argv[1:]
28
29 for path in files:
30 if not os.path.exists(path):
31 print(path, "does not exist, skipping")
32 continue
33 if not need_convert(path):
34 print(path, "does not need to be converted, skipping")
35 continue
36 backup_path = path + ".bak"
37 os.rename(path, backup_path)
38 old_db = bsddb3.dbshelve.open(backup_path, flags='r')
39 new_db = shelve.open(path, flag='c')
40 # convert old keys from bytes to strings
41 try:
42 for key, val in old_db.items():
43 new_db[key.decode()] = val
44 except Exception as e:
45 print("error converting '%s'" % (path))
46 os.rename(backup_path, path)
47 old_db.close()
48 new_db.close()
49
50 if __name__ == '__main__':
51 main()