-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·125 lines (110 loc) · 3.04 KB
/
run
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
#!/bin/bash
CPTOOL_DIR=`dirname $0`
source "$CPTOOL_DIR/util"
print_usage() {
echo "Run"
echo " run competitive programming solution. The program will compiled first if not yet compiled. The program"
echo " will be killed if still running after some period of time, you can change this behaviour using --timeout"
echo " option"
echo ""
echo "Usage: cptool run [OPTION]... [LANGUAGE] SOLUTION"
echo ""
echo "Available commands:"
echo " -h --help Show this help screen."
echo " --version Show version."
echo " --hide-time Hide time indicator"
echo " -t, --timeout TIME Kill program if still running after TIME"
echo " TIME is a floating point number with an optional suffix:"
echo " 's' for seconds (the default), 'm' for minutes, 'h' for hours or 'd' for days"
echo " The default value of this option is 10s"
echo ""
}
run_solution() {
local hide_time="$HIDE_TIME"
local timeout="$TIMEOUT"
local language="$1"
local solution="$2"
$CPTOOL_DIR/compile "$language" "$solution"
if [ $? -ne 0 ]; then
exit $?
fi
local lang_folder
lang_folder=`get_language "$language"`
if [ $? -eq 0 ]; then
. "$lang_folder/lang.conf"
if [ -z "$verbose_name" ]; then
local verbose_name="$name";
fi
if [ -z "$extension" ]; then
local extension="$language"
fi
local PROGRAM=`get_solution_target_path false "$language" "$solution"`
if [ ! -f $PROGRAM ]; then
show_error "file $PROGRAM doesn't exists when running solution"
exit 1
fi
export PROGRAM="$PROGRAM"
if [ $hide_time = true ]; then
eval "timeout -k $timeout $timeout \"$lang_folder/run\""
else
eval "time timeout -k $timeout $timeout \"$lang_folder/run\""
fi
if [ $? -eq 0 ]; then
show_success "$solution exit successfully"
exit 0
else
show_error "run script failed when running solution"
exit 1
fi
else
show_error "language definition doesn't exists when running solution"
exit 1
fi
}
DEFAULT_LANGUAGE="cpp"
HIDE_TIME=false
TIMEOUT=10s
LANGUAGE=""
SOLUTION=""
parsing_options=true
parsing_timeout=false
if [ $# -eq 0 ]; then
print_usage
exit 0
fi
for arg in "$@"
do
if [ $parsing_options = true ]; then
if [ $parsing_timeout = true ]; then
parsing_timeout=false
TIMEOUT=$arg
elif [ $arg = "-h" ] || [ $arg = "--help" ]; then
print_usage
exit $?
elif [ $arg = "--version" ]; then
print_version
exit $?
elif [ $arg = "--hide-time" ]; then
HIDE_TIME=true
elif [ $arg = "-t" ] || [ $arg = "--timeout" ]; then
parsing_timeout=true
else
parsing_options=false
LANGUAGE=$arg
fi
elif [ -z "$SOLUTION" ]; then
SOLUTION=$arg
else
print_usage
exit 1
fi
done
if [ -z "$LANGUAGE" ]; then
show_error "please specify solution name."
print_usage
exit 1
elif [ -z "$SOLUTION" ]; then
SOLUTION="$LANGUAGE"
LANGUAGE="$DEFAULT_LANGUAGE"
fi
run_solution "$LANGUAGE" "$SOLUTION"