Exemple #1
0
 def runContainer(exeName, interpreter, question, param):
     """
     :param exeName: 要运行的可执行文件名
     :param param: 要传入的参数
     :param interpreter: 要运行的解释器名[C++,C,JAVA,PYTHON]
     :param question : 问题类,用来提供各种限制参数
     :return:
     """
     #当shell语句成功实现才返回真,否则返回值,不进以后续操作
     if not OJRunner.createRunShellFile(exeName, interpreter, question,
                                        param):
         Log.CustomerLOG("CREATE RUN SHELL FAIL")
         return None
     fileName = 'run_' + exeName + '_' + interpreter + '.sh'
     try:
         result = DockerRunner.runProgram(fileName)
     except Exception, e:
         print e
Exemple #2
0
 def customer(threadId):
     """
     :param threadId: 本线程在进程的是第几个由编写者创建的
     :return:None
     """
     while True:
         if OJRunner.customerMuxter and OJRunner.mutex:
             time.sleep(0.5)
         #进程同步,在若在没人访问队列时,访问并把信号量设为False
         OJRunner.customerMuxter = False
         if len(OJRunner.queue) > 0:
             #取取队列头,并清掉它
             code = OJRunner.queue[0]
             Log.CustomerLOG("The QUEUE LENGTH : " +
                             str(len(OJRunner.queue)))
             OJRunner.queue.remove(code)
             Log.CustomerLOG("The QUEUE : " + str(OJRunner.queue))
             Log.CustomerLOG("The QUEUE LENGTH : " +
                             str(len(OJRunner.queue)))
             #允许它人访问,将信号量置为True
             OJRunner.customerMuxter = True
             if code.getType() == 'cpp':
                 compileType = 'cpp'
             elif code.getType() == 'C':
                 compileType = 'c'
             elif code.getType() == 'JAVA':
                 compileType = 'java'
             else:
                 compileType = 'python'
             #进行编译,若编译失败则返回False,则在数据库中将记录更新为Compilation Error
             if not OJRunner.compile(code.getCodeName(), code.getCodeName(),
                                     compileType):
                 OJRunner.waitingDataBase()
                 OJDBA.updateCompilerError(code.getCodeName())
                 OJRunner.databaseMuxter = True
                 continue
             #从数据库取出测试数据后,进行运行
             #因数据库不能同时访问,所以只有当数据库信号量为真时,才可进行访问,拿到数据库访问权,访问该问题编号对应的洞晓试数据
             OJRunner.waitingDataBase()
             data = list(DataBase.DataBaseLinker.getInstance().execute(
                 "select * from TestData where question_id='" +
                 code.getQuestionID() + "'"))
             OJRunner.databaseMuxter = True
             #执行十次运行,每次随机选出一组测试案例,并将未被测试数据的长度的最后一组代替当前被选种的组,且未测试数据长度减一
             i = 0
             acceptSuccess = True  #用来标识是否成功AC
             dictLength = len(data)
             while i < DATA.JUDGEMENT_TIMES:
                 Log.CustomerLOG("Run No." + str(i) + " Time : ")
                 target = {}
                 position = random.randint(0, dictLength - 1)
                 #将结果以JSON的格式进行解析
                 testData = json.loads(data[position]['test_data'])
                 targetResult = json.loads(data[position]['result_data'])
                 target['answer'] = targetResult
                 #运行容器去跑代码并得到结果
                 question = OJDBA.getQuestionById(code.getQuestionID())
                 result = OJRunner.runContainer(code.getCodeName(),
                                                compileType, question,
                                                testData)
                 if result == None:  #若结果为NONE说明没跑成功,目前先直接退出
                     Log.CustomerLOG("RESULT IS NONE")
                     acceptSuccess = False  #AC失败,将标识设为False
                     break
                 Log.CustomerLOG(result)
                 OJResult = OJRunner.analysisResult(result, target)
                 Log.CustomerLOG(
                     "-------------------------------------------------")
                 #Log.CustomerLOG("Test Data : " + str(testData) )
                 Log.CustomerLOG("Program run result : " + result)
                 Log.CustomerLOG("Test run result : " + str(target))
                 Log.CustomerLOG("Analysis Result : " + str(OJResult[0]))
                 Log.CustomerLOG(
                     "-------------------------------------------------")
                 #检测结果是否为AC,若不是则写入数据库跳出,执行下一份代码,若是则继续评测
                 if OJResult[0] != 'Accepted':
                     acceptSuccess = False  #AC失败,将标识设为False
                     OJRunner.waitingDataBase()
                     OJDBA.updateOtherResult(OJResult[0],
                                             code.getCodeName())
                     OJRunner.databaseMuxter = True
                     break
                 #将最后一个赋值给当前随机的这个,并总未测试数据总长减一
                 data[position] = data[dictLength - 1]
                 dictLength -= 1
                 i += 1
             if acceptSuccess:
                 OJDBA.updateOtherResult('Accepted', code.getCodeName())
         else:
             OJRunner.customerMuxter = True
             print 'Thread.' + str(threadId) + ' is sleeping......'
             time.sleep(2)