PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/gen-exam/gen-exam-sql.sh

https://gitlab.com/baptiste_/unix-training
Shell | 275 lines | 229 code | 36 blank | 10 comment | 18 complexity | 0bd69c6e7f1f5583093b1f25f607a814 MD5 | raw file
  1. . "$EXAM_DIR"/exam-lib.sh
  2. . "$EXAM_DIR"/exam-obfuscation-lib.sh
  3. usage () {
  4. cat << EOF
  5. Usage: $(basename $0) [options]
  6. Options:
  7. --help This help message.
  8. --output DIR, -o DIR
  9. Output directory
  10. --cleanup Remove output directory
  11. --cleanup-db Remove existing lines for this subject in database
  12. --apply Apply generated SQL direcly
  13. --subject N Subject number
  14. --verbose Be verbose
  15. --postgresql Use PostgreSQL syntax
  16. --mysql Use MySQL syntax
  17. --list list.csv Use list.csv as student list file
  18. --tar Compress (.tar.gz) generated files
  19. EOF
  20. }
  21. tar=no
  22. list_students=${exam_list_students:-list_students.csv}
  23. outdir=exam_genere
  24. outsql=
  25. cleanup=no
  26. clean_db=no
  27. apply=no
  28. # $exam_subject_number may be defined in the exam file.
  29. subject=$exam_subject_number
  30. basedir=$(pwd)
  31. verbose=no
  32. dbtype=${exam_dbtype:-mysql}
  33. php_only=no
  34. while test $# -ne 0; do
  35. case "$1" in
  36. "--help"|"-h")
  37. usage
  38. exit 0
  39. ;;
  40. "--output"|"-o")
  41. shift
  42. outdir=$1
  43. ;;
  44. "--cleanup")
  45. cleanup=yes
  46. ;;
  47. "--cleanup-db")
  48. cleanup_db=yes
  49. ;;
  50. "--apply")
  51. apply=yes
  52. ;;
  53. "--subject")
  54. shift
  55. subject=$1
  56. ;;
  57. "--verbose")
  58. verbose=yes
  59. ;;
  60. "--postgresql")
  61. dbtype=postgresql
  62. ;;
  63. "--mysql")
  64. dbtype=mysql
  65. ;;
  66. "--demo")
  67. die "--demo must be the first option, sorry"
  68. ;;
  69. "--list")
  70. shift
  71. list_students="$1"
  72. ;;
  73. "--php-only")
  74. php_only=yes
  75. ;;
  76. "--tar"|"--archive") # --tar is deprecated
  77. archive=yes
  78. ;;
  79. "--zip")
  80. # Already dealt with in exam-main.sh
  81. ;;
  82. *)
  83. echo "ERROR: Unrecognized option $1"
  84. usage
  85. exit 1
  86. ;;
  87. esac
  88. shift
  89. done
  90. if [ "$outsql" = "" ]; then
  91. outsql=$outdir/questions.sql
  92. fi
  93. if [ "$subject" = "" ]; then
  94. die "Please specify subject number with --subject N."
  95. fi
  96. if [ "$cleanup" = "yes" ]; then
  97. rm -fr "$outdir"
  98. rm -f "$outsql"
  99. fi
  100. mkdir -p "$outdir" || die "Cannot create directory $outdir"
  101. list_students=$(absolute_path "$list_students")
  102. outsql=$(absolute_path "$outsql")
  103. EXAM_DIR=$(absolute_path "$EXAM_DIR")
  104. cd "$outdir" || die "Cannot enter directory $outdir"
  105. # make outdir absolute.
  106. outdir=$(pwd)
  107. exam_install_php
  108. exam_config_php sql > "$outdir"/php/inc/config.php
  109. if [ "$php_only" = "yes" ]; then
  110. echo "PHP files generated in:"
  111. echo
  112. echo " $outdir"/php/
  113. echo
  114. echo "Skipping SQL generation (--php-only in use)"
  115. exit 0
  116. fi
  117. if [ -f "$outsql" ]; then
  118. die "$outsql already exists.
  119. Remove it manually or use --cleanup."
  120. fi
  121. prepare_questions
  122. case "$dbtype" in
  123. "postgresql")
  124. sql_raw "SET client_encoding = 'UTF8';"
  125. ;;
  126. "mysql")
  127. sql_raw "SET NAMES 'UTF8';"
  128. ;;
  129. esac
  130. if [ "$cleanup_db" = "yes" ]; then
  131. sql_raw "DELETE FROM exam_unix_subject_questions WHERE id_subject = '$subject';"
  132. sql_raw "DELETE FROM exam_unix_forms WHERE id_subject = '$subject';"
  133. sql_raw "DELETE FROM exam_unix_question WHERE id_subject = '$subject';"
  134. sql_raw "DELETE FROM exam_unix_logins WHERE id_subject = '$subject';"
  135. sql_raw "DELETE FROM exam_unix_subject WHERE id = '$subject';"
  136. fi
  137. sql_raw "INSERT INTO exam_unix_subject(id, descriptif) VALUES ('$subject', '$(sql_escape "$exam_subject_title")');"
  138. sql_newline
  139. coefficients=()
  140. for login in $(get_logins); do
  141. question=1
  142. session=$(get_session "$login")
  143. machine=$(get_machine "$login")
  144. first_name=$(get_first_name "$login" | sql_escape_pipe)
  145. family_name=$(get_family_name "$login" | sql_escape_pipe)
  146. student_id=$(get_student_id "$login" | sql_escape_pipe)
  147. echo "login = $login; session = $session; machine = $machine; first_name = $first_name; family_name = $family_name"
  148. studentdir="$outdir/php/subjects/$session/$machine"
  149. mkdir -p "$studentdir"
  150. cd "$studentdir"
  151. sql_comment "Etudiant $login"
  152. sql_echo "Importing questions/answers for $first_name $family_name ($login)"
  153. sql_raw "INSERT INTO exam_unix_logins
  154. (id_subject, session, machine, login, initial_login, first_name, initial_first_name, family_name, initial_family_name, student_id, initial_student_id)
  155. VALUES ('$subject', '$session', '$machine', '$login', '$login', '$first_name', '$first_name', '$family_name', '$family_name', '$student_id', '$student_id');"
  156. all_questions
  157. sql_newline
  158. done
  159. # Don't stay inside a student's directory (it will be tar-ed and
  160. # deleted).
  161. cd "$outdir"
  162. sql_comment "Coefficients of questions"
  163. sum=0
  164. for (( i = 1 ; i <= ${#coefficients[@]} ; i++ )); do
  165. sql_coef $i ${coefficients[$i]}
  166. sum=$(($sum + ${coefficients[$i]}))
  167. done
  168. echo "Number of questions: ${#coefficients[@]}"
  169. echo "Total coefficients: $sum"
  170. if [ "$archive" = "yes" ]; then
  171. echo "Packing subject directory to sujet.tar.gz and sujet.zip ..."
  172. for login in $(get_logins); do
  173. session=$(get_session "$login")
  174. machine=$(get_machine "$login")
  175. (
  176. cd "$outdir/php/subjects/$session/$machine" &&
  177. tar czf ../"$machine"-sujet.tar.gz . &&
  178. zip -r ../"$machine"-sujet.zip . &&
  179. cd .. &&
  180. rm -fr "$machine" &&
  181. mkdir "$machine" &&
  182. mv "$machine"-sujet.tar.gz "$machine"/sujet.tar.gz &&
  183. mv "$machine"-sujet.zip "$machine"/sujet.zip
  184. )
  185. done
  186. fi
  187. # A few useful SQL and shell scripts generated in the target dir.
  188. "$EXAM_DIR"/init-db.sh > "$outdir"/init-db.sql
  189. "$EXAM_DIR"/init-db.sh --drop-tables > "$outdir"/init-db-drop-tables.sql
  190. case "$dbtype" in
  191. "postgresql")
  192. cat > "$outdir"/hard-reset-db.sh <<EOF
  193. #! /bin/sh
  194. cd "\$(dirname "\$0")"
  195. # WARNING: untested code!
  196. cat ./init-db-drop-tables.sql ./questions.sql | \\
  197. psql -h "$exam_dbhost" -d "$exam_dbname" -U "$exam_dbuser"
  198. EOF
  199. chmod +x "$outdir"/hard-reset-db.sh
  200. ;;
  201. "mysql")
  202. cat > "$outdir"/hard-reset-db.sh <<EOF
  203. #! /bin/sh
  204. cd "\$(dirname "\$0")"
  205. cat ./init-db-drop-tables.sql ./questions.sql | \\
  206. mysql --user="$exam_dbuser" -h "$exam_dbhost" -p --database="$exam_dbname"
  207. EOF
  208. chmod +x "$outdir"/hard-reset-db.sh
  209. ;;
  210. *)
  211. echo "Unknown database type $dbtype"
  212. ;;
  213. esac
  214. if [ "$apply" = "yes" ]; then
  215. # needs a password.
  216. case "$dbtype" in
  217. "postgresql")
  218. psql -h "$exam_dbhost" -d "$exam_dbname" -U "$exam_dbuser" -f "$outsql"
  219. ;;
  220. "mysql")
  221. mysql -h "$exam_dbhost" -p --user="$exam_dbuser" --database="$exam_dbname" < "$outsql"
  222. ;;
  223. *)
  224. echo "Unknown database type $dbtype"
  225. ;;
  226. esac
  227. fi
  228. for d in "./php/subjects/"*/
  229. do
  230. ln -s "$d" session-"$(basename "$d")"
  231. done
  232. echo
  233. echo "Generated files in $outdir"
  234. echo "- ${outsql#$outdir/}"
  235. echo "- init-db.sql and init-db-drop-tables.sql to initialize and"
  236. echo " reset the DB (use with care)".
  237. echo "- php/subjects/{1,2}/ : files to put on students account for sessions 1 and 2"
  238. echo " (there are symlinks at the toplevel to help you find these)"
  239. echo "- php/ : PHP files to put on the server. php/inc/config.php is the configuration file."