#!/usr/bin/env python3

import sys

testname = sys.argv[1] #takes testname as input
workspace = sys.argv[2] #takes WORKSPACE root dir as input


#opens the textfile corresponding to the testname from the tmp directory and saves it to pippo
with open(workspace + "/oneDNN-results/" + testname + "-output.txt", 'r') as handle:
    pippo = handle.readlines()

#reads through each line of the testname and checks for PASS/FAIL results proving that the test has been completed correctly. If they exist then it adds them to a res_{testname}.txt file in SQUAD format.
with open(workspace + "/oneDNN-results/res_" + testname + "-output.txt", 'w+') as whandle:
    TestSuccess = False
    TestFailed = False
    for i in range(0,len(pippo)-2):
        line = pippo[i]
        line = line[2:]
        if 'FAILED' in pippo[i]:
            TestSuccess = True
            TestFailed = True
            break
        elif 'PASSED' in pippo[i]:
            if TestSuccess == False:
                TestSuccess = True

    #if no PASS/FAIL results only writes "{testname} : fail" to the result.
    if TestSuccess == False or TestFailed == True:
        whandle.write("\"" + testname + "\"" + ": " + "\"" + "fail" + "\"")
    elif TestSuccess == True:
        whandle.write("\"" + testname + "\"" + ": " + "\"" + "pass" + "\"") 
