File size: 1,524 Bytes
41e79e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import tempfile
from src.safe_subprocess import run
from pathlib import Path
from src.generic_eval import main

LANG_NAME = "Java"
LANG_EXT = ".java"

#Following files have problems:
#137, 
#22: Any
#148: Elipsis

def eval_script(path: Path):

    sys_env = os.environ.copy()
    javatuples_path = Path("/usr/multiple/javatuples-1.2.jar")

    sys_env["CLASSPATH"] =  f"{javatuples_path}"

    with tempfile.TemporaryDirectory() as outdir:
        #Each Java file contains the class with same name `JAVA_CLASS_NAME`
        #Hence, javac will same JAVA_CLASS_NAME.class file for each problem
        #Write class for each problem to a different temp dir
        #Use UTF8 encoding with javac
        result = run(["javac", "-encoding", "UTF8", "-d", outdir, path], env=sys_env)
         
        if result.exit_code != 0:
            # Well, it's a compile error. May be a type error or
            # something. But, why break the set convention
            status = "SyntaxError"
        else:
            result = run(["java", "-ea", "-cp", f"{outdir}:{javatuples_path}", "Problem"], env = sys_env)
            if result.timeout:
                status = "Timeout"
            elif result.exit_code == 0:
                status = "OK"
            else:
                status = "Exception"

    return {
        "status": status,
        "exit_code": result.exit_code,
        "stdout": result.stdout,
        "stderr": result.stderr,
    }

if __name__ == "__main__":
    main(eval_script, LANG_NAME, LANG_EXT)