def runAction(options, action, testAction): problem = loadProblemProperties() util.removeFiles("t-{}.log".format(action), "t-run.log", "t-run.err", "t-run.out", "run.dat") make("check") make("interact", quiet=True) def resolveSolution(solution, problem): if solution == "": return problem["source"] if solution == "*": return resolveSolution(problem["solution"], problem) if os.path.exists(solution): return solution return problem["id"] + "_" + solution solution = findSuffix(resolveSolution(options.solution, problem)) make(solution) stopOnFirstError = options.stop_on_first_error or defaults.stopOnFirstError exitcode = functools.reduce( (lambda success, test: (stopOnFirstError and success) or testAction( options, problem, test, solution) or success), options.test if options.test else testNames(problem), 0) if os.path.exists("run.dat"): stats = loadProperties("run.dat") log.info("MAX TIME IS: {}ms", stats["max.timeConsumed"]) log.info("MAX MEMORY IS: {:,}B", int(stats["max.memoryConsumed"])) util.removeFiles(problem["input"], problem["output"], problem["log"]) return exitcode
def compile(program): try: return util.execute(*substituteProgram(program, compileCommand)) finally: util.removeFiles(*[ util.replaceSuffix(program, suffix) for suffix in tempSuffixes ])
def problem_examples(options): problem = loadProblemProperties() statementsFile = options.statements + "/" + problem["id"] + ".tex" if not os.path.exists(statementsFile): raise PException('Statements "{}" not found ', problem["id"], statementsFile) checker, interact, tests = findCheckerAndTests() def unescapeTex(line): return (line.replace("\~", "---###TILDE###---").replace( "~", "").replace("$\\sim$", "~").replace("{}", "").replace( "\\\\", "---###SLASH###---").replace("\\", "").replace( "---###SLASH###---", "\\").replace("---###TILDE###---", "~")) tests = 0 state = ["none"] input = [] output = [] for rline in util.readLines(statementsFile): line = rline.strip() if state[0] == "none": if line == "\\exmp{": tests += 1 state[0] = "input" input[0:len(input)] = [] elif state[0] == "input": if line == "}{": state[0] = "output" output[0:len(output)] = [] if input != util.readLines("tests/" + testName(problem, tests)): raise PException( 'EXAMPLES FAILED FOR PROBLEM {}. Example input {} is not equal to test.', problem["id"], tests) else: input += [unescapeTex(rline)] elif state[0] == "output": if line == "}" or line == "}%": state[0] = "none" util.writeFile("__output", "{}", "".join(output)) if run( "Check", "tests/" + testName(problem, tests), "__output", "tests/" + testName(problem, tests) + ".a".format(tests)): raise PException( 'EXAMPLES FAILED FOR PROBLEM {}. Example output {} rejected by checker.', problem["id"], tests) util.removeFiles("__output") else: output += [unescapeTex(rline)] if state[0] != "none": raise PException('Invalid examples murkup.') if tests == 0: raise PException('No examples found.') return 0
def problem_build(options): def makeAndRun(program, *args): make(program) exitcode = run(program, *args) if exitcode: raise PException("Error running {}. Exitcode={}", program, exitcode) def buildTest(options, problem, test, solution): if not os.path.exists("tests/" + test): return 0 exitcode = runTest(options, "build", problem, test, solution) if exitcode: return exitcode shutil.copyfile(problem["output"], "tests/" + test + ".a") return runChecker("build", problem, test) problem = loadProblemProperties() log.info("Generating tests") if os.path.exists("Tests.java"): makeAndRun("Tests.java") else: os.chdir("tests") if os.path.exists(findSuffix("doall", "~~~")): util.removeFiles(*testNames(problem)) makeAndRun("doall") else: for test in testNames(problem): if findSuffix("do" + test, "~~~") != "~~~": util.removeFiles(test, test + ".a", test + ".b") makeAndRun("do" + test, ">", test) os.chdir("..") def hashFile(filename): with open(filename, "rb") as file: return hashlib.sha256(file.read()).hexdigest() if options.generate_hashes: log.info("Generating test hashes") with open("tests.sha256", "wb") as file: for test in testNames(problem): log.debug("Generating hash for test {}", test) if os.path.exists("tests/" + test): file.write((hashFile("tests/" + test) + " tests/" + test).encode("utf8")) file.write(b"\n") elif os.path.exists("tests.sha256"): log.info("Verifying test hashes") for line in util.readLines("tests.sha256"): hash, file = line.strip().split(" ") log.debug("Verifying hash for test {}", file) if hash != hashFile(file): raise PException('Hash mismatch for test "{}"', file) return runAction(options, "build", buildTest)
def problem_xml(options): problem = loadProblemProperties() contest = loadContestProperties() if options.time_limit: contest["timelimit"] = options.time_limit if options.memory_limit: contest["memorylimit"] = options.memory_limit if options.prefix: contest["problem-prefix"] = options.prefix util.removeFiles("problem.xml") prefix = contest["problem-prefix"] prefix = prefix if prefix == "" or prefix.endswith(".") else prefix + "." checker, interact, tests = findCheckerAndTests() checkerLine = ( '<binary executable-id = "java.check" file = "check.jar"/>' if checker.lower() == "check.jar" else '<binary executable-id = "x86.exe.win32" file = "check.exe"/>') util.writeFile( "problem.xml", """ <problem id = "{id}" version = "1.0" > <judging> <script type = "%icpc"> <testset test-count = "{testNumber}" input-href = "tests/{tests}" answer-href = "tests/{tests}.a" input-name = "{input}" output-name = "{output}" time-limit = "{timelimit}" memory-limit = "{memorylimit}" /> <verifier type = "%testlib"> {checkerLine} </verifier> </script> </judging> </problem>""".format(id=prefix + problem["id"], checkerLine=checkerLine, timelimit=(problem if hasattr(problem, "timelimit") else contest)["timelimit"], memorylimit=(problem if hasattr(problem, "memorylimit") else contest)["memorylimit"], testNumber=len(tests), tests=problem["tests"], input=problem["input"], output=problem["output"])) return 0
def runTest(options, action, problem, test, solution): inf = "tests/" + test log.info("Running {} on problem {} test {}.", solution, problem["id"], test) shutil.copyfile(inf, problem["input"]) util.removeFiles(problem["output"]) exitcode = time(options, solution) if exitcode: logActionError(action, problem, test, exitcode) return exitcode
def javac(program): temp = "__" + name util.removeDirs(temp) try: util.removeFiles(name + ".jar") os.makedirs(temp) return util.execute( "javac", "-sourcepath", ".", "-d", temp, "-cp", joinClasspath(classpath()), program) or util.execute( "jar", "cf", name + ".jar", "-C", temp, ".") finally: util.removeDirs(temp)
def images(): localFilePathList = [] try: track_event('spotainize_common', 'api', '/eval/images') urlList = request.get_json()["urlList"] if (len(urlList) > 10): return {'error': 'No more than 10.'} localFilePathList = downloadImages(urlList) result = list(evaluate(localFilePathList)) removeFiles(localFilePathList) return {'score': result} except Exception as e: removeFiles(localFilePathList) return {'error': str(e)}
def problem_clean(options): problem = loadProblemProperties() if os.path.exists(findSuffix("tests/doall")): for test in testNames(problem): util.removeFiles("tests/" + test) if findSuffix("Tests") != "Tests": util.removeDirs("tests") for test in testNames(problem): if os.path.exists(findSuffix("tests/do" + test)): util.removeFiles("tests/" + test) util.removeFiles("tests.jar", "check.jar", "validator.jar", "interact.jar") util.removeFiles(*["tests/" + test + ".a" for test in testNames(problem)] + ["tests/" + test + ".b" for test in testNames(problem)]) util.removeFiles(*[ dir + "/" + filename for dir in filter(os.path.exists, [".", "tests"]) for filename in os.listdir(dir) for suffix in defaults.cleanSuffixes if filename.endswith(suffix) ]) util.removeFiles("run.dat", "problem.xml", "t-run.err") return 0
def contest_clean(options): util.removeFiles("challenge.xml") return 0