def gradeSolutionSingle(self): # 사용자 출력 파일과 정답 파일을 라인별로 비교, 일치율을 점수로 환산 answerOpenCommand = "%s%s%s" % (self.answerPath, self.problemName, FileNameNPathResources.const.DefaultOutputTotalResultFileName) stdLines = FileTools.readFileLines(FileNameNPathResources.const.OutputResultFileName) answerLines = FileTools.readFileLines(answerOpenCommand) answerLineCount = len(answerLines) stdLineCount = len(stdLines) count = stdLineCount - answerLineCount _min = stdLineCount if count < 0 else answerLineCount count = abs(count) strip = string.rstrip logging.debug('single grade') for i in xrange(_min): stdLine = strip(stdLines[i], '\r\n ') answerLine = strip(answerLines[i], '\r\n ') if stdLine != answerLine: # if not same each line count += 1 return self.getSolutionScore(count, answerLineCount)
def compileCode(self): fileList = glob.glob(self.filePath + FileNameNPathResources.const.AllFile) if len(fileList) is 0: print ENUMResources.const.SERVER_ERROR, 0, 0, 0 sys.exit() FileTools.copyAllFile(fileList, os.getcwd()) # make compile command command = self.command.CompileCommand() if not command: return True # code compile call(command, shell=True) # check compile error if os.path.getsize(FileNameNPathResources.const.CompileErrorFileName) > 0: print ENUMResources.const.COMPILE_ERROR, 0, 0, 0 sys.exit() # if not make execution file elif os.path.exists(self.runFileName) or os.path.exists(self.runFileName + ".class"): return True else: print ENUMResources.const.SERVER_ERROR, 0, 0, 0 sys.exit()
def __init__(self, args): self.filePath = args[1] self.problemPath = args[2] self.saveDirectoryName = args[3] self.gradeMethod = args[4] self.caseCount = int(args[5]) self.limitTime = int(args[6]) self.limitMemory = int(args[7]) self.usingLang = args[8] self.version = args[9] self.problemName = args[10] self.answerPath = "%s%s%s%s%s%s" % (self.problemPath, FileNameNPathResources.const.FileSeparator, self.problemName, '_', self.gradeMethod, FileNameNPathResources.const.FileSeparator) # make execution file name try: os.mkdir(self.saveDirectoryName) except Exception: FileTools.saveResult(ENUMResources.const.SERVER_ERROR, 0, 0, 0) sys.exit() self.filePath = "%s%s" % (self.filePath, FileNameNPathResources.const.FileSeparator) self.runFileName = self.makeRunFileName() os.chdir(self.saveDirectoryName) logging.debug(self.saveDirectoryName + ' parameter setting')
def gradeSolutionSingle(self): # 사용자 출력 파일과 정답 파일을 라인별로 비교, 일치율을 점수로 환산 answerOpenCommand = "%s%s%s" % ( self.answerPath, self.problemName, FileNameNPathResources.const.DefaultOutputTotalResultFileName) stdLines = FileTools.readFileLines( FileNameNPathResources.const.OutputResultFileName) answerLines = FileTools.readFileLines(answerOpenCommand) answerLineCount = len(answerLines) stdLineCount = len(stdLines) count = stdLineCount - answerLineCount _min = stdLineCount if count < 0 else answerLineCount count = abs(count) strip = string.rstrip logging.debug('single grade') for i in xrange(_min): stdLine = strip(stdLines[i], '\r\n ') answerLine = strip(answerLines[i], '\r\n ') if stdLine != answerLine: # if not same each line count += 1 return self.getSolutionScore(count, answerLineCount)
def gradeSolutionMulti(self): # 사용자 출력 파일을 각 case별 출력을 순서대로 각 라인을 비교, 일치율을 점수로 환산 answerOpenCommand = "%s%s%s" % ( self.answerPath, self.problemName, FileNameNPathResources.const.DefaultOutputTotalResultFileName) answerLines = FileTools.readFileLines(answerOpenCommand) stdLines = FileTools.readFileLines( FileNameNPathResources.const.OutputResultFileName) strip = string.rstrip totalCount = len(answerLines) loopCount = len(stdLines) caseCount = 1 count = abs(loopCount - totalCount) i = 0 appendFlag = True logging.debug('multi grade') if loopCount is 0: self.makeTestCase(1) return ENUMResources.const.WRONG_ANSWER, 0 while i < loopCount: answerOpenCommand = "%s%s%s%i%s" % ( self.answerPath, self.problemName, FileNameNPathResources.const.CaseFile, caseCount, FileNameNPathResources.const.OutputCaseName) answers = FileTools.readFileLines(answerOpenCommand) for answer in answers: stdLine = strip(stdLines[i], '\r\n ') answerLine = strip(answer, '\r\n ') if stdLine != answerLine: count += 1 # 틀린 case는 한번만 저장 if appendFlag: self.makeTestCase(caseCount) appendFlag = False i += 1 if i >= loopCount: break if caseCount is self.caseCount: count += loopCount - i break caseCount += 1 if appendFlag and loopCount < totalCount: self.makeTestCase(caseCount) return self.getSolutionScore(count, loopCount)
def gradeSolutionMulti(self): # 사용자 출력 파일을 각 case별 출력을 순서대로 각 라인을 비교, 일치율을 점수로 환산 answerOpenCommand = "%s%s%s" % (self.answerPath, self.problemName, FileNameNPathResources.const.DefaultOutputTotalResultFileName) answerLines = FileTools.readFileLines(answerOpenCommand) stdLines = FileTools.readFileLines(FileNameNPathResources.const.OutputResultFileName) strip = string.rstrip totalCount = len(answerLines) loopCount = len(stdLines) caseCount = 1 count = abs(loopCount - totalCount) i = 0 appendFlag = True logging.debug('multi grade') if loopCount is 0: self.makeTestCase(1) return ENUMResources.const.WRONG_ANSWER, 0 while i < loopCount: answerOpenCommand = "%s%s%s%i%s" % (self.answerPath, self.problemName, FileNameNPathResources.const.CaseFile, caseCount, FileNameNPathResources.const.OutputCaseName) answers = FileTools.readFileLines(answerOpenCommand) for answer in answers: stdLine = strip(stdLines[i], '\r\n ') answerLine = strip(answer, '\r\n ') if stdLine != answerLine: count += 1 # 틀린 case는 한번만 저장 if appendFlag: self.makeTestCase(caseCount) appendFlag = False i += 1 if i >= loopCount: break if caseCount is self.caseCount: count += loopCount - i break caseCount += 1 if appendFlag and loopCount < totalCount: self.makeTestCase(caseCount) return self.getSolutionScore(count, loopCount)
def check_extensions(self, files): if self.debug_GetOpts: print files # # check for incompatible mismatch of file types. This is not # so easy to write corrections to argparse to handle. flag_file_mismatch = False if self.program == 'anal_loops.py' and len(self.f_heatmap) > 0: flag_file_mismatch = True elif self.program == 'anal_CTCFs.py' and len(self.f_heatmap) > 0: flag_file_mismatch = True elif self.program == 'chreval.py' and len(self.f_activity) > 0: flag_file_mismatch = True # if flag_file_mismatch: emsg = "ERROR: Incompatible file type, '%s' only accepts \n" % self.program emsg += " files with option -ff and extension(s) %s\n" % self.allowed_extns self.error(emsg) # if len(files) == 0: emsg = "ERROR: no input file specified." self.error(emsg) # if self.program == "chreval.py" and len(files) > 1: emsg = "ERROR: %s only accepts one file at a time." % self.program self.error(emsg) # ft = FileTools() for flnm in files: if not ft.check_ext(flnm, self.allowed_extns): emsg = " terminating....." self.error(emsg) # self.flnm += [flnm] self.flhd += [ft.flhd] self.ext += [ft.ext] # if self.debug_GetOpts: print "input file information" print self.flnm print self.flhd print self.ext
def compileCode(self): # 제출 파일 복사 fileList = glob.glob(self.filePath + FileNameNPathResources.const.AllFile) if len(fileList) is 0: FileTools.saveResult(ENUMResources.const.SERVER_ERROR, 0, 0, 0) sys.exit() FileTools.copyAllFile(fileList, os.getcwd()) # 컴파일 명령어 설정 command = self.command.CompileCommand() if command == 'PYTHON': return True # 컴파일 logging.debug('compile') call(command, shell=True) # 컴파일 에러 확인 if os.path.exists( self.runFileName) or os.path.exists(self.runFileName + '.class'): return True elif os.path.getsize(FileNameNPathResources.const.MessageFile) > 0: FileTools.saveResult(ENUMResources.const.COMPILE_ERROR, 0, 0, 0) sys.exit() else: FileTools.saveResult(ENUMResources.const.SERVER_ERROR, 0, 0, 0) sys.exit()
def compileCode(self): # 제출 파일 복사 fileList = glob.glob(self.filePath + FileNameNPathResources.const.AllFile) if len(fileList) is 0: FileTools.saveResult(ENUMResources.const.SERVER_ERROR, 0, 0, 0) sys.exit() FileTools.copyAllFile(fileList, os.getcwd()) # 컴파일 명령어 설정 command = self.command.CompileCommand() if command == 'PYTHON': return True # 컴파일 logging.debug('compile') call(command, shell = True) # 컴파일 에러 확인 if os.path.exists(self.runFileName) or os.path.exists(self.runFileName + '.class'): return True elif os.path.getsize(FileNameNPathResources.const.MessageFile) > 0: FileTools.saveResult(ENUMResources.const.COMPILE_ERROR, 0, 0, 0) sys.exit() else: FileTools.saveResult(ENUMResources.const.SERVER_ERROR, 0, 0, 0) sys.exit()
def gradeChecker(self): # 별도의 채점 프로그램으로 채점 copyCommand = "%s%s%s" % (self.answerPath, self.problemName, '_checker') FileTools.copyFile(copyCommand, 'checker') logging.debug('checker grade') call('./checker 1>result.txt', shell = True) score = self.getScore('result.txt') if score is 100: return ENUMResources.const.SOLVED, score else: return ENUMResources.const.WRONG_ANSWER, score
def gradeChecker(self): # 별도의 채점 프로그램으로 채점 copyCommand = "%s%s%s" % (self.answerPath, self.problemName, '_checker') FileTools.copyFile(copyCommand, 'checker') logging.debug('checker grade') call('./checker 1>result.txt', shell=True) score = self.getScore('result.txt') if score is 100: return ENUMResources.const.SOLVED, score else: return ENUMResources.const.WRONG_ANSWER, score
def execution(self): # 전체 input case파일 복사 if self.caseCount > 0: copyCommand = "%s%s%s" % ( self.answerPath, self.problemName, FileNameNPathResources.const.DefaultInputTotalCaseFileName) FileTools.copyFile(copyCommand, FileNameNPathResources.const.InputCaseFileName) # 실행 명령어 설정 runCommandList = self.command.ExecuteCommand() #자식 프로세스 생성 후 자식 프로세스는 프로그램 실행, 부모 프로세스는 자식 프로세스의 자원 사용을 체크 logging.debug('execution') pid = os.fork() # 자식 프로세스 if pid == 0: self.runProgram(runCommandList) # 부모 프로세스 else: result, time, usingMem = self.watchRunProgram(pid) # ms 단위로 환산 userTime = int(time * 1000) # 실행 후 런타임 에러 및 사용 시간 메모리 초과 확인 if result == 'Grading': if os.path.isfile('run.err') and os.path.getsize('run.err') > 0: result = ENUMResources.const.RUNTIME_ERROR elif userTime > self.limitTime: result = ENUMResources.const.TIME_OVER elif (usingMem >> 10) > self.limitMemory: result = ENUMResources.const.MEMORY_OVERFLOW elif not result: FileTools.saveResult(ENUMResources.const.SERVER_ERROR, 0, 0, 0) sys.exit() return result, userTime, usingMem
def execution(self): # 전체 input case파일 복사 if self.caseCount > 0: copyCommand = "%s%s%s" % (self.answerPath, self.problemName, FileNameNPathResources.const.DefaultInputTotalCaseFileName) FileTools.copyFile(copyCommand, FileNameNPathResources.const.InputCaseFileName) # 실행 명령어 설정 runCommandList = self.command.ExecuteCommand() #자식 프로세스 생성 후 자식 프로세스는 프로그램 실행, 부모 프로세스는 자식 프로세스의 자원 사용을 체크 logging.debug('execution') pid = os.fork() # 자식 프로세스 if pid == 0: self.runProgram(runCommandList) # 부모 프로세스 else: result, time, usingMem = self.watchRunProgram(pid) # ms 단위로 환산 userTime = int(time * 1000) # 실행 후 런타임 에러 및 사용 시간 메모리 초과 확인 if result == 'Grading': if os.path.isfile('run.err') and os.path.getsize('run.err') > 0: result = ENUMResources.const.RUNTIME_ERROR elif userTime > self.limitTime: result = ENUMResources.const.TIME_OVER elif (usingMem >> 10) > self.limitMemory: result = ENUMResources.const.MEMORY_OVERFLOW elif not result: FileTools.saveResult(ENUMResources.const.SERVER_ERROR, 0, 0, 0) sys.exit() return result, userTime, usingMem
def evaluation(self): score = 0 logging.debug(self.parameter.saveDirectoryName + ' execution start') # 프로그램 실행 객체 생성 후 프로그램 실행 execution = ExecutionTools.ExecutionTools(self.parameter, self.command) success, runTime, usingMem = execution.execution() logging.debug(self.parameter.saveDirectoryName + ' execution end') # 정상적으로 시행된 경우 if success == 'Grading': logging.debug(self.parameter.saveDirectoryName + ' grade start') #채점 객체 생성 후 채점 evaluation = GradingTools.GradingTools(self.parameter, self.command) success, score = evaluation.grade() logging.debug(self.parameter.saveDirectoryName + ' grade end') #채점 완료 후 프로그램 종료 FileTools.saveResult(success, score, runTime, usingMem)
def getUsingMemory(self, pid, usingMem, minflt): # 프로세스 임시파일 접근 procFileOpenCommand = "%s%i%s" % ( FileNameNPathResources.const.ProcessDirName, pid, FileNameNPathResources.const.ProcessStatusFileName) fileLines = FileTools.readFileLines(procFileOpenCommand) split = string.split # 물리 메모리 측정 for i in xrange(14, 19): index = fileLines[i].find('VmRSS') if index != -1: words = split(fileLines[i]) temp = int(words[index + 1]) break if temp > usingMem: usingMem = temp return usingMem
def getUsingMemory(self, pid, usingMem, minflt): # 프로세스 임시파일 접근 procFileOpenCommand = "%s%i%s" % (FileNameNPathResources.const.ProcessDirName, pid, FileNameNPathResources.const.ProcessStatusFileName) fileLines = FileTools.readFileLines(procFileOpenCommand) split = string.split # 물리 메모리 측정 for i in xrange(14,19): index = fileLines[i].find('VmRSS') if index != -1: words = split(fileLines[i]) temp = int(words[index+1]) break; if temp > usingMem: usingMem = temp return usingMem
def main(cl): if len(cl) == 1: usage() sys.exit(0) # if cl[1] == '-h' or cl[1] == '--help': usage() sys.exit(0) # pdbflnm_in = cl[1] if not path.isfile(pdbflnm_in): print "ERROR: cannot find %s" % pdbflnm_in usage() sys.exit(1) # ft = FileTools() ft.check_ext(pdbflnm_in, EXTS, PROGRAM) pdbflnm_out = ft.flhd + "_v.pdb" if len(cl) == 3: pdbflnm_out = cl[2] ft.check_ext(pdbflnm_out, EXTS, PROGRAM) # dp = DataPDB() dp.readpdb(pdbflnm_in) s = dp.show_ATOM() fp = open(pdbflnm_out, "w") fp.write(s) fp.write("TER\n") s = dp.show_CONECT() fp.write(s) fp.write("END\n") fp.close()
def getScore(self, fileName): scores = FileTools.readFileLines('result.txt') return int(scores[0])
def read_heatmap(self, flnm, EXTS = ["heat", "eheat"], PROGRAM = "read_MatrixFile", flag_display = True): debug_read_heatmap = flag_display flag_extended_heatmap = False splf = flnm.split('.') ext = splf[len(splf)-1] if debug_read_heatmap: print "file name: %s" % flnm # ft = FileTools() if not ft.check_ext(flnm, EXTS): print "ERROR: %s only accepts files with extention '%s'." % (PROGRAM, EXTS) print " input file '%s' --> (extension(s) '%s')" % (flnm, ext) sys.exit(1) # try: fp = open(flnm, 'r') except IOError: print "ERROR: cannot open file '%s'." % flnm sys.exit(1) # lfp = fp.readline() fileInfoLine = lfp.strip().split(':') fp.close() # Determine what file type is being submitted. if debug_read_heatmap: print "fileInfoLine: ", fileInfoLine # if len(fileInfoLine) > 1: # means it is probably the extended format, unless by # chance this is the first line after the editing step. if fileInfoLine[0] == "file version": flag_extended_heatmap = True else: print "ERROR: %s does not appear to be a recognizable file type" % flnm print " first line: '%s'" % fileInfoLine sys.exit(1) # # gmtrx = None if flag_extended_heatmap: if debug_read_heatmap: print "going to read_extended_heatmap()" # gmtrx = self.read_extended_heatmap(flnm, PROGRAM, flag_display) else: if debug_read_heatmap: print "going to read_basic_heatmap()" # gmtrx = self.read_basic_heatmap(flnm, PROGRAM, flag_display) # N = gmtrx.length mtrx = gmtrx.heatmap clusters = gmtrx.clusters return gmtrx # mtrx, clusters, N