Advertisement
Merevoli

Untitled

Aug 9th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import json
  2. import subprocess
  3.  
  4. UNLIMITED = -1
  5. VERSION = 0x020101
  6.  
  7. RESULT_SUCCESS = 0
  8. RESULT_WRONG_ANSWER = -1
  9. RESULT_CPU_TIME_LIMIT_EXCEEDED = 1
  10. RESULT_REAL_TIME_LIMIT_EXCEEDED = 2
  11. RESULT_MEMORY_LIMIT_EXCEEDED = 3
  12. RESULT_RUNTIME_ERROR = 4
  13. RESULT_SYSTEM_ERROR = 5
  14.  
  15. ERROR_INVALID_CONFIG = -1
  16. ERROR_FORK_FAILED = -2
  17. ERROR_PTHREAD_FAILED = -3
  18. ERROR_WAIT_FAILED = -4
  19. ERROR_ROOT_REQUIRED = -5
  20. ERROR_LOAD_SECCOMP_FAILED = -6
  21. ERROR_SETRLIMIT_FAILED = -7
  22. ERROR_DUP2_FAILED = -8
  23. ERROR_SETUID_FAILED = -9
  24. ERROR_EXECVE_FAILED = -10
  25. ERROR_SPJ_ERROR = -11
  26.  
  27.  
  28. def run(max_cpu_time,
  29. max_real_time,
  30. max_memory,
  31. max_stack,
  32. max_output_size,
  33. max_process_number,
  34. have_output,
  35. exe_path,
  36. input_path,
  37. output_path,
  38. error_path,
  39. args,
  40. env,
  41. log_path,
  42. seccomp_rule_name,
  43. uid,
  44. gid,
  45. memory_limit_check_only=0):
  46. str_list_vars = ["args", "env"]
  47. int_vars = ["max_cpu_time", "max_real_time",
  48. "max_memory", "max_stack", "max_output_size", "have_output",
  49. "max_process_number", "uid", "gid", "memory_limit_check_only"]
  50. str_vars = ["exe_path", "input_path", "output_path", "error_path", "log_path"]
  51.  
  52. proc_args = ["/usr/lib/judger/libjudger.so"]
  53.  
  54. for var in str_list_vars:
  55. value = vars()[var]
  56. if not isinstance(value, list):
  57. raise ValueError("{} must be a list".format(var))
  58. for item in value:
  59. if not isinstance(item, str):
  60. raise ValueError("{} item must be a string".format(var))
  61. proc_args.append("--{}={}".format(var, item))
  62.  
  63. for var in int_vars:
  64. value = vars()[var]
  65. if not isinstance(value, int):
  66. raise ValueError("{} must be a int".format(var))
  67. if value != UNLIMITED:
  68. proc_args.append("--{}={}".format(var, value))
  69.  
  70. for var in str_vars:
  71. value = vars()[var]
  72. if not isinstance(value, str):
  73. raise ValueError("{} must be a string".format(var))
  74. proc_args.append("--{}={}".format(var, value))
  75.  
  76. if not isinstance(seccomp_rule_name, str) and seccomp_rule_name is not None:
  77. raise ValueError("seccomp_rule_name must be a string or None")
  78. if seccomp_rule_name:
  79. proc_args.append("--seccomp_rule={}".format(seccomp_rule_name))
  80.  
  81. proc = subprocess.Popen(proc_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  82. out, err = proc.communicate()
  83. if err:
  84. raise ValueError("Error occurred while calling judger: {}".format(err))
  85. out = out.decode('utf-8')
  86. json_part = out.split("|")[0]
  87. output_part = "".join(out.split("|")[1:])
  88. result = json.loads(json_part)
  89.  
  90. result["output"] = output_part
  91. return result
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement