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
import os,json,sys


STREAM    = "EXPRESS"
JSON_NAME = "certif.json"



def splitByTag(line,tags=["td","th"]):
  values = []
  pos = 0
  while pos > -1:
    firstTag=None
    firstTagPos=len(line)
    for tag in tags:
      posTag=line.find("<"+tag,pos)
      if posTag<firstTagPos and posTag>-1:
        firstTag=tag
        firstTagPos=posTag
    if not firstTag:
      break
    tag=firstTag
    posStartTag     = line.find("<"+tag,pos)
    posStartContent = line.find(">",posStartTag)+1
    posEnd 	    = line.find("</"+tag,posStartContent)
    pos = posEnd
    values.append(line[posStartContent:posEnd])
  return values

def getComment(line):
   tag="<span title=\""
   startComment=line.find(tag)+len(tag)
   stopComment=line.find("\"",startComment)
   return(line[startComment:stopComment])

def getRunQuality(fName=JSON_NAME):
  runQuality = {}
  if os.path.isfile(fName):
    with open(fName) as f:
      runQuality=json.load(f)
  return runQuality

def getHTMLtable(fName=".certif_temp.html"):
  table=""
  title =""
  with open(fName,"r") as certifFile:
    certifRaw = certifFile.read()
    if len(certifRaw)<100:
      return(0,0)
    title = certifRaw[certifRaw.find("<title>")+len("<title>"):certifRaw.find("</title>")]
    table=certifRaw.split("table>")[1]
  
  return(title,table)


def generateJSON():  
  # Getting certification HTML file
  os.system("wget http://vocms061.cern.ch/event_display/RunList/status.Collisions17.html -O .certif_temp.html > /dev/null 2>&1") 
  
  if not os.path.isfile(".certif_temp.html"):
    print("Unable to download file")
    return(1)
  
  runQuality = getRunQuality()
  
  if runQuality == {}:
    print("Warning, no %s found. Creating new list."%JSON_NAME)
  
  (title,table)=getHTMLtable()
  if table==0:
    print("Error, Unable to download run table.")
    return(1)
  runQuality["Last update"] = title[title.find("(")+1:title.find(")")]
  
  #Clean table and split per line
  table.replace("\n","").replace("<tr>","")
  table=table.split("</tr>")


  lenExpected = -1 	#Expected width of line
  colNumber = 0		#Column id to read status

  for line in table:
    entry = splitByTag(line)
    if lenExpected < 0 : 
      lenExpected = len(entry)
      for i in range(len(entry)):
        if STREAM.lower() in entry[i].lower():
          colNumber = i
    else:
      if len(entry)==lenExpected:
        comment=getComment(entry[colNumber])
        runQuality[entry[0]]={"qual":entry[colNumber][:4],"comment":comment}
      elif len(entry)>0:
        print("Error, unrecognized line !")
        return 1
    

  with open(JSON_NAME,'w') as data_file:    
      data_file.write(json.dumps(runQuality))


  os.system("rm .certif_temp.html")
  return(0)

def get():
   if generateJSON()!=0:
      print("ERROR, JSON file not updated... Loading old file.")
   
   if not JSON_NAME in os.listdir("."):
      print("ERROR, no JSON file available...")
      return({})
   else:
      return getRunQuality()

def checkRun(runNumber, runQuality):
   if not str(runNumber) in runQuality.keys():
      print("WARNING : no certification info for run %s"%runNumber)
      return(0)
   else:
      print("Data certification for run %s is %s"%(runNumber,runQuality[str(runNumber)]["qual"]))
      if not "GOOD" in runQuality[str(runNumber)]["qual"]:
         print("Reason : %s"%runQuality[str(runNumber)]["comment"])
      return("GOOD" in runQuality[str(runNumber)]["qual"])

if __name__ == "__main__":
   generateJSON()
   qual = get()
   for key in qual.keys():
      checkRun(int(key),qual)