tazpkg view modules/depends @ rev 955

modules/search: allow search file with dash at start: tazpkg -sf "-spi"
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Dec 22 00:02:54 2017 +0200 (2017-12-22)
parents a02e36d44d06
children
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # depends - TazPkg module
4 # Show packages dependencies and reverse dependencies trees
7 # Connect function libraries
8 . /lib/libtaz.sh
10 # Get TazPkg working environment
11 . @@MODULES@@/getenv
16 # recursive dependencies scan
18 dep_scan() {
19 for i in $1; do
20 case " $ALL_DEPS " in
21 *\ $i\ *) continue;;
22 esac
23 ALL_DEPS="$ALL_DEPS $i"
25 if [ -z "$mark" ]; then
26 ii="$i"
27 else
28 ii="- $i"; grep -q ^$i$'\t' "$PKGS_DB/installed.info" && ii="+ $i"
29 fi
30 [ -n "$2" ] && echo "$2$ii ($(awk -F$'\t' -vp="$i" '
31 $1==p{split($7,s," ");print s[2]}' "$PKGS_DB/packages.info"))"
33 DEPENDS="$(awk -F$'\t' -vp="$i" '$1==p{print $8 " "}' "$PKGS_DB/installed.info")"
34 [ -z "$DEPENDS" ] && \
35 DEPENDS="$(awk -F$'\t' -vp="$i" '$1==p{print $8 " "}' "$PKGS_DB/packages.info")"
36 [ -n "$DEPENDS" ] && dep_scan "$DEPENDS" "$2 "
37 done
38 }
41 # recursive reverse dependencies scan
43 rdep_scan() {
44 SEARCH="$1"
46 DB='installed'; [ -n "$all" ] && DB='packages'
48 awk -F$'\t' '$8 {print $1 " " $8}' "$PKGS_DB/$DB.info" | \
49 awk -F' ' -v search="$SEARCH" '
50 function show_deps(pkg, space, i, n, mydeps) {
51 if (processed[pkg]) return
52 processed[pkg] = 1;
54 if (space) printf "%s %s\n", space, pkg;
56 n = split(deps[pkg], mydeps, " ");
57 for (i = 1; i <= n; i++)
58 show_deps(mydeps[i], "==" space);
59 }
61 {
62 for (i = 2; i <= NF; i++)
63 deps[$i] = deps[$i] " " $1;
64 }
66 END {
67 show_deps(search, "")
68 }
69 ' | while read spc pkg; do
70 if [ -z "$mark" ]; then
71 pkgi="$pkg"
72 else
73 pkgi="- $pkg"; grep -q ^$pkg$'\t' "$PKGS_DB/installed.info" && pkgi="+ $pkg"
74 fi
76 echo -n $spc | sed 's/=/ /g'
78 echo "$pkgi ($(awk -F$'\t' -vp="$pkg" '$1==p{split($7,s," ");print s[2]}' \
79 "$PKGS_DB/$DB.info"))"
80 done
81 }
84 # Human readable sum
86 hrsum() {
87 awk '
88 BEGIN {
89 K = 1024; M = K * 1024; G = M * 1024;
90 SUM=0;
91 }
92 /K/ { SUM += $1 * K }
93 /M/ { SUM += $1 * M }
94 /G/ { SUM += $1 * G }
95 END {
96 if (SUM >= G) {
97 SUM /= G; P = "GiB";
98 } else if (SUM >= M) {
99 SUM /= M; P = "MiB";
100 } else {
101 SUM /= K; P = "KiB";
102 }
103 if (SUM >= 100) printf "%d%s", SUM, P; else printf "%.1f%s", SUM, P;
104 }' | sed 's|\.0||'
105 }
110 case "$1" in
112 depends)
113 # Display dependencies tree
114 unset ALL_DEPS
115 TMP_DIR=$(mktemp -d)
116 dep_scan "$2" '' | tee "$TMP_DIR/depends"
117 if [ -n "$total" ]; then
118 separator
119 NUM="$(wc -l < "$TMP_DIR/depends")"
120 TOTAL="$(sed 's|.*(\([^)]*\))|\1|' "$TMP_DIR/depends" | hrsum)"
121 _p 'Total: %s package (%s)' 'Total: %s packages (%s)' "$NUM" \
122 "$NUM" "$TOTAL"
123 newline
125 if [ -n "$mark" ]; then
126 NUM="$(grep '^ *-' "$TMP_DIR/depends" | wc -l)"
127 TOTAL="$(grep '^ *-' "$TMP_DIR/depends" | sed 's|.*(\([^)]*\))|\1|' | hrsum)"
128 _p 'To install: %s package (%s)' 'To install: %s packages (%s)' "$NUM" \
129 "$NUM" "$TOTAL"
130 newline
131 fi
132 fi
133 rm -rf "$TMP_DIR"
134 ;;
137 rdepends)
138 # Display reverse dependencies tree
139 cd $INSTALLED
140 unset ALL_DEPS
141 rdep_scan "$2"
142 ;;
144 esac