def edit_supported(source_lvn, current_lvn):
    """
        Function that edits the supported_versions.conf.
        It opens the file and inserts a line of this form:

        Adaptation:  bfr_<src_lvn>  <===> bfr_<trg_lvn>

        It calculates the target lvn itself by examining what is the
        latest lvn in the vevak file,and adds 2.The latest lvn should
        normally be always the same as the src_lvn...

        @rtype  None

    """

    print 'Going to edit supported_versions.conf file\n'
    log.info('Going to edit supported_versions.conf file')
    print 'Calculating target LVN'
    log.info('Calculating target LVN')

    print '======================'
    if int(current_lvn) != int(source_lvn):
        log.critical(
            'It seems that source lvn:%s differs from current lvn:%s in %s.Usually they need to be equal.'
            % (source_lvn, current_lvn, options.branch))

    trg_lvn = str(int(current_lvn) + 2)
    print 'Target lvn will be :', trg_lvn
    log.info('Target lvn will be :%s' % trg_lvn)
    print '======================='

    S = open(supported_file, 'r+')
    supported_list = S.readlines()
    for i, line in enumerate(supported_list):
        if re.search('End of file', line, re.I):
            #            print 'Found End of file at line : ',i
            break

    if options.branch == 'trunk':
        bfr = '1'
    else:
        #: 1st way to retrieve BFR :#
        bfr_lvn = re.findall('\d+' + '_' + '\d+', '\n'.join(supported_list))
        bfrs = [line.split('_')[0] for line in bfr_lvn]
        c = Counter(bfrs)
        bfr_1 = c.most_common()[0][0]
        #: 2nd backup way to retrieve BFR :#
        try:
            f = open('Makefile.inc')
        except IOError:
            log.error('Unable to open Makefile.inc to retrieve the BFR')
            sys.exit(12)
        else:
            for line in f:
                if (re.search('BFR', line) and re.search(
                        '\d{6}', line)) and not re.search('9{4,}', line):
                    bfr_2 = re.search('(\d{6})', line).group(1)
        if int(bfr_1) == int(bfr_2):
            bfr = bfr_2
        else:
            print 'Collected BFRs that were retrieved with 2 methods are different'
            print '%s != %s' % (str(bfr_1), str(bfr_2))
            log.critical(
                'Collected BFRs that were retrieved with 2 methods are different,but will proceed with %s'
                % str(bfr_2))
            log.critical('%s != %s', str(bfr_1), str(bfr_2))
            bfr = bfr_2
            # sys.exit(2)
    supported_list.insert(
        i, 'Adaptation:  ' + str(bfr) + '_' + source_lvn + ' <===> ' +
        str(bfr) + '_' + trg_lvn + '\n')
    S.seek(0)
    S.write(''.join(supported_list))
    S.close()
    git.diff()
    log.info(
        'supported versions edited.This is the diff:  ================= \n\n\n %s ==============',
        git.stdOut)
    print 'Done\n'
    log.info('Done')
def edit_supported(source_lvn, current_lvn):
    """
        Function that edits the supported_versions.conf.
        It opens the file and inserts a line of this form:

        Adaptation:  bfr_<src_lvn>  <===> bfr_<trg_lvn>

        It calculates the target lvn itself by examining what is the
        latest lvn in the vevak file,and adds 2.The latest lvn should
        normally be always the same as the src_lvn...

        @rtype  None

    """

    print 'Going to edit supported_versions.conf file\n'
    log.info('Going to edit supported_versions.conf file')
    print 'Calculating target LVN'
    log.info('Calculating target LVN')

    print '======================'
    if int(current_lvn) != int(source_lvn):
        log.critical('It seems that source lvn:%s differs from current lvn:%s in %s.Usually they need to be equal.' % (
            source_lvn, current_lvn, options.branch))

    trg_lvn = str(int(current_lvn) + 2)
    print 'Target lvn will be :', trg_lvn
    log.info('Target lvn will be :%s' % trg_lvn)
    print'======================='

    S = open(supported_file, 'r+')
    supported_list = S.readlines()
    for i, line in enumerate(supported_list):
        if re.search('End of file', line, re.I):
            #            print 'Found End of file at line : ',i
            break

    if options.branch == 'trunk':
        bfr = '1'
    else:
        #: 1st way to retrieve BFR :#
        bfr_lvn = re.findall('\d+' + '_' + '\d+', '\n'.join(supported_list))
        bfrs = [line.split('_')[0] for line in bfr_lvn]
        c = Counter(bfrs)
        bfr_1 = c.most_common()[0][0]
        #: 2nd backup way to retrieve BFR :#
        try:
            f = open('Makefile.inc')
        except IOError:
            log.error('Unable to open Makefile.inc to retrieve the BFR')
            sys.exit(12)
        else:
            for line in f:
                if (re.search('BFR', line) and re.search('\d{6}', line)) and not re.search('9{4,}', line):
                    bfr_2 = re.search('(\d{6})', line).group(1)
        if int(bfr_1) == int(bfr_2):
            bfr = bfr_2
        else:
            print 'Collected BFRs that were retrieved with 2 methods are different'
            print '%s != %s' % (str(bfr_1), str(bfr_2))
            log.critical(
                'Collected BFRs that were retrieved with 2 methods are different,but will proceed with %s' % str(bfr_2))
            log.critical('%s != %s', str(bfr_1), str(bfr_2))
            bfr = bfr_2
            # sys.exit(2)
    supported_list.insert(i, 'Adaptation:  ' + str(bfr) + '_' +
                          source_lvn + ' <===> ' + str(bfr) + '_' + trg_lvn + '\n')
    S.seek(0)
    S.write(''.join(supported_list))
    S.close()
    git.diff()
    log.info('supported versions edited.This is the diff:  ================= \n\n\n %s ==============', git.stdOut)
    print 'Done\n'
    log.info('Done')
