#!/usr/bin/env python3 import bsddb3.dbshelve import dbm import os import shelve import sys auto_list = ['ImperialBank_DB', 'crossfiremail', 'crossfireboard', 'SlotMachine_file', 'PShop', 'PicDB'] def need_convert(path): try: shelve.open(path, flag='r') return False except dbm.error: return True def main(): if len(sys.argv) <= 1: print("usage: cfdb_convert FILES") print(" cfdb_convert --auto") print("Convert Python 2 databases to Python 3 databases") if len(sys.argv) >= 2: if sys.argv[1] == '--auto': files = auto_list else: files = sys.argv[1:] for path in files: if not os.path.exists(path): print(path, "does not exist, skipping") continue if not need_convert(path): print(path, "does not need to be converted, skipping") continue backup_path = path + ".bak" os.rename(path, backup_path) old_db = bsddb3.dbshelve.open(backup_path, flags='r') new_db = shelve.open(path, flag='c') # convert old keys from bytes to strings try: for key, val in old_db.items(): new_db[key.decode()] = val except Exception as e: print("error converting '%s'" % (path)) os.rename(backup_path, path) old_db.close() new_db.close() if __name__ == '__main__': main()