Line Code
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#!/usr/bin/env python3
from ConfigParser import configparser as ConfigParser
from copy import copy
from optparse import OptionParser, Option, OptionValueError
import coral
import sys, os
def stripws(myinput):
    result=('').join(myinput.split(' '))
    return result
class logDBReader:
    def __init__(self):
        """
        Class add entry in the tag inventory
        usage: %prog [options]
        -c, --connect=connectstring: connection string to the log DB (required)
        -u, --user=user: user name
        -p, --password=password: password
        -P, --path=path: path to authentication.xml
        -v, --verbose: switch on verbose mode
        -h, --help: print usage
        """
        self.__parser=OptionParser()
        self.__connectstring=''
        self.__destDb=''
        self.__tag=''
        self.__user=''
        self.__password=''
        self.__authpath=''
        self.__verbose=False
        self.__logtableName='COND_LOG_TABLE'
    def parsecmdln(self):
        """
        Parse commandline
        """
        usage = "usage: \%prog [options] \n"
        self.__parser.add_option("-c","--connect",action="store",dest="connectstring",type="string",help="connection string to the log DB")
        self.__parser.add_option("-d","--destination",action="store",dest="destDb",type="string",help="connection string to the destination DB to filter")
        self.__parser.add_option("-t","--tag",action="store",dest="tag",type="string",help="IOV tag to filter")
        self.__parser.add_option("-u","--user",action="store",dest="user",type="string",help="user name")
        self.__parser.add_option("-p","--password",action="store",dest="password",type="string",help="password")
        self.__parser.add_option("-P","--path",action="store",dest="authpath",type="string",help="path to authentication.xml")
        self.__parser.add_option("-v","--verbose",action="store_true",dest="verbose",help="verbose mode")
        self.__parser.set_defaults(connectstring='')
        self.__parser.set_defaults(destDb='')
        self.__parser.set_defaults(tag='')
        self.__parser.set_defaults(verbose=False)
        self.__parser.set_defaults(connectstring='')
        self.__parser.set_defaults(user='')
        self.__parser.set_defaults(password='')
        self.__parser.set_defaults(authpath='')
        (options, args) = self.__parser.parse_args()
        self.__verbose=self.__parser.values.verbose
        self.__connectstring=self.__parser.values.connectstring
        if not self.__connectstring:
            raise ValueError("Please provide a valid connection string")
        if self.__parser.values.verbose is True:
            print 'connectstring: ',self.__connectstring
        if len(self.__parser.values.destDb) !=0 :
            self.__destDb=self.__parser.values.destDb
            if self.__parser.values.verbose is True:
                print 'destination Database: ',self.__destDb
        if len(self.__parser.values.tag) !=0 :
            self.__tag=self.__parser.values.tag
            if self.__parser.values.verbose is True:
                print 'tag: ',self.__tag
        if len(self.__parser.values.user) !=0 :
            self.__user=self.__parser.values.user
            if self.__parser.values.verbose is True:
                print 'user: ',self.__user
        if len(self.__parser.values.password) !=0 :
            self.__password=self.__parser.values.password
            if self.__parser.values.verbose is True:
                print 'password: ',self.__password
        if len(self.__parser.values.authpath)!=0:
            self.__authpath=self.__parser.values.authpath
            if self.__parser.values.verbose is True:
                print 'authpath: ',self.__authpath
    
    def dumpAll(self):
        """
        Dump the content of the log 
        """
        #context = coral.Context()
        #if self.__verbose is True:
        #    context.setVerbosityLevel( 'DEBUG' )
        #else:
        #    context.setVerbosityLevel( 'ERROR' )
        svc = coral.ConnectionService()
        config=svc.configuration()
        if len(self.__authpath)!=0:
            os.environ["CORAL_AUTH_PATH"]=self.__authpath
            config.setDefaultAuthenticationService('CORAL/Services/XMLAuthenticationService')
        else:
            os.environ["CORAL_AUTH_USER"]=self.__user
            os.environ["CORAL_AUTH_PASSWORD"]=self.__password
            config.setDefaultAuthenticationService('CORAL/Services/EnvironmentAuthenticationService')
            
        session=svc.connect(self.__connectstring,accessMode=coral.access_ReadOnly )
        try:
            session.transaction().start(True)
            schema = session.nominalSchema()
            query = schema.tableHandle(self.__logtableName).newQuery()
            for currentRow in iter(query.execute()):
                print str(currentRow) 
            session.transaction().commit()
            del query
            del session
        except Exception, e:
            print str(e)
            del session
    
    
    def dumpTag(self):
        """
        Dump the content of the log filtering by IOV tag
        """
        #context = coral.Context()
        #if self.__verbose is True:
        #    context.setVerbosityLevel( 'DEBUG' )
        #else:
        #    context.setVerbosityLevel( 'ERROR' )
        svc = coral.ConnectionService()
        config=svc.configuration()
        if len(self.__authpath)!=0:
            os.environ["CORAL_AUTH_PATH"]=self.__authpath
            config.setDefaultAuthenticationService('CORAL/Services/XMLAuthenticationService')
        else:
            os.environ["CORAL_AUTH_USER"]=self.__user
            os.environ["CORAL_AUTH_PASSWORD"]=self.__password
            config.setDefaultAuthenticationService('CORAL/Services/EnvironmentAuthenticationService')
            
        session=svc.connect(self.__connectstring,accessMode=coral.access_ReadOnly )
        try:
            conditionstring = "IOVTAG = :iovtag"
            session.transaction().start(True)
            schema = session.nominalSchema()
            query = schema.tableHandle(self.__logtableName).newQuery()
            queryBind = coral.AttributeList()
            queryBind.extend("iovtag", "string")
            queryBind["iovtag"].setData(self.__tag)
            query.setCondition (conditionstring,queryBind)
            for currentRow in iter(query.execute()):
                print str(currentRow) 
            session.transaction().commit()
            del query
            del session
        except Exception, e:
            print str(e)
            del session

    def dumpDestDb(self):
        """
        Dump the content of the log filtering by destination Database and IOV tag
        """
        #context = coral.Context()
        #if self.__verbose is True:
        #    context.setVerbosityLevel( 'DEBUG' )
        #else:
        #    context.setVerbosityLevel( 'ERROR' )
        svc = coral.ConnectionService()
        config=svc.configuration()
        if len(self.__authpath)!=0:
            os.environ["CORAL_AUTH_PATH"]=self.__authpath
            config.setDefaultAuthenticationService('CORAL/Services/XMLAuthenticationService')
        else:
            os.environ["CORAL_AUTH_USER"]=self.__user
            os.environ["CORAL_AUTH_PASSWORD"]=self.__password
            config.setDefaultAuthenticationService('CORAL/Services/EnvironmentAuthenticationService')
            
        session=svc.connect(self.__connectstring,accessMode=coral.access_ReadOnly )
        try:
            conditionstring = "DESTINATIONDB = :destdb"
            session.transaction().start(True)
            schema = session.nominalSchema()
            query = schema.tableHandle(self.__logtableName).newQuery()
            queryBind = coral.AttributeList()
            queryBind.extend("destdb", "string")
            queryBind["destdb"].setData(self.__destDb)
            if self.__tag:
                 conditionstring += " AND IOVTAG = :iovtag"
                 queryBind.extend("iovtag", "string")
                 queryBind["iovtag"].setData(self.__tag)
            query.setCondition (conditionstring,queryBind)
            for currentRow in iter(query.execute()):
                print str(currentRow) 
            session.transaction().commit()
            del query
            del session
        except Exception, e:
            print str(e)
            del session
    
    def dump(self):
        if(self.__destDb):
            self.dumpDestDb()
        elif self.__tag:
            self.dumpTag()
        else:
            self.dumpAll()

if __name__ == "__main__":
    dumper=logDBReader()
    dumper.parsecmdln()
    dumper.dump()