forked from lunar-linux/lunar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepends.lunar
552 lines (474 loc) · 16.8 KB
/
depends.lunar
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
#!/bin/bash
# #
# This code is written for Lunar Linux, see #
# http://lunar-linux.org #
# #
############################################################
# #
# $FUNCTIONS/depends #
# includes find_depends is_depends in_depends #
# remove_depends add_depends run_depends #
# satisfy_depends #
# #
# 20020710 #
# #
############################################################
# #
# Copyrighted Kagan Kongar 2002 under GPLv2 #
# #
# Copyright Auke Kok 2004 under GPLv2 #
# #
############################################################
# function : find_depends
# usage : find_depends "module name"
# purpose : recursive dependency finder, no need to be installed
# NOTE: this only finds required dependencies!
function find_depends() {
local TMP_FDEPS
debug_msg "find_depends ($@)"
find_depends_intern() {
local DEP DEPS DEPFILE SECTION
SECTION=$(find_section $1)
if [[ $SECTION =~ ^zlocal ]]; then
# this module lives in zlocal
# now we need to run the depends file
# you guessed it: local decls.
depends() {
echo "$1"
}
optional_depends() {
# No quotes, this prevent us from parsing newlines
# if someone by accident add a newline in optional_depends
:
}
# yeah, this sucks:
message() {
:
}
DEPFILE=$MOONBASE/$SECTION/$1/DEPENDS
if [ -e $DEPFILE ] ; then
DEPS=$(MODULE=$1 . $DEPFILE)
fi
else
DEPS=$(awk -F: -v mod=$1 '{if ($1==mod){print $2}}' "$DEPENDS_CACHE")
fi
for DEP in $DEPS ; do
DEP=$(expand_alias "$DEP")
# this is our shortcut out:
if ! grep -qx "$DEP" "$TMP_FDEPS" ; then
echo "$DEP" >> $TMP_FDEPS
if grep -q "^$1:$DEP:required:" "$DEPENDS_CACHE" ; then
echo "$DEP"
find_depends_intern "$DEP"
fi
fi
done
}
TMP_FDEPS=$(temp_create "found.depends")
find_depends_intern $(expand_alias $1)
temp_destroy $TMP_FDEPS
}
# function : sort_by_dependency
# usage : LIST=$(sort_by_dependency $LIST)
# purpose : return a LIST sorted by dependency
sort_by_dependency() {
local TMP_LIST TMP_ALL TMP_TSRT A B
debug_msg "sort_by_dependency ($@)"
TMP_LIST=$(temp_create "deptree.in")
TMP_ALL=$(temp_create "deptree.all")
for M in $* ; do
echo "$M" >> $TMP_LIST
done
# tsort the existing dep relations in all of moonbase
TMP_TSRT=$(temp_create "dependency.sort")
awk -F: '{print $1,$2}' "$DEPENDS_CACHE" | while read A B ; do
B=$(MODULE=$A NEVER_ASK=1 DEPS_ONLY= expand_alias $B)
echo "$A $B" >> $TMP_TSRT
done
tsort "$TMP_TSRT" 2> /dev/null | tac > $TMP_ALL
temp_destroy $TMP_TSRT
# append all modules that do not have a DEPENDS file at all at the end
# note that this is logically WRONG, but it actually will help with
# unincluded depends, and therefore is *better* behaviour
for M in $* ; do
if ! grep -q -x "$M" "$TMP_ALL" ; then
echo $M >> $TMP_ALL
fi
done
# now reverse grep over the files:
cat "$TMP_ALL" | grep -x -f "$TMP_LIST"
temp_destroy $TMP_LIST
temp_destroy $TMP_ALL
}
is_depends() {
debug_msg "is_depends ($@)"
# Is $1 a previously selected dependency of any module.
return $(grep -q ":$1:on:" "$DEPENDS_STATUS")
}
in_depends() {
local DEP
debug_msg "in_depends ($@)"
DEP=$(expand_alias $2)
# Was $2 presented as a dependency for module $1
return $(grep -q "^$1:$DEP:on:" "$DEPENDS_STATUS")
}
remove_depends() {
debug_msg "remove_depends ($@)"
lock_file $DEPENDS_STATUS_BACKUP &&
lock_file $DEPENDS_STATUS ||
return 1
if [ -n "$2" ] ; then
if grep -q "^$1:$2:" "$DEPENDS_STATUS" ; then
grep -v "^$1:$2:" $DEPENDS_STATUS_BACKUP > $DEPENDS_STATUS
verbose_msg "removing depends for \"$1\" on \"$2\""
fi
elif grep -q "^$1:" "$DEPENDS_STATUS" ; then
grep -v "^$1:" "$DEPENDS_STATUS_BACKUP" | \
grep -v ":$1:on:optional:" > $DEPENDS_STATUS
verbose_msg "removing all depends for and optional on \"$1\""
fi
cp $DEPENDS_STATUS $DEPENDS_STATUS_BACKUP
unlock_file $DEPENDS_STATUS &&
unlock_file $DEPENDS_STATUS_BACKUP
}
add_depends() {
debug_msg "add_depends ($@)"
if ! grep -q "^$1:$2:$3:$4:$5:$6$" "$DEPENDS_STATUS" ; then
lock_file $DEPENDS_STATUS_BACKUP &&
lock_file $DEPENDS_STATUS &&
if grep -q "^$1:$2:" "$DEPENDS_STATUS" ; then
grep -v "^$1:$2:" "$DEPENDS_STATUS_BACKUP" > $DEPENDS_STATUS
else
verbose_msg "adding \"$4\" depends for \"$1\" on \"$2\" ($3)"
fi
echo "$1:$2:$3:$4:$5:$6" >> $DEPENDS_STATUS &&
cp $DEPENDS_STATUS $DEPENDS_STATUS_BACKUP &&
unlock_file $DEPENDS_STATUS &&
unlock_file $DEPENDS_STATUS_BACKUP
fi
}
run_depends() {
debug_msg "run_depends ($@)"
# local definitions of depends and optional_depends!
depends() {
local DEP
debug_msg " depends ($@)"
DEP=$(expand_alias $1)
add_depends "$MODULE" "$DEP" "on" "required" "$2" "$3"
if ! module_installed $DEP ; then
if module_exiled $DEP ; then
message "${MODULE_COLOR}${MODULE}:${PROBLEM_COLOR} ! Error: required dependency ${MODULE_COLOR}$DEP${DEFAULT_COLOR}${PROBLEM_COLOR} is exiled and cannot be installed${DEFAULT_COLOR}"
return 1
elif ! in_depends $MODULE $DEP ; then
message "${MODULE_COLOR}${MODULE}:${DEFAULT_COLOR}" \
"${MESSAGE_COLOR}Adding required dependency${DEFAULT_COLOR}" \
"${MODULE_COLOR}$DEP${DEFAULT_COLOR}"
fi
# don't check depends if there are already checked
if grep -q "^$DEP\$" "$TEMP_PREPAREDDEPS" 2>/dev/null ; then
return 0
fi
lin --deps $DEP
echo "$DEP" >> $TEMP_PREPAREDDEPS
fi
}
optional_depends() {
debug_msg " optional_depends ($@)"
local DEP DEFAULT DEPMOD_AVAIL
# parameters: $1 = module name
# $2 = configure parameter if module wanted
# $3 = configure parameter if module declined
# $4 = description of why to use this module
# $5 = optional default choice (recommended)
DEP=$(expand_alias $1)
if in_depends $MODULE $DEP ; then
if ! module_installed $DEP ; then
# don't check depends if there are already checked
if grep -q "^$DEP\$" "$TEMP_PREPAREDDEPS" 2>/dev/null ; then
return 0
fi
lin --deps $DEP
echo "$DEP" >> $TEMP_PREPAREDDEPS
fi
elif ! grep -q "^$MODULE:$DEP:" "$DEPENDS_STATUS" ; then
if module_exiled $DEP ; then
verbose_msg "\"$MODULE\" optionally depends on exiled module \"$DEP\""
add_depends "$MODULE" "$DEP" "off" "optional" "$2" "$3"
else
if module_installed $DEP ; then
DEFAULT="${5:-y}"
DEPMOD_AVAIL="Use optional (${FILE_COLOR}installed${QUERY_COLOR}) module ${MODULE_COLOR}$DEP${DEFAULT_COLOR}${MESSAGE_COLOR}"
else
DEFAULT="${5:-n}"
DEPMOD_AVAIL="Install & use optional (${PROBLEM_COLOR}not installed${QUERY_COLOR}) module ${MODULE_COLOR}$DEP${DEFAULT_COLOR}${MESSAGE_COLOR}"
fi
# this is shortcut case s1) as discussed in lunar-dev ML
if module_enforced $DEP ; then
verbose_msg "Enforcing optional dependency \"$DEP\""
add_depends "$MODULE" "$DEP" "on" "optional" "$2" "$3"
# don't check depends if there are already checked
if grep -q "^$DEP\$" "$TEMP_PREPAREDDEPS" 2>/dev/null ; then
return 0
fi
lin --deps $DEP
echo "$DEP" >> $TEMP_PREPAREDDEPS
elif module_installed $DEP && [ -z "$2" -a -z "$3" ] ; then
verbose_msg "Auto-adding optional dependency \"$DEP\""
add_depends "$MODULE" "$DEP" "on" "optional" "$2" "$3"
# don't check depends if there are already checked
if grep -q "^$DEP\$" "$TEMP_PREPAREDDEPS" 2>/dev/null ; then
return 0
fi
lin --deps $DEP
echo "$DEP" >> $TEMP_PREPAREDDEPS
elif query "$DEPMOD_AVAIL
${QUERY_COLOR}Purpose: ${DEFAULT_COLOR}${MESSAGE_COLOR}$4 ${QUERY_COLOR}?" $DEFAULT ; then
add_depends "$MODULE" "$DEP" "on" "optional" "$2" "$3"
# don't check depends if there are already checked
if grep -q "^$DEP\$" "$TEMP_PREPAREDDEPS" 2>/dev/null ; then
return 0
fi
lin --deps $DEP
echo "$DEP" >> $TEMP_PREPAREDDEPS
else
add_depends "$MODULE" "$DEP" "off" "optional" "$2" "$3"
fi
fi
fi
}
grep -q "^"$MODULE"\$" "$TEMP_PREPAREDDEPS" 2>/dev/null && return 0
if has_module_file $MODULE DEPENDS ; then
if [ -n "$SINGLE_MODULE" ] ; then
# we only need to show this once, but we get here twice per module
message "${CHECK_COLOR}Checking dependencies for" \
"${MODULE_COLOR}${MODULE}" \
"${DEFAULT_COLOR}"
fi
run_module_file $MODULE DEPENDS
fi
}
# function : satisfy_depends
# usage : satisfy_depends (operates on $MODULE variable)
# purpose : re-check module deps based on DEPENDS_CONFIG and DEPENDS_STATUS
# NOTE: this is where a missing dependency gets installed! IOW we really do
# "satisfy" any dependencies here!
satisfy_depends() {
local TMP_FILE DEP_MODULE DEP_STATUS DEP_ON DEP_OFF LINE
debug_msg "satisfy_depends ($@)"
if [ -n "$DEPS_ONLY" ] ; then
return 0
fi
# make sure we start with clean OPTS here:
unset OPTS
if [ -s "$MODULE_CONFIG" ] ; then
. $MODULE_CONFIG
fi
TMP_FILE=$(temp_create "${MODULE}.satify-depends")
grep "^$MODULE:" "$DEPENDS_STATUS" > $TMP_FILE
# first recursively check if all required dependencies are installed
for DEP_MODULE in $(find_depends $MODULE | grep -v '%') ; do
if [ $DEP_MODULE != $MODULE ]; then
if ! module_installed $DEP_MODULE ; then
if module_exiled $DEP_MODULE ; then
message "${MODULE_COLOR}${MODULE}:${PROBLEM_COLOR} ! Error: required dependency ${MODULE_COLOR}$DEP_MODULE${DEFAULT_COLOR}${PROBLEM_COLOR} is exiled and cannot be installed${DEFAULT_COLOR}"
exit 1
elif ! SINGLE_MODULE=1 lin $DOWNLOAD_ONLY $SILENT $COMPILE $DEP_MODULE ; then
exit 1
fi
fi
fi
done
# then parse $OPTS
while read -u3 LINE; do
DEP_MODULE=$(echo $LINE | cut -d: -f2)
DEP_STATUS=$(echo $LINE | cut -d: -f3)
DEP_ON=$(echo $LINE | cut -d: -f5)
DEP_OFF=$(echo $LINE | cut -d: -f6)
if [ -n "$FIX" ] ; then
if [ $DEP_STATUS == "on" ] ; then
lin $FIX $DEP_MODULE
fi
elif [ "$DEP_STATUS" == "off" ] || module_exiled $DEP_MODULE ; then
OPTS="$OPTS $DEP_OFF"
elif [ "$DEP_STATUS" == "on" ] && ! module_installed $DEP_MODULE ; then
if SINGLE_MODULE=1 lin $DOWNLOAD_ONLY $SILENT $COMPILE $DEP_MODULE ; then
OPTS="$OPTS $DEP_ON"
else
exit 1
fi
else
OPTS="$OPTS $DEP_ON"
fi
done 3< $TMP_FILE
temp_destroy $TMP_FILE
}
# conflicts... remove conflicting modules
conflicts() {
local DEP
debug_msg "conflicts ($@)"
DEP=$1
if module_installed $DEP ; then
message "${PROBLEM_COLOR}WARNING: ${DEFAULT_COLOR}${MODULE_COLOR}$DEP${DEFAULT_COLOR}${PROBLEM_COLOR} conflicts with module ${MODULE_COLOR}$MODULE${DEFAULT_COLOR}"
if query "Do you want to remove module ${DEFAULT_COLOR}${MODULE_COLOR}$DEP${DEFAULT_COLOR}${QUERY_COLOR} ? " n ; then
lrm $DEP
else
message "${PROBLEM_COLOR}ERROR: Cannot continue installing conflicting modules, exiting!${DEFAULT_COLOR}"
exit 1
fi
fi
true
}
run_conflicts() {
debug_msg "run_conflicts ($@)"
if has_module_file $MODULE CONFLICTS ; then
run_module_file $MODULE CONFLICTS
fi
}
build_module_depends() {
debug_msg "build_module_depends ($@)"
if [ -n "$DEPS_ONLY" ] ; then
(
if ! run_conflicts $1 ; then
exit 1
fi &&
run_details $1 &&
run_configure &&
RECONFIGURE= run_depends
)
else
if ! run_conflicts $1 ; then
exit 1
fi &&
run_details $1 &&
RECONFIGURE= run_depends &&
RECONFIGURE= satisfy_depends
fi
}
build_depends() {
debug_msg "build_depends ($@)"
for MODULE in $@ ; do
if ! module_installed $MODULE || [ ! -n "$PROBE" ] ; then
if ! module_held $MODULE ; then
build_module_depends $MODULE
else
verbose_msg "Skipping dependency checking of held module \"$MODULE\""
fi
fi
done
}
create_depends_cache()
{(
local TMP_DEP_CACHE DEPFILES DEPFILE MODULE
debug_msg "create_depends_cache($@)"
if [[ ! -w $DEPENDS_CACHE && -e $DEPENDS_CACHE ]] ; then
return 1
fi
if [ $MODULE_INDEX -nt $DEPENDS_CACHE ] ; then
verbose_msg "Generating a new depends cache..."
TMP_DEP_CACHE=$(temp_create "depends.cache")
# you guessed it: local decls.
depends() {
echo "$MODULE:$1:required:$2:$3:"
}
optional_depends() {
# No quotes, this prevent us from parsing newlines
# if someone by accident add a newline in optional_depends
echo $MODULE:$1:optional:$2:$3:$4
}
# yeah, this sucks:
message() {
:
}
# speedups for system moonbases:
if [ "$MOONBASE" == "/var/lib/lunar/moonbase" ] && [ -f "$INSTALL_LOGS/moonbase-$(installed_version moonbase)" ] ; then
DEPFILES=$(grep "/DEPENDS$" "$INSTALL_LOGS/moonbase-$(installed_version moonbase)")
# don't forget zlocal:
if [ "$ZLOCAL_OVERRIDES" == "on" ] ; then
DEPFILES="$DEPFILES $(find $MOONBASE/zlocal -type f -name DEPENDS)"
fi
else
if [ "$ZLOCAL_OVERRIDES" != "on" ] ; then
DEPFILES=$(find $MOONBASE -type f -name DEPENDS ! -regex "$MOONBASE/zlocal/.*")
else
DEPFILES=$(find $MOONBASE -type f -name DEPENDS)
fi
fi
# fast method for re-creating the depends.cache, might take long though
for DEPFILE in $DEPFILES ; do
TMP=${DEPFILE%/*} MODULE=${TMP##*/} . $DEPFILE
done > $TMP_DEP_CACHE &&
install -m644 $TMP_DEP_CACHE $DEPENDS_CACHE
temp_destroy $TMP_DEP_CACHE
else
verbose_msg "Skipping depends cache regeneration"
fi
)}
update_module_depends()
{
local MODULE
debug_msg "update_module_depends($@)"
# we declare these local to override the systems default ones:
optional_depends() {
local DEP
debug_msg " optional_depends ($@)"
DEP=$(NEVER_ASK=1 DEPS_ONLY= expand_alias $1)
# check for the current depend selection
if grep -q "^$MODULE:$DEP:on:optional:" "$DEPENDS_STATUS_BACKUP" ; then
echo "$MODULE:$DEP:on:optional:$2:$3"
elif grep -q "^$MODULE:$DEP:off:" "$DEPENDS_STATUS_BACKUP" ; then
echo "$MODULE:$DEP:off:optional:$2:$3"
fi
# if we don't know the answer we leave it open for the user to decide
}
depends() {
local DEP
debug_msg " depends ($@)"
DEP=$(NEVER_ASK=1 DEPS_ONLY= expand_alias $1)
echo "$MODULE:$DEP:on:required:$2:$3"
}
run_depends() {
local SECTION SCRIPT_DIRECTORY MODULE
debug_msg " run_depends ($@)"
(
MODULE=$1
SECTION=$(find_section $MODULE)
SCRIPT_DIRECTORY=$MOONBASE/$SECTION/$MODULE
if has_module_file $MODULE DEPENDS ; then
run_module_file $MODULE DEPENDS | grep -v '%'
fi
)
}
# here starts the real work:
MODULE=$1
# remove whatever depends was residing in the depends state file and
# append the new output:
lock_file $DEPENDS_STATUS_BACKUP &&
lock_file $DEPENDS_STATUS &&
(
grep -v "^$MODULE:" "$DEPENDS_STATUS_BACKUP" > $DEPENDS_STATUS
run_depends $MODULE >> $DEPENDS_STATUS
cp $DEPENDS_STATUS $DEPENDS_STATUS_BACKUP
)
unlock_file $DEPENDS_STATUS
unlock_file $DEPENDS_STATUS_BACKUP
}
# updates the depends file for the changed module depends
update_depends()
{
local MODULE
debug_msg "update_depends($@)"
for MODULE in $(cut -d: -f1 $DEPENDS_STATUS | sort -u) ; do
update_module_depends $MODULE
done
}
# list all installed modules depending on $1
list_installed_depending() {
local SUBDEP
debug_msg "list_installed_depending($2)"
for SUBDEP in $(grep ":$1:" "$DEPENDS_CACHE" | cut -d: -f1) ; do
if $(module_installed $SUBDEP ) ; then
list_installed_depending $SUBDEP | sort | uniq
echo $SUBDEP
fi
done
}