forked from Submitty/Submitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_homework_function.sh
245 lines (201 loc) · 10.7 KB
/
build_homework_function.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
##########################################################################
# HELPER FUNCTION FOR INSTALLING INDIVIDUAL HOMEWORKS
##########################################################################
function clean_homework {
# which assignment to cleanup
semester="$1"
course="$2"
assignment="$3"
course_dir="$SUBMITTY_DATA_DIR/courses/$semester/$course"
echo "clean old build: $course_dir $3"
# cleanup all files associated with this assignment
rm -rf "$course_dir/test_input/${3}"
rm -rf "$course_dir/test_output/${3}"
rm -rf "$course_dir/provided_code/${3}"
rm -rf "$course_dir/instructor_solution/${3}"
rm -rf "$course_dir/generated_output/${3}"
rm -rf "$course_dir/custom_validation_code/${3}"
rm -rf "$course_dir/build/${3}"
rm -rf "$course_dir/bin/${3}"
rm -rf "$course_dir/config/build/build_${3}.json"
rm -rf "$course_dir/config/complete_config/complete_config_${3}.json"
}
function fix_permissions {
hw_config="$1"
hw_bin_path="$2"
hw_build_path="$3"
course_dir="$4"
assignment="$5"
course_group="$6"
# set the permissions
chmod -f u=rw,g=rw,o=r "$hw_config"
chmod -f u=rwx,g=rwx,o=x "$hw_bin_path"
chmod -f u=rwx,g=rwx,o=x "$hw_bin_path/*out"
# copy the provided code, instructor_solution, test input, test output, and custom validation code files to the appropriate directories
if [ -d "$hw_build_path/provided_code/" ]; then
rsync -ruz --delete "$hw_build_path/provided_code/" "$course_dir/provided_code/$assignment/"
fi
if [ -d "$hw_build_path/instructor_solution/" ]; then
rsync -ruz --delete "$hw_build_path/instructor_solution/" "$course_dir/instructor_solution/$assignment/"
fi
if [ -d "$hw_build_path/test_input/" ]; then
rsync -ruz --delete "$hw_build_path/test_input/" "$course_dir/test_input/$assignment/"
fi
if [ -d "$hw_build_path/test_output/" ]; then
rsync -ruz --delete "$hw_build_path/test_output/" "$course_dir/test_output/$assignment/"
else
# if there is no test_output provided in the config, create an empty directory to give support for expected_string output
if [ -d $course_dir/test_output/$assignment ]; then
rm -rf $course_dir/test_output/$assignment
fi
mkdir $course_dir/test_output/$assignment
fi
if [ -d "$hw_build_path/custom_validation_code/" ]; then
rsync -ruz --delete "$hw_build_path/custom_validation_code/" "$course_dir/custom_validation_code/$assignment/"
fi
# change permissions so other instructor users in this course & DAEMON_USER can re-run the build course script
find "$course_dir/build/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/build/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/build/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/provided_code/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/provided_code/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/provided_code/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/instructor_solution/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/instructor_solution/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/instructor_solution/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/generated_output/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/generated_output/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/generated_output/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/test_input/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/test_input/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/test_input/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/test_output/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/test_output/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/test_output/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/custom_validation_code/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/custom_validation_code/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/custom_validation_code/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/config/build/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/config/build/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/config/build/" -exec chgrp -f "${course_group}" {} \;
find "$course_dir/config/complete_config/" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$course_dir/config/complete_config/" -type f -exec chmod -f ug+rw,o= {} \;
find "$course_dir/config/complete_config/" -exec chgrp -f "${course_group}" {} \;
}
function build_homework {
CONF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/../config
SUBMITTY_INSTALL_DIR=$(jq -r '.submitty_install_dir' "${CONF_DIR}/submitty.json")
SUBMITTY_DATA_DIR=$(jq -r '.submitty_data_dir' "${CONF_DIR}/submitty.json")
# location of the homework source files, including:
# $hw_source/config.h
# $hw_source/provided_code/<instructor code files>
# $hw_source/instructor_solution/<instructor code files>
# $hw_source/test_input/<input files>
# $hw_source/test_output/<output files>
# $hw_source/custom_validation_code/<instructor code files>
hw_source="$1"
# where it should be installed (what semester, course, and assignment number/name)
semester="$2"
course="$3"
assignment="$4"
course_dir="$SUBMITTY_DATA_DIR/courses/$semester/$course"
GRADINGCODE="${SUBMITTY_INSTALL_DIR}/src/grading"
# check that the user executing this script is in the course group
course_group=`stat -c "%G" "$course_dir"`
my_username=`id -u -n`
if groups "$my_username" | grep -q "\b$course_group\b"; then
# echo "good: $my_username is in $course_group"
: # null statement in bash
elif [[ "$EUID" -eq 0 ]]; then
# echo "ok: script run by root"
: # null statement in bash
else
echo "ERROR! This script must be run by root, or a member member of the course group."
echo " $my_username is NOT in $course_group"
exit 1
fi
hw_build_path="$course_dir/build/$assignment"
hw_bin_path="$course_dir/bin/$assignment"
hw_config="$course_dir/config/build/build_${assignment}.json"
echo "--------------------------------------------------------------------------------------"
date
echo "INSTALL assignment config: $hw_source"
echo " destination: $hw_build_path"
# copy the files to the build directory
rsync -ruz --delete "$hw_source/" "$hw_build_path" || ( echo 'ERROR: configuration source directory does not exist or DAEMON_USER cannot read this directory' ; exit 1 )
find "$hw_build_path" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$hw_build_path" -type f -exec chmod -f ug+rw,o= {} \;
find "$hw_build_path" -type f -exec chgrp -f ${course_group} {} \;
# grab the universal cmake file
cp "$SUBMITTY_INSTALL_DIR/src/grading/Sample_CMakeLists.txt" "$hw_build_path/CMakeLists.txt"
chmod -f 660 "$hw_build_path/CMakeLists.txt"
# go to the build directory
pushd "$hw_build_path" > /dev/null
# Use the C Pre-Processor to strip the C & C++ comments from config.json
cpp "${course_dir}/build/${assignment}/config.json" complete_config.json
cpp_res="$?"
if (( "$cpp_res" != 0 )); then
echo -e "\nFailed to run cpp preprocessor on ${course_dir}/build/${assignment}/config.json"
popd > /dev/null
exit 1
fi
# Run the complete config json through a python json syntax checker.
python3 "${GRADINGCODE}/json_syntax_checker.py" complete_config.json
py_res="$?"
if (( "$py_res" != 0 )); then
echo -e "\nFailed to load the instructor config.json"
popd > /dev/null
exit 1
fi
# Add allowed minutes in database from config if exists
python3 "${SUBMITTY_INSTALL_DIR}/bin/set_allowed_mins.py" "${hw_build_path}/complete_config.json" "${semester}" "${course}" "${assignment}"
set_minutes="$?"
if (("$set_minutes" != 0)); then
echo -e "\nFailed to set override allowed minutes. A student listed in config.json is missing from course."
popd > /dev/null
exit 1
fi
"${SUBMITTY_INSTALL_DIR}/bin/configure.out" complete_config.json "${course_dir}/config/build/build_${assignment}.json" "$assignment"
configure_res="$?"
if (( "$configure_res" != 0 )); then
echo -e "\nFailed to create a complete_config.json"
popd > /dev/null
exit 1
fi
# Remove the intermediate config
rm complete_config.json
# build the configuration, compilation, runner, and validation executables
# configure cmake, specifying the clang compiler
CXX=/usr/bin/clang++ cmake . > "$hw_build_path/log_cmake_output.txt" 2>&1
cmake_res="$?"
chmod -f 660 "$hw_build_path/log_cmake_output.txt"
find "$hw_build_path" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$hw_build_path" -type f -exec chmod -f ug+rw,o= {} \;
if (( "$cmake_res" != 0 )); then
echo -e "\nCMAKE ERROR\nlogfile: $hw_build_path/log_cmake_output.txt\n\n"
cat "$hw_build_path/log_cmake_output.txt"
fix_permissions "$hw_config" "$hw_bin_path" "$hw_build_path" "$course_dir" "$assignment" "$course_group"
popd > /dev/null
exit 1
fi
# build (in parallel, 8 threads)
# quit (don't continue on to build other homeworks) if there is a compile error
make -j 8
# capture exit code of make
make_res="$?"
find "$hw_build_path" -type d -exec chmod -f ug+rwx,g+s,o= {} \;
find "$hw_build_path" -type f -exec chmod -f ug+rw,o= {} \;
if (( "$make_res" != 0 )); then
echo -e "\nMAKE ERROR\n\n"
fix_permissions "$hw_config" "$hw_bin_path" "$hw_build_path" "$course_dir" "$assignment" "$course_group"
popd > /dev/null
exit 1
fi
# run the schema validator on the complete_config generated by the build process
"$SUBMITTY_INSTALL_DIR"/bin/config_syntax_check.py "$hw_source" "$assignment" "$semester" "$course"
# generate queue file for generated_output
"$SUBMITTY_INSTALL_DIR"/bin/make_generated_output.py "$hw_source" "$assignment" "$semester" "$course"
fix_permissions "$hw_config" "$hw_bin_path" "$hw_build_path" "$course_dir" "$assignment" "$course_group"
popd > /dev/null
}
##########################################################################