Guest User

Untitled

a guest
Dec 23rd, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import epicbox
  2. import argparse
  3. from multiprocessing import Pool, Manager
  4. from string import Template
  5.  
  6. import comparator
  7. import languages
  8.  
  9. parser = argparse.ArgumentParser()
  10.  
  11. parser.add_argument("filename", help="Input file name.", type=str)
  12. parser.add_argument("--inputs", type=str, nargs="+")
  13. parser.add_argument("--outputs", type=str, nargs="+")
  14. parser.add_argument(
  15.     "--prog_language",
  16.     help="Programming language of input.",
  17.     type=str,
  18.     default="python",
  19.     choices=[*languages.supported.keys()],
  20. )
  21. parser.add_argument(
  22.     "--time", help="Maximum time of program execution in ms.", type=int, default=1000
  23. )
  24. parser.add_argument(
  25.     "--memory", help="Maximum RAM a program can use in mb.", type=int, default=250
  26. )
  27.  
  28. args = parser.parse_args()
  29.  
  30. epicbox.configure(profiles=languages.supported[args.prog_language]["profiles"])
  31.  
  32. source = (
  33.     "1" + args.filename[args.filename.rfind(".") :]
  34.     if args.filename.rfind(".") != -1
  35.     else "a"
  36. )
  37. language = languages.supported[args.prog_language]
  38.  
  39. exec = "a.out"
  40. files = [{"name": source, "content": bytes(open(args.filename).read(), "utf-8")}]
  41. limits = {"cputime": args.time, "memory": args.memory}
  42.  
  43. if not args.inputs or not args.outputs:
  44.     exit(0)
  45.  
  46.  
  47. def evaluate(exec, files, inStr, outStr, i, madDict):
  48.     print("Started input: " + str(i))
  49.     result = epicbox.run(
  50.         language["run"]["docker_profile"],
  51.         language["run"]["command"].substitute(exec=exec, source=source),
  52.         limits=limits,
  53.         files=files + [{"name": "in.txt", "content": bytes(inStr, "utf-8")}],
  54.         stdin=inStr,
  55.         workdir=madDict["work_dir"],
  56.     )
  57.     print(result)
  58.     return comparator.default(result["stdout"].decode("utf-8"), outStr)
  59.  
  60.  
  61. if __name__ == "__main__":
  62.     with epicbox.working_directory() as workdir:
  63.         if language["compile"] != {}:
  64.             result = epicbox.run(
  65.                 language["compile"]["docker_profile"],
  66.                 language["compile"]["command"].substitute(exec=exec, source=source),
  67.                 files=files,
  68.                 workdir=workdir,
  69.             )
  70.         manager = Manager()
  71.  
  72.         d = manager.dict()
  73.         d["work_dir"] = workdir
  74.  
  75.         with Pool(1) as p:
  76.             evalData = [
  77.                 (
  78.                     exec,
  79.                     files if language["compile"] == {} else [],
  80.                     open(i).read(),
  81.                     open(o).read(),
  82.                     j,
  83.                     d,
  84.                 )
  85.                 for j, (i, o) in enumerate(zip(args.inputs, args.outputs))
  86.             ]
  87.             print(p.starmap(evaluate, evalData))
  88.  
Advertisement
Add Comment
Please, Sign In to add comment