~adigitoleo/geobits

cf1e17a0ee13fcf7330486ac95ac98f375a7b26b — adigitoleo 6 months ago 0778510
feat: Add PBS job linter
1 files changed, 59 insertions(+), 0 deletions(-)

A tools/q,lint.sh
A tools/q,lint.sh => tools/q,lint.sh +59 -0
@@ 0,0 1,59 @@
#!/bin/sh
readonly SCRIPTNAME="${0##*/}"
set -eu
helpf() { echo 'q,lint <JOBFILE> -- check PBS job script for typos/errors'; }
is_command() {
    if 1>/dev/null 2>&1 command -v "$1"; then
        return 0
    else
        >&2 printf '%s\n' "$SCRIPTNAME: command '$1' not found"; return 1
    fi
}
die() { >&2 printf '%s\n' "$SCRIPTNAME: $1"; exit 1; }

[ $# -eq 0 ] && { helpf; exit 1; }
[ "$1" = "-h" ] && { helpf; exit 1; }

[ -x "$1" ] || die 'job script is not executable'

# TODO: Check that the ncpus, mem, storage and walltime are legal for the
# chosen queue type. Maybe also check that the group name is listed?
lineno=1
while IFS= read -r line || [ -n "$line" ]; do
    case $lineno in
        1) case "$line" in "#!/bin/bash");;
            *) die "line 1 should be bash shebang, not '$line'";;
            esac;;
        2) case "$line" in '#PBS -P '*);;
            *) die 'line 2 should set group declaration via "#PBS -P"';;
            esac;;
        3) case "$line" in '#PBS -q '*);;
            *) die 'line 3 should set queue choice via "#PBS -q"';;
            esac;;
        4) case "$line" in '#PBS -l walltime'*);;
            *) die 'line 4 should set requested walltime';;
            esac;;
        5) case "$line" in '#PBS -l ncpus'*);;
            *) die 'line 5 should set requested number of cpus';;
            esac;;
        6) case "$line" in '#PBS -l mem'*);;
            *) die 'line 6 should set requested memory';;
            esac;;
        7) case "$line" in '#PBS -l storage'*);;
            *) die 'line 7 should set requested storage';;
            esac;;
        8) case "$line" in '#PBS -l wd');;
            *) die 'line 8 should be "#PBS -l wd"';;
            esac;;
        9) case "$line" in '#PBS -o '*);;
            *) die 'line 9 should set file name for redirected stdout';;
            esac;;
        10) case "$line" in '#PBS -e '*);;
            *) die 'line 10 should set file name for redirected stderr';;
            esac;;
        *) ;;
    esac
    lineno=$(( $lineno + 1 ))
done <"$1"

[ $(grep -c '^[^#].*' "$1") -gt 0 ] || die "all lines are commented!"