Source

mana-core-athenaservices / python / TPCnvDb.py

The default branch has multiple heads

# @file: AthenaServices/python/TPCnvDb.py
# @purpose: simple interface to the rootmap files to easily locate t/p cnvs
# @author: Sebastien Binet <binet@cern.ch>
# @date:   September 2010

from __future__ import with_statement

__version__ = "$Revision: 321236 $"
__author__  = "Sebastien Binet <binet@cern.ch>"
__doc__ = """simple interface to the rootmap files to easily locate t/p cnvs"""

__all__ = [
    'find_tpcnv_db',
    'load_tpcnv_db',
    ]

### imports -------------------------------------------------------------------
import os
import sys
    
### functions -----------------------------------------------------------------

def find_tpcnv_db():
    """helper function finding the 'tpcnv.db' file(s).
       @returns the opened db
    """
    db = {}
    libdirs = reversed(os.environ['LD_LIBRARY_PATH'].split(os.pathsep))
    for libdir in libdirs:
        fname = os.path.join(libdir, 'tpcnv.db')
        if not os.path.exists(fname):
            continue
        with open(fname, 'r') as dbfile:
            for line in dbfile:
                line = line.strip()
                if line.startswith("#"):
                    continue
                if line.count(";") <= 0:
                    continue
                is_ara_cnv = '0'
                if line.count(";") == 4:
                    libname, tpcnv_name, trans_type, pers_type, latest_vers = \
                         line.split(";")
                else:
                    libname, tpcnv_name, trans_type, pers_type, latest_vers, \
                             is_ara_cnv = \
                             line.split(";")
                db[tpcnv_name] = {
                    'tpcnv':       tpcnv_name,
                    'trans_type':  trans_type,
                    'pers_type':   pers_type,
                    'latest_vers': latest_vers,
                    'is_ara_cnv':  is_ara_cnv,
                    'libname':     libname,
                    }
    return db

def load_tpcnv_db():
    """load the T/P converter db from the InstallArea and
       fill the AthTPCnvSvc in-memory registry
    """

    db = find_tpcnv_db()

    import AthenaPython.PyAthena as pyathena
    svc = pyathena.py_svc('AthTPCnvSvc', iface='ITPCnvSvc')
    fill = svc.add_tpcnv
    for cnv, infos in db.iteritems():
        is_latest_version = False
        if infos['latest_vers'] != '0':
            is_latest_version = True
        if infos['is_ara_cnv'] != '0':
            # reject all ARA-specific converters...
            continue
        sc = fill(cnv, infos['trans_type'], infos['pers_type'],
                  is_latest_version)
        sc.ignore()

    return

def test_load_alltpcnv():
    """loop over all registered t/p cnvs and try to load them.
    @return 0 if everything went fine, 1 otherwise.
    """
    db = find_tpcnv_db()
    import AthenaPython.PyAthena as pyathena
    svc = pyathena.py_svc('AthTPCnvSvc', iface='ITPCnvSvc')
    if not svc:
        raise RuntimeError("could not retrieve service [AthTPCnvSvc]")

    _ignore_list = [
        'CaloShowerContainerCnv_p1', # instantiating it segfaults...
        ]
    
    failed = []
    success =0
    for cnv, infos in db.iteritems():
        is_latest_version = False
        if infos['latest_vers'] != '0':
            is_latest_version = True
        infos['cnv'] = cnv
        if cnv in _ignore_list:
            continue
        if infos['is_ara_cnv'] != '0':
            # reject all ARA-specific converters...
            continue
        print "::: loading t/p cnv [%(cnv)s] [%(trans_type)s] => [%(pers_type)s]..." % infos
        try:
            obj = svc.load_tpcnv(cnv)
            assert obj
            tobj = svc.t2p_cnv(infos['trans_type'])
            assert tobj
            pobj = svc.p2t_cnv(infos['pers_type'])
            assert pobj
            print "::: loading t/p cnv [%(cnv)s] [%(trans_type)s] => [%(pers_type)s]... [ok]" % infos
            success += 1
        except Exception,err:
            print "::: loading t/p cnv [%(cnv)s] [%(trans_type)s] => [%(pers_type)s]... [ERR]" % infos
            failed.append(cnv)

    sc = 1
    if failed:
        print "::: failed: [%s]\n%r" % (len(failed), failed)
        sc = 1
    else:
        print "::: successfully auto-loaded [%s] t/p cnvs" % (success,)
        sc = 0
        
    return sc

### clean-up ------------------------------------------------------------------