Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-03 05:26:36

0001 from __future__ import print_function
0002 import os,json,sys
0003 
0004 
0005 STREAM    = "EXPRESS"
0006 JSON_NAME = "certif.json"
0007 
0008 
0009 
0010 def splitByTag(line,tags=["td","th"]):
0011   values = []
0012   pos = 0
0013   while pos > -1:
0014     firstTag=None
0015     firstTagPos=len(line)
0016     for tag in tags:
0017       posTag=line.find("<"+tag,pos)
0018       if posTag<firstTagPos and posTag>-1:
0019         firstTag=tag
0020         firstTagPos=posTag
0021     if not firstTag:
0022       break
0023     tag=firstTag
0024     posStartTag     = line.find("<"+tag,pos)
0025     posStartContent = line.find(">",posStartTag)+1
0026     posEnd      = line.find("</"+tag,posStartContent)
0027     pos = posEnd
0028     values.append(line[posStartContent:posEnd])
0029   return values
0030 
0031 def getComment(line):
0032    tag="<span title=\""
0033    startComment=line.find(tag)+len(tag)
0034    stopComment=line.find("\"",startComment)
0035    return(line[startComment:stopComment])
0036 
0037 def getRunQuality(fName=JSON_NAME):
0038   runQuality = {}
0039   if os.path.isfile(fName):
0040     with open(fName) as f:
0041       runQuality=json.load(f)
0042   return runQuality
0043 
0044 def getHTMLtable(fName=".certif_temp.html"):
0045   table=""
0046   title =""
0047   with open(fName,"r") as certifFile:
0048     certifRaw = certifFile.read()
0049     if len(certifRaw)<100:
0050       return(0,0)
0051     title = certifRaw[certifRaw.find("<title>")+len("<title>"):certifRaw.find("</title>")]
0052     table=certifRaw.split("table>")[1]
0053   
0054   return(title,table)
0055 
0056 
0057 def generateJSON():  
0058   # Getting certification HTML file
0059   os.system("wget http://vocms061.cern.ch/event_display/RunList/status.Collisions17.html -O .certif_temp.html > /dev/null 2>&1") 
0060   
0061   if not os.path.isfile(".certif_temp.html"):
0062     print("Unable to download file")
0063     return(1)
0064   
0065   runQuality = getRunQuality()
0066   
0067   if runQuality == {}:
0068     print("Warning, no %s found. Creating new list."%JSON_NAME)
0069   
0070   (title,table)=getHTMLtable()
0071   if table==0:
0072     print("Error, Unable to download run table.")
0073     return(1)
0074   runQuality["Last update"] = title[title.find("(")+1:title.find(")")]
0075   
0076   #Clean table and split per line
0077   table.replace("\n","").replace("<tr>","")
0078   table=table.split("</tr>")
0079 
0080 
0081   lenExpected = -1  #Expected width of line
0082   colNumber = 0     #Column id to read status
0083 
0084   for line in table:
0085     entry = splitByTag(line)
0086     if lenExpected < 0 : 
0087       lenExpected = len(entry)
0088       for i in range(len(entry)):
0089         if STREAM.lower() in entry[i].lower():
0090           colNumber = i
0091     else:
0092       if len(entry)==lenExpected:
0093         comment=getComment(entry[colNumber])
0094         runQuality[entry[0]]={"qual":entry[colNumber][:4],"comment":comment}
0095       elif len(entry)>0:
0096         print("Error, unrecognized line !")
0097         return 1
0098     
0099 
0100   with open(JSON_NAME,'w') as data_file:    
0101       data_file.write(json.dumps(runQuality))
0102 
0103 
0104   os.system("rm .certif_temp.html")
0105   return(0)
0106 
0107 def get():
0108    if generateJSON()!=0:
0109       print("ERROR, JSON file not updated... Loading old file.")
0110    
0111    if not JSON_NAME in os.listdir("."):
0112       print("ERROR, no JSON file available...")
0113       return({})
0114    else:
0115       return getRunQuality()
0116 
0117 def checkRun(runNumber, runQuality):
0118    if not str(runNumber) in runQuality.keys():
0119       print("WARNING : no certification info for run %s"%runNumber)
0120       return(0)
0121    else:
0122       print("Data certification for run %s is %s"%(runNumber,runQuality[str(runNumber)]["qual"]))
0123       if not "GOOD" in runQuality[str(runNumber)]["qual"]:
0124          print("Reason : %s"%runQuality[str(runNumber)]["comment"])
0125       return("GOOD" in runQuality[str(runNumber)]["qual"])
0126 
0127 if __name__ == "__main__":
0128    generateJSON()
0129    qual = get()
0130    for key in qual.keys():
0131       checkRun(int(key),qual)
0132