-
Notifications
You must be signed in to change notification settings - Fork 7
/
pp.util
553 lines (494 loc) · 14.8 KB
/
pp.util
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#
# misc utilities
#
#@ $pp_errors: true if pp_error() has been called
pp_errors=false
#-- define some colour strings
#@ $pp_col_redfg: terminal string to change foreground colour to red
#@ $pp_col_bluefg: terminal string to change foreground colour to blue
#@ $pp_col_reset: terminal string to reset colours to default
if test -n "$TERM" -a -t 1 && (tput op) >/dev/null 2>/dev/null; then
pp_col_redfg=`tput setf 4` 2>/dev/null
pp_col_bluefg=`tput setf 1` 2>/dev/null
pp_col_reset=`tput op` 2>/dev/null
else
pp_col_redfg='['
pp_col_bluefg='['
pp_col_reset=']'
fi
#@ pp__warn(type,msg): prints a warning message including $pp_lineno if nonnull
pp__warn () {
if test x"" = x"$pp_lineno"; then
echo "$1 $2" >&2
else
echo "$1 line $pp_lineno: $2" >&2
fi
}
#@ pp_warn(msg): prints a warning message
pp_warn () {
pp__warn "pp: ${pp_col_redfg}warning${pp_col_reset}" "$*"
}
# pp_error(msg): prints an error message and sets $pp_errors to 'true'
pp_error () {
pp__warn "pp: ${pp_col_redfg}error${pp_col_reset}" "$*"
pp_errors=true
}
#@ pp_die(msg): prints an error message and dies with exit code 1
pp_die () {
pp_error "$@"
exit 1
}
#@ pp_die_if_errors(msg): dies with an error if previous errors had occurred
pp_die_if_errors () {
$pp_errors && pp_die "$@"
}
#@ pp_debug(msg): writes a message only if the --debug option was given
pp_debug () {
$pp_opt_debug && echo "${pp_col_bluefg}debug${pp_col_reset} $*" >&2
}
#@ pp_verbose(msg): executes a command, printing it if --verbose flag
pp_verbose () {
$pp_opt_verbose && echo "pp: ${pp_col_bluefg}info${pp_col_reset} $*" >&2
"$@";
}
#@ pp_substitute(): expands %{} and %() in standard input stream
# - substitutes each %{var} with shell variable $var
# - substitutes each %(cmd) with the output of `cmd`
# - otherwise passes characters unchanged.
# - assumes input does not contain ^U ^V or ^W characters
pp_substitute () {
sed -e 's,%(\([^)]*\)),`\1`,g' \
-e 's,%{\([^}]*\)},${\1},g' \
-e 's,$,,' |
tr '' '\012' |
sed -e '/^[^]/s/["$`\\]/\\&/g' \
-e 's/^//' \
-e '1s/^/echo "/' \
-e '$s,$,",' \
-e 's,,"echo ",g' |
tr -d '\012' |
tr '' '\012'
echo
}
#@ pp_incr(var): increements variable $var by one
pp_incr () {
eval "$1=\`expr \$$1 + 1\`"
}
#@ pp_decr(var): decrements variable $var by one
pp_decr () {
eval "$1=\`expr \$$1 - 1\`"
}
#@ pp_check_var_is_defined(): check the variable is set and/or non-empty
# Otherwise, generates an error, and sets the variable to "undefined"
pp_check_var_is_defined () {
if eval test -z "\"\$$1\""; then
pp_error "\$$1: not set"
eval "$1=undefined"
fi
}
#@ pp_contains(list word): true if list contains word. list is space-delimited
pp_contains () {
case " $1 " in
*" $2 "*) return 0;;
*) return 1;;
esac
}
#@ pp_contains_all(list word...): true if all words are found in the list
pp_contains_all () {
typeset _s _c
_l="$1"; shift
for _w
do
pp_contains "$_l" "$_w" || return 1
done
return 0
}
#@ pp_contains_any(list word...): true if any word is found in the list
pp_contains_any () {
typeset _s _c
_l="$1"; shift
for _w
do
pp_contains "$_l" "$_w" && return 0
done
return 1
}
#@ pp_add_to_list(list word): adds word into $list, if not there already
pp_add_to_list () {
if eval test -z \"\$$1\"; then
eval $1='"$2"'
elif eval pp_contains '"$'$1'"' '"$2"'; then
: already there
else
eval $1='"$'$1' $2"'
fi
}
#@ pp_unique(word...): removes duplicates from the input
pp_unique () {
typeset result element
result=
for element
do
pp_add_to_list result $element
done
echo $result
}
#@ pp_mode_strip_altaccess(str): given a string `ls` mode pattern, remove a
# trailing '+' or '.' if there is one.
# '+' indicates an "alternate access mode", usually an ACL.
# '.' indicates the file has an SELinux security context.
pp_mode_strip_altaccess () {
case "$1" in
??????????[+.])
echo `echo "$1" | cut -b -10`;;
*)
echo "$1";;
esac
}
#@ pp_mode_from_ls(str): converts an ls mode pattern into an octal mode number
# e.g. "-rw-rw-r--" becomes "664"
pp_mode_from_ls () {
typeset umode gmode omode smode
set -- `pp_mode_strip_altaccess "$1"`
case "$1" in
?--[-X]??????) umode=0;;
?--[xs]??????) umode=1;;
?-w[-X]??????) umode=2;;
?-w[xs]??????) umode=3;;
?r-[-X]??????) umode=4;;
?r-[xs]??????) umode=5;;
?rw[-X]??????) umode=6;;
?rw[xs]??????) umode=7;;
*) pp_error "bad user mode $1";;
esac
case "$1" in
????--[-S]???) gmode=0;;
????--[xs]???) gmode=1;;
????-w[-S]???) gmode=2;;
????-w[xs]???) gmode=3;;
????r-[-X]???) gmode=4;;
????r-[xs]???) gmode=5;;
????rw[-X]???) gmode=6;;
????rw[xs]???) gmode=7;;
*) pp_error "bad group mode $1";;
esac
case "$1" in
???????--[-T]) omode=0;;
???????--[xt]) omode=1;;
???????-w[-T]) omode=2;;
???????-w[xt]) omode=3;;
???????r-[-T]) omode=4;;
???????r-[xt]) omode=5;;
???????rw[-T]) omode=6;;
???????rw[xt]) omode=7;;
*) pp_error "bad other mode $1";;
esac
case "$1" in
???[-x]??[-x]??[-x]) smode=;;
???[-x]??[-x]??[tT]) smode=1;;
???[-x]??[Ss]??[-x]) smode=2;;
???[-x]??[Ss]??[tT]) smode=3;;
???[Ss]??[-x]??[-x]) smode=4;;
???[Ss]??[-x]??[tT]) smode=5;;
???[Ss]??[Ss]??[-x]) smode=6;;
???[Ss]??[Ss]??[tT]) smode=7;;
*) pp_error "bad set-id mode $1";;
esac
echo "$smode$umode$gmode$omode"
}
#@ pp_find_recurse($pp_destdir/dir): recursive find (don't follow symlinks)
pp_find_recurse () {
pp_debug "find: ${1#$pp_destdir}/"
for f in "$1"/.* "$1"/*; do
case "$f" in */.|*/..) continue;; esac # should never happen!
if test -d "$f" -o -f "$f" -o -h "$f"; then
if test -d "$f" -a ! -h "$f"; then
echo "${f#$pp_destdir}/"
pp_find_recurse "$f"
else
echo "${f#$pp_destdir}"
fi
fi
done
}
#@ pp_prepend(file) <text: prepend text to the beginning of the file
# if file does not exist, it is created
pp_prepend () {
#test -t && pp_warn "pp_prepend: stdin is a tty?"
if test -f $1; then
pp_debug "prepending to $1"
mv $1 $1._prepend
cat - $1._prepend >$1
rm -f $1._prepend
else
pp_debug "prepend: creating $1"
cat >$1
fi
}
#@ pp_note_file_used(path): record file used for later coverage analysis
pp_note_file_used() {
echo "$1" >> $pp_wrkdir/all.files
}
#@ pp_create_dir_if_missing(path,mode): add directory if missing
# - cleans up afterwards
pp_create_dir_if_missing () {
case "$1" in
*/) pp_error "pp_create_dir_if_missing: trailing / forbidden";;
"") return 0;;
*) if test ! -d "$pp_destdir$1"; then
pp_debug "fabricating directory $1/"
pp_create_dir_if_missing "${1%/*}"
mkdir "$pp_destdir$1" &&
pp_note_file_used "$1/"
pp_remove_later "$1" &&
chmod ${2:-755} "$pp_destdir$1"
fi;;
esac
}
#@ pp_add_file_if_missing(path,cpt,mode,flag): add file to component only if missing
# returns false if the file already exists; otherwise adds
# adds an entry to the %files.$cpt list. If the path is in a missing
# directory, that directory is also created under $pp_destdir.
# This is usually used for system scripts. The mode defaults to 755
# and the component defaults to run. The file is scheduled for removal
# after packaging is complete.
pp_add_file_if_missing () {
typeset dir
#-- check that the file isn't already declared in the component
if test -s $pp_wrkdir/%files.${2:-run}; then
awk "\$6 == \"$1\" {exit 1}" < $pp_wrkdir/%files.${2:-run} || return 1
fi
pp_create_dir_if_missing "${1%/*}"
pp_debug "fabricating file $1"
echo "f ${3:-755} - - ${4:--} $1" >> $pp_wrkdir/%files.${2:-run}
pp_note_file_used "$1"
pp_remove_later "$1"
return 0
}
#@ pp_add_transient_file(path): notify of creation of a transient file
pp_add_transient_file () {
test -f "$pp_destdir$1" && pp_die "$pp_destdir$1: exists"
pp_create_dir_if_missing "${1%/*}"
pp_debug "transient file $1"
pp_note_file_used "$1"
pp_remove_later "$1"
}
#@ pp_remove_later(path): adds file for deletion during cleanup
# the file is prepended to $pp_wrkdir/pp_cleanup
pp_remove_later () {
{
echo "$1"
test -s $pp_wrkdir/pp_cleanup && cat $pp_wrkdir/pp_cleanup
} > $pp_wrkdir/pp_cleanup.new
mv $pp_wrkdir/pp_cleanup.new $pp_wrkdir/pp_cleanup
}
#@ pp_ls_readlink(file): print the content of a symbolic link using ls
pp_ls_readlink () {
if test -h "$1"; then
ls -1ld "$1" | sed -ne 's,.* -> ,,p'
else
echo "$1: not a symbolic link" >&2
return 1
fi
}
#@ pp_remove_later_now(): removes all files registered with pp_remove_later
pp_remove_later_now () {
typeset f
if test -s $pp_wrkdir/pp_cleanup; then
pp_debug "pp_remove_later_now"
while read f; do
pp_debug "removing $pp_destdir$f"
if test -d $pp_destdir$f; then
rmdir $pp_destdir$f
else
rm $pp_destdir$f
fi
done < $pp_wrkdir/pp_cleanup
rm $pp_wrkdir/pp_cleanup
fi
}
#@ pp_readlink()
pp_readlink() {
pp_debug "&& pp_readlink_fn=$pp_readlink_fn"
if test -n "$pp_readlink_fn"; then
pp_debug "&& calling $pp_readlink_fn $*"
"$pp_readlink_fn" "$@"
else
readlink "$@"
fi
}
#@ pp_install_script_common(): generate common code in the install script
pp_install_script_common () {
cat <<-.
# Automatically generated for
# $name $version ($pp_platform)
# by PolyPackage $pp_version
usage () {
case "$1" in
"list-services")
echo "usage: \$0 list-services" ;;
"list-components")
echo "usage: \$0 list-components" ;;
"list-files")
echo "usage: \$0 list-files {cpt...|all}" ;;
"install")
echo "usage: \$0 install {cpt...|all}" ;;
"uninstall")
echo "usage: \$0 uninstall {cpt...|all}" ;;
"start")
echo "usage: \$0 start {svc...}" ;;
"stop")
echo "usage: \$0 stop {svc...}" ;;
"print-platform")
echo "usage: \$0 print-platform" ;;
*)
echo "usage: \$0 [-q] command [args]"
echo " list-services"
echo " list-components"
echo " list-files {cpt...|all}"
echo " install {cpt...|all}"
echo " uninstall {cpt...|all}"
echo " start {svc...}"
echo " stop {svc...}"
echo " print-platform"
;;
esac >&2
exit 1
}
if test x"\$1" = x"-q"; then
shift
verbose () { "\$@"; }
verbosemsg () { : ; }
else
verbose () { echo "+ \$*"; "\$@"; }
verbosemsg () { echo "\$*"; }
fi
.
}
#@ pp_functions(func...): emit platform-dependent function definitions
pp_functions () {
typeset func deps allfuncs
allfuncs=
while test $# -gt 0; do
pp_add_to_list allfuncs "$1"
deps=`pp_backend_function "$1:depends"`
shift
set -- `pp_unique "$@" $deps`
done
for func in $allfuncs
do
pp_debug "generating function code for '$1'"
echo ""
echo "$func () {"
case "$func" in
pp_mkgroup|pp_mkuser|pp_havelib) echo <<.;;
if test \$# -lt 1; then
echo "$func: not enough arguments" >&2
return 1
fi
.
esac
pp_backend_function "$func" || cat <<.
echo "$func: not implemented" >&2
return 1
.
echo "}"
done
}
#@ pp_function(func): emits a single platform-dependent function definition
pp_function () {
pp_functions "$1"
}
#@ pp_makevar(str): makes a variable name out of a string
pp_makevar () {
#-- convert all non alpha/digits to underscores
echo "$*" | tr -c '[a-z][A-Z][0-9]\012' '[_*]'
}
#@ pp_getpwuid(uid): returns username for a LOCAL uid
pp_getpwuid () {
awk -F: '$3 == uid { if (!found) print $1; found=1; } END { if (!found) exit 1; }' uid="$1" \
< /etc/passwd || pp_error "no local username for uid $1"
}
#@ pp_getgruid(gid): returns group for a LOCAL gid
pp_getgrgid () {
awk -F: '$3 == gid { if (!found) print $1; found=1; } END { if (!found) exit 1; }' gid="$1" \
< /etc/group || pp_error "no local group for gid $1"
}
#@ pp_backend_function_getopt(): emits code for pp_getopt
pp_backend_function_getopt () {
cat <<'..'
# Portable getopt; normalises command line arguments
# Usage: eval `pp_getopt abc: "$@"`
pp_getopt () {
_pp_optstring="$1"; shift; eval `_pp_getopt "$_pp_optstring"`
}
_pp_getopt_meta=s,[\\\\\"\'\`\$\&\;\(\)\{\}\#\%\ \ ],\\\\\&,g
_pp_protect () {
sed "$_pp_getopt_meta" <<. | tr '\012' ' '
$*
.
}
_pp_protect2 () {
sed "s,^..,,$pp_getopt_meta" <<. | tr '\012' ' '
$*
.
}
_pp_nonl () {
tr '\012' ' ' <<.
$*
.
}
_pp_getopt () {
_pp_nonl '_pp_nonl set --; while test $# -gt 0; do case "$1" in "--") shift; break;;'
sed 's/\([^: ]:*\)/<@<\1>@>/g;
s/<@<\(.\):>@>/"-\1") _pp_nonl -"\1"; _pp_protect "$2"; shift; shift;; "-\1"*) _pp_nonl -"\1"; _pp_protect2 "$1"; shift;;/g;s/<@<\(.\)>@>/ "-\1") _pp_nonl -"\1"; shift;; "-\1"*) _pp_nonl -"\1"; _pp_tmp="$1"; shift; set -- -`_pp_protect2 "$_pp_tmp"` "$@";;/g' <<.
$1
.
_pp_nonl '-*) echo "$1: unknown option">&2; return 1;; *) break;; esac; done; _pp_nonl --; while test $# -gt 0; do _pp_nonl "$1"; shift; done; echo'
echo
}
..
}
#@ pp_copy_unstripped(file): keep a copy of $file with the unstripped binaries
pp_copy_unstripped () {
typeset filedir realdir
filedir="`dirname ${1#$pp_destdir}`"
realdir="$pp_wrkdir/unstripped/$filedir"
mkdir -p "$realdir"
# Can't use hardlinks because `strip` modifies the original file in-place
cp "$1" "$realdir"
}
#@ pp_package_stripped_binaries(): tar.gz the unstripped directory tree
pp_package_stripped_binaries () {
(cd "$pp_wrkdir/unstripped" && tar -c .) \
| gzip > "$name-dbg-$version.tar.gz"
rm -rf "$pp_wrkdir/unstripped"
}
#@ pp_strip_binaries(): Strip all ELF binaries in $pp_destdir
pp_strip_binaries () {
if test x"$pp_opt_save_unstripped" = x"true"; then
rm -rf "$pp_wrkdir/unstripped"
mkdir "$pp_wrkdir/unstripped"
fi
for f in `find "$pp_destdir" -type f`; do
if file "$f" | awk '{print $2}' | grep ^ELF >/dev/null 2>&1; then
if test x"$pp_opt_save_unstripped" = x"true"; then
if file "$f" | LC_MESSAGES=C grep 'not stripped' >/dev/null 2>&1; then
pp_debug "Saving unstripped binary $f"
pp_copy_unstripped "$f"
else
pp_debug "$f is already stripped; not saving a copy"
fi
fi
pp_debug "Stripping unnecessary symbols from $f"
strip "$f"
fi
done
if test x"$pp_opt_save_unstripped" = x"true"; then
pp_package_stripped_binaries
fi
}
pp_is_version_greater () {
smaller_version="$(echo -e "$1\n$2" | sort -V | head -1)"
test x"$smaller_version" = x"$1"
}