judgement.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import lorun
  2. import os
  3. RESULT_STR = [
  4. 'Accepted',
  5. 'Presentation Error',
  6. 'Time Limit Exceeded',
  7. 'Memory Limit Exceeded',
  8. 'Wrong Answer',
  9. 'Runtime Error',
  10. 'Output Limit Exceeded',
  11. 'Compile Error',
  12. 'System Error'
  13. ]
  14. def compile_c(src_path):
  15. if os.system('gcc %s -o main' % src_path) != 0:
  16. return False
  17. return True
  18. def compile_cpp(src_path):
  19. if os.system('g++ %s -o main' % src_path) != 0:
  20. return False
  21. return True
  22. def compile_java(src_path):
  23. if os.system('javac %s' % src_path) != 0:
  24. return False
  25. return True
  26. def compile_python(src_path):
  27. if os.system('python3 -m py_compile %s' % src_path) != 0:
  28. return False
  29. if os.path.exists('py_compile'):
  30. os.remove('py_compile')
  31. return True
  32. def run(args, in_path, out_path, time_limit, memory_limit):
  33. fin = open(in_path)
  34. ftemp = open('temp.out', 'w')
  35. runcfg = {
  36. 'args': args,
  37. 'fd_in': fin.fileno(),
  38. 'fd_out': ftemp.fileno(),
  39. 'timelimit': time_limit, # in MS
  40. 'memorylimit': memory_limit, # in KB
  41. }
  42. rst = lorun.run(runcfg)
  43. fin.close()
  44. ftemp.close()
  45. if rst['result'] == 0:
  46. ftemp = open('temp.out')
  47. fout = open(out_path)
  48. crst = lorun.check(fout.fileno(), ftemp.fileno())
  49. fout.close()
  50. ftemp.close()
  51. os.remove('temp.out')
  52. if crst != 0:
  53. return {'result':crst}
  54. return rst
  55. # 源代码文件,测试用例目录,测试用例个数,时间限制ms,内存限制KB,语言
  56. def judge(src_path, td_path, td_total, time_limit, memory_limit, language):
  57. args = []
  58. src_file = ''
  59. rst_list = []
  60. if language == 'c':
  61. if not compile_c(src_path):
  62. return {'result_list': [{'result': RESULT_STR[7]}]}
  63. args = ['./main']
  64. src_file = 'main'
  65. elif language == 'cpp':
  66. if not compile_cpp(src_path):
  67. return {'result_list': [{'result': RESULT_STR[7]}]}
  68. args = ['./main']
  69. src_file = 'main'
  70. elif language == 'java':
  71. if not compile_java(src_path):
  72. return {'result_list': [{'result': RESULT_STR[7]}]}
  73. args = ['java', 'Main']
  74. src_file = 'Main.class'
  75. time_limit *= 3
  76. elif language == 'python':
  77. if not compile_python(src_path):
  78. return {'result_list': [{'result': RESULT_STR[7]}]}
  79. args = ['python3', 'main.py']
  80. src_file = 'main.pyc'
  81. # 遍历每一个测试用例
  82. for i in range(td_total):
  83. in_path = os.path.join(td_path, '%d.in' % i)
  84. out_path = os.path.join(td_path, '%d.out' % i)
  85. if os.path.isfile(in_path) and os.path.isfile(out_path):
  86. rst = run(args, in_path, out_path, time_limit, memory_limit)
  87. rst['result'] = RESULT_STR[rst['result']]
  88. rst_list.append(rst)
  89. else:
  90. os.remove(src_file)
  91. rst_list.append({'result': RESULT_STR[8]})
  92. return {'result_list': rst_list,
  93. 'message': 'test data:%d incompleted' % i}
  94. if os.path.exists(src_file):
  95. os.remove(src_file)
  96. return {'result_list': rst_list}