From 67d02c643aa0f0a86aaf0f9e1b0e37987216709f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Drago=C8=99-Iulian=20ARGINT?=
 <dragos.argint@stud.acs.upb.ro>
Date: Sun, 30 Apr 2023 21:11:00 +0300
Subject: [PATCH] Add option to calculate grade if timeout occurs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Dragoș-Iulian ARGINT <dragos.argint@stud.acs.upb.ro>
---
 checker/checker.sh | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/checker/checker.sh b/checker/checker.sh
index 749ba80..94099c7 100755
--- a/checker/checker.sh
+++ b/checker/checker.sh
@@ -34,14 +34,41 @@ usage()
 	exit 1
 }
 
+
+
+recover_grade_from_timeout()
+{
+	local output=$1
+	if [ ! -f $output ]; then
+		echo "$output not available"
+	else
+		points_total=$(echo $(cat $output | grep "....passed" | egrep -o "/.*[0-9]+\.*[0-9]*.*\]" | egrep -o "[0-9]+\.*[0-9]*" | head -n 1))
+		list=$(echo $(cat $output | grep "....passed" | egrep  -o "\[.*[0-9]+\.*[0-9]*.*\/" |  egrep -o "[0-9]+\.*[0-9]*") | sed -e 's/\s\+/,/g')
+		recovered_points=$(python3 -c "print(sum([$list]))")
+		echo "Recovered from timeout => Total: [$recovered_points/$points_total]"
+		echo "Please note that this is not a DIRECT checker output! Please contact a teaching assistant"
+		python3 -c "print('Total: ' + str(int ($recovered_points * 100 / $points_total)) + '/' + '100')"
+	fi
+}
+
 timeout_exceeded()
 {
-	echo TIMEOUT EXCEEDED !!! killing the process
-	echo "<VMCK_NEXT_END>"
+	local output=$1
 	pkill -SIGKILL qemu
-	exit 0
+	echo ""
+	echo "TIMEOUT EXCEEDED !!! killing the process"
+	if [[ $RECOVER_GRADE_TIMEOUT == 0 ]]; then
+		echo "The Recover Grade Timeout option is not set! Please contact a teaching assistant!"
+	else
+		recover_grade_from_timeout $output
+	fi
+	echo "<VMCK_NEXT_END>"
+	# exit successfully for vmchecker-next to process output
+        exit 0 # TODO: fixme
 }
 
+timeout_exceeded /tmp/bogdan
+exit 1
 compute_total()
 {
 
@@ -132,7 +159,7 @@ run_checker()
 		if [ ! -f $module_path ]; then
 			error_message $assignment_mod
 			# exit successfully for vmchecker-next to process output
-			exit 0 # TODO: changeme 
+			exit 0 # TODO: fixme
 		fi
 	
 		# copy *.ko in checker
@@ -159,7 +186,7 @@ run_checker()
 					dump_output $output
 					compute_total $output
 				fi
-				timeout_exceeded
+				timeout_exceeded $output
 			fi
 			sleep 2
 			(( timeout += 2 ))
@@ -173,12 +200,15 @@ run_checker()
 
 case $1 in
 	0-list)
+		RECOVER_GRADE_TIMEOUT=0 # If set to 1, in case of a timeout, will calculate the total grade based on the output directory
 		run_checker $ASSIGNMENT0_MOD $ASSIGNMENT0_DIR $ASSIGNMENT0_CHECKER_LOCAL_DIR $ASSIGNMENT0_CHECKER_DIR $ASSIGNMENT0_OUTPUT $ASSIGNMENT0_FINISHED $1
 		;;
 	1-tracer)
+		RECOVER_GRADE_TIMEOUT=0 # If set to 1, in case of a timeout, will calculate the total grade based on the output directory
 		run_checker $ASSIGNMENT1_MOD $ASSIGNMENT1_DIR $ASSIGNMENT1_CHECKER_LOCAL_DIR $ASSIGNMENT1_CHECKER_DIR $ASSIGNMENT1_OUTPUT $ASSIGNMENT1_FINISHED $1 $ASSIGNMENT1_HEADER_OVERWRITE $ASSIGNMENT1_CHECKER_AUX_LIST
 		;;
 	2-uart)
+		RECOVER_GRADE_TIMEOUT=0 # If set to 1, in case of a timeout, will calculate the total grade based on the output directory
 		run_checker $ASSIGNMENT2_MOD $ASSIGNMENT2_DIR $ASSIGNMENT2_CHECKER_LOCAL_DIR $ASSIGNMENT2_CHECKER_DIR $ASSIGNMENT2_OUTPUT $ASSIGNMENT2_FINISHED $1 $ASSIGNMENT2_HEADER_OVERWRITE $ASSIGNMENT2_CHECKER_AUX_LIST
  		;;
 	*)
-- 
GitLab