-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_script.sh
More file actions
139 lines (121 loc) · 3.76 KB
/
executor_script.sh
File metadata and controls
139 lines (121 loc) · 3.76 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
LANGUAGE=$1
CODE=$2
INPUT_FILE=$3
OUTPUT_FILE=$4
execute_code() {
cmd=$1
code=$2
input_file=$3
output_file=$4
mkdir -p /home/executor/sandbox
cp $(which $cmd) /home/executor/sandbox/
chmod +x /home/executor/sandbox/$(basename $cmd)
echo "$input_file" > /home/executor/sandbox/input
if [[ $cmd == "python3" ]]; then
if [[ -z "$input_file" ]]; then
echo "$code" > /home/executor/sandbox/code
else
echo -e "INPUT_PATH='/home/executor/sandbox/$(basename $input_file)'\nOUTPUT_PATH ='$output_file'\n$code" > /home/executor/sandbox/code
cp "$input_file" /home/executor/sandbox/
fi
elif [[ $cmd == "lua" ]]; then
if [[ -z "$input_file" ]]; then
echo "$code" > /home/executor/sandbox/code
else
echo -e "local INPUT_PATH = '/home/executor/sandbox/$(basename $input_file)'\nlocal OUTPUT_PATH = '$output_file'\n$code" > /home/executor/sandbox/code
cp "$input_file" /home/executor/sandbox/
fi
elif [[ $cmd == "node" ]]; then
if [[ -z "$input_file" ]]; then
echo "$code" > /home/executor/sandbox/code
else
echo -e "const fs = require('fs');\nconst INPUT_PATH = '/home/executor/sandbox/$(basename $input_file)';\nconst OUTPUT_PATH = '$output_file';\n$code" > /home/executor/sandbox/code
cp "$input_file" /home/executor/sandbox/
fi
else
echo "$code" > /home/executor/sandbox/code
fi
output=$(mktemp /home/executor/sandbox/tmp.XXXXXXXXXX)
error=$(mktemp /home/executor/sandbox/tmp.XXXXXXXXXX)
/home/executor/sandbox/$(basename $cmd) /home/executor/sandbox/code > "$output" 2> "$error"
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
cat "$error"
echo "EXECUTOR_ERROR"
rm "$output" "$error"
exit 1
else
cat "$output"
rm "$output" "$error"
fi
}
compile_and_execute_rust() {
code=$1
input_file=$2
output_file=$3
if [[ -z "$input_file" ]]; then
echo "$code" > /home/executor/sandbox/temp.rs
else
file_path="/home/executor/sandbox/temp.rs"
echo "$code" | awk 'BEGIN{print "const INPUT_PATH: &str = \"'/mnt/shared/$(basename $input_file)'\";\nconst OUTPUT_PATH: &str = \"'/mnt/shared/output/$(basename $output_file)'\";"} 1' > $file_path
# DEBUG
#cat /home/executor/sandbox/temp.rs > /mnt/shared/output/debug_temp.rs
fi
if [ ! -s /home/executor/sandbox/temp.rs ]; then
echo "No code to compile"
echo "EXECUTOR_ERROR"
exit 1
fi
# DEBUG
#echo "$code_with_paths" > /mnt/shared/output/$(basename $output_file).rs
export TMPDIR=/home/executor/sandbox
COMPILE_RESULT=$(rustc /home/executor/sandbox/temp.rs -o /home/executor/sandbox/temp 2>&1)
COMPILE_EXIT_CODE=$?
if [ $COMPILE_EXIT_CODE -ne 0 ]; then
echo "$COMPILE_RESULT"
echo "EXECUTOR_ERROR"
exit 1
fi
output=$(mktemp /home/executor/sandbox/tmp.XXXXXXXXXX)
error=$(mktemp /home/executor/sandbox/tmp.XXXXXXXXXX)
if [[ -z "$input_file" ]]; then
/home/executor/sandbox/temp > "$output" 2> "$error"
else
/home/executor/sandbox/temp /mnt/shared/input/$(basename $input_file) > "$output" 2> "$error"
fi
if [ ! -s "$output" ]; then
EXEC_RESULT=$(cat "$error")
EXEC_EXIT_CODE=1
else
EXEC_RESULT=$(cat "$output")
EXEC_EXIT_CODE=0
fi
if [ $EXEC_EXIT_CODE -ne 0 ]; then
cat "$error"
echo "EXECUTOR_ERROR"
rm "$output" "$error"
exit 1
else
cat "$output"
rm "$output" "$error"
fi
}
case $LANGUAGE in
"python")
execute_code "python3" "$CODE" "$INPUT_FILE" "$OUTPUT_FILE"
;;
"lua")
execute_code "lua" "$CODE" "$INPUT_FILE" "$OUTPUT_FILE"
;;
"javascript")
execute_code "node" "$CODE" "$INPUT_FILE" "$OUTPUT_FILE"
;;
"rust")
compile_and_execute_rust "$CODE" "$INPUT_FILE" "$OUTPUT_FILE"
;;
*)
echo "Unsupported language EXECUTOR_ERROR"
exit 1
;;
esac