File: cfdb_convert

package info (click to toggle)
crossfire 1.75.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,168 kB
  • sloc: ansic: 83,169; sh: 4,659; perl: 1,736; lex: 1,443; makefile: 1,199; python: 43
file content (51 lines) | stat: -rwxr-xr-x 1,460 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/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()