-
Notifications
You must be signed in to change notification settings - Fork 24
/
functions
executable file
·694 lines (607 loc) · 16.4 KB
/
functions
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/bin/sh
__sm.package.cli()
{
trace_filter package || set -o xtrace
typeset _token index
typeset -a _ignored_args _package_args
_package_args=( $@ )
for (( index=1 ; index <= ${#_package_args} ; index++ ))
do
_token="${_package_args[$index]}"
case "${_token}" in
(src)
src_path="${_package_args[$((++index))]}"
[[ -n "${src_path}" ]] ||
__sm.log.error "a path must follow keyword 'src'"
;;
(data)
data_path="${_package_args[$((++index))]}"
[[ -n "${data_path}" ]] ||
__sm.log.error "a path must follow keyword 'data'"
;;
(user)
package_user="${_package_args[$((++index))]}"
[[ -n "${package_user}" ]] ||
__sm.log.error "a user name must follow keyword 'user'"
;;
(version)
package_version="${_package_args[$((++index))]}"
[[ -n "${package_version}" ]] ||
__sm.log.error "a version number string must follow keyword 'version'"
;;
(base_url)
package_base_url="${_package_args[$((++index))]}"
[[ -n "${package_base_url}" ]] ||
__sm.log.error "a base path url must follow keyword 'base_url'"
;;
(file)
package_file="${_package_args[$((++index))]}"
[[ -n "${package_file}" ]] ||
__sm.log.error "a path must follow keyword 'file'"
;;
(dir)
package_directory="${_package_args[$((++index))]}"
[[ -n "${package_directory}" ]] ||
__sm.log.error "a single extracted directory name must follow keyword 'dir'"
;;
(archive_format)
archive_format="${_package_args[$((++index))]}"
[[ -n "${archive_format}" ]] ||
__sm.log.error "an archive format (tar.gz,tar.bz2,tar.xz,zip,...) must follow keyword 'archive_format'"
;;
(checksum)
package_checksum="${_package_args[$((++index))]}"
[[ -n "${package_checksum}" ]] ||
__sm.log.error "an checksum sum string must follow keyword 'checksum'. Format: '{type},{sum}'. eg. 'sha256,abcde...'"
;;
(force)
force_flag=1
;;
(static)
static_flag=1
;;
(shared)
shared_flag=1
;;
(licence)
extension_license
succeed
;;
(help)
__sm.package.usage
succeed
;;
(--configure)
_token="${_package_args[$((++index))]}"
__sm.package.configure.flags "${_token}"
;;
(--with*|--enable*|--disable*)
__sm.package.configure.flags "${_token}"
;;
(*)
_ignored_args+=("${_token}")
;;
esac
done
__sm.package.init "${_ignored_args[@]}"
}
__sm.package.init()
{
trace_filter package || set -o xtrace
typeset default_configure_flags _flag
typeset -ga configure_flags >/dev/null
true ${package_name:=${1:-${extension}}}
[[ -n "${package_name}" ]] || __sm.log.fail "Package name must be given.\n"
case "${package_name}" in
(*\=*) # exact version specifier
package_version="${package_name#*\=}"
package_name="${package_name%%\=*}"
;;
(*:*) # minimum version specifier
# TODO: concept of 'minimum' ;)
package_version="${package_name#*:}"
package_name="${package_name%%:*}"
;;
(*)
true "${package_version:="${2:-}"}" "${archive_format:="${3:-}"}"
;;
esac
true \
"${source_path:="${sm_path}/src"}" \
"${archives_path:="${sm_path}/archives"}" \
${static_flag:=0}
__sm.package.exports
# Ensure that package_version is set at this point
for variable in version base_url checksum_url user
do
if variable is empty "package_${variable}"
then
config read file defaults from ${package_name} key ${variable} into ${variable} prefix package
fi
done
for variable in website_url archive_format
do
if variable is empty "${variable}"
then
config read file defaults from ${package_name} key ${variable} into ${variable}
fi
done
for variable in package_dependencies configure_flags make_flags configure_flag_static configure_flag_shared
do
config read file defaults from ${package_name} key ${variable} into ${variable} prefix default
done
if command exists "${package_name}_initialize"
then
__sm.string.contains.word "${package_name}_initialize" "${FUNCNAME[*]}" ||
"${package_name}_initialize"
fi
__sm.package.package_dependencies ${=default_package_dependencies[*]}
__sm.package.configure.flags ${=default_configure_flags[*]}
__sm.package.make.flags ${=default_make_flags[*]}
[[ ${static_flag:-0} != 1 ]] || __sm.package.configure.flags ${default_configure_flag_static:---static}
[[ ${shared_flag:-0} != 1 ]] || __sm.package.configure.flags ${default_configure_flag_shared:---enable-shared}
true \
"${package_version:="${package_default_version}"}" \
"${package_dir:="${package_name}-${package_version}"}" \
"${init_scripts_path:="$(init_scripts_path)"}" \
"${archive_format:=tar.gz}" \
"${package_file:="${package_name}-${package_version}.${archive_format}"}" \
"${install_base_path:="${packages_path}/versions/${package_name}"}" \
"${install_path:="${install_base_path}/${package_version}"}"
__sm.package.configure.flag --prefix "${install_path}"
# This check should only happen in places that require a version...
#[[ -n "${package_version}" ]] ||
# __sm.log.fail "Package version must be specified.\n"\
# "(For example version=x.y.z set in extension's config/defaults file.)\n"
fhs_dirs=( bin etc include lib libexec sbin share man )
paths create "${active_path}"
for dir in ${fhs_dirs}
do
paths create "${active_path}/${dir}"
done
if user is root
then
true \
"${log_path:="/var/log/${package_name}"}" \
"${package_user:="${package_name}"}"
else
true \
"${log_path:="${active_path}/log"}" \
"${package_user:="$USER"}"
fi
extension_patches_path="${extension_path}/patches"
}
__sm.package.error()
{
trace_filter package || set -o xtrace
typeset _message _log _lines
_message="$1"
_log="$2"
_lines="${3:-50}"
if [[ -n ${_log} && -s "${_log}" ]]
then # Append tail of __sm.log.error log to the __sm.log.error message.
_message="${_message}\n\nTail of ${_log}:\n$( tail -n ${_lines} ${_log} )"
fi
__sm.log.error "\n${_message}"
}
__sm.package.define()
{
trace_filter package || set -o xtrace
typeset key value
while (( $# > 0 ))
do
key="$1" && shift
value="$1"
if ! shift
then
if echo "$key" | grep '='
then
__sm.log.fail "Invalid key '${key}'; Most likely the '=' is supposed to be a space?"
else
__sm.log.fail "Value not specified for key '${key}'; They should be specified in key/value pairs :)"
fi
fi
case "$key" in
(name)
package_name="${value}"
;;
(version)
package_version="${value}"
;;
(file)
package_file="${value}"
;;
(dir)
package_dir="${value}"
;;
(url)
package_url="${value}"
;;
(base_url)
package_base_url="${value}"
;;
(docs_url)
package_docs_url="${value}"
;;
(patches_url)
package_patches_url="${value}"
;;
(checksum_url)
package_checksum_url="${value}"
;;
(bin_path)
bin_path="${value}"
;;
(packages_path)
packages_path="${value}"
;;
(source_path)
source_path="${value}"
;;
(target_path)
target_path="${value}"
;;
(archive_format)
archive_format="${value}"
;;
# TODO: Simplify into
#(+([[[:alnum:]]|])_path|name|version|url|checksum_url)
# eval "${key}=${value}"
# ;;
(*)
__sm.log.fail "Unknown key '${key}' (value: ${value})."
;;
esac
done
}
__sm.package.configure.flags()
{
trace_filter package || set -o xtrace
typeset _flag
for _flag in "$@"
do
if ! __sm.string.contains.word "${_flag}" "${configure_flags[*]}"
then
configure_flags+=( "${_flag}" )
fi
done
}
__sm.package.configure.flag()
{
trace_filter package || set -o xtrace
typeset _key _value
_key="$1"
_value="$2"
if ! __sm.string.contains.key "${_key}" "${configure_flags[*]}"
then
configure_flags+=( "${_key}=${_value}" )
fi
}
__sm.package.make.flags()
{
trace_filter package || set -o xtrace
typeset _flag
for _flag in "$@"
do
if ! __sm.string.contains.word "${_flag}" "${make_flags[*]}"
then
make_flags+=( "${_flag}" )
fi
done
}
__sm.package.package_dependencies()
{
trace_filter package || set -o xtrace
typeset _flag
typeset -axg package_dependencies
for _flag in "$@"
do
if ! __sm.string.contains.word "${_flag}" "${package_dependencies[*]}"
then
package_dependencies+=( "${_flag}" )
fi
done
}
__sm.package.set.cflags()
{
trace_filter package || set -o xtrace
export CFLAGS
if
! __sm.string.contains.word "-I${active_path}/include" "${CFLAGS}"
then
CFLAGS="-I${active_path}/include${CFLAGS:+ }${CFLAGS:-}"
fi
if
! __sm.string.match " -m" " ${CFLAGS}" &&
__sm.os.type.is darwin && __sm.os.arch.type.is x86_64
then
CFLAGS="-m64${CFLAGS:+ }${CFLAGS:-}"
fi
}
__sm.package.set.cpath()
{
trace_filter package || set -o xtrace
export CPATH
if ! __sm.string.contains.word "${active_path}/include" "${CPATH:-}"
then
CPATH="${CPATH:-}${CPATH:+ }${active_path}/include"
fi
}
__sm.package.set.ldflags()
{
trace_filter package || set -o xtrace
export LDFLAGS
if __sm.paths.exists "${active_path}/lib" &&
! __sm.string.contains.word "-L${active_path}/lib" "${LDFLAGS:-}"
then
LDFLAGS="-L${active_path}/lib${LDFLAGS:+ }${LDFLAGS:-}"
fi
if __sm.paths.exists "${active_path}/lib64" &&
! __sm.string.contains.word "-L${active_path}/lib64" "${LDFLAGS:-}"
then
LDFLAGS="-L${active_path}/lib64${LDFLAGS:+ }${LDFLAGS:-}"
fi
}
__sm.package.set.cc()
{
trace_filter package || set -o xtrace
export CC
if __sm.os.type.is darwin && [[ -x /usr/bin/gcc-4.2 ]]
then CC=/usr/bin/gcc-4.2
fi
}
__sm.package.common.methods()
{
typeset method
for method in activate deactivate install update uninstall
do
__sm.package.$method()
{
trace_filter package || set -o xtrace
typeset method
method=${0##*.}
case "${package_strategy:=gnu}" in
(gnu|binary|java)
includes internal/package/strategies/${package_strategy}
__sm.package.${package_strategy}.$method "$@"
;;
*)
__sm.log.fail "Unknown package strategy ${package_strategy} for $method"
;;
esac
}
done
}
__sm.package.common.methods
__sm.package.is.active()
{
trace_filter package || set -o xtrace
typeset _name _version _path
_name="${1:-}"
_version="${2:-}"
(( $# )) && shift || __sm.log.fail "No package name was given, or name is empty."
_path="${packages_path}/versions/${_name}"
if (( $# )) && shift
then # version was passed
[[ -d "${_path}/${_version}" ]]
else # version was not passed
[[ -L "${_path}/active" && -d $(readlink "${_path}/active") ]]
fi
}
__sm.package.docs()
{
trace_filter package || set -o xtrace
__sm.os.open "${package_docs_url}"
}
__sm.package.website()
{
trace_filter package || set -o xtrace
__sm.os.open "${package_website_url}"
}
__sm.package.dependencies()
{
trace_filter package || set -o xtrace
typeset dependency _dependencies _missing_dependencies IFS
_dependencies=(${package_dependencies[@]})
_missing_dependencies=()
unset package_dependencies
# TODO: improve dependency management.
for dependency in "${_dependencies[@]}"
do
if ! __sm.package.is.active ${dependency/-/ }
then
_missing_dependencies+=( ${dependency} )
fi
done
if [[ -n "${_missing_dependencies}" ]]
then
IFS=','
__sm.log.error "${extension} requires ${_missing_dependencies[*]}.\n
Please install via sm package module, and retry. Typically, this can be done as follows:\n
\tsm ${_missing_dependencies[*]} package install\n"
fi
}
__sm.package.info()
{
trace_filter package || set -o xtrace
(( $# == 0 )) || __sm.package.cli $@
typeset _installed _path package_checksum _installed
_path="${packages_path}/versions/${package_name}"
true "${package_dir:="${package_name}-${package_version}"}" \
"${package_url:="${package_base_url}/${package_file}"}"
[[ -n "${package_checksum}" ]] || __sm.config.read "${package_name}" checksum "${package_file}" package_checksum
log "package_name: ${package_name}"
if paths exist "${_path}"
then
_installed=($( __sm.package.versions.installed ${package_name} ${package_version} ))
log " versions_installed: ${_installed[*]}"
else
log " versions_installed: none"
fi
log " default_version: ${package_default_version}"
if [[ -d "${_path}" ]] && [[ -L "${_path}/active" ]]
then
log " version_active: $(readlink "${_path}/active")"
else
log " version_active: none"
fi
log " base_url: ${package_base_url}"
log " download_url: ${package_url}"
log " archive_format: ${package_file}"
log " stored archive_checksum: ${package_checksum:-}"
log " source_path: ${source_path}/${package_dir}"
if [[ -n "${package_dependencies}" ]]
then
log " dependencies: ${package_dependencies[*]}"
fi
}
__sm.package.man()
{
trace_filter package || set -o xtrace
(( $# == 0 )) || __sm.package.cli $@
true "${package_path:="${packages_path}/versions/${package_name}/${package_version}"}"
MANPATH="${package_path}/man"
paths exist $MANPATH ||
__sm.log.error "No man page available for package ${package_name} ${package_version}"
man ${extension_actions[@]:-${package_name}} || true
}
__sm.package.versions.installed()
{
trace_filter package || set -o xtrace
versionedfs versions path "${packages_path}" name "${package_name}"
}
__sm.packages.list.installed()
{
trace_filter package || set -o xtrace
versionedfs versions path "${packages_path}"
}
__sm.package.is.installed()
{
trace_filter package || set -o xtrace
versionedfs versions path "${packages_path}" name "${package_name}" | grep "${1:-}" >/dev/null
}
__sm.packages.list.active()
{
trace_filter package || set -o xtrace
if [[ -n "$1" ]]
then
versionedfs active path "${packages_path}" name "$1"
else
versionedfs active path "${packages_path}"
fi
}
__sm.packages.available()
{
trace_filter package || set -o xtrace
__sm.extension.installed module "internal/package" "$@"
}
__sm.packages.list()
{
trace_filter package || set -o xtrace
typeset _args
__sm.packages.list.${1:-installed} "$@"
}
__sm.packages.list.known()
{
NIY "pkg list known"
}
__sm.package.usage()
{
trace_filter package || set -o xtrace
log "
Usage:
$0 [options]
options:
--prefix - specify prefix path
--src) - specify source directory
--data) - specify data directory
--user) - specify user to install as
--version) - specify version to install
--licence) - view licence
--help) - view this usage information
"
}
__sm.package.exports()
{
typeset variable
typeset -a variables
variables=(
archive_format
bin_path
configure_command
data_path
extension_patches_path
force_flag
package_base_url
package_checksum
package_checksum_url
package_dir
package_directory
package_docs_url
package_file
package_name
package_patches_url
packages_path
package_url
package_user
package_version
shared_flag
source_path
src_path
static_flag
target_path
CFLAGS
CPATH
LDFLAGS
)
for variable in ${variables[@]}
do
typeset -g ${variable}
done
typeset -ga configure_flags make_flags package_dependencies
}
__sm.package.reset()
{
typeset variable
typeset -a variables
variables=(
archive_format
bin_path
configure_command
configure_flags
data_path
force_flag
install_base_path
install_path
log_path
make_flags
package_base_url
package_checksum
package_checksum_url
package_default_version
package_dependencies
package_dir
package_directory
package_docs_url
package_file
package_name
package_patches_url
package_url
package_user
package_version
shared_flag
source_path
src_path
static_flag
target_path
website_url
CFLAGS
CPATH
LDFLAGS
)
for variable in ${variables[@]}
do
unset ${variable}
done
__sm.package.exports
}