1
0

judgement.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. def judge(src_path, td_path, td_total, time_limit, memory_limit, language):
  56. args = []
  57. src_file = ''
  58. rst_list = []
  59. if language == 'c':
  60. if not compile_c(src_path):
  61. return {'result_list': [{'result': RESULT_STR[7]}]}
  62. args = ['./main']
  63. src_file = 'main'
  64. elif language == 'cpp':
  65. if not compile_cpp(src_path):
  66. return {'result_list': [{'result': RESULT_STR[7]}]}
  67. args = ['./main']
  68. src_file = 'main'
  69. elif language == 'java':
  70. if not compile_java(src_path):
  71. return {'result_list': [{'result': RESULT_STR[7]}]}
  72. args = ['java', 'Main']
  73. src_file = 'Main.class'
  74. time_limit *= 3
  75. elif language == 'python':
  76. if not compile_python(src_path):
  77. return {'result_list': [{'result': RESULT_STR[7]}]}
  78. args = ['python3', 'main.py']
  79. src_file = 'main.pyc'
  80. for i in range(td_total):
  81. in_path = os.path.join(td_path, '%d.in' % i)
  82. out_path = os.path.join(td_path, '%d.out' % i)
  83. if os.path.isfile(in_path) and os.path.isfile(out_path):
  84. rst = run(args, in_path, out_path, time_limit, memory_limit)
  85. rst['result'] = RESULT_STR[rst['result']]
  86. rst_list.append(rst)
  87. else:
  88. os.remove(src_file)
  89. rst_list.append({'result': RESULT_STR[8]})
  90. return {'result_list': rst_list,
  91. 'message': 'test data:%d incompleted' % i}
  92. if os.path.exists(src_file):
  93. os.remove(src_file)
  94. return {'result_list': rst_list}