Advertisement
Shanix

Untitled

Sep 29th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.78 KB | None | 0 0
  1. #####################################################################
  2. # #
  3. # CSC 116 Automated 'Grading' Tool. Will compile and execute files #
  4. # from students, and check style, after download/extraction from #
  5. # Moodle. Generates 2 output files for each step - an #
  6. # output_[step].txt file, and an output_[step]_error.txt file, #
  7. # both from stdout and stderr. #
  8. # #
  9. # Arguments expected: .java file to compile #
  10. # directory of student programs. #
  11. # Use rename script if they aren't in their #
  12. # own directories before running script. #
  13. # y/n, whether to use Renaming Script or not #
  14. # #
  15. # Optional arguments: expected output file for comparison #
  16. # input file for stdin #
  17. # #
  18. # Use: sh CompileAndExecuteScript file.java folder/ #
  19. # Note: This script assumes you have Zach Butler & #
  20. # Dr. Jessica Schmidt's RenameScript program in the #
  21. # same directory. Later updates will obselete it. #
  22. # #
  23. #####################################################################
  24.  
  25. #####################################################################
  26. # #
  27. # A quick note on how to download student submissions from Moodle. #
  28. # (As well as setting up for the script) #
  29. # 1. Go to the assignment quick grade page. #
  30. # 2. From the drop-down menu next to 'Grading Action', choose #
  31. # 'Download all Submissions'. It will download an archive file. #
  32. # 3. Save this archive as described in the directory structure #
  33. # diagram as detailed below, and extract it so your setup #
  34. # matches the expected. Then, delete the archive file. #
  35. # 4. Run the script. Report errors to spschoen@ncsu.edu #
  36. # (or whoever is maintaining the script) #
  37. # #
  38. #####################################################################
  39.  
  40. #####################################################################
  41. # #
  42. # Expected file/directory structure (before script): #
  43. # Directory #
  44. # -- CompileAndExecuteScript.sh #
  45. # -- RenameScript.java #
  46. # -- Assignment Directory #
  47. # -- Student_Numbers_assignmsubmission_file_JavaProgram.java #
  48. # -- Student_Numbers_assignmsubmission_file_JavaProgram.java #
  49. # -- ... #
  50. # #
  51. #####################################################################
  52.  
  53. #####################################################################
  54. # #
  55. # Expected file/directory structure (after script): #
  56. # Directory #
  57. # -- CompileAndExecuteScript.sh #
  58. # -- RenameScript.java #
  59. # -- Assignment Directory #
  60. # -- Student 1 Directory #
  61. # -- JavaProgram.java #
  62. # -- JavaProgram.class #
  63. # -- output_compile.txt #
  64. # -- output_compile_error.txt #
  65. # -- output_execute.txt #
  66. # -- output_execute_error.txt #
  67. # -- output_style.txt #
  68. # -- output_style_error.txt #
  69. # -- Student 2 Directory #
  70. # -- ... #
  71. # #
  72. #####################################################################
  73.  
  74. #####################################################################
  75. # #
  76. # Changelog #
  77. # 16/9/12 - Initial version. #
  78. # 16/9/29 - Updated documentation. SO MUCH DOCUMENTATION. #
  79. # Started work on arguments. #
  80. # Integrated RenameScript.java #
  81. # #
  82. #####################################################################
  83.  
  84. #!/bin/bash/
  85.  
  86. #read -t 1 -n 10000 discard
  87. #saved for later: to ignore stdin
  88.  
  89. compileAndExecuteAndStyle() {
  90.  
  91. #Check if file supplied by user exists AND is readable.
  92. #Note: change -r to -f if you only care that the file exists.
  93. #But reading is probably important.
  94. #Does -r imply -f?
  95. if [ ! -r $COMP_FILENAME ]; then
  96. echo "ERR: $COMP_FILENAME not found or could not be read."
  97. echo ${PWD##*/}
  98. #echo "Exiting program with status 1."
  99. #exit 1
  100. return
  101. fi
  102.  
  103. #Yeah I'm not super happy with deleting files in the script
  104. #But it's probably the best thing to do.
  105. #I'm explicitly writing them out because I don't want to take chances.
  106. echo "NOTE: Deleting old output files if they exist."
  107. rm -f output_compile.txt output_compile_error.txt
  108. rm -f output_execute.txt output_execute_error.txt
  109. rm -f output_style.txt output_style_error.txt
  110.  
  111. echo "--------------------------------------------------------"
  112.  
  113. #Compile the given file notice.
  114. echo "NOTE: Attempting to compile $COMP_FILENAME"
  115. echo "NOTE: Output and errors will be printed to output_compile.txt and output_compile_error.txt"
  116.  
  117. #Compile the given file, output anything to these files.
  118. javac $COMP_FILENAME > output_compile.txt 2> output_compile_error.txt
  119.  
  120. COMP_OUTPUT="output_compile.txt"
  121. COMP_OUTPUT_ERROR="output_compile_error.txt"
  122.  
  123. #Make sure we compiled and were able to output to files.
  124. if [ ! -r $COMP_OUTPUT ]; then
  125. echo "ERR: Could not create output file for compilation."
  126. echo "Exiting program with status 2"
  127. exit 2
  128. fi
  129.  
  130. if [ ! -r $COMP_OUTPUT_ERROR ]; then
  131. echo "ERR: Could not create output file for compilation error."
  132. echo "Exiting program with status 2"
  133. exit 2
  134. fi
  135.  
  136. #Check if there were compilation errors.
  137. if [ $(stat -c%s output_compile_error.txt) -gt 0 ]; then
  138. echo "ERR: Compilation errors detected. Would you like to continue? (y\n)"
  139. read confirmation
  140. if [ "$confirmation" != "y" ]; then
  141. echo "Exiting program with status 3."
  142. exit 3
  143. else
  144. echo "NOTE: Continuing. Errors may compound."
  145. fi
  146. else
  147. echo "NOTE: No compilation errors detected."
  148. fi
  149.  
  150. echo "NOTE: Compilation complete."
  151. echo "--------------------------------------------------------"
  152.  
  153. #Drop the .java from the filename, execute from that.
  154. EXEC_FILENAME=${COMP_FILENAME%.java}
  155. echo "NOTE: Attempting to execute $EXEC_FILENAME"
  156. echo "NOTE: Output and errors will be printed to output_execute.txt and output_execute_error.txt"
  157.  
  158. #Execute the program to the output files.
  159. java $EXEC_FILENAME > output_execute.txt 2> output_execute_error.txt
  160.  
  161. if [ $(stat -c%s output_execute_error.txt) -gt 0 ]; then
  162. echo "ERR: Execution errors detected. Would you like to continue? (y\n)"
  163. read confirmation
  164. if [ "$confirmation" != "y" ]; then
  165. echo "Exiting program with status 4."
  166. exit 4
  167. else
  168. echo "NOTE: Continuing. Errors may compound."
  169. fi
  170. else
  171. echo "NOTE: No execution errors detected."
  172. fi
  173.  
  174. echo "NOTE: Execution complete."
  175.  
  176. echo "--------------------------------------------------------"
  177.  
  178. echo "NOTE: Automated Style Checker executing."
  179. echo "NOTE: This program assumes your style checker exists in ~/cs/."
  180.  
  181. #It sure as shit assumes it because I need to figure out how to check if a file exists
  182. #But not extension. I think the problem might be that it's looking for the
  183. #checkstyle directory in the cs directory, not the checkstyle file.
  184.  
  185. #if [ ! -a $CHECKSTYLE ]; then
  186. # echo "ERR: Could not find checkstyle in ~/cs/. Please install Checkstyle to ~/cs/"
  187. # echo "Exiting program with status 5"
  188. # exit 5
  189. #fi
  190.  
  191. ~/cs/checkstyle $COMP_FILENAME > output_style.txt 2> output_style_error.txt
  192.  
  193. if [ $(stat -c%s output_style_error.txt) -gt 0 ]; then
  194. echo "ERR: Errors occured while checking style. Details in output_style_error.txt"
  195. fi
  196.  
  197. #This is some funky math.
  198. #So, the amount of bytes written by checkstyle is 199 + name of file -.java
  199. #So we check for size of file > (199 + length of program name)
  200. #If greater, we have style errors. If not, it's equal and we're good to go.
  201. STYLE_FILESIZE=$(stat -c%s output_style.txt)
  202. COMP_FILENAME_WITHOUT_JAVA=${COMP_FILENAME%.java}
  203.  
  204. if [ "$STYLE_FILESIZE" -gt $(( 199 + ${#COMP_FILENAME_WITHOUT_JAVA} )) ]; then
  205. echo "NOTE: Style errors detected. ;-;"
  206. else
  207. echo "NOTE: No style errors detected. Great!"
  208. fi
  209.  
  210. sleep 1
  211. }
  212.  
  213. #####################################################################
  214. # #
  215. # Privacy divider. Please respect the privacy of the code. #
  216. # #
  217. #####################################################################
  218.  
  219. #Cleaning everything up.
  220. clear
  221.  
  222. #Quick sleep just because.
  223. sleep 0.25
  224.  
  225. #Saving wherever we started
  226. myDir=$(pwd)
  227.  
  228. #Don't forget to change directory to wherever we are.
  229. cd "$(dirname "$0")"
  230.  
  231. #Check if user supplied a file as an argument.
  232. if [ $# -lt 3 ]; then
  233. echo "ERR: Below minimum argument count."
  234. echo "Expected [something].java [folder of assignments] Rename [y/n]"
  235. echo "Optional arguments following: [expected output] [inputFile]"
  236. echo "Exiting program with status 0."
  237. exit 0
  238. fi
  239.  
  240. if [ $# -gt 5 ]; then
  241. echo "ERR: Above maximum argument count."
  242. echo "Expected [something].java [folder of assignments] Rename [y/n]"
  243. echo "Optional arguments following: [expected output] [inputFile]"
  244. echo "Exiting program with status 0."
  245. exit 0
  246. fi
  247.  
  248. #Making sure Argument 1 is a java file. Or has a java extension at least.
  249. #Grep as suggested by Curtis Moore. Everyone can thank Curtis more.
  250. if grep -q "*.java" <<<$1; then
  251. echo "ERR: Java file argument lacks java extension."
  252. echo "Exiting program with status 0."
  253. exit 0
  254. fi
  255.  
  256. #We're sure it's a java file, so we go ahead and assign a value.
  257. COMP_FILENAME="$1"
  258.  
  259. #Making sure argument 2 is a directory.
  260. if [ ! -d $2 ]; then
  261. echo "ERR: Argument 2 is not a directory."
  262. echo "Exiting program with status 0."
  263. exit 0
  264. fi
  265.  
  266. #Making sure argument 3 is y or n
  267.  
  268. #Trying to compress this to RENAME_VAR=${$2,,} causes a bad substitution error.
  269. #So we have to be a bit inefficient with it
  270. RENAME_VAR=$3
  271. RENAME_VAR=${RENAME_VAR,,}
  272.  
  273. #Check if the value, after lowercase conversion, is y or n.
  274. #TODO: should any value not y just not do rename?
  275. if [ "$RENAME_VAR" != "y" ] && [ "$RENAME_VAR" != "n" ]; then
  276. echo "ERR: Argument 3 is not y or n. Will not take command."
  277. echo "Exiting program with status 0."
  278. exit 0
  279. fi
  280.  
  281. if [ "$RENAME_VAR" == "y" ]; then
  282. if [ ! -r "RenameScript.java" ]; then
  283. echo "ERR: RenameScript.java does not exist."
  284. echo "Exiting program with status 0."
  285. exit 0
  286. fi
  287. #TODO: do this in the script/a separate script
  288. javac RenameScript.java; java RenameScript $2
  289. fi
  290.  
  291. if [ $# >= 4 ]; then
  292. if [ ! -r $4 ]; then
  293. echo "ERR: Expected output file does not exist."
  294. echo "Exiting program with status 0."
  295. exit 0
  296. fi
  297. if [ $# == 5 ] && [ ! -r $5 ]; then
  298. echo "ERR: Input file does not exist."
  299. echo "Exiting program with status 0."
  300. exit 0
  301. fi
  302. fi
  303.  
  304. #Change directory to the directory of many folders.
  305. #Life has many directories edboy
  306. #Note to future self: this has to be last, because of file checks.
  307. cd $2
  308.  
  309. #The working loop. Loops through each directory in the $2 directory,
  310. #And runs the compilation function on it, with the filename argument.
  311. for d in *; do
  312. #check if * is a directory, not a file
  313. if [ -d "${d}" ]; then
  314. cd "${d}"
  315. if [ -r $COMP_FILENAME ]; then
  316. #echo "ERR: $COMP_FILENAME not found in folder ${d}."
  317. #echo "Exiting program with status 1."
  318. #exit 1
  319. clear
  320. echo "NOTE: Current working directory: ${d}"
  321. compileAndExecuteAndStyle "$COMP_FILENAME"
  322. else
  323. echo "Err: File does not exist"
  324. fi
  325. cd ..
  326. fi
  327. done
  328.  
  329. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement