Example #1
0
def run_case_in_suite(casename, currentBenchfile, currentBenchinfo,logger, stop_at_fail,logdir, cmd, dry_run):

    patDash  = re.compile('\s*(python |python[\d.]+ |python.exe |)\s* cr.py\s+(.+)\s*', re.DOTALL|re.IGNORECASE)
    m =  re.match(patDash, cmd)
    returncode = 0
    if m:
        argstring = m.group(2)
        import shlex
        lstArg = shlex.split(argstring)
        #0-case.csv, 1-bench, 2-mode, 4...-2- args
        casefile = lstArg[0]
        benchfile = lstArg[1]
        mode       = lstArg[2]
        args= lstArg[3:]
        newBenchInfo =None
        if currentBenchfile!=benchfile:
           # from common import bench2dict
            bench =bench2dict(benchfile)
            releaseDUTs()
        else:
            bench = currentBenchinfo
        from Parser import  caseParser

        cs = caseParser(casename, mode, logdir, logger)
        sdut, lvar, lsetup, lrun, ltear =cs.load(casefile)
        ldut = list(sdut)
        errormessage =[]

        duts= initDUT(errormessage,bench,ldut,logger, logdir, dry_run)
        seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown]
        returncode, caseErrorMessage= case_runner(casename,duts,seq, mode)
    else:
        import subprocess
        pp =None
        if cmd.startswith('\w+.py') :
            exe_cmd ='python '+ cmd+" "+logdir
            pp = subprocess.Popen(args = exe_cmd ,shell =True)

        import time
        ChildRuning = True
        first =True
        while ChildRuning:
            if pp.poll() is None:
                interval = 1
                if first:
                    first=False
                time.sleep(interval)
            else:
                ChildRuning = False

        returncode = pp.returncode
Example #2
0
def run1case(casename, cmd,benchfile, benchinfo, dut_pool, logdir, logger, sharedata, dry_run =False):
    errormessage = ''
    caselogger = createLogger('caselog.txt', logdir)
    bench = benchinfo
    try:
        for dut_name in dut_pool:
            try:
                dut_pool[dut_name].logger = caselogger
                dut_pool[dut_name].openLogfile(logdir)
                dut_pool[dut_name].FailFlag    =False # the flag means in Session's perspective view, case failed
                dut_pool[dut_name].ErrorMessage=None # to store the error message
                dut_pool[dut_name].info(dut_pool[dut_name].name, 'session info reseted')
            except Exception as e:
                caselogger.error('failed to update logger for dut:%s'%dut_name)

        patDash  = re.compile('\s*(python |python[\d.]+ |python.exe |)\s*(cr.py|cr.exe)\s+(.+)\s*', re.DOTALL|re.IGNORECASE)
        m =  re.match(patDash, cmd)
        returncode = 1
        if m:

            argstring = m.group(3)
            import shlex
            if os.name =='nt':
                posix =False
            else:
                posix =True
            lstArg = shlex.split(argstring, posix=posix)
            #0-case.csv, 1-bench, 2-mode, 4...-2- args
            casefile = lstArg[0]
            case_benchfile = lstArg[1]
            case_mode       = lstArg[2]
            case_args= lstArg
            case_args.insert(0,'cr.py')


            if case_benchfile!=benchfile:
                #from common import bench2dict
                caselogger.info('loading a new bench:%s'%case_benchfile)
                global gPathLocker
                gPathLocker.acquire()
                bench =bench2dict(case_benchfile)
                gPathLocker.release()
                benchfile = case_benchfile
                caselogger.info('releasing duts in old dut_pool')
                releaseDUTs(dut_pool, caselogger)
                dut_pool ={}
            from Parser import  caseParser
            caselogger.info('loading case: %s'% casename)
            cs = caseParser(casename, case_mode, logdir, caselogger)
            sdut, lvar, lsetup, lrun, ltear =cs.load(casefile, case_args)
            ldut = list(sdut)
            newduts= []
            oldduts = []
            for nd in ldut:
                if dut_pool.has_key(nd):
                    oldduts.append(nd)
                    dut_pool[nd].FailFlag    =False # the flag means in Session's perspective view, case failed
                    dut_pool[nd].ErrorMessage=None # to store the error message
                    caselogger.info(dut_pool[nd].name+ ' cleared FailFlag and ErrorMessage')
                else:
                    newduts.append(nd)
            if dry_run is not True:
                for od in oldduts :
                    dut_pool[od].FailFlag    =False # the flag means in Session's perspective view, case failed
                    dut_pool[od].ErrorMessage=None
                    if dut_pool[od].isAlive() :
                        dut_pool[od].logger = caselogger
                        dut_pool[od].openLogfile(logdir)
                    else:
                        dut_pool[od].closeSession()
                        dut_pool.pop(od)
                        newduts.append(od)
            errormessage =[]
            duts= initDUT(errormessage,bench,newduts,caselogger, logdir,sharedata, dry_run=dry_run)


            #duts['cfa84'].isAlive()
            #duts['sba94'].isAlive()
            #print('initedduts')
            for k in duts.keys():
                dut_pool[k]=duts[k]
            #print('initedduts 2')
            for key in duts.keys():
                dut_pool[key]= duts[key]
            #print('initedduts 3 ' )
            seq = [cs.seqSetup, cs.seqRun, cs.seqTeardown]
            caselogger.info('starting to run case: %s'%cmd)
            returncode, STRerrormessage= case_runner(casename,dut_pool,seq, case_mode, caselogger)

            if returncode:
                import pprint
                STRerrormessage = pprint.pformat(STRerrormessage)
                caselogger.error('Case Failed:')
                for line in STRerrormessage.replace('\\r', '\r').replace('\\n', '\n').replace('\r\n', '\n').split('\n'):
                    caselogger.error(line)
                errormessage.append(STRerrormessage)

            else:
                caselogger.info('Case PASS')

        else:
            import subprocess
            pp =None

            patPython = re.compile(r'\s*(python\s+|python.exe\s+|)(.*[./\w_-]+.py)', re.IGNORECASE|re.DOTALL)
            m=re.match(patPython, cmd)
            if m :
                newcmd =m.group(2)
                if os.path.exists('y.exe'):
                    exe_cmd ='python -O '+ cmd+" -l "+logdir
                else:
                    exe_cmd ='python '+ cmd+" -l "+logdir
                caselogger.info('running case: %s'%exe_cmd)
                pp = subprocess.Popen(args = exe_cmd ,shell =True,stderr=subprocess.PIPE)
                import time
                ChildRuning = True
                first =True
                while ChildRuning:
                    if pp.poll() is None:
                        interval = 1
                        if first:
                            first=False
                        time.sleep(interval)
                    else:
                        ChildRuning = False

                returncode = pp.returncode
            if returncode:
                try:
                    stdout, stderr =pp.communicate()
                    try:
                        with open('%s/case_error.txt'%logdir,'r') as f:
                            errormessage +='\n'.join(f.readlines()[:10])
                    except Exception as e:
                        pass

                        if stderr!='':
                            errormessage +=stderr
                        else:
                            errormessage = stderr
                        with open('%s/case_error.txt'%logdir,'a+') as f:
                            f.write(stderr)

                except Exception as e:
                    print(e)
    except KeyboardInterrupt :
        sys.exit(1)
    except Exception as e:

        if returncode ==0:
            returncode =1

        #import traceback
        errormessage = '%s\n%s'%(e.__str__(),traceback.format_exc())
        caselogger.error('Case FAIL')
        for line in errormessage.split('\n'):
            caselogger.error(line)
    return  returncode, errormessage, benchfile,bench, dut_pool