← Back to team overview

schooltool-developers team mailing list archive

New helper script

 

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

  schooltool, schooltool.gradebook andschooltool.lyceum.journal trunks
are updated with bin/python script.  It runs a python interpreter with
respective eggs and can be used to run your own script.  To get it, just
check out the latest code and run make.

  To use the attached example:

  Get the latest schooltool trunk, run "make instance"
  Copy some old Data.fs to instance/var/
  (databases with gradebook and journal information are good)
  Copy the attached count_broken.py to the schooltool dir.
  $ bin/python count_broken.py

  This script will iterate the database and count the broken objects.
It might be slow to open a large database first time.

Happy coding!
Justas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkxHKAsACgkQaCT+W0+kcjROPACeLSYZGPvgpFDXZUjj48c7dzG3
hxMAniWZwyoKag8lQrb6xN+kkm6NFLdv
=8cSZ
-----END PGP SIGNATURE-----
import sys
from pprint import pprint

from ZODB.FileStorage import FileStorage
from ZODB.DB import DB
from ZODB.broken import Broken
from ZODB.broken import broken_cache


def iter_storage(connection):
    storage = connection._storage
    next_oid = None
    while True:
        oid, tid, data, next_oid = storage.record_iternext(next_oid)
        yield connection.get(oid)
        if next_oid is None:
            return


if __name__ == '__main__':
    print 'Opening database...'
    storage = FileStorage('instance/var/Data.fs', read_only=True)
    db = DB(storage)
    connection = db.open()
    root = connection.root()

    sys.stdout.write('Reading database ')
    sys.stdout.flush()
    n_broken = 0
    for n, obj in enumerate(iter_storage(connection)):
        if isinstance(obj, Broken):
            n_broken += 1
        if n % 200 == 0:
            sys.stdout.write('.')
            sys.stdout.flush()

    print ''
    print 'Objects in database:', n+1
    print 'Broken objects:', n_broken
    pprint(broken_cache.keys())

    db.close()