Skip to content

Commit f5f46eb

Browse files
committed
scripts : add lib.sh and lib_test.sh
1 parent 3e5ca79 commit f5f46eb

File tree

3 files changed

+107
-34
lines changed

3 files changed

+107
-34
lines changed

scripts/check-requirements.sh

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/bin/bash
2+
#### BEGIN SETUP #####
23
set -euo pipefail
4+
this=$(realpath -- "$0"); readonly this
5+
cd "$(dirname "$this")"
6+
shellcheck --external-sources "$this"
7+
# shellcheck source=lib.sh
8+
source 'lib.sh'
9+
#### END SETUP ####
310

411
#
512
# check-requirements.sh checks all requirements files for each top-level
@@ -26,27 +33,9 @@ set -euo pipefail
2633
# finally imports the python script to check for `ImportError`.
2734
#
2835

29-
log() {
30-
local level=$1 msg=$2
31-
printf >&2 '%s: %s\n' "$level" "$msg"
32-
}
33-
34-
debug() {
35-
log DEBUG "$@"
36-
}
37-
38-
info() {
39-
log INFO "$@"
40-
}
41-
42-
fatal() {
43-
log FATAL "$@"
44-
exit 1
45-
}
46-
4736
cleanup() {
48-
if [[ -n ${workdir+x} && -d $workdir && -w $workdir ]]; then
49-
info "Removing $workdir"
37+
if _isset workdir && [[ -d $workdir && -w $workdir ]]; then
38+
_log_info "Removing $workdir"
5039
local count=0
5140
rm -rfv -- "$workdir" | while read -r; do
5241
if (( count++ > 750 )); then
@@ -55,7 +44,7 @@ cleanup() {
5544
fi
5645
done
5746
printf '\n'
58-
info "Removed $workdir"
47+
_log_info "Removed $workdir"
5948
fi
6049
}
6150

@@ -69,43 +58,40 @@ if (( do_cleanup )); then
6958
trap cleanup EXIT
7059
fi
7160

72-
this=$(realpath -- "$0"); readonly this
73-
cd "$(dirname "$this")/.." # PWD should stay in llama.cpp project directory
74-
75-
shellcheck "$this"
61+
cd .. # PWD should be llama.cpp project directory
7662

7763
readonly reqs_dir=requirements
7864

7965
if [[ ${1+x} ]]; then
8066
tmp_dir=$(realpath -- "$1")
8167
if [[ ! ( -d $tmp_dir && -w $tmp_dir ) ]]; then
82-
fatal "$tmp_dir is not a writable directory"
68+
_log_fatal "$tmp_dir is not a writable directory"
8369
fi
8470
else
8571
tmp_dir=/tmp
8672
fi
8773

8874
workdir=$(mktemp -d "$tmp_dir/check-requirements.XXXX"); readonly workdir
89-
info "Working directory: $workdir"
75+
_log_info "Working directory: $workdir"
9076

9177
check_requirements() {
9278
local reqs=$1
9379

94-
info "$reqs: beginning check"
80+
_log_info "$reqs: beginning check"
9581
pip --disable-pip-version-check install -qr "$reqs"
96-
info "$reqs: OK"
82+
_log_info "$reqs: OK"
9783
}
9884

9985
check_convert_script() {
10086
local py=$1 # e.g. ./convert-hf-to-gguf.py
10187
local pyname=${py##*/} # e.g. convert-hf-to-gguf.py
10288
pyname=${pyname%.py} # e.g. convert-hf-to-gguf
10389

104-
info "$py: beginning check"
90+
_log_info "$py: beginning check"
10591

10692
local reqs="$reqs_dir/requirements-$pyname.txt"
10793
if [[ ! -r $reqs ]]; then
108-
fatal "$py missing requirements. Expected: $reqs"
94+
_log_fatal "$py missing requirements. Expected: $reqs"
10995
fi
11096

11197
local venv="$workdir/$pyname-venv"
@@ -129,15 +115,15 @@ EOF
129115
rm -rf -- "$venv"
130116
fi
131117

132-
info "$py: imports OK"
118+
_log_info "$py: imports OK"
133119
}
134120

135121
readonly ignore_eq_eq='check_requirements: ignore "=="'
136122

137123
for req in "$reqs_dir"/*; do
138124
# Check that all sub-requirements are added to top-level requirements.txt
139125
if ! grep -qF "$req" requirements.txt; then
140-
fatal "$req needs to be added to requirements.txt"
126+
_log_fatal "$req needs to be added to requirements.txt"
141127
fi
142128

143129
# Make sure exact release versions aren't being pinned in the requirements
@@ -171,4 +157,4 @@ for py in convert-*.py; do
171157
check_convert_script "$py"
172158
done
173159

174-
info 'Done! No issues found.'
160+
_log_info 'Done! No issues found.'

scripts/lib.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
if [[ ${BASH_SOURCE[0]} -ef $0 ]]; then
4+
echo >&2 "This script should be sourced, not executed!"
5+
exit 1
6+
fi
7+
8+
_log() {
9+
local level=$1 msg=$2
10+
printf >&2 '%s: %s\n' "$level" "$msg"
11+
}
12+
13+
_log_debug() {
14+
_log DEBUG "$@"
15+
}
16+
17+
_log_info() {
18+
_log INFO "$@"
19+
}
20+
21+
_log_fatal() {
22+
_log FATAL "$@"
23+
exit 1
24+
}
25+
26+
# Return true if the variable with name $1 is set
27+
_isset() {
28+
(( $# != 1 )) && return false
29+
if [[ -n ${!1+x} ]]; then
30+
return 0
31+
else
32+
return 1
33+
fi
34+
}
35+
36+
_isnotset() {
37+
! _isset "$@"
38+
}

scripts/lib_test.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
if [[ ! ${BASH_SOURCE[0]} -ef $0 ]]; then
3+
echo >&2 "This script should be executed, not sourced!"
4+
exit 1
5+
fi
6+
#### BEGIN SETUP #####
7+
set -euo pipefail
8+
this=$(realpath -- "$0"); readonly this
9+
cd "$(dirname "$this")"
10+
shellcheck --external-sources "$this"
11+
# shellcheck source=lib.sh
12+
source 'lib.sh'
13+
#### END SETUP ####
14+
15+
pass() {
16+
local test_func="${FUNCNAME[1]}"
17+
_log 'PASSED' "$test_func"
18+
}
19+
20+
fail() {
21+
local test_func="${FUNCNAME[1]}"
22+
_log 'FAILED' "$test_func: $1"
23+
}
24+
25+
test_lib_sh_execution() {
26+
if bash lib.sh 2>/dev/null; then
27+
fail 'lib.sh should fail execution, but did not'
28+
else pass; fi
29+
}; test_lib_sh_execution
30+
31+
test_isset() {
32+
# shellcheck disable=SC2034
33+
local foo=1
34+
if ! _isset 'foo'; then
35+
fail 'foo was not detecting as set'
36+
elif _isset 'bar'; then
37+
fail 'bar was detected as set'
38+
else pass; fi
39+
}; test_isset
40+
41+
test_isnotset() {
42+
# shellcheck disable=SC2034
43+
local foo=1
44+
if _isnotset 'foo'; then
45+
fail 'foo was detected as not set'
46+
elif ! _isnotset 'bar'; then
47+
fail 'bar was detected as set'
48+
else pass; fi
49+
}; test_isnotset

0 commit comments

Comments
 (0)