def endDowntime(fname,end_time=None,entry="All",frontend="All",security_class="All",comment=""): comment=comment.replace("\r", " "); comment=comment.replace("\n", " "); if end_time is None: end_time=long(time.time()) try: fd=open(fname,'r+') except IOError: return 0 # no file -> nothing to end try: fcntl.flock(fd,fcntl.LOCK_EX) # read the old info inlines=fd.readlines() outlines=[] lnr=0 closed_nr=0 for long_line in inlines: lnr+=1 line=long_line.strip() if len(line)==0: outlines.append(long_line) continue # pass on empty lines if line[0:1]=='#': outlines.append(long_line) continue # pass on comments arr=line.split() if len(arr)<2: outlines.append(long_line) continue # pass on malformed lines #make sure this is for the right entry if ((entry!="All")and(len(arr)>2)and(entry!=arr[2])): outlines.append(long_line) continue if ((entry=="All")and(len(arr)>2)and("factory"==arr[2])): outlines.append(long_line) continue if ((frontend!="All")and(len(arr)>3)and(frontend!=arr[3])): outlines.append(long_line) continue #make sure that this time tuple applies to this security_class if ((security_class!="All")and(len(arr)>4)and(security_class!=arr[4])): outlines.append(long_line) continue cur_start_time=0 if arr[0]!='None': cur_start_time=timeConversion.extractISO8601_Local(arr[0]) if arr[1]!='None': cur_end_time=timeConversion.extractISO8601_Local(arr[1]) if arr[1]=='None' or ((cur_start_time<long(time.time())) and (cur_end_time>end_time)): # open period -> close outlines.append("%-30s %-30s"%(arr[0],timeConversion.getISO8601_Local(end_time))) if (len(arr)>2): sep=" "; t=2 for param in arr[2:]: if t<5: outlines.append("%s%-20s" % (sep,param)); else: outlines.append("%s%s" % (sep,param)); t=t+1 if (comment!=""): outlines.append("; %s" % (comment)); outlines.append("\n"); closed_nr+=1 else: # closed just pass on outlines.append(long_line) #Keep parsing file, since there may be multiple downtimes #pass # end for # go back to start to rewrite fd.seek(0) fd.writelines(outlines) fd.truncate() finally: fd.close() return closed_nr
lnr+=1 line=long_line.strip() if len(line)==0: continue # ignore empty lines if line[0:1]=='#': continue # ignore comments arr=line.split() # Read in lines of the downtime file # Start End Entry Security_Class Comment if len(arr)<2: if raise_on_error: raise ValueError, "%s:%i: Expected pair, got '%s'"%(fname,lnr,line) else: continue # ignore malformed lines try: start_time=timeConversion.extractISO8601_Local(arr[0]) except ValueError,e: if raise_on_error: raise ValueError, "%s:%i: 1st element: %s"%(fname,lnr,e) else: continue #ignore errors try: if arr[1]=='None': end_time=None else: end_time=timeConversion.extractISO8601_Local(arr[1]) except ValueError,e: if raise_on_error: raise ValueError, "%s:%i: 2nd element: %s"%(fname,lnr,e) else: