#!/usr/bin/python
# Filename: carod_hook

# for grep
from jobhooks.functions import grep

################################################################################
# getStringValue
################################################################################
def getStringValue(find, data):
    result = ''
    
    resultArr = grep('^'+find+'\s*=\s*"(.+)"$', data)
    
    if resultArr != None and resultArr[0] != None:
        result = resultArr[0]
        
    return result

################################################################################
# getNonStringValue
################################################################################
def getNonStringValue(find, data):
    result = ''
    
    resultArr = grep('^'+find+'\s*=\s*(.+)$', data)
    
    if resultArr != None and resultArr[0] != None:
        result = resultArr[0]
        
    return result

################################################################################
# getIntValue
################################################################################
def getIntValue(find, data):
    result = 0
    
    resultStr = getNonStringValue(find, data)
    if resultStr != None and resultStr != '':
        result = resultStr
        
    return result

################################################################################
# customizeClassAds
################################################################################
def customizeClassAds(nodeData, jobData):
    # Carry forward the default class ads
    myJobData = jobData

    # Get the target platform of the job
    target = getStringValue('TARGET', jobData)

    # Validate Target
#     if target == None:
#         myJobData += 'Cmd = "/bin/echo"\n'
#         myJobData += 'Arguments = "TARGET value is missing"\n'
#     elif target == 'Test':
#         myJobData += ''
#     else:
#         myJobData += 'Cmd = "/bin/echo"\n'
#         myJobData += 'Arguments = "TARGET is undefined: '+ target +'"\n'

    # Get the machine that this job will run upon
    machine = getStringValue('Machine', nodeData)
    myJobData += 'ExecutingMachine = "' + machine + '"\n'

    # Get the slot that this job will run upon
    slots = grep('^SlotID\s*=\s*(.+)$', nodeData)
    if slots == None:
        myJobData += 'ExecutingSlot = "None"\n'
    else:
        myJobData += 'ExecutingSlot = "' + str(slots[0].rstrip().lstrip()) + '"\n'

    # Get the UUID of the job
    uuid = getStringValue('UUID', jobData)

    # Get the retry count
    retryCount = getIntValue('RetryCount', jobData)

    #
    # Add/Replace Condor I/O locations
    #
    myJobData += 'In = "/dev/null"\n'
    myJobData += 'Out = "/iots2/working/condormsg/condor_local/job.'+ uuid +'.'+ str(retryCount) +'.out"\n'
    myJobData += 'Err = "/iots2/working/condormsg/condor_local/job.'+ uuid +'.'+ str(retryCount) +'.err"\n'
    
    myJobData += 'Owner = "condor"\n'
    myJobData += 'Env = "LD_LIBRARY_PATH=/home/condor:/usr/local/lib"\n'
    
    return (myJobData)

################################################################################
# shouldRetryJob
################################################################################
def shouldRetryJob(jobData):
    MAX_RETRY_COUNT = 3

    # Get the exit code
    exitCode = getIntValue('ExitCode', jobData)
   
    # Get the retry count
    retryCount = getIntValue('RetryCount', jobData)
    
    # Should we retry?
    if int(exitCode) != 0 and int(retryCount) < 3:
        return (True)
    else:
        return (False)

################################################################################
# buildRetryJob
################################################################################
def buildRetryJob(jobData):
    myJobData = ''

    # Get the retry count
    retryCount = getIntValue('RetryCount', jobData)
    retryCount = int(retryCount) + 1;

    # Build the job string with standard Condor values 
    myJobData += 'Iwd = "'+ getStringValue('Iwd', jobData) +'"\n'
    myJobData += 'Requirements = '+ getNonStringValue('Requirements', jobData) +'\n'
    myJobData += 'Cmd = "'+ getStringValue('Cmd', jobData) +'"\n'
    myJobData += 'Arguments = "'+ getStringValue('Arguments', jobData) +'"\n'

    # Build the job string with custom values 
    myJobData += 'RetryCount = '+ str(retryCount) +'\n'
    myJobData += 'SubmitMachine = "'+ getStringValue('SubmitMachine', jobData) +'"\n'
    myJobData += 'SubmitUsername = "'+ getStringValue('SubmitUsername', jobData) +'"\n'
    myJobData += 'UUID = "'+ getStringValue('UUID', jobData) +'"\n'
    myJobData += 'TARGET = "'+ getStringValue('TARGET', jobData) +'"\n'

    return (myJobData)
