Skip to content

Commit 7c3482d

Browse files
committed
initial script
0 parents  commit 7c3482d

7 files changed

Lines changed: 192 additions & 0 deletions

File tree

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# cc-judge
2+
> The future judge for CodeClassroom.(_WIP_)
3+
4+
5+
### Installation
6+
1. Create virtual environment.
7+
```bash
8+
virtualenv -p python3 venv && cd venv && source bin/activate
9+
```
10+
2. Clone the repository.
11+
```bash
12+
git clone https://github.com/codeclassroom/cc-judge.git
13+
```
14+
3. Install Dependencies.
15+
```bash
16+
pip install -r requirements.txt
17+
```
18+
4. Run this command to test the script.
19+
```bash
20+
python3 judge.py test_java.java Java output.txt
21+
```
22+
23+
### Usage
24+
Currently it can be used as a script only.
25+
26+
```bash
27+
python3 judge.py *arg1* *arg2* *arg3* *arg4*
28+
```
29+
30+
1. `arg1` = Program to compile/interpret (e.g helloword.java, test.cpp).
31+
32+
2. `arg2` = programming language, currently available languages :
33+
- C++ (g++ 7.2.0)
34+
- Python (3.6.0)
35+
- Java (OpenJDK 8)
36+
- C (gcc 6.4.0)
37+
38+
3. `arg3` = Expected Output textfile (e.g output.txt)
39+
40+
4. `arg4` = Standard Input, a textfile which contains the input to program (e.g input.txt)
41+
42+
The whole command should look like.
43+
```bash
44+
python3 judge.py myfile.java Java output.txt input.txt
45+
```
46+
or if you don't have a `stdin`.
47+
```bash
48+
python3 judge.py myfile.java Java output.txt
49+
```
50+
51+
52+
### Pointers
53+
- In a `Java` program the class name should always be ***Main***.
54+
55+
56+
### TODO 📑
57+
```
58+
❌ Compile multiple files asynchronously.
59+
❌ Convert the whole script into a module.
60+
❌ Add --help to display script usage.
61+
```
62+
63+
### Author
64+
65+
👥 **Bhupesh Varshney**
66+
67+
- Twitter: [@bhupeshimself](https://twitter.com/bhupeshimself)
68+
- Github: [@Bhupesh-V](https://github.com/Bhupesh-V)

input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
world

judge.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import requests
2+
import time
3+
import sys
4+
5+
# language IDs on judge0
6+
languages = {
7+
"C++" : 10,
8+
"Java" : 27,
9+
"Python": 34,
10+
"C": 4
11+
}
12+
13+
api_params = {
14+
"number_of_runs": "1",
15+
"cpu_time_limit": "2",
16+
"cpu_extra_time": "0.5",
17+
"wall_time_limit": "5",
18+
"memory_limit": "128000",
19+
"stack_limit": "64000",
20+
"max_processes_and_or_threads": "30",
21+
"enable_per_process_and_thread_time_limit": 'false',
22+
"enable_per_process_and_thread_memory_limit": 'true',
23+
"max_file_size": "1024"
24+
}
25+
26+
27+
28+
def readCode(program):
29+
with open(program, 'r') as myfile:
30+
data = myfile.read()
31+
return data
32+
33+
34+
def readExpectedOutput(output_file):
35+
with open(output_file, 'r') as out:
36+
data = out.read()
37+
return data
38+
39+
40+
def read_stdin(output_file):
41+
with open(output_file, 'r') as out:
42+
data = out.read()
43+
return data
44+
45+
46+
def readStatus(token):
47+
while True:
48+
res = requests.get("https://api.judge0.com/submissions/" + token['token'])
49+
response = res.json()
50+
status = response['status']['description']
51+
#time.sleep(0.5) # wait for judge0 to compile
52+
if status != "Processing" and status != "In Queue":
53+
break
54+
55+
print("Processing 🔵 , \n" + program)
56+
print("Expected Output : \n" + stdout)
57+
58+
if response['status']['description'] == "Accepted":
59+
print("Output : \n" + str(response['stdout']))
60+
print("Compile Success ✅")
61+
else:
62+
print("Compile Failed ❌")
63+
print("Output : " + str(response['stdout']))
64+
print("Error : " + str(response['stderr']))
65+
print("Message : " + str(response['message']) + ", " + response['status']['description'])
66+
67+
68+
def compile(program, language_id, *argv):
69+
if len(argv) == 2:
70+
stdout = argv[0]
71+
stdin = argv[1]
72+
api_params['stdin'] = stdin
73+
elif len(argv) == 1:
74+
stdout = argv[0]
75+
76+
api_params['expected_output'] = stdout
77+
api_params['language_id'] = language_id
78+
api_params['source_code'] = program
79+
80+
res = requests.post("https://api.judge0.com/submissions", data=api_params)
81+
token = res.json()
82+
readStatus(token)
83+
84+
85+
if __name__ == '__main__':
86+
if len(sys.argv) == 4:
87+
program = sys.argv[1]
88+
language = sys.argv[2]
89+
output_file = sys.argv[3]
90+
stdout = readExpectedOutput(output_file)
91+
inputCode = readCode(program)
92+
compile(inputCode, languages[language],stdout)
93+
94+
elif len(sys.argv) == 5:
95+
program = sys.argv[1]
96+
language = sys.argv[2]
97+
output_file = sys.argv[3]
98+
in_file = sys.argv[4]
99+
stdin = read_stdin(in_file)
100+
stdout = readExpectedOutput(output_file)
101+
inputCode = readCode(program)
102+
compile(inputCode, languages[language], stdout, stdin)

output.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 4 9 16
2+
25 36 49
3+
64 81
4+
100

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.18.4

test_java.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import static java.lang.Math.pow;
2+
3+
// program to print a given pattern
4+
5+
class Main {
6+
public static void main (String[] args) {
7+
int p;
8+
for (int i = 1; i <= 10; i++) {
9+
p = (int) (pow (i, 2));
10+
System.out.print (p + " ");
11+
if (i == 4 || i == 7 || i == 9) System.out.println ();
12+
}
13+
}
14+
}

test_python.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name = str(input())
2+
print("Hello, " + name)

0 commit comments

Comments
 (0)