Endian safe export of Pickled GDBM Cabinet Files

You proberly wont need this unless you work for an old institution which uses GDBM, Python, Solaris to one of their core business and need to export this to a modern Linux Server. (i did once)

#!/usr/bin/env python

import sys
import anydbm
try:
    import cPickle
    pickle = cPickle
except ImportError:
    import pickle

def dbm2pck(dbm, f):
    for key in dbm.keys():
        pickle.dump((key, dbm[key]), f)

def pck2dbm(f, dbm):
    while 1:
        try:
            key, val = pickle.load(f)
        except EOFError:
            break
        else:
            dbm[key] = val

def usage():
    print "Usage: %s (dump|load) dbmfile pckfile" % (sys.argv[0],)
    sys.exit()

if __name__ == '__main__':
    if len(sys.argv) < 4:
        usage()
    cmd, dbmfile, pckfile = sys.argv[1:]
    if cmd == 'dump':
        dbm = anydbm.open(dbmfile, "r")
        pck = open(pckfile, "w")
        dbm2pck(dbm, pck)
    elif cmd == 'load':
        pck = open(pckfile)
        dbm = anydbm.open(dbmfile, "c")
        pck2dbm(pck, dbm)
    else:
        usage()
    dbm.close()
    pck.close()
    print "%s done." % cmd
Dette indlæg blev udgivet i Knowledge Base, Old Base. Bogmærk permalinket.

Skriv et svar