judge.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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('python -m py_compile %s' % src_path) != 0:
  28. return False
  29. return True
  30. def run(args, in_path, out_path, time_limit, memory_limit):
  31. fin = open(in_path)
  32. ftemp = open('temp.out', 'w')
  33. runcfg = {
  34. 'args': args,
  35. 'fd_in': fin.fileno(),
  36. 'fd_out': ftemp.fileno(),
  37. 'timelimit': time_limit, # in MS
  38. 'memorylimit': memory_limit, # in KB
  39. }
  40. rst = lorun.run(runcfg)
  41. fin.close()
  42. ftemp.close()
  43. if rst['result'] == 0:
  44. ftemp = open('temp.out')
  45. fout = open(out_path)
  46. crst = lorun.check(fout.fileno(), ftemp.fileno())
  47. fout.close()
  48. ftemp.close()
  49. os.remove('temp.out')
  50. if crst != 0:
  51. return {'result': crst}
  52. return rst
  53. def judge(src_path, td_path, td_total, time_limit, memory_limit, language):
  54. args = []
  55. src_file = ''
  56. rst_list = []
  57. if language == 'c':
  58. if not compile_c(src_path):
  59. return {'result': RESULT_STR[7]}
  60. args = ['./main']
  61. src_file = 'main'
  62. elif language == 'cpp':
  63. if not compile_cpp(src_path):
  64. return {'result': RESULT_STR[7]}
  65. args = ['./main']
  66. src_file = 'main'
  67. elif language == 'java':
  68. if not compile_java(src_path):
  69. return {'result': RESULT_STR[7]}
  70. args = ['java Main']
  71. src_file = 'Main.class'
  72. elif language == 'python':
  73. if not compile_python(src_path):
  74. return {'result': RESULT_STR[7]}
  75. args = ['python main.py']
  76. src_file = 'main.py'
  77. for i in range(td_total):
  78. in_path = os.path.join(td_path, '%d.in' % i)
  79. out_path = os.path.join(td_path, '%d.out' % i)
  80. if os.path.isfile(in_path) and os.path.isfile(out_path):
  81. rst = run(args, in_path, out_path, time_limit, memory_limit)
  82. rst['result'] = RESULT_STR[rst['result']]
  83. rst_list.append(rst)
  84. else:
  85. os.remove(src_file)
  86. return {'result': RESULT_STR[8],
  87. 'message': 'test data:%d incompleted' % i}
  88. os.remove(src_file)
  89. return rst_list