This repository was archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdev_tools.sh
258 lines (203 loc) · 7.09 KB
/
dev_tools.sh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
REMOTE="saasbook"
IMAGE="pl-fpp-ruby-autograder"
TMP_OUT=/tmp/dev_out
indent() {
local indent=1
if [ -n "$1" ]; then indent=$1; fi
pr -to $(($indent * 2))
return $?
}
build() { docker build -t $REMOTE/$IMAGE . ; }
push() { docker push $REMOTE/$IMAGE:latest ; }
buildPush() { build && push ; }
buildImage() { # build $IMAGE:dev
# only report errors
hash="$(sudo docker build -q -t $IMAGE:dev . | cut -d: -f2)"
echo -e \> Image name / hash: \\n\\t $IMAGE:dev / $hash
}
deleteImage() { # delete $IMAGE:dev locally
echo -n Deleting image ...
sudo docker image rm $IMAGE:dev > /dev/null
if [[ $? != "0" ]]; then return 1; fi
echo done.
}
runImage() { # run $IMAGE:dev as `autograder_test`
# silence both stderr and stdout
deleteCont &> /dev/null
echo Running Image ...
( sudo docker run --name autograder_test --network none --mount type=bind,source=`pwd`/.container_mount/grade,target=/grade $IMAGE:dev /grader/run.py \
2>.container_mount/stderr \
1>.container_mount/stdout & )
sleep 1
docker container ls | indent 2
code="$(docker container wait autograder_test)"
echo \> Container exited with code $code | indent 2
if [[ $code != "0" ]]; then
echo \> Stdout:
cat .container_mount/stdout | indent
echo \> Stderr:
cat .container_mount/stderr | indent
fi
rm .container_mount/stdout
rm .container_mount/stderr
#echo ==============================================================
return $code
}
deleteCont() { # delete the container named `autograder_test`
sudo docker container rm autograder_test
}
prep_mount() { # assuming $1 is the variants_dir (the question/tests/ directory)
which jq > /dev/null
if [[ $? != "0" ]]; then
(echo "jq is required to run this script, please install and add it to your \$PATH" 1>&2)
return 1
fi
echo "Preparing mount files ... "
clean 2>/dev/null
# make all the necessary dirs
mkdir .container_mount
mkdir .container_mount/grade
mkdir .container_mount/grade/data
mkdir .container_mount/grade/serverFilesCourse
mkdir .container_mount/grade/student
mkdir .container_mount/grade/tests
# load the variants
# script_dir="$(pwd)/${0::-18}"
variant_dir="$(pwd)/$1/"
cp -r $variant_dir/* .container_mount/grade/tests/
rm .container_mount/grade/tests/data.json
rm .container_mount/grade/tests/expected.json
# load submission files
if [[ -f $variant_dir/data.json ]]; then
cp $variant_dir/data.json .container_mount/grade/data/
elif [[ -d $variant_dir/submission ]]; then
cp -r $variant_dir/submission/* .container_mount/grade/student
echo {\"submitted_answers\": {\"student-parsons-solution\": `jq -Rs . < .container_mount/grade/student/_submission_file`}} \
> .container_mount/grade/data/data.json
## double-check that _submission_file isn't in /grade/student
rm .container_mount/grade/student/_submission_file
else
(echo "No submission found: Exiting" 1>&2)
return 1
fi
# now that the files are in place, install the packages
pd=`pwd`
rvm use 2.6.10 | indent
if [[ "${PIPESTATUS[0]}" != "0" ]]; then return 1; fi
cd .container_mount/grade/tests/app
bundle package --all --all-platforms --quiet > /dev/null
if [[ $? != "0" ]]; then
cd $pd
return 1
fi
# bundle install --local > /dev/null
cd $pd
}
clean() {
sudo chown -R $USER .container_mount/grade/
if [[ $? != "0" ]]; then return 1; fi
rm -r .container_mount/
if [[ $? != "0" ]]; then return 1; fi
return
}
clean_up() { # Remove .container_mount/ and delete $IMAGE:dev and $IMAGE:latest locally
clean
deleteImage
}
compare() { # assuming $1 is the variant directory, $2 is the script directory
# compare the result
echo Comparison:
output_loc="`pwd`/.container_mount/grade/results/results.json"
expect_loc="$1/expected.json"
script="$2/tests/verify_result.py"
python3 $script $expect_loc $output_loc | indent 2
return "${PIPESTATUS[0]}"
}
run_test() { # $1 is variant_dir (the question/tests/ directory)
if [[ $1 == "" ]]; then
echo Test not provided, assuming \`run_tests\`
run_tests
if [[ $? != "0" ]]; then return 1; fi
fi
# basically remove "run_test.sh" from the script call to get the directory
script_dir=`pwd`
prep_mount $1
if [[ $? != "0" ]]; then return 1; fi
echo Running the grader
buildImage | indent
runImage | indent
if [[ "${PIPESTATUS[0]}" == 0 ]]; then
deleteCont > /dev/null
if [[ $? != "0" ]]; then return 1; fi
deleteImage | indent
if [[ "${PIPESTATUS[0]}" != "0" ]]; then return 1; fi
else return 1; fi
echo done.
compare $1 $script_dir
return $?
}
run_tests() { # run all tests in tests/
tests=`ls -d tests/*/`
script_dir=`pwd`
failures=0
failed=""
buildImage
while IFS= read -r variant_dir; do
echo Running test $variant_dir
prep_mount $variant_dir | indent 2
if [[ "${PIPESTATUS[0]}" != "0" ]]; then
echo "Preparing Mount Failed!" | indent 2
return 1
fi
runImage | indent 2
if [[ "${PIPESTATUS[0]}" != "0" ]]; then
failures=$((failures+1));
failed="$failed\n> $variant_dir"
else
compare $variant_dir $script_dir | indent 2
if [[ "${PIPESTATUS[0]}" != "0" ]]; then
failures=$((failures+1));
failed="$failed\n> $variant_dir"
fi
fi
done <<< "$tests"
if [[ $failures == "0" ]]; then
deleteCont | indent
deleteImage | indent
fi
echo -e "Failures: $failures $failed"
return $((1 - ($failures == 0)))
}
new_test() { # $1 is the new test name (must be a valid filename)
if [[ $1 == "" ]]; then
echo Error: Please supply a test name that is a valid filename
return 1
fi
# make the base folders and files
cd tests/
mkdir $1
mkdir $1/app
mkdir $1/app/spec
touch $1/app/script.rb
touch $1/app/Gemfile
touch $1/solution
# initialize the spec file
echo -e "require_relative '../script.rb'\n" > $1/app/spec/script_spec.rb
# populate the json objects with filler
meta_content="{\n \"submission_file\": \"script.rb\",\n \"submission_root\": \"\"\n}\n"
expected_content="{\n \"gradable\":true,\n \"tests\":[],\n \"score\":0.0\n}\n"
data_content="{\n \"submitted_answers\" : {\n \"student-parsons-solution\": \"\"\n },\n \"gradable\": true\n}\n"
echo -e "$meta_content" >> $1/meta.json
echo -e "$expected_content" >> $1/expected.json
echo -e "$data_content" >> $1/data.json
# return to base directory
cd ../
return
}
debug() {
sudo docker run -it --rm \
--mount type=bind,source=`pwd`/.container_mount/grade,target=/grade \
--mount type=bind,source=`pwd`/debug_tools.sh,target=/tools.sh \
$IMAGE:dev
return
}