def check_sanity(commited=None):
    """ Function that checks the sanity status from buildbot 
        If sanity is red it calls itself recursively in order
        to wait until sanity is green.If it is red for 30 minutes
        it aborts.

        @rtype  tuple
        $rvalue flag that tells is sanity is green and the green revision

    """

    global retry
    is_green = False

    print 'Checking sanity status'

    #: the buildbot file shall be kept into a list in order to parse it later :#
    try:
        f = urllib2.urlopen(buildbot_url).readlines()
    except urllib2.HTTPError as e:
        print 'Unable to fetch buildbot url '
        print e
        sys.exit(3)

    #: it will be used to get the revision in the sanity box :#
    sanity_reg = re.compile(
        r'(?<=href=)"builders/SANITY/builds/\d+">(\d{6})</a><br />(\w+)<br')

    #: check the validity of the buildbot file that has now become a list ---check line 133:#

    if 'SANITY' in ' '.join(f):
        log.info(
            'SANITY string is found in the buildbot file.Proceed with parsing')
    else:
        log.error('Failed to find SANITY in buildbot file.Check manually')
        sys.exit(3)

    if not commited:
        #: we are in the case where the script calls for the first time check_sanity in order to proceed with the commits :#
        for line in f:

            if 'SANITY' in line and 'failed' in line:

                #: enter an 3min loop periodically until sanity box is green.Break the loop then. :#

                while (retry < 10):

                    #: give it some time until it tries again :#
                    #: There are some cases about sanity that need to be handled accordingly :#
                    #: 1)The sanity has left in red state and no-one is looking at this
                    #: |
                    #: |-----> the script will try for 30 minutes and abort.Before aborting it must restore
                    #:         the files that have been changed to their previous state
                    #: 2)The sanity is either red or green but it is expected by the responsible team to be fixed
                    #:   in less than 1h

                    time_period = 180
                    log.info(
                        'Sleeping for 3 mins in order to wait for sanity to be green'
                    )
                    time.sleep(time_period)
                    log.info('Retrying sanity check:%s' % retry)
                    retry += 1
                    return check_sanity()

                log.critical(
                    'Tried for 30minutes and sanity is still red.Aborting...num of retries:%s'
                    % retry)
                sys.exit(8)

            elif 'SANITY' in line and 'successful' in line:
                green_rev = sanity_reg.search(line).group(1)
                is_green = True
                print '================================================='
                print 'Green revision currently on buildbot SANITY is ', green_rev
                print '================================================='
                print 'Done\n'

                break

            else:
                pass

    #: we should get here when we call check_sanity with commited revision as parameter :#
    #: in order to verify if this revision ended with green sanity                      :#

    else:
        #: this is the case that we make sure our commits turned sanity into green :#
        for line in f:
            if 'SANITY/builds' in line:
                while sanity_reg.search(line).group(1) != commited:
                    print 'revision in buildot:', sanity_reg.search(
                        line).group(1)
                    print 'revision that commited:', commited
                    sanity_reg.search(line).group(1) != commited
                    log.info(
                        'Currently building.Wait until our commited revision is in the sanity box and turns green'
                    )
                    time.sleep(120)
                    return check_sanity(commited)
                log.info('Commited revision %s is in sanity box' % commited)
                if 'successful' in line:
                    log.info('Commited revision %s turned green.SUCCESS!!!' %
                             commited)
                    print '=========================================='
                    print '    We commited %s and is green ' % commited
                    print '=========================================='
                    green_rev = commited
                    is_green = True
                    break
                else:
                    log.error(
                        'We most probably broke sanity @ %s.Check manually' %
                        commited)
                    error = 'Hi,\n\n Commited revision %s has resulted in red sanity.\n\nCould you please check?' % commited
                    sender = find_sender(username())
                    # send_mail(error,sender)
                    sys.exit(4)

    return (is_green, green_rev)
