-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib.bash
308 lines (284 loc) · 4.72 KB
/
lib.bash
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# BASH
agree=false
def=false
function source_conf {
if [ -f "$PP/conf.ini" ]
then
source $PP/conf.ini
fi
}
function save_conf {
OLD="$PP/conf.ini"
NEW="$PP/new.conf.ini"
echo -n "Saving conf... "
if [ -f "$NEW" ]
then
if [ -f "$OLD" ]
then
if diff "$NEW" "$OLD" >/dev/null
then
echo "nothing new"
else
mv "$NEW" "$OLD"
echo "ok"
fi
else
mv "$NEW" "$OLD"
echo "ok"
fi
else
echo "no new conf"
fi
}
function find_cluster {
for i in $(cd $PP/cluster/; ls *.bash)
do
hn=$(head -n 1 $PP/cluster/$i | sed 's/^ *# *//')
if hostname | grep "$hn" >/dev/null
then
echo $i | sed 's/.bash$//'
fi
done
}
function source_cluster {
if test -z "$CLUSTER"
then
true
elif test -f "$PP/cluster/$CLUSTER.bash"
then
if test -z "$ONLY_PRINT"
then
ONLY_PRINT=false
fi
if test -z "$RUN_WAIT"
then
RUN_WAIT=false
fi
source "$PP/cluster/$CLUSTER.bash"
else
echo "Cluster defaults '$CLUSTER' not found in cluster/:"
(cd $PP/cluster/; ls)
exit 3;
fi
}
function source_engine {
if test -z "$ONLY_PRINT"
then
ONLY_PRINT="true"
fi
BATCH=false
ENG="$1"
if test -z "$ENG"
then
ENG="$ENGINE_RUN"
fi
if test -f "$PP/engine/$ENG.bash"
then
source "$PP/engine/$ENG.bash"
else
echo "Engine '$ENG' not found in engine/:"
(cd $PP/engine/; ls)
exit 3;
fi
}
function : {
if test -z "$2"
then
var="$1"
else
var="$1_$2"
fi
echo ${!var}
}
function ask_init {
echo "# Configuration generated by config script" >$PP/new.conf.ini
}
function ask {
VAR="$1"
shift
COMM="$1"
VAL="$(: $VAR)"
ASK="$(: $VAR ASK)"
DEF="$(: $VAR DEF)"
OLDDEF="$(: $VAR OLDDEF)"
CHECK="${VAR}_CHECK"
# echo "var:$VAR | val:$VAL | ask:$ASK | def:$DEF | check:$CHECK"
if $def || test "x$DEF" != "x$OLDDEF"
then
VAL="$DEF"
fi
case "$ASK-$advanced" in
-*|advanced-true)
if $agree
then
echo "$COMM: $VAL"
else
read -e -p "$COMM: " -i "$VAL" NEW
VAL=$NEW
fi
;;
yn-*|advancedyn-true)
if ask_yn "$DEF" "$COMM"
then
VAL="y"
else
VAL="n"
fi
;;
*)
VAL="$DEF"
;;
esac
# eval "$VAR=\"$VAL\""
eval $VAR=$(printf "%q" "$VAL")
if test "x$(type -t "$CHECK")" == "xfunction"
then
if test "x$ASK" != "xfix"
then
if ! $CHECK "$VAL"
then
exit -1;
fi
fi
fi
echo >>$PP/new.conf.ini
echo "$COMM" | sed 's/^/# /' >>$PP/new.conf.ini
echo "${VAR}=$(printf "%q" "${VAL}")" >>$PP/new.conf.ini
echo "${VAR}_OLDDEF=$(printf "%q" "${DEF}")" >>$PP/new.conf.ini
}
function defgrant {
sacctmgr -p --quiet list User $USER | sed -n 2p | cut -d '|' -f 2 | tail -n1
}
function checkgrant {
[ -z "$1" ] && return 1
NL=$(sacctmgr --quiet list Account "$1" | wc -l)
[ $NL -ne 3 ] && return 1
return 0
}
function ask_yn {
DEF=$1
shift
MSG=$1
if $agree
then
echo "$MSG: $DEF"
REP="$DEF"
else
MSG="$MSG [$DEF]"
echo -n "$MSG: "
read REP
fi
case "$REP" in
y|Y|yes|Yes|YES) RET=y;;
n|N|no|NO) RET=n;;
"") RET=$DEF;;
*)
echo "Say yes or no" >&2
exit -1
esac
if test "$RET" == "y"
then
return 0;
else
return 1;
fi
}
function ask_yes {
ask_yn y "$@"
}
function ask_no {
ask_yn n "$@"
}
function def {
if test "$(: ${1} ASK)" != "no"
then
eval "${1}_DEF=\"$2\""
eval "${1}_ASK=\"$3\""
fi
}
function adv {
if test "$(: ${1} ASK)" != "no"
then
# echo "Setting $1 to advance with default $2"
eval "${1}_DEF=\"$2\""
eval "${1}_ASK=\"advanced$3\""
fi
}
function fix {
eval "${1}_DEF=\"$2\""
eval "${1}_ASK=\"no\""
}
function module_list {
for i in $@
do
case "$i" in
-*)
echo "module unload" ${i:1}
;;
purge)
echo "module purge"
;;
*)
echo "module load" $i
;;
esac
done
}
function arg {
if ! test -z "$2"
then
echo "$1" "$2"
fi
}
function check_integer {
if ! test "$1" -eq "$1" 2>/dev/null
then
echo "'$1' not an integer"
exit 2
fi
}
function display_scr {
F="$1"
shift
while ! test -z "$1"
do
echo -e "\x1B[32m# Additional command-line option:\x1B[92m $F\x1B[0m"
F="$1"
shift
done
echo
cat "$F" | sed -E -e 's/$/\x1B[0m/' -e 's/^([^#][^#]*)/\x1B[93m\1/' -e 's/(#.*)$/\x1B[32m\1/' -e 's/(#SBATCH|#PBS) /\1 \x1B[92m/'
echo
}
#SCRIPT=$(mktemp)
#function rm_script {
# if test -f "$SCRIPT"
# then
# echo "Deleting script"
# rm $SCRIPT
# fi
#}
#trap rm_script EXIT
function update_log {
LOG_FILE="$1"
if test -f "$LOG_FILE"
then
if test -z "$LOG_SKIP"
then
LOG_SKIP=0
fi
exec 3>&1
LOG_BYTES=$( dd bs=1 skip=$LOG_SKIP if="$LOG_FILE" 2>&1 1>&3 | sed -n -E -e 's/^([0123456789]*)[+]0 records out.*$/\1/p')
LOG_SKIP=$[$LOG_SKIP+$LOG_BYTES]
fi
}
function env_prepare {
echo
echo "set -e # exit on error"
if ! test -z "$ADD_PATH"
then
echo "export PATH=\"\$PATH:$ADD_PATH\""
fi
module_list $MODULES_BASE $MODULES_ADD $MODULES_RUN
echo
}