blumonilr 5 éve
szülő
commit
aff09caf04
5 módosított fájl, 100 hozzáadás és 4 törlés
  1. 1 2
      app.py
  2. 7 0
      demo/add.py
  3. BIN
      demo/add.pyc
  4. 2 2
      demo/test.py
  5. 90 0
      judge.py

+ 1 - 2
app.py

@@ -1,5 +1,5 @@
 from flask import Flask
-
+import judge
 app = Flask(__name__)
 
 
@@ -10,7 +10,6 @@ def hello_world():
 
 @app.route('/api/judge', methods = ['POST'])
 def judge():
-
     return {'result': 'System Error'}
 
 

+ 7 - 0
demo/add.py

@@ -0,0 +1,7 @@
+from sys import stdin
+
+if __name__ == '__main__':
+    str = stdin.readline()
+    a = int(str.split()[0])
+    b = int(str.split()[1])
+    print(a+b)

BIN
demo/add.pyc


+ 2 - 2
demo/test.py

@@ -17,7 +17,7 @@ RESULT_STR = [
 ]
 
 def compileSrc(src_path):
-    if os.system('gcc %s -o m'%src_path) != 0:
+    if os.system('python %s'%src_path) != 0:
         print('compile failure!')
         return False
     return True
@@ -27,7 +27,7 @@ def runone(p_path, in_path, out_path):
     ftemp = open('temp.out', 'w')
     
     runcfg = {
-        'args':['./m'],
+        'args':['python ./add.py'],
         'fd_in':fin.fileno(),
         'fd_out':ftemp.fileno(),
         'timelimit':1000, #in MS

+ 90 - 0
judge.py

@@ -14,3 +14,93 @@ RESULT_STR = [
 ]
 
 
+def compile_c(src_path):
+    if os.system('gcc %s -o main' % src_path) != 0:
+        return False
+    return True
+
+
+def compile_cpp(src_path):
+    if os.system('g++ %s -o main' % src_path) != 0:
+        return False
+    return True
+
+
+def compile_java(src_path):
+    if os.system('javac %s' % src_path) != 0:
+        return False
+    return True
+
+
+def compile_python(src_path):
+    if os.system('python -m py_compile %s' % src_path) != 0:
+        return False
+    return True
+
+
+def run(args, in_path, out_path, time_limit, memory_limit):
+    fin = open(in_path)
+    ftemp = open('temp.out', 'w')
+
+    runcfg = {
+        'args': args,
+        'fd_in': fin.fileno(),
+        'fd_out': ftemp.fileno(),
+        'timelimit': time_limit,  # in MS
+        'memorylimit': memory_limit,  # in KB
+    }
+
+    rst = lorun.run(runcfg)
+    fin.close()
+    ftemp.close()
+
+    if rst['result'] == 0:
+        ftemp = open('temp.out')
+        fout = open(out_path)
+        crst = lorun.check(fout.fileno(), ftemp.fileno())
+        fout.close()
+        ftemp.close()
+        os.remove('temp.out')
+        if crst != 0:
+            return {'result': crst}
+
+    return rst
+
+
+def judge(src_path, td_path, td_total, time_limit, memory_limit, language):
+    args = []
+    src_file = ''
+    rst_list = []
+    if language == 'c':
+        if not compile_c(src_path):
+            return {'result': RESULT_STR[7]}
+        args = ['./main']
+        src_file = 'main'
+    elif language == 'cpp':
+        if not compile_cpp(src_path):
+            return {'result': RESULT_STR[7]}
+        args = ['./main']
+        src_file = 'main'
+    elif language == 'java':
+        if not compile_java(src_path):
+            return {'result': RESULT_STR[7]}
+        args = ['java Main']
+        src_file = 'Main.class'
+    elif language == 'python':
+        if not compile_python(src_path):
+            return {'result': RESULT_STR[7]}
+        args = ['python main.py']
+        src_file = 'main.py'
+    for i in range(td_total):
+        in_path = os.path.join(td_path, '%d.in' % i)
+        out_path = os.path.join(td_path, '%d.out' % i)
+        if os.path.isfile(in_path) and os.path.isfile(out_path):
+            rst = run(args, in_path, out_path, time_limit, memory_limit)
+            rst['result'] = RESULT_STR[rst['result']]
+            rst_list.append(rst)
+        else:
+            os.remove(src_file)
+            return {'result': RESULT_STR[8],
+                    'message': 'test data:%d incompleted' % i}
+    os.remove(src_file)
+    return rst_list