Exemplo n.º 1
0
def read(fname, raise_on_error=False):
    try:
        fd = open(fname, 'r')
        try:
            fcntl.flock(fd, fcntl.LOCK_SH | fcntl.LOCK_NB)
            lines = fd.readlines()
        finally:
            fd.close()
    except IOError as e:
        if raise_on_error:
            raise
        else:
            return []  # no file -> no downtimes
#############################################################################
    out = []
    lnr = 0
    for long_line in lines:
        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 as 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 as e:
            if raise_on_error:
                raise ValueError("%s:%i: 2nd element: %s" % (fname, lnr, e))
            else:
                continue  #ignore errors

        out.append((start_time, end_time))
    return out  # out is a list
def read(fname, raise_on_error=False):
        try:
            fd = open(fname, 'r')
            try:
                fcntl.flock( fd, fcntl.LOCK_SH | fcntl.LOCK_NB )
                lines = fd.readlines()
            finally:
                fd.close()
        except IOError as e:
            if raise_on_error:
                raise
            else:
                return [] # no file -> no downtimes
#############################################################################
        out=[]
        lnr=0
        for long_line in lines:
            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 as 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 as e:
                if raise_on_error:
                    raise ValueError("%s:%i: 2nd element: %s"%(fname, lnr, e))
                else:
                    continue #ignore errors

            out.append((start_time, end_time))
        return out # out is a list
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:
Exemplo n.º 5
0
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 purgeOldPeriods(fname, cut_time=None, raise_on_error=False):
    if cut_time is None:
        cut_time = long(time.time())
    elif cut_time <= 0:
        cut_time = long(time.time()) + cut_time

    try:
        fd = open(fname, 'r+')
    except IOError as e:
        if raise_on_error:
            raise
        else:
            return 0  # no file -> nothing to purge

    try:
        fcntl.flock(fd, fcntl.LOCK_EX)
        # read the old info
        inlines = fd.readlines()

        outlines = []
        lnr = 0
        cut_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:
                if raise_on_error:
                    raise ValueError("%s:%i: Expected pair, got '%s'" %
                                     (fname, lnr, line))
                else:
                    outlines.append(long_line)
                    continue  # pass on malformed lines

            try:
                if arr[1] == 'None':
                    end_time = None
                else:
                    end_time = timeConversion.extractISO8601_Local(arr[1])
            except ValueError as e:
                if raise_on_error:
                    raise ValueError("%s:%i: 2nd element: %s" %
                                     (fname, lnr, e))
                else:
                    outlines.append(long_line)
                    continue  #unknown, pass on

            if end_time is None:
                outlines.append(long_line)
                continue  #valid forever, pass on

            if end_time >= cut_time:
                outlines.append(long_line)
                continue  # end_time after cut_time, have to keep it

            # if we got here, the period ended before the cut date... cut it
            cut_nr += 1
            pass  # end for

        # go back to start to rewrite
        fd.seek(0)
        fd.writelines(outlines)
        fd.truncate()
    finally:
        fd.close()

    return cut_nr
def read(fname, raise_on_error=False):
    try:
        fd = open(fname, 'r')
        try:
            fcntl.flock(fd, fcntl.LOCK_SH)
            lines = fd.readlines()
        finally:
            fd.close()
    except IOError as e:
        if raise_on_error:
            raise
        else:
            return []  # no file -> no downtimes

    out = []
    lnr = 0
    for long_line in lines:
        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 as 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 as e:
            if raise_on_error:
                raise ValueError("%s:%i: 2nd element: %s" % (fname, lnr, e))
            else:
                continue  #ignore errors

        # Addition.  If more arguments exists, parse
        # Entry, Frontend, Security_Class, Comment
        if (len(arr) >= 3):
            entry = arr[2]
        else:
            entry = "factory"
        if (len(arr) >= 3):
            frontend = arr[3]
        else:
            frontend = "All"
        if (len(arr) >= 4):
            security_class = arr[4]
        else:
            security_class = "All"
        if (len(arr) >= 5):
            comment = arr[5:]
        else:
            comment = ""

        out.append(
            (start_time, end_time, entry, frontend, security_class, comment))
        # end for long_line in lines:

    return out
def read(fname, raise_on_error=False):
        try:
            fd=open(fname, 'r')
            try:
                fcntl.flock(fd, fcntl.LOCK_SH)
                lines=fd.readlines()
            finally:
                fd.close()
        except IOError as e:
            if raise_on_error:
                raise
            else:
                return [] # no file -> no downtimes

        out=[]
        lnr=0
        for long_line in lines:
            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 as 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 as e:
                if raise_on_error:
                    raise ValueError("%s:%i: 2nd element: %s"%(fname, lnr, e))
                else:
                    continue #ignore errors


            # Addition.  If more arguments exists, parse
            # Entry, Frontend, Security_Class, Comment
            if (len(arr)>=3):
                entry=arr[2]
            else:
                entry="factory"
            if (len(arr)>=3):
                frontend=arr[3]
            else:
                frontend="All"
            if (len(arr)>=4):
                security_class=arr[4]
            else:
                security_class="All"
            if (len(arr)>=5):
                comment=arr[5:]
            else:
                comment=""

            out.append((start_time, end_time, entry, frontend, security_class, comment))
            # end for long_line in lines:
            
        return out
def purgeOldPeriods(fname,cut_time=None, raise_on_error=False):
        if cut_time is None:
            cut_time=long(time.time())
        elif cut_time<=0:
            cut_time=long(time.time())+cut_time

        try:
            fd=open(fname, 'r+')
        except IOError as e:
            if raise_on_error:
                raise
            else:
                return 0 # no file -> nothing to purge
        
        try:
            fcntl.flock(fd, fcntl.LOCK_EX)
            # read the old info
            inlines=fd.readlines()

            outlines=[]
            lnr=0
            cut_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:
                    if raise_on_error:
                        raise ValueError("%s:%i: Expected pair, got '%s'"%(fname, lnr, line))
                    else:
                        outlines.append(long_line)
                        continue # pass on malformed lines

                try:
                    if arr[1]=='None':
                        end_time=None
                    else:
                        end_time=timeConversion.extractISO8601_Local(arr[1])
                except ValueError as e:
                    if raise_on_error:
                        raise ValueError("%s:%i: 2nd element: %s"%(fname, lnr, e))
                    else:
                        outlines.append(long_line)
                        continue #unknown, pass on
                
                if end_time is None:
                    outlines.append(long_line)
                    continue #valid forever, pass on
                
                if end_time>=cut_time:
                    outlines.append(long_line)
                    continue # end_time after cut_time, have to keep it

                # if we got here, the period ended before the cut date... cut it
                cut_nr+=1
                pass # end for
            
            # go back to start to rewrite
            fd.seek(0)
            fd.writelines(outlines)
            fd.truncate()
        finally:
            fd.close()

        return cut_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 test_extract_is_o8601__local(self):
     os.environ['TZ'] = tz
     time.tzset()
     self.assertEqual(now, extractISO8601_Local(iso_local))
     self.assertEqual(now_dst, extractISO8601_Local(iso_local_dst))
Exemplo n.º 12
0
 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)))
Exemplo n.º 13
0
 def test_extract_is_o8601__local(self):
     os.environ['TZ'] = tz
     time.tzset()
     self.assertEqual(now, extractISO8601_Local(iso_local))
     self.assertEqual(now_dst, extractISO8601_Local(iso_local_dst))
Exemplo n.º 14
0
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