-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2_test
executable file
·125 lines (108 loc) · 3.4 KB
/
p2_test
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
TMP_DIR=/tmp/p2-grading/
REQUIRED_FILES="actor.php movie.php search.php review.php sql/create.sql sql/load.sql"
ZIP_FILE=project2.zip
# exit the script with the provided error message
function error_exit()
{
echo "ERROR: $1" 1>&2
rm -rf ${TMP_DIR}
exit 1
}
# check if the given list of files exist
function check_files()
{
for FILE in $1; do
if [ ! -f ${FILE} ]; then
error_exit "Cannot find ${FILE} in $2"
fi
done
}
# retrieve a file from a URL (first argument) and check its SHA1 (second argument)
# and save it in a directory (third argument)
function retrieve_file()
{
URL=$1
SHA1=$2
RETRIEVE_DIR=$3
FILENAME=$(basename ${URL})
if [ -z ${RETRIEVE_DIR} ]; then
TESTFILE="${FILENAME}"
else
mkdir -p ${RETRIEVE_DIR}
TESTFILE="${RETRIEVE_DIR}/${FILENAME}"
fi
# check whether the file has already been retrieved and exists
if [ -f ${TESTFILE} ]; then
sha1sum ${TESTFILE} | grep ${SHA1} &> /dev/null
if [ $? -eq 0 ]; then
# the file already exists and its checksum matches
return
fi
fi
# the file does not exist. retrieve the file
curl -s ${URL} > ${TESTFILE}
if [ $? -ne 0 ]; then
error_exit "Failed to retrieve ${FILENAME} file"
fi
sha1sum ${TESTFILE} | grep ${SHA1} &> /dev/null
if [ $? -ne 0 ]; then
error_exit "Failed to retrieve ${FILENAME} file. Checksum mismatch."
fi
}
# use user-provided zip file name
if [ $# -eq 1 ]; then
ZIP_FILE=$1
fi
if [ `whoami` != "cs143" ]; then
error_exit "You need to run this script within the container"
fi
# clean any existing files
rm -rf ${TMP_DIR}
mkdir ${TMP_DIR}
# unzip the submission zip file
if [ ! -f ${ZIP_FILE} ]; then
error_exit "Cannot find $ZIP_FILE"
fi
unzip -q -d ${TMP_DIR} ${ZIP_FILE}
if [ "$?" -ne "0" ]; then
error_exit "Cannot unzip ${ZIP_FILE} to ${TMP_DIR}"
fi
# change directory to the grading folder
cd ${TMP_DIR}
# check the existence of the required files
check_files "${REQUIRED_FILES}" "root folder of the zip file"
echo "Removing all files in /home/cs143/www/grading/"
rm -rf /home/cs143/www/grading
if [ $? -ne 0 ]; then
error_exit "An error was encountered while cleaning up grading/ subdirectory"
fi
# drop existing tables
echo "Dropping existing tables in the class_db database"
echo "drop table if exists Movie, Actor, MovieGenre, MovieActor, Review;" | mysql class_db
# Download the data.zip file into the sql diretory
cd sql
echo "Downloading and unzipping data.zip file to load database..."
retrieve_file https://oak.cs.ucla.edu/classes/cs143/project1/data.zip 584e1c7069bb61c53de4d4ea0daed2eba2164dc8 .
unzip data.zip
# create and load the downloaded data into MySQL
echo "Running your create.sql script..."
mysql class_db < ./create.sql
if [ $? -ne 0 ]; then
error_exit "An error was encountered while running your create.sql file"
fi
echo "Running your load.sql script..."
mysql class_db < ./load.sql
if [ $? -ne 0 ]; then
error_exit "An error was encountered while running your load.sql file"
fi
echo "Linking your submission to the grading/ subdirectory"
ln -sf ${TMP_DIR} /home/cs143/www/grading
if [ $? -ne 0 ]; then
error_exit "An error was encountered while linking to grading/"
fi
echo "All done!"
echo ""
echo "Please ensure that you have a fully functional Web site available"
echo "at http://localhost:8888/grading/"
exit 0