Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:10

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