CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/CondCore/RegressionTest/python/testRegression.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import sys
00003 import re
00004 import os
00005 import shlex, subprocess
00006 import getopt
00007 import random
00008 import time
00009 from xml.dom.minidom import parse, parseString
00010 import string
00011 try:
00012     import cx_Oracle
00013 except ImportError, e:
00014     print "Cannot import cx_Oracle:", e
00015         
00016 #global varables
00017 resCount = 0
00018 resNames = []
00019 mLabel = ''
00020 DATABASE = "cms_orcoff_prep"
00021 USERNAME = "CMS_COND_REGRESSION"
00022 AUTH_PATH = "/afs/cern.ch/cms/DB/conddb/test/authentication.xml"
00023 #end
00024 
00025 def getText(nodelist):
00026     rc = []
00027     for node in nodelist:
00028                 if node.nodeType == node.TEXT_NODE:
00029                         rc.append(node.data)
00030     return ''.join(rc)
00031 def ParseXML(filename, label):
00032         results = []
00033         initResults = []
00034         finalResults = []
00035         dom = parse(filename)
00036         xml = dom.getElementsByTagName("xml")
00037         execTest = False
00038         env = []
00039         if xml != None:
00040                 for xm in xml:
00041                         test = xm.getElementsByTagName("test")
00042                         for it in test:
00043                                 #print it.toxml()
00044                                 if "name" in  it.attributes.keys():
00045                                         tLabel = str(it.attributes["name"].value)
00046                                         if  tLabel == label:
00047                                                 execTest = True
00048                                                 global mLabel
00049                                                 mLabel = label
00050                                 if execTest == True:
00051                                         inits = it.getElementsByTagName("init")
00052                                         finals = it.getElementsByTagName("final")
00053                                         seqs = it.getElementsByTagName("sequence")
00054                                         for init in inits:
00055                                                 commands = init.getElementsByTagName("command")
00056                                                 for command in commands:
00057                                                         if "exec" in  command.attributes.keys():
00058                                                                 initResults.append(str(command.attributes["exec"].value))
00059                                                         if "env" in  command.attributes.keys():
00060                                                                         env.append(str(command.attributes["env"].value))
00061                                         for seq in seqs:
00062                                                 commands = seq.getElementsByTagName("command")
00063                                                 for command in commands:
00064                                                         if "exec" in  command.attributes.keys():
00065                                                                 results.append(str(command.attributes["exec"].value))
00066                                                         if "result" in  command.attributes.keys():
00067                                                                 global resNames
00068                                                                 resNames.append(str(command.attributes["result"].value))
00069                                                         else: 
00070                                                                 resNames.append("%NONE")
00071                                                         if "env" in  command.attributes.keys():
00072                                                                 env.append(str(command.attributes["env"].value))
00073                                         for final in finals:
00074                                                 commands = final.getElementsByTagName("command")
00075                                                 for command in commands:
00076                                                         if "exec" in  command.attributes.keys():
00077                                                                 finalResults.append(str(command.attributes["exec"].value))
00078                                                         if "env" in  command.attributes.keys():
00079                                                                         env.append(str(command.attributes["env"].value))
00080                                 execTest = False
00081         return initResults,results, finalResults,  env
00082 def extractLogin(login):
00083         pattern = re.compile(r'value="([^"]+)')
00084         matching = pattern.search(login)
00085         version = 0
00086         if matching:
00087                 g = matching.groups()
00088                 return g[0]
00089 def getLogin(auth, connStr):
00090         pfile = open(auth, "r")
00091         plist = pfile.readlines()
00092         for i in range (0, len(plist)):
00093                 if string.find(plist[i], '<connection name="'+connStr+'">') != -1:
00094                         PASSWORD = extractLogin(plist[i+2])
00095         return (PASSWORD)
00096         
00097 def setConn():
00098         os.environ['TNS_ADMIN'] = "/afs/cern.ch/project/oracle/admin"
00099         coralConnStr = "oracle://"+DATABASE+"/"+USERNAME+""
00100         PASSWORD = getLogin(AUTH_PATH, coralConnStr)
00101         conn_string = str(USERNAME+"/"+PASSWORD+"@"+DATABASE)   
00102         conn = cx_Oracle.connect(conn_string)   
00103         return {'r0':conn, 'r1':coralConnStr, 'r2':USERNAME, 'r3':PASSWORD, 'r4':AUTH_PATH}
00104 def WebStatusRunID(label):
00105         result = setConn()
00106         con = result.get('r0')
00107         curs = con.cursor()
00108         sqlstr = "SELECT MAX(RUNID) FROM TEST_STATUS WHERE LABEL = :labl"
00109         curs.prepare(sqlstr)
00110         curs.execute(sqlstr, labl=label)
00111         max = 0
00112         for row in curs:
00113                 return row[0]
00114 def WebStatusHeaders(label):
00115         result = setConn()
00116         con = result.get('r0')
00117         curs = con.cursor()
00118         sqlstr = "SELECT DISTINCT RUNID, TO_CHAR(RDATE, 'DD.MM.YYYY HH24:MI:SS'), LABEL, T_RELEASE, T_ARCH "
00119         sqlstr +="FROM TEST_STATUS WHERE LABEL = :labl ORDER BY RUNID DESC"
00120         curs.prepare(sqlstr)
00121         curs.execute(sqlstr, labl = label)
00122         result = curs.fetchall()
00123         return result
00124                 
00125 def WebLabels():
00126         result = setConn()
00127         con = result.get('r0')
00128         curs = con.cursor()
00129         sqlstr = "SELECT DISTINCT LABEL FROM TEST_STATUS"
00130         curs.prepare(sqlstr)
00131         curs.execute(sqlstr)
00132         return curs
00133 
00134 def WebStatus(id, label):
00135         result = setConn()
00136         con = result.get('r0')
00137         curs = con.cursor()
00138         sqlstr = "SELECT ID, R_RELEASE, R_ARCH FROM TEST_STATUS WHERE RUNID = :ids AND LABEL = :labl ORDER BY R_RELEASE" 
00139         curs.prepare(sqlstr)
00140         curs.execute(sqlstr, ids=id, labl=label)
00141         result = curs.fetchall()
00142         return result
00143         
00144 def WebResultsList(runID, label):
00145         result = setConn()
00146         con = result.get('r0')
00147         curs = con.cursor()
00148         sqlstr = "SELECT ID FROM TEST_STATUS WHERE RUNID = :rid AND LABEL = :labl"
00149         curs.prepare(sqlstr)
00150         curs.execute(sqlstr, rid = runID, labl = label)
00151         idList = []
00152         for row in curs:
00153                 idList.append(row[0])
00154         sqlstr = "SELECT ID, STATUS, NAME FROM TEST_RESULTS WHERE ID >= :mi AND ID <= :ma AND LABEL = :labl"
00155         curs.prepare(sqlstr)
00156         curs.execute(sqlstr, mi=min(idList), ma=max(idList), labl = label)
00157         result = curs.fetchall()
00158         count = len(result)/len(idList)
00159         print count
00160         return result, count
00161 def WebReleasesHeaders(label, release="", arch=""):
00162         result = setConn()
00163         con = result.get('r0')
00164         curs = con.cursor()
00165         if(release != "" and arch != ""):
00166                 sqlstr = "SELECT DISTINCT RUNID, TO_CHAR(RDATE, 'DD.MM.YYYY HH24:MI:SS'), LABEL, T_RELEASE, T_ARCH FROM TEST_STATUS WHERE T_RELEASE = :rel AND T_ARCH = :arc AND LABEL = :labl ORDER BY RUNID DESC"
00167                 curs.prepare(sqlstr)
00168                 curs.execute(sqlstr, rel=release, arc=arch, labl=label)
00169         elif(release != ""):
00170                 sqlstr = "SELECT DISTINCT RUNID, TO_CHAR(RDATE, 'DD.MM.YYYY HH24:MI:SS'), LABEL, T_RELEASE, T_ARCH FROM TEST_STATUS WHERE T_RELEASE = :rel AND LABEL = :labl ORDER BY RUNID DESC"
00171                 curs.prepare(sqlstr)
00172                 curs.execute(sqlstr, rel=release, labl=label)
00173         elif(arch != ""):
00174                 sqlstr = "SELECT DISTINCT RUNID, TO_CHAR(RDATE, 'DD.MM.YYYY HH24:MI:SS'), LABEL, T_RELEASE, T_ARCH FROM TEST_STATUS WHERE T_ARCH = :arc AND LABEL = :labl ORDER BY RUNID DESC"
00175                 curs.prepare(sqlstr)
00176                 curs.execute(sqlstr, arc=arch, labl=label)
00177         result = curs.fetchall()
00178         return result
00179 def WebReadLogStatusDB(label, runID):
00180         result = setConn()
00181         con = result.get('r0')
00182         curs = con.cursor()
00183         sqlstr = "SELECT LOG FROM TEST_STATUS WHERE RUNID = :rid"
00184         curs.prepare(sqlstr)
00185         curs.execute(sqlstr, rid = runID)
00186         for row in curs:
00187                 return row[0].read()
00188 def BuildLogfile(label, runID):
00189         logFile = open("logs/TestLog"+str(runID)+".txt", "w")
00190         logFile.write('\Output:'+stdout_value)
00191         
00192 
00193 def extractID(release):
00194         pattern = re.compile("^CMSSW_(\d+)_(\d+)_(\d+|\D)(_pre(\d+)|_patch(\d+))?")
00195         matching = pattern.match(release)
00196         version = 0
00197         if matching:
00198                 g = matching.groups()
00199                 if(g[2].isdigit()):
00200                         if(g[4] is not None and g[4].isdigit()):
00201                                         version = int(g[0]) * 1000000 + int(g[1]) * 10000 + int(g[2]) * 100 + int(g[4])
00202                         else:
00203                                 version = int(g[0]) * 1000000 + int(g[1]) * 10000 + int(g[2]) * 100
00204                 else:
00205                         version = int(g[0]) * 1000000 + int(g[1]) * 10000 +9999
00206                 if(version is not None):
00207                         return version
00208 def extractErr(runID, stdoutStr):
00209         reStr = "\!L\!([^!]+)\!TR\!([^!]+)\!TA\!([^!]+)\!RR\!([^!]+)\!RA\!([^!]+)"
00210         for i in range (0, resCount):
00211                 reStr +=  "\!C"+str(i)+"\!(\d+)"
00212 
00213         pattern = re.compile(reStr)
00214         matching = pattern.findall(stdoutStr)
00215         stdoutMod = pattern.sub("", stdoutStr)
00216         for match in matching:
00217                 WriteStatusDB(runID, match)
00218         return stdoutMod
00219 
00220 def CreateReleaseDB():
00221         curs = conn.cursor()
00222         sqlstr = "CREATE TABLE VERSION_TABLE (ID NUMBER, RELEASE VARCHAR2(50), ARCH VARCHAR2(30), PATH VARCHAR(255), CONSTRAINT PK_ID PRIMARY KEY(RELEASE, ARCH) )"
00223         curs.prepare(sqlstr)
00224         curs.execute(sqlstr)
00225         print 'VERSION TABLE CREATED'
00226 def DropReleaseDB():
00227         curs = conn.cursor()
00228         sqlstr = "DROP TABLE VERSION_TABLE"
00229         curs.prepare(sqlstr)
00230         curs.execute(sqlstr)
00231         print 'VERSION TABLE DROPPED'
00232 def DeleteReleaseDB(release, arch):
00233         curs = conn.cursor()
00234         sqlstr = "DELETE FROM VERSION_TABLE WHERE RELEASE = :rel AND ARCH = :arc"
00235         curs.prepare(sqlstr)
00236         curs.execute(sqlstr, rel = release, arc = arch)
00237         conn.commit()
00238         print 'VALUE DELETED'
00239 def ReadReleaseDB():
00240         curs = conn.cursor()
00241         sqlstr = "SELECT ID, RELEASE, ARCH, PATH FROM VERSION_TABLE ORDER BY ID, RELEASE, ARCH"
00242         curs.prepare(sqlstr)
00243         curs.execute(sqlstr)
00244         print 'VERSION TABLE READ'
00245         print 'ID     RELEASE     ARCH     PATH'
00246         for row in curs:
00247                 print row
00248                 
00249 def CreateStatusDB():
00250         curs = conn.cursor()
00251         sqlstr = "CREATE TABLE TEST_STATUS (ID NUMBER, RUNID NUMBER, RDATE DATE, LABEL VARCHAR2(20), "
00252         sqlstr += "T_RELEASE VARCHAR2(50), T_ARCH VARCHAR2(30), R_RELEASE VARCHAR(50), R_ARCH VARCHAR2(30), LOG CLOB, "
00253         sqlstr += "CONSTRAINT PK_ID2 PRIMARY KEY(ID, RUNID, LABEL))"
00254         curs.prepare(sqlstr)
00255         curs.execute(sqlstr)
00256         CreateResultsDB()
00257         CreateSequenceDB()
00258         conn.commit()
00259         print 'STATUS TABLE CREATED'
00260 def DropStatusDB():
00261         curs = conn.cursor()
00262         sqlstr = "DROP TABLE TEST_STATUS"
00263         curs.prepare(sqlstr)
00264         curs.execute(sqlstr)
00265         DropResultsDB()
00266         DropSequenceDB()
00267         print 'STATUS TABLE DROPPED'
00268 def ReadStatusDB():
00269         curs = conn.cursor()
00270         sqlstr = "SELECT ID, RUNID, TO_CHAR(RDATE, 'DD.MM.YYYY HH24:MI:SS'), LABEL, T_RELEASE, T_ARCH, R_RELEASE, R_ARCH, LOG "
00271         sqlstr +="FROM TEST_STATUS ORDER BY ID"
00272         curs.prepare(sqlstr)
00273         curs.execute(sqlstr)
00274         print 'STATUS TABLE READ'
00275         print 'ID  RUNID RDATE   LABEL  T_RELEASE     T_ARCH      R_RELEASE     R_ARCH'
00276         for row in curs:
00277                 print row[0:8]
00278                 print ReadResultsDB(row[0])
00279                 print row[8].read() 
00280 def GetMaxRunIDStatusDB(label):
00281         curs = conn.cursor()
00282         sqlstr = "SELECT MAX(RUNID) FROM TEST_STATUS WHERE LABEL = :labl"
00283         curs.prepare(sqlstr)
00284         curs.execute(sqlstr, labl=label)
00285         for row in curs:
00286                 if(row[0] is not None):
00287                         return row[0]
00288                 else:
00289                         return 0
00290 def ReadCurrentStatusDB(runID):
00291         curs = conn.cursor()
00292         sqlstr = "SELECT ID, RUNID, TO_CHAR(RDATE, 'DD.MM.YYYY HH24:MI:SS'), LABEL, T_RELEASE, T_ARCH, R_RELEASE, R_ARCH "
00293         sqlstr +="FROM TEST_STATUS WHERE RUNID = :rid ORDER BY ID"
00294         curs.prepare(sqlstr)
00295         curs.execute(sqlstr, rid= runID)
00296         print 'STATUS TABLE READ'
00297         print 'ID  RUNID RDATE  LABEL   T_RELEASE     T_ARCH      R_RELEASE     R_ARCH'
00298         for row in curs:
00299                 print row
00300                 print ReadResultsDB(row[0])
00301 def WriteStatusDB(runID, match):
00302         curs = conn.cursor()
00303         id = NextIDVal(match[0])
00304         sqlstr = "INSERT INTO TEST_STATUS(ID, RUNID, RDATE, LABEL, T_RELEASE, T_ARCH, R_RELEASE, R_ARCH)"
00305         sqlstr +="VALUES(:ids, :rid, :ts, :labl, "
00306         sqlstr +=":trel, :tarc, :rrel, :rarc)"
00307         curs.prepare(sqlstr)
00308         curs.execute(sqlstr, ids = id , rid = runID, ts=timeStamp, labl=match[0], trel = match[1], tarc = match[2], rrel = match[3], rarc = match[4])
00309         conn.commit()
00310         for i in range(5, len(match)):
00311                 if resNames[i-5] != "%NONE":
00312                         WriteResultsDB(id, label, resNames[i-5], match[i])
00313 def AddLogStatusDB(label, runID, logStr):
00314         curs = conn.cursor()
00315         sqlstr = "UPDATE TEST_STATUS SET LOG = :lstr WHERE LABEL = :labl AND RUNID = :rid"
00316         curs.prepare(sqlstr)
00317         curs.execute(sqlstr, lstr = logStr, labl=label, rid=runID)
00318         conn.commit()
00319 def CheckIfOkStatusDB(runID, label):
00320         curs = conn.cursor()
00321         sqlstr = "SELECT ID FROM TEST_STATUS WHERE RUNID = :rid AND LABEL = :labl"
00322         curs.prepare(sqlstr)
00323         curs.execute(sqlstr, rid = runID, labl = label)
00324         idList = []
00325         for row in curs:
00326                 idList.append(row[0])
00327         curs = conn.cursor()
00328         sqlstr = "SELECT ID, STATUS FROM TEST_RESULTS WHERE ID >= :mi AND ID <= :ma AND LABEL = :labl"
00329         curs.prepare(sqlstr)
00330         #print "ID = %d label=%s minid=%d maxid=%d" %(runID,label, min(idList),max(idList))
00331         curs.execute(sqlstr, mi=min(idList), ma=max(idList), labl = label)
00332         for row in curs:
00333                 if row[1] != 0:
00334                         return False
00335         return True
00336 
00337 def CreateResultsDB():  
00338         curs = conn.cursor()
00339         sqlstr = "CREATE TABLE TEST_RESULTS (RID NUMBER, ID NUMBER, LABEL VARCHAR(20), NAME VARCHAR(100), STATUS NUMBER)"
00340         curs.prepare(sqlstr)
00341         curs.execute(sqlstr)
00342         sqlstr = "CREATE SEQUENCE AUTO_INC INCREMENT BY 1 START WITH 1"
00343         curs.prepare(sqlstr)
00344         curs.execute(sqlstr)
00345         conn.commit()
00346 def DropResultsDB():
00347         curs = conn.cursor()
00348         sqlstr = "DROP TABLE TEST_RESULTS"
00349         curs.prepare(sqlstr)
00350         curs.execute(sqlstr)
00351         sqlstr = "DROP SEQUENCE AUTO_INC"
00352         curs.prepare(sqlstr)
00353         curs.execute(sqlstr)
00354         conn.commit()
00355 def WriteResultsDB(id, label, name, status):
00356         curs = conn.cursor()
00357         sqlstr = "INSERT INTO TEST_RESULTS(RID, ID, LABEL, NAME, STATUS)"
00358         sqlstr +="VALUES(:rid, :ids, :labl, :nam, :stat)"
00359         curs.prepare(sqlstr)
00360         curs.execute(sqlstr, rid = GetAutoIncResultsDB(), ids = id, labl = label, nam = name, stat = status)
00361         conn.commit()
00362 def ReadResultsDB(id):
00363         curs = conn.cursor()
00364         sqlstr = "SELECT LABEL, NAME, STATUS FROM TEST_RESULTS WHERE ID = :ids"
00365         curs.prepare(sqlstr)
00366         curs.execute(sqlstr, ids = id)
00367         conn.commit()
00368         for row in curs:
00369                 print row
00370 def GetAutoIncResultsDB():
00371         curs = conn.cursor()
00372         sqlstr = "SELECT AUTO_INC.NextVal FROM DUAL"
00373         curs.prepare(sqlstr)
00374         curs.execute(sqlstr)
00375         for row in curs:
00376                 return row[0]
00377         
00378 def CreateSequenceDB():
00379         curs = conn.cursor()
00380         sqlstr = "CREATE TABLE SEQUENCES (LABEL VARCHAR(20), ID NUMBER, RUNID NUMBER)"
00381         curs.prepare(sqlstr)
00382         curs.execute(sqlstr)
00383         conn.commit()
00384 def WriteSequenceDB(label):
00385         curs = conn.cursor()
00386         sqlstr = "INSERT INTO SEQUENCES (LABEL, ID, RUNID) VALUES (:labl, 0, 0)"
00387         curs.prepare(sqlstr)
00388         curs.execute(sqlstr, labl=label)
00389 def ReadSequenceDB(label):
00390         curs = conn.cursor()
00391         sqlstr = "SELECT * FROM SEQUENCES WHERE LABEL = :labl"
00392         curs.prepare(sqlstr)
00393         curs.execute(sqlstr, labl=label)
00394         for row in curs:
00395                 print row
00396 def CheckLabelSequenceDB(label):
00397         curs = conn.cursor()
00398         sqlstr = "SELECT * FROM SEQUENCES WHERE LABEL = :labl"
00399         curs.prepare(sqlstr)
00400         curs.execute(sqlstr, labl=label)
00401         for row in curs:
00402                 return True
00403         return False
00404 def DropSequenceDB():
00405         curs = conn.cursor()
00406         sqlstr = "DROP TABLE SEQUENCES"
00407         curs.prepare(sqlstr)
00408         curs.execute(sqlstr)
00409         conn.commit()
00410 def NextIDVal(label):
00411         curs = conn.cursor()
00412         sqlstr = "SELECT ID FROM SEQUENCES WHERE LABEL = :labl FOR UPDATE"
00413         curs.prepare(sqlstr)
00414         curs.execute(sqlstr, labl=label)
00415         maxID = 0
00416         for row in curs:
00417                 maxID = row[0]
00418         maxID += 1
00419         sqlstr = "UPDATE SEQUENCES SET ID = :maxid WHERE LABEL = :labl"
00420         curs.prepare(sqlstr)
00421         curs.execute(sqlstr, maxid = maxID, labl=label)
00422         conn.commit()
00423         curs = conn.cursor()
00424         sqlstr = "SELECT ID FROM SEQUENCES WHERE LABEL = :labl"
00425         curs.prepare(sqlstr)
00426         curs.execute(sqlstr, labl=label)
00427         for row in curs:
00428                 return row[0]
00429 def NextRunIDVal(label):
00430         curs = conn.cursor()
00431         sqlstr = "SELECT RUNID FROM SEQUENCES WHERE LABEL = :labl"
00432         curs.prepare(sqlstr)
00433         curs.execute(sqlstr, labl=label)
00434         maxID = 0
00435         for row in curs:
00436                 maxID = row[0]
00437         maxID += 1
00438         sqlstr = "UPDATE SEQUENCES SET RUNID = :maxid"
00439         curs.prepare(sqlstr)
00440         curs.execute(sqlstr, maxid = maxID)
00441         sqlstr = "SELECT RUNID FROM SEQUENCES WHERE LABEL = :labl"
00442         curs.prepare(sqlstr)
00443         curs.execute(sqlstr, labl=label)
00444         for row in curs:
00445                 return row[0]
00446                 
00447 def GetDate():
00448         curs = conn.cursor()
00449         sqlstr = "SELECT SYSTIMESTAMP AS \"NOW\" FROM DUAL"
00450         curs.prepare(sqlstr)
00451         curs.execute(sqlstr)
00452         for row in curs:
00453                 return row[0]   
00454 def TestCompat(label, release, arch, path):
00455         relID = extractID(release)
00456         print "Testing exec "+label+" "+release+" "+arch+" from "
00457         print path+" : "
00458         curs = conn.cursor()
00459         cmds = """
00460 echo ""
00461 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
00462 echo "Candidate release: """+release+""" "
00463 echo "Arch: """+arch+""" "
00464 echo "Path: """+path+""" "
00465 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
00466 """
00467         sqlstr = "SELECT RELEASE, ARCH, PATH FROM VERSION_TABLE WHERE ID < :rid"
00468         curs.prepare(sqlstr)
00469         curs.execute(sqlstr, rid = relID)
00470         for row in curs:
00471                 cmds += RunTest(label, release, arch, path, row[0], row[1], row[2])
00472         return cmds
00473 def TestCompatRef(label, release, arch, path, refRelease, refArch, refPath):
00474         relID = extractID(release)
00475         print "Testing "+release+" "+arch+" from "
00476         print path+" : "
00477         curs = conn.cursor()
00478         cmds = """
00479 echo ""
00480 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
00481 echo "Candidate release: """+release+""" "
00482 echo "Arch: """+arch+""" "
00483 echo "Path: """+path+""" "
00484 echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
00485 """
00486         cmds += RunTest(label, release, arch, path, refRelease, refArch, refPath)
00487         return cmds
00488 def WriteReleaseDB(release, arch, path):
00489         curs = conn.cursor()
00490         relID = extractID(release)
00491         print "relID "+str(relID)
00492         sqlstr = "INSERT INTO VERSION_TABLE(ID, RELEASE, ARCH, PATH) VALUES(:rid, :rel, :arc, :pat)"
00493         curs.execute(sqlstr, rid = relID, rel = release, arc = arch, pat = path)
00494         conn.commit()
00495         print 'TABLE WRITTEN'   
00496 def SetEnv(release, arch, path):
00497         random.seed()
00498         srcPath = os.path.join(path, release,"src")
00499         cmds = """
00500 if [ $RETVAL = 0 ]; then
00501 echo "Setting environment variables for """+release+""" """+arch+""" "
00502 echo "path : """+path+""""
00503         eval pushd """+srcPath+"""
00504         RETVAL=$?
00505         if [ $RETVAL = 0 ]; then
00506                 export SCRAM_ARCH="""+arch+"""
00507                 RETVAL=$?
00508                 if [ $RETVAL = 0 ]; then
00509                         eval `scram runtime -sh`
00510                         RETVAL=$?
00511                         if [ $RETVAL = 0 ]; then
00512                                 export TNS_ADMIN=/afs/cern.ch/project/oracle/admin
00513                                 RETVAL=$?
00514                                 if [ $RETVAL = 0 ]; then
00515                                         eval popd
00516                                         RETVAL=$?
00517                                 fi
00518                         fi
00519                 fi
00520         TRELEASE="""+release+"""
00521         TARCH="""+arch+"""
00522         TPATH="""+path+"""
00523         TMAPNAME="""+release+"""_"""+arch+"""
00524         TMAINDB="""+coralConnStr+"""
00525         TAUXDB="""+"oracle://cms_orcoff_prep/CMS_COND_WEB"+"""
00526         TUSERNAME="""+USERNAME+"""
00527         TPASSWORD="""+PASSWORD+"""
00528         TTEST=$LOCALRT/test/$TARCH
00529         TBIN=$LOCALRT/bin/$TARCH
00530         TAUTH="""+AUTH_PATH+"""
00531         TSEED="""+str(random.randrange(1, 10))+"""
00532 echo "Environment variables set successfully"
00533         else
00534 echo "Setting environment failed on """+release+""" """+arch+""" return code :  $RETVAL"
00535         fi
00536 fi
00537 echo "----------------------------------------------" 
00538 """
00539         return cmds
00540 def Command(runStr):
00541         cmds = """
00542 if [ $RETVAL = 0 ]; then
00543 echo "Executing Command """+runStr+""" "
00544 echo "with $TRELEASE $TARCH :"
00545         """+runStr+"""
00546         RETVAL=$?
00547         if [ $RETVAL != 0 ]; then
00548 echo "Task failed on $TRELEASE $TARCH return code :  $RETVAL"
00549         else
00550 echo "Task performed successfully"
00551         fi
00552 fi
00553 """
00554         return cmds
00555 def RunTest(label, release, arch, path, refRelease, refArch, refPath ):
00556         cmds ="""
00557         RETVAL=0
00558 echo "*****************************************************************************************"
00559 echo "Reference release: """+refRelease+""" "
00560 echo "Arch: """+refArch+""" "
00561 echo "Path: """+refPath+""" "
00562 echo "*****************************************************************************************"
00563 """
00564         nr = 0
00565         enr = 0
00566         envType = 0
00567         print "init"
00568         for res in iResults:
00569                 cmds += """
00570 echo "==============================================="
00571 """
00572                 print res
00573                 if setEnvs[enr] == "ref" and envType != 1:
00574                         cmds += SetEnv(refRelease, refArch,refPath)
00575                         envType = 1
00576                 elif setEnvs[enr] == "cand" and envType != 2:
00577                         cmds += SetEnv(release, arch, path)
00578                         envType = 2
00579                 cmds +=Command(res)
00580                 enr +=1
00581         print "cmd"
00582         for res in pResults:
00583                 cmds += """
00584 echo "==============================================="
00585 """
00586                 print res
00587                 if setEnvs[enr] == "ref" and envType != 1:
00588                         cmds += SetEnv(refRelease, refArch,refPath)
00589                         envType = 1
00590                 elif setEnvs[enr] == "cand" and envType != 2:
00591                         cmds += SetEnv(release, arch, path)
00592                         envType = 2
00593                 cmds +=Command(res)
00594                 cmds += 'RCODE['+str(nr)+']=$RETVAL'
00595                 nr +=1
00596                 enr +=1
00597         print "final"
00598         cmds += """
00599         RETVAL=0
00600         """
00601         for res in fResults:
00602                 cmds += """
00603 echo "==============================================="
00604 """
00605                 print res
00606                 if setEnvs[enr] == "ref" and envType != 1:
00607                         cmds += SetEnv(refRelease, refArch,refPath)
00608                         envType = 1
00609                 elif setEnvs[enr] == "cand" and envType != 2:
00610                         cmds += SetEnv(release, arch, path)
00611                         envType = 2
00612                 cmds +=Command(res)
00613                 enr +=1
00614         global resCount
00615         resCount = nr
00616         cmds += """
00617 echo "==============================================="
00618         echo "Script return code : ${RCODE[1]}"
00619 echo "!L!"""+label+"""!TR!"""+release+"""!TA!"""+arch+"""!RR!"""+refRelease+"""!RA!"""+refArch
00620         for i in range (0, nr):
00621                 cmds+= "!C"+str(i)+"!${RCODE["+str(i)+"]}"
00622         cmds += "\""
00623         return cmds
00624 def CmdUsage():
00625         print "Command line arguments :"
00626         print "-c (-s) creates descriptor(status) db schema"
00627         print "-d drops descriptor(status) db schema. Optional : -R [release] -A [arch] to drop single entry (for descriptor db)"
00628         print "-w writes data to db. Goes only with -R [release] -A [arch] -P [path]"
00629         print "-r (-s) reads contents of descriptor(status) db"
00630         print "-t (-o) (-i) runs test. Goes only with -L [label] -R [release] -A [arch] -P [path] " 
00631         print "(-o) specifies reference release. supply additional parameters --R [refRelease] --A [refArch] --P [refPath]"
00632         print "(-i) argument forces candidate to test with itself as a reference"
00633 def CheckPath (release, arch, path):
00634         if(os.path.exists(path)):
00635                 if(os.path.exists(os.path.join(path, release))):
00636                         if(os.path.exists(os.path.join(path, release, "test", arch))):
00637                                 return True
00638                         else:
00639                                 print "Architecture not found"
00640                                 return False
00641                 else:
00642                         print "Release not found"
00643                         return False
00644         else:
00645                 print "Path not found"
00646                 return False
00647 def ReadArgs():
00648         try:
00649                 opts, args = getopt.getopt(sys.argv[1:], "cdwrthsoiL:R:A:P:", ['R=', 'A=', 'P='])
00650         except getopt.GetoptError, err:
00651                 # print help information and exit:
00652                 print str(err) # will print something like "option -a not recognized"
00653                 CmdUsage()
00654                 sys.exit(2)
00655         RELEASE = None
00656         ARCH = None
00657         PATH = None
00658         REF_RELEASE = None
00659         REF_ARCH = None
00660         REF_PATH = None
00661         LABEL = None
00662         wflag = False
00663         tflag = False
00664         dflag = False
00665         sflag = False
00666         cflag = False
00667         rflag = False
00668         oflag = False
00669         iflag = False
00670         cmds = ""
00671         for o, a in opts:
00672                 if o == "-c":
00673                         cflag = True
00674                 elif o == "-d":
00675                         dflag = True
00676                 elif o == "-w":
00677                         wflag = True
00678                 elif o == "-r":
00679                         rflag = True
00680                 elif o == "-t":
00681                         tflag = True
00682                 elif o == "-s":
00683                         sflag = True
00684                 elif o == "-o":
00685                         oflag = True
00686                 elif o == "-i":
00687                         iflag = True
00688                 elif o == "-L":
00689                         LABEL = a
00690                 elif o == "-R":
00691                         RELEASE = a
00692                 elif o == "-A":
00693                         ARCH = a
00694                 elif o == "-P":
00695                         PATH = a
00696                 elif o in ("-K", "--R"):
00697                         REF_RELEASE = a
00698                 elif o in ("-L", "--A"):
00699                         REF_ARCH = a
00700                 elif o in ("-M", "--P"):
00701                         REF_PATH = a
00702                 elif o == "-h":
00703                         CmdUsage()
00704                 else:
00705                         assert False, "unhandled option"
00706         if(cflag == True):
00707                 if(sflag == True):
00708                         CreateStatusDB()
00709                 else:
00710                         CreateReleaseDB()
00711         if(rflag == True):
00712                 if(sflag == True):
00713                         ReadStatusDB()
00714                 else:
00715                         ReadReleaseDB()
00716         if(wflag == True):
00717                 if(RELEASE != None and ARCH != None and PATH != None):
00718                                 WriteReleaseDB(RELEASE, ARCH, PATH)
00719                 else:
00720                         print "Bad arguments for -w"
00721         if(dflag == True):
00722                 if(sflag == True):
00723                         DropStatusDB();
00724                 else:
00725                         if(RELEASE != None and ARCH != None):
00726                                 DeleteReleaseDB(RELEASE, ARCH)
00727                         else:
00728                                 DropReleaseDB();
00729         if(tflag == True): 
00730                 if LABEL != None :
00731                         global pResults, iResults, fResults, setEnvs
00732                         iResults,pResults, fResults , setEnvs = ParseXML("sequences.xml", LABEL)
00733                         if(iResults == None or pResults == None or fResults == None):
00734                                 print "Error : sequences.xml reading failed!"
00735                         if(RELEASE != None and ARCH != None and PATH != None):
00736                                 if(oflag == True):
00737                                         if(REF_RELEASE != None and REF_ARCH != None and REF_PATH != None):
00738                                                 if(CheckPath(RELEASE, ARCH, PATH) == True):
00739                                                         if(CheckPath(REF_RELEASE, REF_ARCH, REF_PATH) == True):
00740                                                                 cmds=TestCompatRef(LABEL, RELEASE, ARCH, PATH, REF_RELEASE, REF_ARCH, REF_PATH)
00741                                                         else :
00742                                                                 print "Bad reference release arguments"
00743                                                 else :
00744                                                         print "Bad test release arguments"
00745                                         else:
00746                                                 print "Bad arguments for -t"
00747                                 else:
00748                                         if(CheckPath(RELEASE, ARCH, PATH) == True):
00749                                                 cmds = ''
00750                                                 if iflag == True:
00751                                                         cmds += TestCompatRef(LABEL, RELEASE, ARCH, PATH, RELEASE, ARCH, PATH)
00752                                                 cmds += TestCompat(LABEL, RELEASE, ARCH, PATH)
00753                                         else :
00754                                                 print "Bad test release arguments"
00755                 else:
00756                         print "Bad arguments for -t"
00757         return (cmds, LABEL)
00758 
00759 result = setConn()
00760 conn, coralConnStr, USERNAME, PASSWORD,AUTH_PATH = result.get('r0'), result.get('r1'), result.get('r2'), result.get('r3'), result.get('r4')
00761 
00762 cmdList, label = ReadArgs()
00763 #print cmdList
00764 timeStamp = GetDate()
00765 runID =0
00766 
00767 if cmdList != "":
00768         cmdList+="""
00769         echo "End of test"
00770         echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" """
00771         pipe = subprocess.Popen(cmdList, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
00772         stdout_value = pipe.communicate()[0]
00773         
00774         if CheckLabelSequenceDB(label) == False:
00775                 WriteSequenceDB(label)
00776         runID = NextRunIDVal(label)
00777         stdout_value = extractErr(runID, stdout_value)  
00778         print '\Output:', stdout_value
00779         AddLogStatusDB(label, runID, stdout_value)
00780         print "Test '%s' runID=%d" %(label, runID)
00781         stat = "SUCCESS"
00782         if(CheckIfOkStatusDB(runID, label) == False):
00783                 stat = "FAILURE"
00784         print "Exit status=%s" %stat
00785         print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
00786 conn.close()