better errors

This commit is contained in:
matthias@arch 2023-12-13 16:29:26 +01:00
parent e1d2cb5f72
commit 04eceded4f
2 changed files with 99 additions and 50 deletions

View File

@ -12,82 +12,123 @@ FMT_WARN='\e[33m%s\e[0m\n'
echo Making Readme of day $day in $readme echo Making Readme of day $day in $readme
function find_lang { function find_lang {
# 1: fileext, 2: lang, 3: howto # args: 1: fileext, 2: lang, 3: howto
sourcefile=$(ls $day_dir/*.$1 2> /dev/null) sources=($(IFS=$'\n' ls $day_dir/*.$1 2> /dev/null))
[[ $? != 0 ]] && return [[ $? != 0 ]] && return
lang=$2 if [[ ${#sources[@]} -gt 1 ]]; then
sourcefilebase=$(basename $sourcefile) printf "$FMT_WARN" "Found multiple sources for language $2 (using first as main): ${sources[@]}"
printf "$FMT_VAR" "Determined Langugae" "$2 ($sourcefilebase)" fi
sed -i "s/LANG/$2/" $readme if [[ ${#sources[@]} -gt 1 ]]; then
howto=$(echo "$3" | sed "s|SOURCE|$sourcefilebase|") printf "$FMT_WARN" "Found multiple sources for language $2 (using first as main): ${sources[@]}"
sed -i "s(HOWTORUN($howto(" $readme fi
loc=$(sed -r '/^\s*(#|\/\/|\/\*|;)/d;/^\s*$/d' $sourcefile | wc -l) mainsource=${sources[0]}
sed -i "s(LOC($loc(" $readme if [[ -z $lang ]]; then
lang=$2
else
lang=$lang,$2
fi
mainsource_base=$(basename $mainsource)
printf "$FMT_VAR" "Determined Language" "$2 ($mainsource_base)"
if [[ -n $3 ]]; then
howto=$(echo "$3" | sed "s|SOURCE|$mainsource_base|")
else
printf "$FMT_WARN" "No HOWTO provided for language $2"
fi
for file in ${sources[@]}; do
loc=$(( $loc + $(sed -r '/^\s*(#|\/\/|\/\*|;)/d;/^\s*$/d' "$file" | wc -l) ))
done
} }
function get_time { function get_time {
cd $day_dir cd $day_dir
exe=$(find . -type f -executable) exe=$(find . -type f -executable)
if [[ -z $exe ]]; then
printf "$FMT_ERR" "Found no executable for day $day"
cd ..
return 2
fi
exe=$(basename $exe) exe=$(basename $exe)
if [[ $(echo $exe | wc -w) != 1 ]]; then if [[ $(echo $exe | wc -w) != 1 ]]; then
printf "$FMT_ERR" "Found multiple or no executables for day $day: '$exe'" printf "$FMT_ERR" "Found multiple executables for day $day: '$exe'"
cd .. cd ..
return 1 return 1
time=Error
else
time=$({ time ./$exe; } 2>&1)
if [[ $? != 0 ]]; then
printf "$FMT_ERR" "Execution error in './$exe' in '$(pwd)'. Output:"
printf "\e[34m"
echo -e "$time"
printf "\e[0m"
cd ..
return 1
fi
time=$(echo -e "$time" | tail -3 | head -1 | awk '{print $2}')
re='([[:digit:]]+)m([[:digit:]]+),([[:digit:]]+)s'
[[ $time =~ $re ]]
min=${BASH_REMATCH[1]}
sec=${BASH_REMATCH[2]}
msec=${BASH_REMATCH[3]}
if [[ $min != 0 ]]; then
time="$min m $sec,$msec s"
else
time="$sec,$msec s"
fi
cd ..
# elif [[ $sec != 0 ]]; then
# time="$sec,$msec s"
# else
# time="$((msec+0)) ms"
# fi
fi fi
time=$({ time ./$exe; } 2>&1)
if [[ $? != 0 ]]; then
printf "$FMT_ERR" "Execution error in './$exe' in '$(pwd)'. Output:"
printf "\e[34m"
echo -e "$time"
printf "\e[0m"
cd ..
return 1
fi
time=$(echo -e "$time" | tail -3 | head -1 | awk '{print $2}')
re='([[:digit:]]+)m([[:digit:]]+),([[:digit:]]+)s'
[[ $time =~ $re ]]
min=${BASH_REMATCH[1]}
sec=${BASH_REMATCH[2]}
msec=${BASH_REMATCH[3]}
if [[ $min != 0 ]]; then
time="$min m $sec,$msec s"
else
time="$sec,$msec s"
fi
# elif [[ $sec != 0 ]]; then
# time="$sec,$msec s"
# else
# time="$((msec+0)) ms"
# fi
cd ..
} }
gifts="" gifts=""
for i in $(seq $day); do gifts+=":gift:"; done for i in $(seq $day); do gifts+=":gift:"; done
sed "s/DAY/$day/g" README.md.temp | sed "s/:gift:/$gifts/" > $day_dir/README.md sed "s/DAY/$day/g" README.md.temp | sed "s/:gift:/$gifts/" > "$readme"
get_time get_time
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
cd ..
printf "$FMT_VAR" "exectime" "$time" printf "$FMT_VAR" "exectime" "$time"
sed -i "s/EXECTIME/$time/" $readme sed -i "s/EXECTIME/$time/" "$readme"
else else
cd ..
printf "$FMT_WARN" "No execution time determined" printf "$FMT_WARN" "No execution time determined"
sed -i '/.*EXECTIME.*/d' $readme sed -i '/.*EXECTIME.*/d' "$readme"
fi fi
if [[ -z $1 ]]; then if [[ -z $1 ]]; then
printf "$FMT_WARN" "No comment provided" printf "$FMT_WARN" "No comment provided"
sed -i "/<!-- COMMENT -->/d" $readme sed -i '/<!-- COMMENT -->/d' "$readme" || echo "ERROR comment"
else else
printf "$FMT_VAR" "Comment" "'$1'" printf "$FMT_VAR" "Comment" "'$1'"
sed -i "s|COMMENT|$1|" $readme sed -i "s|COMMENT|$1|" "$readme" || echo "ERROR comment2"
fi fi
dayexec="day${day_dir}"
loc=0
find_lang py Python "python3 SOURCE" find_lang py Python "python3 SOURCE"
find_lang rs Rust "rustc SOURCE\n./day${day_dir}" find_lang rs Rust "rustc SOURCE\n./$dayexec"
find_lang cpp C++ "g++ SOURCE -o day${day_dir}\n ./day${day_dir}" find_lang cpp C++ "g++ SOURCE -o ${dayexec}\n./${dayexec}"
find_lang c C "gcc SOURCE -o ${dayexec}\n./${dayexec}"
find_lang java Java "javac Main.java\njava Main"
find_lang s Assembly ""
find_lang s65 Assembly "javac Main.java\njava Main"
find_lang awk Awk "awk -f SOURCE input.txt"
find_lang sh Bash "./SOURCE"
find_lang php PhP "php -S localhost:8000 -t .\nfirefox http://localhost:8000"
find_lang js Javascript ""
if [[ -z $lang ]]; then
printf "$FMT_WARN" "No languages found"
else
sed -i "s/LANG/$lang/" $readme
fi
if [[ -n $howto ]]; then
sed -i "s|HOWTO|$howto|" $readme
fi
if [[ $loc == 0 ]]; then
printf "$FMT_WARN" "No lines of code found"
else
sed -i "s(LOC($loc(" $readme
fi

View File

@ -2,8 +2,16 @@
table="| Day | Language | Lines of code | Execuction time | Comment |\n" table="| Day | Language | Lines of code | Execuction time | Comment |\n"
table+="|:---:|:---:| ---:| ---: |--- |\n" table+="|:---:|:---:| ---:| ---: |--- |\n"
for day in $(seq -f "%02g" 1 25); do maxday=$(ls -d */ | sed -r 's|0*(.*)/|\1|g' | tail -1)
[[ ! -d "$day" ]] && continue for day in $(seq -f "%02g" 1 $maxday); do
if [[ ! -d "$day" ]]; then
echo "Missing directory for day $day"
continue
fi
if [[ ! -f "$day/README.md" ]]; then
echo "Missing README for day $day"
continue
fi
# echo $day # echo $day
lang=$(grep -E "Today's language:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/') lang=$(grep -E "Today's language:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/')
exectime=$(grep -E "Execution time:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/') exectime=$(grep -E "Execution time:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/')