forked from freebsd/portmaster
-
Notifications
You must be signed in to change notification settings - Fork 1
/
portmaster.subr
972 lines (909 loc) · 24.6 KB
/
portmaster.subr
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
#!/bin/sh
# external tools manipulation
: "${PKG:="pkg"}"
# common useful definitions
PM_TRUE=0
PM_FALSE=1
########### PMLIST functions ###########
PMLIST_FS=':'
PMLIST_NAME=pmlist_list
pmlist_list="$PMLIST_FS"
# $1 the element to be found
# $2 the list
pmlist_is_present() {
local _list
[ -z "$1" ] && return $PM_TRUE
[ -z "$2" ] && return $PM_FALSE
_list="$2"
case $_list in
*:$1:*|$1:*|*:${1}|$1) return $PM_TRUE ;;
esac
return $PM_FALSE
}
# the merge function appends new elements of the second list at the end of the first list
# $1 the first list
# $2 the second list
pmlist_merge() {
local _merge _e
_merge=$1
[ -z "$2" ] && echo $_merge && return
for _e in $(echo $2 | tr ':' ' ') ; do
if ! pmlist_is_present $_e $_merge ; then
_merge=${_merge}':'$_e
fi
done
echo $_merge
}
# it gets the first element of the list
# $1 the list
pmlist_pop() {
local _list _e
_list=$1
# removing the heading and the trailing separator
_list="${_list%':'}"
_list="${_list#':'}"
[ -z "$_list" ] && return
_e=${_list%%':'*}
echo $_e
}
# it the the list without the first element
# $1 the list
pmlist_pop_list() {
local _list _e
_list=$2
_e=$1
[ -z "$_e" ] && echo $_list
_list="${_list#':'}"
_list="${_list#$_e}"
_list="${_list#':'}"
echo $_list
}
# it performs the intersection between two list
# in other words, it returns the list of all common elements
# $1 the first list
# $2 the second list
pmlist_intersect() {
local _forlist _retlist
_retlist=""
_forlist="$(echo $1 | tr ':' ' ')"
for e in $_forlist ; do
if pmlist_is_present $e $2 ; then
_retlist=$_retlist:$e
fi
done
echo $_retlist
}
# log utility function via logger (syslog)
# syslog facility and tag are configured globally
# log level supported are:
# * PMLOG_DEBUG (performed if PM_DEBUG is true)
# * PMLOG_INFO
# * PMLOG_WARN (messages are printed on the console as well)
# * PMLOG_ERR (messages are printed on the console as well)
# $1 the log level; if not specified, PMLOG_INFO is used as default
PMLOG_LEVEL="info"
PMLOG_FACILITY="local5"
PMLOG_TAG="portmaster"
pmlog () {
local level _tag _opt
level=${PMLOG_LEVEL}
_tag=${PMLOG_TAG}
case $1 in
PMLOG_DEBUG)
[ "$PM_DEBUG" = "$PM_TRUE" ] && return
level=debug
_tag="${PMLOG_TAG}(debug)"
shift ;;
PMLOG_WARN)
level=warn
_tag="${PMLOG_TAG}(warn)"
_opt="-s"
shift ;;
PMLOG_ERR)
level=err
_tag="${PMLOG_TAG}(ERROR)"
_opt="-s"
shift ;;
PMLOG_INFO)
level=info
shift ;;
esac
logger $_opt -p ${PMLOG_FACILITY}.${level} -t ${_tag} $@
}
# true if the current shell is the parent shell
pm_is_parent() {
if [ "$$" -eq "$PM_PARENT_PID" ]; then
return $PM_TRUE
else
return $PM_FALSE
fi
}
# true if the current shell runs with superuser priviligese
pm_has_su_privileges() {
if [ $(id -u) -eq 0 ]; then
return $PM_TRUE
else
return $PM_FALSE
fi
}
pm_wrap_make() {
( make $* )
}
# flags:
# ARR - prepone an arrow '===>>> '
# VERB - print iff VEROBSE is active
# DIND - double indent AKA '\t\t'
pm_print() {
local _prefix _vonly
_vonly=$PM_FALSE
while true; do
case "$1" in
ARR) _prefix='===>>> '
shift ;;
VERB) _vonly=$PM_TRUE
shift ;;
DIND) _prefix='\t\t'
shift ;;
*) break ;;
esac
done
if [ -z "$PM_VERBOSE" -a $_vonly -eq $PM_TRUE ]; then
return
fi
builtin echo -e ${_prefix}$*
}
# convenient wrapper
pm_print_arr() {
pm_print ARR $*
}
pm_print_verb() {
pm_print VERB $*
}
# flags:
# PM_TERM_TITLE_INIT : initialize the title using the argument $1
pm_term_title_print() {
[ -n "$PM_NO_TERM_TITLE" ] && return
case "$TERM" in cons*) return ;; esac
if [ "$1" = "PM_TERM_TITLE_INIT" ]; then
shift
PM_TERM_PARENT_PORT=$1
shift
fi
printf "\033]0;portmaster: ${PM_TERM_PARENT_PORT}${1}\077"
}
# ask the user to type y or n and return properly
# if the user type y or PM_YES, PM_TRUE is returned
# if the user type n or PM_NO, PM_FALSE is returned
# $1 is the default value (affects the output only)
# $2 the message
pm_get_yesno() {
local default answer
default=$1 ; shift
[ -n "${PM_YES}" ] && return $PM_TRUE
[ -n "${PM_NO}" ] && return $PM_FALSE
while true ; do
echo -e "$* [${default}] \c"
read answer
echo
case "$answer" in
[Nn])
return $PM_FALSE ;;
[Yy])
return $PM_TRUE ;;
'')
case "$default" in
[Nn]) return $PM_FALSE ;;
[Yy]) return $PM_TRUE ;;
esac
;;
*)
echo -e "\t===>>> $answer is not a valid answer"
esac
done
}
### temp_file framework ###
# create a temporary file
# $1 a string that identifies the scope
pm_mktemp_file() {
local _temp
_temp=$(/usr/bin/mktemp -t pm-${PM_PARENT_PID}-$1 2>&1) ||
fail "mktemp for $1 failed: \n\t${_temp#mktemp: }"
echo $_temp
PM_TEMP_FILE_LIST="${PM_TEMP_FILE_LIST}${_temp} "
}
# temporary files cleaner
# this function should be invoked by pm_trap_exit
pm_mktemp_cleanup() {
local _f
if [ -z "${PM_TEMP_FILE_LIST}" ]; then
return # nothing to do!
fi
for _f in ${PM_TEMP_FILE_LIST} ; do
rm -f $_f
done
}
# removing trailing glob chars
pm_globstrip () {
local in
in=${1%[*]}
in=${in%\\}
echo $in
}
# initialize global variables | TODO to be adopted
# it substitute the init_packages_var
pm_init_global_vars () {
if [ -z "$pd" ]; then
if [ -z "$PORTSDIR" ]; then
pd=$(pm_make_b -f/usr/share/mk/bsd.port.mk -V PORTSDIR 2>/dev/null)
[ -z "$pd" ] && [ -d /usr/ports ] && pd=/usr/ports
[ -z "$pd" ] && fail "Not able to find any ports directory"
else
pd=$PORTSDIR
fi
[ ! -r "${pd}/Mk/bsd.port.mk" ] &&
fail "The ports directory ($pd) does not seem to contain a ports tree"
export pd
fi
if [ -z "$pdb" ]; then
if [ -z "$PKG_DBDIR" ]; then
pdb=$(pm_make -f/usr/share/mk/bsd.port.mk -V PKG_DBDIR 2>/dev/null)
[ -z "$pdb" ] && [ -d /var/db/pkg ] && pdb=/var/db/pkg
[ -z "$pdb" ] && fail 'The value of PKG_DBDIR cannot be empty'
else
pdb=$PKG_DBDIR
fi
[ ! -d "$pdb" ] && fail "The pkg dbdir $pdb does not seems correct"
export pdb
fi
if [ -z "$port_dbdir" ]; then
port_dbdir=$(pm_make_b -f/usr/share/mk/bsd.port.mk -V PORT_DBDIR 2>/dev/null)
[ -z "$port_dbdir" ] && [ -d /var/db/ports ] && port_dbdir=/var/db/ports
[ -z "$port_dbdir" ] && fail 'The value of PORT_DBDIR cannot be empty'
[ ! -d "$port_dbdir" ] && fail "The port dbdir $port_dbdir is not a directory"
export port_dbdir
fi
# TODO: it should be performed if the PACKAGES variable is needed
if [ "$1" != "NO_PACKAGES" ] ; then
if [ -z "$PACKAGES" ]; then
PACKAGES=$(pm_make -f/usr/share/mk/bsd.port.mk -V PACKAGES 2>/dev/null)
[ -z "$PACKAGES" ] && [ -d $pd/packages ] && PACKAGES=$pd/packages
# we should relax this check
[ ! -w $PACKAGES ] &&
fail 'PACKAGES cannot be empty and the directory must be writable'
export PACKAGES
fi
fi
if [ -z "$LOCALBASE" ]; then
LOCALBASE=$(pm_wrap_make -f/usr/share/mk/bsd.port.mk -V LOCALBASE 2>/dev/null)
if [ -z "$LOCALBASE" ]; then
pm_print_arr "Unable to determine the value of LOCALBASE"
fail 'the value of LOCALBASE cannot be empty'
fi
fi
LOCALBASE_COMPAT=${LOCALBASE}/lib/compat/pkg
export LOCALBASE_COMPAT
}
# Takes a pkg pattern as input( bash, perl, ...) ; return values
# 0 - Matched one and only one package
# 1 - No match
# 2 - Matched multiple packages
# The variabl glob_pkgs will contain the package names
pm_find_glob_pkgs () {
# Global glob_pkgs
local pattern num
pattern="$(pm_globstrip $1)"
num=$(pkg info -g ${pattern}\* | wc -l | tr -d " ")
glob_pkgs="$(pkg query -g "%n-%v" ${pattern}\*)"
case $num in
0) unset glob_pkgs; return 1 ;;
1) return 0 ;;
*) return 2 ;;
esac
}
# Validate an origin: the directory and its Makefile has to exist
# $1 : the origin to validate
pm_is_valid_origin () {
local _pkg
[ -z "$1" ] && return $PM_FALSE
_pkg="$1"
if [ -d $pd/$_pkg -a -r $pd/$_pkg/Makefile ]; then
return $PM_TRUE
fi
return $PM_FALSE
}
# get the origin from a pkg name [name-version]; return values
# 0 - package found
# 1 - package not found
# 2 - package ignored using as +IGNOREME
# (pdb has to be correctly initialized)
# 3 - bsdpan package (a relics)
pm_get_origin () {
if [ -e "$pdb/$1/+IGNOREME" ]; then
pm_print_arr "$pdb/$1/+IGNOREME exists" >&2
return 2
fi
pkg query '%o' $1 2>/dev/null && return 0
case "$1" in bsdpan-*) return 3 ;; esac
return 1
}
# former iport_from_origin | TODO to be adopted | TODO to be tested
# get the installed pkg name [name-version] from an origin; it echos the installed version
# return values
# 0 - package found
# 1 - package not found
# 2 - multiple packages [TODO: check with subpackages and flavor]
pm_get_inst_pkgname () {
local num rc
num=$( pkg query -e "%o = ${1}" '%n-%v' | wc -l | tr -d " " )
case $num in
0) return 1 ;;
1) rc=0 ;;
*) rc=2 ;;
esac
pkg query -e "%o = ${1}" '%n-%v'
return $rc
}
# look for moved ports origin
# get the new origin or the reason why it's deleted
# reqs: $pd
# return values
# 0 - origin not found
# 1 - ports moved to another origin
# 2 - ports removed
pm_find_moved_origin () {
local origin newOrigin reason
origin=$1
if [ -n "$(grep "^${origin}||" $pd/MOVED)" ]; then
# ports removed
reason=$(grep "^${origin}||" $pd/MOVED | sed "s%^.*||.*|%%")
echo $reason
return 2
fi
if [ "$(grep -c "^${origin}|" $pd/MOVED)" -eq 1 ]; then
# ports moved to another origin
newOrigin=$(grep "^${origin}|" $pd/MOVED | sed "s%^${origin}|%%")
reason=$(echo $newOrigin | sed "s%.*|.*|%%")
origin=${newOrigin%%|*}
newOrigin=$(pm_find_moved_origin $origin)
case $? in
0) echo $origin
return 1 ;;
1) echo $newOrigin
return 1 ;;
2) echo $newOrigin
return 2 ;;
esac
fi
return 0
}
# get the origin from a pkg name [name-version] with MOVED support
# return values:
# 0 - package found
# 1 - package not found
# 2 - package ignored using as +IGNOREME
# (pdb has to be correctly initialized)
# 3 - bsdpan package (a relics)
# 4 - origin not valid (not found)
# 5 - origin (pkg) removed
pm_get_moved_origin() {
local origin
origin=$(pm_get_origin $1)
case $? in
3) return 3 ;;
2) return 2 ;;
1) return 1 ;;
esac
if [ -d $pd/$origin ]; then
echo $origin
return 0;
fi
origin=$(pm_find_moved_origin $origin)
case $? in
0) return 4 ;;
2) return 5 ;;
esac
echo $origin
return 0
}
# get the distfiles from a pkg name, using the portstree
# reqs: $pd
pm_get_distfiles () {
local origin distfiles distsubdir
origin="$(pm_get_origin $1)"
if [ ! -d $pd/$origin ]; then
# check the moved ports
origin=$(pm_find_moved_origin $origin)
case $? in
0|2) return ;;
esac
fi
distsubdir=$(make -C $pd/$origin -V DIST_SUBDIR)
for df in $(make -C $pd/$origin -V ALLFILES) ; do
if [ -z "$distsubdir" ]; then
echo $df
else
echo $distsubdir/$df
fi
done
}
# get ports lists, per category: roots, trunks, branches, leaves
# exports: roots, trunks, branches, leaves
# exports: num_roots, num_trunks, num_branches, num_leaves, num_ports
pm_ports_by_category () {
roots=$( pkg query -e "%#d = 0 && %#r = 0" "%n-%v")
trunks=$( pkg query -e "%#d = 0 && %#r > 0" "%n-%v")
branches=$( pkg query -e "%#d > 0 && %#r > 0" "%n-%v")
leaves=$( pkg query -e "%#d > 0 && %#r = 0" "%n-%v")
num_roots=$( echo $roots | wc -w | tr -d " ")
num_trunks=$( echo $trunks | wc -w | tr -d " ")
num_branches=$( echo $branches | wc -w | tr -d " ")
num_leaves=$( echo $leaves | wc -w | tr -d " ")
num_ports=$( pkg query -a "%n-%v" | wc -w | tr -d " ")
}
# it get the sanity state of the current port
# the state can be FORBIDDEN BORKEN IGNORE
# the state is empty if the port is OK
# $1 empty or a valid origin to cd into
pm_get_port_sanity_state () {
local _state _cwd _origin
_cwd=$(pwd)
_origin=$1
[ -n "$_origin" ] && cd $pd/$_origin
_state=$( egrep '^(FORBIDDEN|BROKEN|IGNORE)=' Makefile | head -n 1 | sed 's/=.*$//' )
if [ -n "$_state" ]; then
if [ -n "$(pm_wrap_make -V $_state)" ]; then
echo "$_state"
return
fi
fi
[ -n "$_origin" ] && cd $_cwd
}
# it checks if the argument (pkg or origin) match the exclude list
# the exclude list is stored in PM_EXCL
# return values:
# PM_TRUE if the argument is OK
# PM_FALSE if the argument has to be excluded
pm_check_for_exclude () {
local _pattern
[ -z "$PM_EXCL" ] && return $PM_TRUE
for _pattern in $PM_EXCL ; do
case "$1" in
*${_pattern}*)
# TODO: if verbose
pm_print_arr "Skipping $1 - excluded by ${_pattern}"
return $PM_FALSE
;;
esac
done
return $PM_TRUE
}
# it checks if the pkg has to be updated (or downgraded)
# $1 the pkgname
# return values:
# 0 - an upgrade (or downgraded) is available
# 1 - no new version available
pm_check_pkg_for_updates () {
local pkgname origin curVersion newVersion state
pkgname=$1
origin=$(pm_get_moved_origin $1)
if [ $? -ne 0 ]; then
return $PM_FALSE
fi
state=$(pm_get_port_sanity_state $origin)
if [ -n "$state" ]; then
pm_print_arr "This port is marked $state"
return $PM_FALSE
fi
if [ "$( pkg query %o $pkgname )" != $origin ]; then
# the port is moved, update is forced
return $PM_TRUE
fi
curVersion=$( pkg query "%v" $pkgname )
newVersion=$( make -C $pd/$origin -V PKGVERSION)
case $(pkg version -t $curVersion $newVersion) in
\<) # the port version is newer
echo $newVersion
;;
=) return $PM_FALSE ;; # both version are the same (options can differ)
\>) # the port version is older (downgrade?)
echo $newVersion
;;
esac
return $PM_TRUE
}
pm_check_origin_for_updates () {
local _origin _pkg
_origin=$1
_pkg=$(pm_get_inst_pkgname $_origin)
case $? in
1) return $PM_TRUE ;; # if not installed, it has to be installed
2) pm_print_arr "I don't know what sub-package is related to $_origin"
esac
pm_check_pkg_for_updates $_pkg
return $?
}
# it creates a backup pkg of the installed pkg
# if NO_BACKUP is set, it retuns true, but no backup is created
# reqs: PACKAGES
# return values:
# 0 - backup pkg created as expected
# 1 - an error occured
pm_backup () {
local pkgname bkpdir
pkgname=$1
[ -n "$NO_BACKUP" ] && return $PM_TRUE
bkpdir=${PACKAGES}/portmaster-backup
if [ ! -d $bkpdir ]; then
mkdir -p $bkpdir
fi
[ ! -d $bkpdir ] && return $PM_FALSE
pkg create --out-dir $bkpdir $pkgname
return $?
}
# it get the complete list of dependencies of a port
# $1 a valid origin
pm_get_origin_dep_list () {
local _dep_list _output _build_dep _run_dep
(
cd $pd/$1
pmlog PMLOG_DEBUG "O_DEPLIST: Determining the build dependency list"
_build_dep=$(make build-depends-list | tr '\n' ':' | sed "s%$pd/%%g")
pmlog PMLOG_DEBUG "O_DEPLIST: Determining the run dependency list"
_run_dep=$(make run-depends-list | tr '\n' ':' | sed "s%$pd/%%g")
pmlog PMLOG_DEBUG "O_DEPLIST: Merging the lists"
_output=$(pmlist_merge $_build_dep $_run_dep)
pmlog PMLOG_DEBUG "O_DEPLIST: Returning the lists"
echo $_output
)
}
# first easy implementation of the real ports builder
# $1 : a valid origin
pm_reinstall_origin() {
[ ! -d $pd/$1 ] && return $PM_FALSE
(
cd $pd/$1
make reinstall clean
)
}
pm_get_dependency_list() {
local _pkg _input_list _dependency_list
_input_list="$1"
_dependency_list=""
_pkg=$(pmlist_pop $_input_list)
_input_list=$(pmlist_pop_list $_pkg $_input_list)
while [ -n "$_pkg" ]; do
pmlog PMLOG_DEBUG "DEPLIST: Determining the dependency list of $_pkg"
# if the pkg is a pkg name, get its origin name before
if pm_is_valid_origin $_pkg ; then
_origin=$_pkg
else
_origin=$(pm_get_moved_origin $_pkg)
fi
_temp_dep_list=$(pm_get_origin_dep_list $_origin)
pmlog PMLOG_DEBUG "DEPLIST: Determined the dependency list"
_dependency_list=$(pmlist_merge $_dependency_list $_temp_dep_list)
_pkg=$(pmlist_pop $_input_list)
_input_list=$(pmlist_pop_list $_pkg $_input_list)
pmlog PMLOG_DEBUG "DEPLIST: Determined the final dependency list"
done
echo $_dependency_list
}
# For every origin or package, it checks if the updateability
pm_check_for_updatables() {
local _pkg _update_list _pkg_to_check_list
_pkg_to_check_list=$1
_update_list=
_pkg=$(pmlist_pop $_pkg_to_check_list)
_pkg_to_check_list=$(pmlist_pop_list $_pkg $_pkg_to_check_list)
while [ -n "$_pkg" ]; do
if pm_is_valid_origin $_pkg ; then
if pm_check_origin_for_updates $_pkg > /dev/null ; then
_update_list=${_update_list}:${_pkg}
fi
else
# if _pkg has to be updated, add its origin and continue
if pm_check_pkg_for_updates $_pkg > /dev/null ; then
_update_list=${_update_list}:$(pm_get_moved_origin $_pkg)
fi
fi
_pkg=$(pmlist_pop $_pkg_to_check_list)
_pkg_to_check_list=$(pmlist_pop_list $_pkg $_pkg_to_check_list)
done
echo $_update_list
}
# $1 the origin
# $2 the updatable list
# $3 the updated list
pm_build () {
local _pkg _uplist _done
local _deps _upd
_pkg=$1
_uplist=$2
_done=$3
_deps="$(pm_get_dependency_list $_pkg)"
_upd="$(pmlist_intersect $_deps $_uplist)"
for d in $_upd ; do
_done=$(pm_build $d $_uplist $_done)
done
pm_reinstall_origin $_pkg
_done=$_done:$_pkg
}
# it updates a list of origins
# $1 the list of updatable origins
pm_update () {
local _origin _updatable _built
_updatable=$1
_origin=$(pmlist_pop $_updatable)
_updatable=$(pmlist_pop_list $_origin $_updatable)
_built=":"
while [ -n "$_origin" ]; do
_built=$(pm_build $_origin $_updatable $_built)
_origin=$(pmlist_pop $_updatable)
_updatable=$(pmlist_pop_list $_origin $_updatable)
done
}
# The general main function
# $1 the user list
pm_mainbuild() {
local _user_list _updatable_list _dep_list
_user_list=$1
[ -z $_user_list ] && echo "user list empty" && return
date
_updatable_list=$(pm_check_for_updatables $_user_list)
[ -z $_updatable_list ] && echo "updatable 1 list empty" && return
echo "updatable 1 : $_updatable_list"
pmlog "Determined the updatable list"
echo updatable 1 && date
_dep_list=$(pm_get_dependency_list $_updatable_list)
echo "dependency 1 : $_dep_list"
pmlog "Determined the dependency list"
_dep_list=$(pm_check_for_updatables $_dep_list)
echo "dependency 2 : $_dep_list"
pmlog "Pruned the dependency list"
_updatable_list=$(pmlist_merge $_dep_list $_updatable_list)
[ -z $_updatable_list ] && echo "updatable 2 list empty" && return
echo "updating all this shit $_updatable_list"
#pm_update $_updatable_list
}
#============ High level functions =============
# --clean-distfiles ; no arguments needed
# reqs: $pd and $DISTDIR
# options: $PM_THOROUGH, $PM_YES, $PM_NO
pm_clean_distfiles () {
local DI_FILES
[ -n "$DISTDIR" ] || fail 'DISTDIR is not defined'
DI_FILES=$(pm_mktemp_file DISTINFO)
pm_print_arr "Gathering distinfo list for installed ports"
if [ -n "$PM_THOROUGH" ]; then # [ -t ]
# get the list of distfiles needed by the whole portstree
for _distinfo in $pd/*/*/distinfo
do
fgrep SIZE "$_distinfo" | sed 's/^SIZE (//g' | sed 's/) =.*$//g'
done > $DI_FILES
else
# get the list of distfiles needed by the installed pkgs
for _pkg in $( pkg query '%n-%v' ) ; do
pm_get_distfiles $_pkg
done > $DI_FILES
fi
# get the list of distfiles stored in the system and
# remove distfiles not related to installed pkgs
pm_print_arr "Checking for stale distfiles\n"
for _dif in $(find -s $DISTDIR -type f) ; do
__dif=${_dif#$DISTDIR}
if ! grep -sql ^$__dif $DI_FILES; then
pm_get_yesno n "\t===>>> Delete stale file: $__dif"
case "$?" in
$PM_TRUE)
pm_print DIND "Deleting $__dif"
/bin/unlink $__dif ;;
*)
continue ;;
esac
fi
done
# TODO: remove empty directories under /usr/ports/distfiles
# TODO: clean temporary files and exit
}
# --check-depends ; no arguments needed
# reqs: superuser privileges
pm_check_depends () {
local origin
# TODO: pkg check needs superuser privileges
pkg check -dnq | while read pkgname missinglib; do
origin=$(pkg query "%o" $pkgname)
pm_print "$pkgname ($origin) requires missing $missinglib"
done
# TODO: a clean exit
}
# --check-port-dbdir ; no arguments needed
# reqs: $port_dbdir
# options: $PM_YES, $PM_NO
pm_check_port_dbdir () {
local dbdir dir
[ -d "$port_dbdir" ] || fail "PORT_DBDIR is empty or $port_dbdir doesn't exist"
pm_print_arr "Checking $port_dbdir"
for dir in ${port_dbdir}/*; do
dbdir=${dir#$port_dbdir/}
if [ -f $dir ]; then
continue # files are pkg(8)'s meta and sqlite db
fi
# TODO: verbosity message "Checking $dbdir"
if ! pkg info -e $dbdir ; then
pm_print_arr "$dbdir does not seem to be installed"
if pm_get_yesno n "\t===>>> Delete $dir?" ; then
pm_rm -rf $dir
fi
fi
done
}
# -l and -L ; LIST_PLUS is -L
pm_list () {
local newVersion
pm_ports_by_category
pm_print_arr "Root ports (No dependencies, not depended on)"
for p in $roots; do
pm_print_arr "$p"
if [ -n "$PM_LIST_PLUS" ]; then
newVersion=$(pm_check_pkg_for_updates $p)
if [ $? -eq $PM_TRUE ]; then
pm_print DIND "New version available: $newVersion"
fi
fi
done
pm_print_arr "$num_roots root ports\n"
pm_print_arr "Trunk ports (No dependencies, are depended on)"
for p in $trunks; do
pm_print_arr "$p"
if [ -n "$PM_LIST_PLUS" ]; then
newVersion=$(pm_check_pkg_for_updates $p)
if [ $? -eq $PM_TRUE ]; then
pm_print DIND "New version available: $newVersion"
fi
fi
done
pm_print_arr "$num_trunks trunk ports\n"
pm_print_arr "Branches ports (Have dependencies, are depended on)"
for p in $branches; do
pm_print_arr "$p"
if [ -n "$PM_LIST_PLUS" ]; then
newVersion=$(pm_check_pkg_for_updates $p)
if [ $? -eq $PM_TRUE ]; then
pm_print DIND "New version available: $newVersion"
fi
fi
done
pm_print_arr "$num_branches branch ports\n"
pm_print_arr "Leaf ports (Have dependencies, not depended on)"
for p in $leaves; do
pm_print_arr "$p"
if [ -n "$PM_LIST_PLUS" ]; then
newVersion=$(pm_check_pkg_for_updates $p)
if [ $? -eq $PM_TRUE ]; then
pm_print DIND "New version available: $newVersion"
fi
fi
done
pm_print_arr "$num_leaves leaf ports\n"
}
# INCOMPLETE AND EXPERIMENTAL
# -e ; the argument has to identify a single package
pm_expunge () {
local pkgname deplist
pkgname=$1
if ! pkg info -e $pkgname ; then
pm_find_glob_pkgs $pkgname
case $? in
1) fail "No match: $pkgname" ;;
2) pm_print_arr "Too many matches: $pkgname =>"
pm_print "$glob_pkgs"
fail "The -e option works with only one port at a time" ;;
0) pkgname=$glob_pkgs
unset glob_pkgs ;;
esac
fi
deplist=$(pkg query "%rn-%rv" $pkgname)
if [ -n "$deplist" ]; then
pm_print_arr "Warning: ports dependent on $pkgname"
echo "$deplist" | tr ' ' '\n'
pm_get_yesno n "\n\t===>>> Delete those packages as well?"
if [ $? -eq $PM_FALSE ]; then
exit 1
fi
fi
# TODO: backup should be recursive for reverse dependency as well?
if [ -n "$BACKUP" ]; then
for bkp in $pkgname $deplist ; do
if ! pm_backup $bkp ; then
pm_get_yesno n "\n\t===>>> Backup failed! Continue?"
[ $? -eq $PM_FALSE ] && exit 1
fi
done
fi
# delete distfiles won't be performed
# slaves ports would remove distfiles used by master ports
# or other sibling ports (qt and php are two examples)
# it's a situation not easily detectable
pm_print_arr "Running pkg delete on $pkgname"
pkg delete -f -y $pkgname
for d in $pkgname $deplist ; do
[ -d $pdb/$d ] && rm -rf $pdb/$d
done
pm_clean_stale
}
# INCOMPLETE AND EXPERIMENTAL
# -s ; clean stale
# this function removes all pkgs automatically installed as dependency and
# that are now not used by any other pkg
# TODO: pkg autoremove should perform the same task, yet with no backup feature
pm_clean_stale () {
local stale_pkgs no_list temp_list p pk
stale_pkgs=$(pkg query -e '%a = 1 && %?r = 0' '%n-%v')
no_list=":"
temp_list="$stale_pkgs"
# temp_list contains all stale files
while [ -n "$temp_list" ]; do
for p in $temp_list ; do
pm_get_yesno n "\n\t===>>> $p is no longer needed; may I delete it?"
case "$?" in
$PM_TRUE)
if [ -n "$BACKUP" ]; then
if ! pm_backup $p ; then
pm_get_yesno n "\n\t===>>> Backup failed! Continue?"
[ $? -eq $PM_FALSE ] && continue
fi
fi
pkg delete -f -y $p
;;
$PM_FALSE) # maintain it
pm_get_yesno y "\n\t===>>> Set $p as not automatic (not stale)?"
if [ $? -eq $PM_TRUE ]; then
pkg set -A 0 $p
else
no_list="$no_list:${p}:"
fi
;;
esac
done
# All stale pks are now deleted, signed as not automatic or added to the no_list
stale_pkgs=$(pkg query -e '%a = 1 && %?r = 0' '%n-%v')
temp_list=""
# Re-alculating the temp_list
for pk in $stale_pkgs ; do
case $no_list in
*:${pk}:*) continue ;;
esac
temp_list="${temp_list}$pk "
done
done
exit 0
}
# INCOMPLETE AND EXPERIMENTAL
# PROBABLY TO BE COMPLETELY REWORKED
# -a : update all
pm_update_all () {
local origin
# ?? read_distinfo ??
pm_term_title_print PM_TERM_TITLE_INIT All
echo "===>>> Starting check of installed ports for available updates"
# initial checks
for pkgname in $(pkg query "%n-%v") ; do
origin=$(pm_get_moved_origin $pkgname)
case $? in
0) ;;
2)
echo "===>>> Skipping $pkgname"
continue
;;
*)
fail "===>>> Error looking for $pkgname origin"
;;
esac
# [ -n "$PM_BUILD_ONLY_LIST" ] && run_dl_g="$run_dl_g ${pd}/${origin} "
if pm_check_for_exclude %pkgname ; then
continue
fi
PM_DEPTH="$pkgname " # ??
# check_for_updates
done
}