#!/usr/bin/env python

# This script is a wrapper to call bioscmos_hightemp.c on Racer-X nodes
# (IQ5000S, IQ5400S, IQ10000X) on boot if the BIOS parameter cmosHighTempOp
# has not already been set to Enabled.  This setting is expected to provide
# a longer lifespan for DIMMs on Racer-X nodes.

import re

import isi.hw.version as hwver
import isi.app.lib.procs as procs

# Substring for checking cmosHighTempOp status
BIOS_STRING = "2.1b2"
CMOS_STRING = "00"
SUPPORTED_LIST = [hwver.HWGEN_RACERX]


#------------------------------------------------------------------------------
def main():
    if hwver.hwgen not in SUPPORTED_LIST:
        print "Not enabling cmosHighTempOp due to incompatible hardware"
        return

    cmosRegex = re.compile("\s*cmosHighTempOp\s*(?P<page>\d+)\s*"
              "(?P<offset>\d+)\s*(?P<bits>\d+)\s*(?P<data>\d+)\s*")
    biosRegex = re.compile("\s*BIOS\s*Version:\s*(?P<biosRev>.*)")

    while True:
        ans = raw_input('Enabling cmosHighTempOp in BIOS - this will reset '
            'all BIOS settings to their defaults - continue? (y|n):')
        if not ans:
            continue
        if ans not in ['y', 'Y', 'n', 'N']:
            print "Please enter y or n"
            continue
        if ans == 'y' or ans == 'Y':
            userResp = True
            break
        if ans == 'n' or ans == 'N':
            userResp = False
            break

    if userResp == False:
        print "Not enabling cmosHighTempOp due to user input, exiting..."
        return

    biosCheckCmd = 'isi_smbios -q'
    (errorCheck, outputCheck) = procs.get_cmd_output(biosCheckCmd)
    foundBiosVer = False
    for line in outputCheck:
        biosMatch = biosRegex.match(line)
        if biosMatch:
            if biosMatch.group("biosRev") == BIOS_STRING:
                foundBiosVer = True
                break
    if foundBiosVer == False:
        print ("Error: BIOS is not version 2.1b2.  BIOS must be 2.1b2 to "
            "change cmosHighTempOp")
        return

    # We know we are in a Racer-X node, so continue with cmosHighTempOp
    # settings change
    infoCmd = '/usr/bin/isi_hwtools/bioscmos_info'
    (errorInfo, outputInfo) = procs.get_cmd_output(infoCmd)
    if errorInfo:
        print ("Error: Could not get bioscmos_info output for checking "
            "cmosHighTempOp status, cmosHighTempOp may not be enabled")
        return

    foundString = False
    for line in outputInfo:
        cmosMatch = cmosRegex.match(line)
        if cmosMatch:
            foundString = True
            if cmosMatch.group("data") == CMOS_STRING:
                # In this case, we have found cmosHighTempOp disabled
                print "cmosHighTempOp not set, attempting to enable..."
                setCmd = '/usr/bin/isi_hwtools/bioscmos_hightemp'
                (errorCmd, outputCmd) = procs.get_cmd_output(setCmd)
                if errorCmd == 0:
                    print "cmosHighTempOp now enabled"
                else:
                    print "Error: Could not enable cmosHighTempOp"
            else:
                print "cmosHighTempOp already enabled"
            break
    if foundString == False:
        print "Error: Did not find cmosHighTempOp setting in BIOS"       


#------------------------------------------------------------------------------
if __name__ == "__main__":
    main()

