-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.sh
executable file
·126 lines (117 loc) · 2.89 KB
/
lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
replace_config() {
sed \
-e "s|@@JOB_NAME@@|${JOB_NAME}|g" \
-e "s|@@OWNER@@|${OWNER}|g" \
-e "s|@@IAM_ROLE@@|${IAM_ROLE}|g" \
-e "s|@@IMAGE@@|${IMAGE}|g" \
-e "s|@@cmdline@@|${cmdline}|g" \
-e "s|@@GPU_NUM@@|${GPU_NUM}|g" \
-e "s|@@GPU_TYPE@@|${GPU_TYPE}|g" \
"$@"
}
check_config() {
local key
for key in $1 ; do
if test -z "${!key}" ; then
echo "Missing or empty configuration key ${key}" 1>&2
exit 1
fi
done
}
parse_args() {
local dollarzero argnames argspecs arg argspec argname argdefault argvalue ok
dollarzero="$1"
shift
argspecs="$1"
shift
argnames=
for argspec in $argspecs ; do
# append argspecs to argnames
case "$argspec" in
*=*)
# if argspec has a default value, strip the value
argnames="$argnames "$(sed -E -e 's/^([^=]*)=(.*)$/\1/g' <<<"$argspec")
;;
*)
argnames="$argnames $argspec"
;;
esac
done
n=0
while test $# -gt 0 ; do
arg=$1
shift
n=$((n+1))
# -- is used to separate additional_args provided by user; we do not touch them in this function
if test "$arg" = "--" ; then
break
fi
ok=0
if test "$arg" = "--help" || test "$arg" = "-h" ; then
echo -n "Usage: $dollarzero" 1>&2
for argname in $argnames ; do
echo -n " --$argname <$argname>" 1>&2
done
echo
exit 0
fi
# loop through all argnames until a match
for argname in $argnames ; do
if test "$arg" = "--$argname" ; then
argvalue=$1
ok=1
n=$((n+1))
shift
eval "$argname"='"$argvalue"'
break
fi
done
if test "$ok" = "0" ; then
echo "Invalid command-line argument ${arg}" 1>&2
exit 1
fi
done
# ensure all argspecs have a value - either assigned or default
for argspec in $argspecs ; do
is_empty=0 # if this argument is explicitly set to be empty, e.g. `argument=`
case "$argspec" in
*=*)
argname=$(sed -E -e 's/^([^=]*)=(.*)$/\1/g' <<<"$argspec")
argdefault=$(sed -E -e 's/^([^=]*)=(.*)$/\2/g' <<<"$argspec")
if test -z "${argdefault}" ; then
echo "--${argname} is explicitly set to be empty"
is_empty=1
fi
;;
*)
argname="$argspec"
argdefault=""
;;
esac
if test -z "${!argname}" ; then
if test -z "${argdefault}" ; then
if test "$is_empty" = "0" ; then
echo "Missing required command-line argument --${argname}" 1>&2
exit 1
fi
fi
eval "$argname"='"$argdefault"'
fi
done
}
requote() {
for arg ; do
echo -n " \""$(sed 's/["\\]/\\\0/g' <<<"$arg")"\""
done
}
get_make_dir() {
PROJECT=$1
# The convention is to have a Makefile in a PROJECT directory at the root of WORKDIR_REPO.
# For repos with exceptions, we handle them here.
if test -f workdir/${PROJECT}/Makefile ; then
echo ${PROJECT}
else
echo .
fi
}