def check_sanity(commited=None):
    """ Function that checks the sanity status from buildbot 
        If sanity is red it calls itself recursively in order
        to wait until sanity is green.If it is red for 30 minutes
        it aborts.

        @rtype  tuple
        $rvalue flag that tells is sanity is green and the green revision

    """

    global retry
    is_green = False

    print'Checking sanity status'

    #: the buildbot file shall be kept into a list in order to parse it later :#
    try:
        f = urllib2.urlopen(buildbot_url).readlines()
    except urllib2.HTTPError as e:
        print 'Unable to fetch buildbot url '
        print e
        sys.exit(3)

    #: it will be used to get the revision in the sanity box :#
    sanity_reg = re.compile(
        r'(?<=href=)"builders/SANITY/builds/\d+">(\d{6})</a><br />(\w+)<br')

    #: check the validity of the buildbot file that has now become a list ---check line 133:#

    if 'SANITY' in ' '.join(f):
        log.info('SANITY string is found in the buildbot file.Proceed with parsing')
    else:
        log.error('Failed to find SANITY in buildbot file.Check manually')
        sys.exit(3)

    if not commited:
        #: we are in the case where the script calls for the first time check_sanity in order to proceed with the commits :#
        for line in f:

            if 'SANITY' in line and 'failed' in line:

                #: enter an 3min loop periodically until sanity box is green.Break the loop then. :#

                while (retry < 10):

                    #: give it some time until it tries again :#
                    #: There are some cases about sanity that need to be handled accordingly :#
                    #: 1)The sanity has left in red state and no-one is looking at this
                    #: |
                    #: |-----> the script will try for 30 minutes and abort.Before aborting it must restore
                    #:         the files that have been changed to their previous state
                    #: 2)The sanity is either red or green but it is expected by the responsible team to be fixed
                    #:   in less than 1h

                    time_period = 180
                    log.info(
                        'Sleeping for 3 mins in order to wait for sanity to be green')
                    time.sleep(time_period)
                    log.info('Retrying sanity check:%s' % retry)
                    retry += 1
                    return check_sanity()

                log.critical(
                    'Tried for 30minutes and sanity is still red.Aborting...num of retries:%s' % retry)
                sys.exit(8)

            elif 'SANITY' in line and 'successful' in line:
                green_rev = sanity_reg.search(line).group(1)
                is_green = True
                print'================================================='
                print 'Green revision currently on buildbot SANITY is ', green_rev
                print'================================================='
                print'Done\n'

                break

            else:
                pass

    #: we should get here when we call check_sanity with commited revision as parameter :#
    #: in order to verify if this revision ended with green sanity                      :#

    else:
        #: this is the case that we make sure our commits turned sanity into green :#
        for line in f:
            if 'SANITY/builds' in line:
                while sanity_reg.search(line).group(1) != commited:
                    print 'revision in buildot:', sanity_reg.search(line).group(1)
                    print 'revision that commited:', commited
                    sanity_reg.search(line).group(1) != commited
                    log.info(
                        'Currently building.Wait until our commited revision is in the sanity box and turns green')
                    time.sleep(120)
                    return check_sanity(commited)
                log.info('Commited revision %s is in sanity box' % commited)
                if 'successful' in line:
                    log.info(
                        'Commited revision %s turned green.SUCCESS!!!' % commited)
                    print'=========================================='
                    print'    We commited %s and is green ' % commited
                    print'=========================================='
                    green_rev = commited
                    is_green = True
                    break
                else:
                    log.error(
                        'We most probably broke sanity @ %s.Check manually' % commited)
                    error = 'Hi,\n\n Commited revision %s has resulted in red sanity.\n\nCould you please check?' % commited
                    sender = find_sender(username())
                    # send_mail(error,sender)
                    sys.exit(4)

    return (is_green, green_rev)