def addPeriod(fname, start_time, end_time, entry="All", frontend="All", security_class="All", comment="", create_if_empty=True): exists = os.path.isfile(fname) if (not exists) and (not create_if_empty): raise IOError("[Errno 2] No such file or directory: '%s'" % fname) comment = comment.replace("\n", " ") comment = comment.replace("\r", " ") fd = open(fname, 'a+') try: fcntl.flock(fd, fcntl.LOCK_EX) if not exists: # new file, create header fd.write( "#%-29s %-30s %-20s %-30s %-20s # %s\n" % ("Start", "End", "Entry", "Frontend", "Sec_Class", "Comment")) if end_time is not None: fd.write("%-30s %-20s %-20s %-30s %-20s # %-20s\n" % (timeConversion.getISO8601_Local(start_time), timeConversion.getISO8601_Local(end_time), entry, frontend, security_class, comment)) else: fd.write("%-30s %-30s %-20s %-30s %-20s # %s\n" % (timeConversion.getISO8601_Local(start_time), "None", entry, frontend, security_class, comment)) finally: fd.close() return 0
def test_get_is_o8601__local(self): os.environ['TZ'] = tz time.tzset() self.assertEqual(iso_local, getISO8601_Local(now)) os.environ['TZ'] = tz_wrong time.tzset() self.assertNotEqual(iso_local, getISO8601_Local(now))
def addPeriod(fname,start_time,end_time,entry="All",frontend="All",security_class="All",comment="",create_if_empty=True): exists=os.path.isfile(fname) if (not exists) and (not create_if_empty): raise IOError, "[Errno 2] No such file or directory: '%s'"%fname comment=comment.replace("\n", " "); comment=comment.replace("\r", " "); fd=open(fname,'a+') try: fcntl.flock(fd,fcntl.LOCK_EX) if not exists: # new file, create header fd.write("#%-29s %-30s %-20s %-30s %-20s # %s\n"%("Start","End","Entry","Frontend","Sec_Class","Comment")) if end_time is not None: fd.write("%-30s %-20s %-20s %-30s %-20s # %-20s\n"%(timeConversion.getISO8601_Local(start_time),timeConversion.getISO8601_Local(end_time),entry,frontend,security_class,comment)) else: fd.write("%-30s %-30s %-20s %-30s %-20s # %s\n"%(timeConversion.getISO8601_Local(start_time),"None",entry,frontend,security_class,comment)) finally: fd.close() return 0;
def addPeriod( fname, start_time, end_time, create_if_empty=True): exists = os.path.isfile(fname) if (not exists) and (not create_if_empty): raise IOError("[Errno 2] No such file or directory: '%s'"%fname) fd = open(fname, 'a+') try: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB ) if not exists: # new file, create header fd.write("#%-29s %-30s\n"%("Start", "End")) if end_time is not None: fd.write("%-30s %-20s\n" % (timeConversion.getISO8601_Local(start_time), timeConversion.getISO8601_Local(end_time))) else: fd.write("%-30s %-30s\n" % (timeConversion.getISO8601_Local(start_time), "None")) finally: fd.close() return 0
def time2xml(the_time, outer_tag, indent_tab=DEFAULT_TAB, leading_tab=""): xml_data = {"UTC":{"unixtime":timeConversion.getSeconds(the_time), "ISO8601":timeConversion.getISO8601_UTC(the_time), "RFC2822":timeConversion.getRFC2822_UTC(the_time)}, "Local":{"ISO8601":timeConversion.getISO8601_Local(the_time), "RFC2822":timeConversion.getRFC2822_Local(the_time), "human":timeConversion.getHuman(the_time)}} return dict2string(xml_data, dict_name=outer_tag, el_name="timezone", subtypes_params={"class":{}}, indent_tab=indent_tab, leading_tab=leading_tab)
def addPeriod(fname, start_time, end_time, create_if_empty=True): exists = os.path.isfile(fname) if (not exists) and (not create_if_empty): raise IOError("[Errno 2] No such file or directory: '%s'" % fname) with open(fname, 'a+') as fd: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) if not exists: # new file, create header fd.write("#%-29s %-30s\n" % ("Start", "End")) if end_time is not None: fd.write("%-30s %-20s\n" % (timeConversion.getISO8601_Local(start_time), timeConversion.getISO8601_Local(end_time))) else: fd.write("%-30s %-30s\n" % (timeConversion.getISO8601_Local(start_time), "None")) return 0
def time2xml(the_time, outer_tag, indent_tab=DEFAULT_TAB, leading_tab=""): xml_data = { "UTC": { "unixtime": timeConversion.getSeconds(the_time), "ISO8601": timeConversion.getISO8601_UTC(the_time), "RFC2822": timeConversion.getRFC2822_UTC(the_time) }, "Local": { "ISO8601": timeConversion.getISO8601_Local(the_time), "RFC2822": timeConversion.getRFC2822_Local(the_time), "human": timeConversion.getHuman(the_time) } } return dict2string(xml_data, dict_name=outer_tag, el_name="timezone", subtypes_params={"class": {}}, indent_tab=indent_tab, leading_tab=leading_tab)
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
def endDowntime(fname, end_time=None): 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 | fcntl.LOCK_NB) # 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 cur_end_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]) # open period -> close if arr[1] == 'None' or ((cur_start_time < long(time.time())) and (cur_end_time > end_time)): outlines.append( "%-30s %-30s" % (arr[0], timeConversion.getISO8601_Local(end_time))) outlines.append("\n") closed_nr += 1 else: outlines.append(long_line) # closed just pass on #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
def test_ISO8601_Local__symmetric(self, flt_time): t = long(flt_time) tstr = getISO8601_Local(flt_time) self.assertEqual(t, extractISO8601_Local(getISO8601_Local(flt_time))) self.assertEqual(tstr, getISO8601_Local(extractISO8601_Local(tstr)))
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 with fd: 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]) # logic short circuit guarantees that cur_end_time is defined (arr[1] != 'None') 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() return closed_nr