tazpkg view modules/search @ rev 846

Remove "busybox" "prefixes" (thanks llev)
We used "busybox wget", etc. to be sure we called Busybox's "wget", not any other "wget". Workaround already done in "getenv" module.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Oct 09 13:14:01 2015 +0300 (2015-10-09)
parents a02e36d44d06
children 24c9c0777880
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # search - TazPkg module
4 # Search packages and files
7 # Connect function libraries
8 . /lib/libtaz.sh
10 # Get TazPkg working environment
11 . @@MODULES@@/getenv
16 # Search pattern in installed packages.
18 search_in_installed_packages() {
19 _ 'Installed packages'; separator '-'
20 num='0'
21 TMPLIST=$(mktemp)
23 awk -F$'\t' -vpattern="$PATTERN" -vt="$TMPLIST" '
24 BEGIN { IGNORECASE = 1 }
25 index($1 "-" $2, pattern) {
26 printf "%-24s%-18s%s\n", $1, $2, $3;
27 printf "1" >> t
28 }' "$PKGS_DB/installed.info" | tazpkg call translate_category
29 num=$(wc -m < "$TMPLIST"); rm "$TMPLIST"
31 footer "$(_p \
32 '%s installed package found for "%s"' \
33 '%s installed packages found for "%s"' "$num" \
34 "$num" "$PATTERN")"
35 }
38 # Search in packages.list for available pkgs.
40 search_in_packages_list() {
41 _ 'Available packages'; separator '-'
42 num='0'
43 TMPLIST=$(mktemp)
45 for i in $PKGS_DB/packages.info $PKGS_DB/undigest/*/packages.info; do
46 [ -e "$i" ] && awk -F$'\t' -vpattern="$PATTERN" -vt="$TMPLIST" '
47 BEGIN { IGNORECASE = 1 }
48 index($1 "-" $2, pattern) {
49 printf "%-24s%-18s%s\n", $1, $2, $3;
50 printf "1" >> t
51 }' "$i" | tazpkg call translate_category
52 done
54 for i in $PKGS_DB/extra.list $PKGS_DB/undigest/*/extra.list; do
55 [ -e "$i" ] && awk -F'|' -vpattern="$PATTERN" -vt="$TMPLIST" '
56 BEGIN { IGNORECASE = 1 }
57 index($1 "-" $5, pattern) {
58 printf "%-24s%-18s%s\n", $1 " (extra)", $5, $4;
59 printf "1" >> t
60 }' "$i" | tazpkg call translate_category
61 done
63 if [ ! -f "$PKGS_DB/packages.info" ]; then
64 newline
65 longline "$(_ \
66 "No \"%s\" found to check for mirrored packages. For more results, please run \
67 \"%s\" once as root before searching." 'packages.info' 'tazpkg recharge')"
68 newline
69 fi
71 num=$(wc -m < "$TMPLIST"); rm "$TMPLIST"
72 footer "$(_p \
73 '%s available package found for "%s"' \
74 '%s available packages found for "%s"' "$num" \
75 "$num" "$PATTERN")"
76 }
79 # search --mirror: Search in packages.txt for available pkgs and give more
80 # info than --list or default.
82 search_in_packages_txt() {
83 _ 'Matching packages name with version and desc'; separator '-'
84 num='0'
85 TMPLIST=$(mktemp)
87 for i in $PKGS_DB/packages.info $PKGS_DB/undigest/*/packages.info; do
88 [ -e "$i" ] && awk -F$'\t' -vpattern="$PATTERN" -vt="$TMPLIST" '
89 BEGIN { IGNORECASE = 1 }
90 index($1 " " $2 " " $4, pattern) {
91 split($7, s, " ");
92 printf "%s\n%s\n%s\n%s (%s installed)\n\n", $1, $2, $4, s[1], s[2];
93 printf "1" >> t;
94 }' "$i"
95 done
97 if [ ! -f "$PKGS_DB/packages.info" ]; then
98 newline
99 longline "$(_ \
100 "No \"%s\" found to check for mirrored packages. For more results, please run \
101 \"%s\" once as root before searching." 'packages.info' 'tazpkg recharge')"
102 newline
103 fi
105 num=$(wc -m < "$TMPLIST"); rm "$TMPLIST"
106 footer "$(_p \
107 '%s available package found for "%s"' \
108 '%s available packages found for "%s"' "$num" \
109 "$num" "$PATTERN")"
110 }
115 case "$1" in
116 pkg)
117 # Search for a package by pattern or name
119 # Input: package_name pattern (part of package name)
120 # Output:
121 # installed: list of installed packages (as info table)
122 # list: list of available packages (installed and installable) (as info table)
123 # mirror: list of available packages (installed and installable) (in special format)
124 # (matching package name, version, or short description)
126 PATTERN="$2"
127 title 'Search result for "%s"' "$PATTERN"
128 [ -n "$installed" ] && search_in_installed_packages
129 [ -n "$list" ] && search_in_packages_list
130 [ -n "$mirror" ] && search_in_packages_txt
131 if [ -z "$installed$list$mirror" ]; then
132 # If none of "--installed", "--list", "--mirror" given
133 search_in_installed_packages
134 search_in_packages_list
135 fi
136 ;;
138 file)
139 # Search for a file by pattern or name
141 # Input: file_name pattern (part of file name)
142 # Output: list of (installed or available) packages with matched filenames by file_name pattern
144 title 'Search result for file "%s"' "$2"
145 TMPLIST=$(mktemp)
147 if [ -n "$mirror" ]; then
148 # Option "--mirror" given: process available packages
149 TMPDIR=$(mktemp -d)
150 for i in $PKGS_DB/files.list.lzma $PKGS_DB/undigest/*/files.list.lzma; do
151 [ -f "$i" ] || continue
152 lzcat "$i" | awk -F: -vtmp="$TMPLIST" -vdir="$TMPDIR" \
153 -vfile="$2" -vcfile="$(colorize 32 "$2")" '
154 BEGIN { efile = gensub("\+", "\\\+", "g", file); }
155 index($2, file) {
156 gsub(efile, cfile, $2);
157 print $2 >> dir"/"$1;
158 printf "1" >> tmp;
159 }'
160 done
162 for pkg in $(cd "$TMPDIR"; ls); do
163 newline
164 emsg "<c 33>$(_ 'Package %s:' $pkg)</c>"
165 cat "$TMPDIR/$pkg"
166 done
168 rm -r "$TMPDIR"
170 else
171 # Option "--mirror" not given: process installed packages
173 # Check all pkg files.list in search match which specify the package
174 # name and the full path to the file(s).
175 for pkg in $(awk -F$'\t' '{print $1}' $PKGS_DB/installed.info); do
176 FL="$INSTALLED/$pkg/files.list"
177 if grep -qs "$2" "$FL"; then
178 newline
179 emsg "<c 33>$(_ 'Package %s:' "$pkg")</c>"
180 awk -vtmp="$TMPLIST" -vfile="$2" -vcfile="$(colorize 32 $2)" '
181 BEGIN { efile = gensub("\+", "\\\+", "g", file); }
182 index($0, file) {
183 gsub(efile, cfile);
184 print " "$0;
185 printf "1" >> tmp;
186 }
187 ' "$FL"
188 fi
189 done
191 fi
193 num=$(wc -m < "$TMPLIST"); rm "$TMPLIST"
194 footer "$(_p '%s file' '%s files' "$num" \
195 "$(colorize 32 "$num")")"
196 ;;
198 file2)
199 # Search for a package name
201 # Input: file_name pattern (part of file name)
202 # Output: list of available packages with matched filenames by file_name pattern
204 title 'Search result for package "%s"' "$2"
206 # Search for a file on mirror and output only the package name
207 TMPLIST=$(mktemp)
208 for i in $PKGS_DB/files.list.lzma $PKGS_DB/undigest/*/files.list.lzma; do
209 [ -f "$i" ] || continue
210 lzcat "$i" | awk -F: -vT="$TMPLIST" -vterm="$2" '
211 BEGIN { P = "" }
212 index($2, term) {
213 if ($1 != P) {
214 print $1;
215 printf "1" >> T;
216 P = $1
217 }
218 }'
219 done
220 match=$(wc -m < "$TMPLIST")
221 rm "$TMPLIST"
223 footer "$(emsg "$(_p '%s package' '%s packages' "$match" \
224 "<c 32>$match</c>")")"
225 ;;
226 esac