tazpkg view modules/get @ rev 969

modules/get: continue fixing cookmode
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Nov 26 14:59:03 2018 +0200 (2018-11-26)
parents ed2904dae9f2
children f079c87ba419
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # get - TazPkg module
4 # Get package to the cache directory
7 # 1. "Plain packages" - compiled and packed on cook.slitaz.org using receipts.
8 #
9 # Recently added other type of packages: packages that can't be compiled due to the closed sources
10 # or due to the giant size of that sources. In this case special script: a) downloads package
11 # compiled for other Linux from official web site, b) converts this package to the TazPkg format,
12 # c) installs this package as "plain" package.
13 #
14 # 2. "Get-packages" - package contains only one special script for downloading/converting.
15 # Sometimes get-package can get some specified version of the package, sometimes - different
16 # (latest) version.
17 #
18 # Packages 1. and 2. can be found in the wok repository: http://hg.slitaz.org/wok/
19 #
20 # 3. "Extra" get-scripts. Next addition - only script, without packaging.
21 # Extra get-scripts repository: http://hg.slitaz.org/get-scripts/
22 # Extra get-scripts mirror: http://mirror.slitaz.org/packages/get/
23 #
24 # 4. Converted "extra" packages. Next addition - some packages like LibreOffice / OpenOffice are
25 # big to compile them like _1. "Plain packages"_ as well as big to convert them on the user side
26 # (you need a lot of time, CPU, and RAM) and sometimes it is unable on the weak user machines.
27 # So, some selected and free packages are converted on the SliTaz server side, packaged and
28 # are ready to download by any user: http://mirror.slitaz.org/packages/extra/
29 #
30 #
31 # Algorithm to get package:
32 # - search package in question in the mirrored packages list, download if exists;
33 # - search "get-package" in the mirrored packages list, download if exists;
34 # - search package in the "extra" get-scripts list, download if exists and run it, get the package.
35 #
36 # Note, here doubling. Imagine, you may wanted "palemoon" package.
37 # 1. No "plain package";
38 # 2. Existing "get-palemoon" package;
39 # 3. Existing "palemoon" extra get-script;
40 # 4. Existing "palemoon" extra converted package.
41 # User will get 2. He can specify "--extra" option to get 3.
42 # FIXME: do something with doubling.
47 # Connect function libraries
48 . /lib/libtaz.sh
49 . /usr/lib/slitaz/libpkg.sh
51 # Get TazPkg working environment
52 . @@MODULES@@/getenv
54 . @@MODULES@@/find-depends
59 # Get cache directory path
61 get_cache_path() {
62 # input: $1 = DB folder such as "$PKGS_DB" or "$PKGS_DB/undigest/*"
63 # $2 (optional): 'extra' for cache for extra-package scripts
64 # output: path to cache directory
66 local cache_dir
68 if [ "$2" == 'extra' ]; then
69 cache_dir="$SAVE_CACHE_DIR/extra/packages"
70 elif [ "$1" == "$PKGS_DB" ]; then
71 # Main repository
72 cache_dir="$SAVE_CACHE_DIR/$SLITAZ_RELEASE/packages"
73 else
74 # Undigest repository
75 cache_dir="$SAVE_CACHE_DIR/${1##*/}/packages"
76 fi
78 # Make cache directory if absent
79 mkdir -p "$cache_dir"
80 chmod a+w "$cache_dir"
82 echo "$cache_dir"
83 }
86 # Download a file from mirror
88 download_from() {
89 # input: "<mirror_url>" "<package_name>-<version>.tazpkg"
91 debug "\ndownload_from('$1', '$2')"
93 case "$1" in
94 # Mirror URL can have a trailing slash or not.
95 http://* | https://* | ftp://*)
96 debug " wget -c -T 30 -U $UA ${1%/}/$2"
97 q=''; [ -n "$quiet" ] && q='-q'
98 # Display abridged wget status (skip info about connections)
99 wget -c $q -T 30 -U $UA ${1%/}/$2 2>&1 | awk '$0~"%"{printf "%s\r",$0}' >&2
100 ;;
101 *)
102 debug " cp ${1%/}/$2 ."
103 cp ${1%/}/$2 .;;
104 esac
105 }
108 # This function may be called by a get script.
110 abort_package() {
111 cd "$CUR_DIR"
112 rm -rf "$tmp_dir"
113 [ -n "$1" ] ||
114 set -- 'Could not download "%s" from "%s". Exiting.' "${TARBALL:-$PACKAGE}" "${WGET_URL:-$WEB_SITE}"
115 printf "$@"
116 exit 1
117 }
120 # Get extra-package file to the cache
122 get_pkg_extra() {
123 # input: $1 extra-package name (like 'dropbox')
124 # $2 (internal): empty or 'redo' (when recursive calling)
125 # action: download stuff and make package
126 # output: full path to created package
128 debug "\nget_pkg_extra('$1', '$2')"
130 local mirror extra_cache converted tmp_dir script
132 # Check extra.list
133 if [ ! -s "$PKGS_DB/extra.list" ]; then
134 # Empty extra.list
135 if [ "$2" != 'redo' ]; then
136 tazpkg recharge >&2
137 get_pkg_extra "$1" 'redo'; exit 0
138 else
139 # Give up
140 _ 'File "%s" empty.' 'extra.list' >&2
141 die 'Unable to find package "%s" in the extra packages list.' "$(boldify $1)"
142 fi
143 fi
145 # Extra.list exists and not empty
146 if ! grep -q "^$1|" "$PKGS_DB/extra.list"; then
147 die 'Unable to find package "%s" in the extra packages list.' "$(boldify $1)"
148 fi
150 # Extra-package found in extra.list
152 if [ -z "$nocache" ]; then
153 # Go to cache for "get-install" command; don't move for "get" command
154 extra_cache="$(get_cache_path "$PKGS_DB" 'extra')"
155 debug "cd '$extra_cache'"
156 cd "$extra_cache"
158 # Extra-cache should contain packages DB files (packages.info, etc.)
159 # to list extra-packages contained in the extra-cache
160 [ ! -e 'packages.info' -a ! -e "packages-$SLITAZ_ARCH.info" ] &&
161 tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
163 pi='packages.info'
164 [ -e "$pi" ] || pi="packages-$SLITAZ_ARCH.info"
166 if [ -e "$pi" ]; then
167 awk -F$'\t' -vp="$1" '$1==p{exit 1}' $pi
168 if [ "$?" -eq 1 ]; then
169 [ -z "$quiet" ] && _ 'Package "%s" already in the cache' "$1" >&2
170 echo -n "$(pwd)/"
171 awk -F$'\t' -vp="$1" '$1==p{printf "%s-%s.tazpkg\n", $1, $2; exit}' $pi
172 exit 0
173 fi
174 fi
175 fi
177 mirror="$(cat "$PKGS_DB/mirror")"
178 debug "mirror='$mirror'"
181 # Try converted extra-packages first
182 # FIXME: Workaround to get packages filenames (even better to have names and versions separate)
183 converted="$(wget -O - http://mirror1.slitaz.org/packages/extra/ 2>/dev/null | \
184 tr \'\" $'\n' | grep "$1-.*\.tazpkg$" | sort -u)"
185 debug "converted='$converted'"
186 if [ -n "$converted" ]; then
187 case "$mirror" in
188 http://*|https://*|ftp://*)
189 # Default 'http://mirror.slitaz.org/packages/cooking/'
190 # -> 'http://mirror.slitaz.org/packages/extra/'
191 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/extra/$converted'"
192 q=''; [ -n "$quiet" ] && q='-q'
193 wget $q -T 30 -U "$UA" "${mirror%packages/*}packages/extra/$converted" \
194 2>&1 | awk '$0~"%"{printf "%s\r",$0}' >&2;;
195 esac
196 if [ -f "$converted" ]; then
197 echo "$extra_cache/$converted"; exit 0
198 fi
199 fi
202 # Fails to download converted extra-package; now try to download and execute get-script
203 cd ..
204 case "$mirror" in
205 http://*|https://*|ftp://*)
206 # Default 'http://mirror.slitaz.org/packages/cooking/'
207 # -> 'http://mirror.slitaz.org/packages/get/'
208 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/get/$1'"
209 q=''; [ -n "$quiet" ] && q='-q'
210 wget $q -T 30 -U "$UA" "${mirror%packages/*}packages/get/$1" \
211 2>&1 | awk '$0~"%"{printf "%s\r",$0}' >&2;;
212 esac
214 if [ ! -f "$1" ]; then
215 # TODO: extra package or extra package script? :) Too complicated...
216 die 'Unable to download extra package "%s".' "$(boldify $1)"
217 fi
219 # Extra-package script downloaded in the /var/cache/tazpkg/extra/
221 unset_receipt
222 PACKAGE="$1"
223 script="$(realpath $1)"
224 tmp_dir="$(mktemp -d)"; cd "$tmp_dir"
225 # Run script
226 set -e
227 . "$script"
228 set +e
230 [ "$PWD" != "$tmp_dir" ] && mv $PACKAGE* $tmp_dir
231 cd $tmp_dir
232 [ -n "$VERSION" ] || VERSION="$(date +%Y%m%d)"
233 [ -d "$PACKAGE-$VERSION" ] || mv $PACKAGE $PACKAGE-$VERSION || abort_package
234 [ -n "$TARBALL" ] || TARBALL="$(basename $WGET_URL)"
236 if [ ! -s "$PACKAGE-$VERSION/receipt" ]; then
237 # Create receipt (if script not created it early) using variables from script
238 cat > "$PACKAGE-$VERSION/receipt" <<EOT
239 # SliTaz package receipt.
241 PACKAGE="$PACKAGE"
242 VERSION="$VERSION"
243 CATEGORY="${CATEGORY:-non-free}"
244 WEB_SITE="${WEB_SITE:-$WEBSITE}"
245 SHORT_DESC="${SHORT_DESC:-The $PACKAGE software package of SliTaz}"
246 MAINTAINER="${MAINTAINER:-nobody@slitaz.org}"
247 LICENSE="${LICENSE:-unknown}"
248 TARBALL="$TARBALL"
249 WGET_URL="${WGET_URL:-$WGETURL}"
250 CONFIG_FILES="$CONFIG_FILES"
251 SUGGESTED="$SUGGESTED"
252 PROVIDE="$PROVIDE"
253 DEPENDS="$DEPENDS"
254 HOST_ARCH="$HOST_ARCH"
255 TAGS="$TAGS"
256 EXTRA_SOURCE_FILES="$EXTRA_SOURCE_FILES"
257 EOT
258 fi
260 # Add dependencies which was found to already defined dependencies
261 DEPENDS="$(unset DEPENDS; . ./$PACKAGE-$VERSION/receipt; echo $DEPENDS)"
262 TMP_DIR="$tmp_dir"
263 for i in $(find_depends "$PACKAGE-$VERSION/fs"); do
264 case " $DEPENDS " in
265 *\ $i\ *) continue;;
266 esac
267 sed -i "s/^DEPENDS=\"/&$i /" "$PACKAGE-$VERSION/receipt"
268 done
270 # Remove empty variables
271 sed -i '/=""$/d' "$PACKAGE-$VERSION/receipt"
273 tazpkg pack "$PACKAGE-$VERSION" gzip >&2
275 if [ -z "nocache" ]; then
276 # move package to the extra-cache for "get-install" command
277 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$extra_cache"
278 # Re-make extra packages database
279 tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
280 else
281 # move package to directory where "tazpkg get" command invoked
282 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$CUR_DIR"
283 fi
285 # Clean
286 rm -rf "$tmp_dir"
288 # Function output: path to package
289 echo "$CUR_DIR/$PACKAGE-$VERSION.tazpkg"
290 }
293 # return possible name for a virtual package name
295 virtual_name() {
296 # input: $1 virtual package name
297 # $2 repository db directory
298 local i
299 unset IFS
300 for i in $(grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"); do
301 if echo $i | fgrep -q : ; then
302 # format 'alternative:newname'
303 # if alternative is installed then substitute newname
304 if [ -f $INSTALLED/${i%:*}/receipt ]; then
305 # substitute package dependency
306 echo ${i#*:}
307 return
308 fi
309 elif ! grep -q "^$1 " "$2/packages.info" || [ -f "$INSTALLED/$i/receipt" ]; then
310 # unconditional substitution
311 echo $i
312 return
313 fi
314 done
315 # the real package name
316 echo $1
317 }
319 virtual_pkg() {
320 # input: $1 virtual package name
321 # $2 repository db directory
322 # output: display possible package name
324 debug "\nvirtual_pkg('$1', '$2')"
326 if [ "$tazpkg_command" != 'get-install' ]; then
327 # 'get' command: download any package
328 if [ -z "$(awk -F$'\t' -vp="$1" '{if ($1 == p) print p}' "$2/packages.info")" ]; then
329 # This package does not exist in the list, it may be a virtual package
330 virtual_name "$1" "$2"
331 else
332 echo $1
333 fi
334 return
335 fi
337 virtual_name "$1" "$2"
338 }
341 # Download package file to the cache
343 get_pkg() {
344 # input: $1 package name either "name" or "name-version"
345 # $2 (internal): empty or 'redo' (when recursive calling)
346 # action: download package from mirror to the cache directory
347 # output: full path to the downloaded package
349 debug "\nget_pkg('$1', '$2')"
350 local repo line namever pkgsum pkgfile pi
352 IFS=$'\n'
353 for rep in $PRIORITY; do
354 pi="$rep/packages.info"
355 [ ! -e "$pi" ] && pi="$rep/packages-$SLITAZ_ARCH.info"
356 [ ! -e "$pi" ] && continue
358 # If found, output string "<name>-<ver>"
359 namever=$(awk -F$'\t' -vpkg="$(virtual_pkg "$1" "$rep")" \
360 '$1==pkg || $1"-"$2==pkg {printf "%s-%s", $1, $2; exit}' "$pi")
362 pkgfile="$namever-$SLITAZ_ARCH.tazpkg"
363 [ -e "$pkgfile" ] ||
364 pkgfile="$namever-any.tazpkg"
366 if [ -n "$namever" ]; then
367 pkgsum=$(awk -vfile="$pkgfile" '{if($2==file)print $1}' $rep/packages.md5)
368 break
369 fi
370 done
371 unset IFS
373 debug " rep='$rep'\n namever='$namever'\n pkgfile='$pkgfile'\n pkgsum='$pkgsum'"
375 if [ -z "$namever" ]; then
376 _ 'Unable to find package "%s" in the mirrored packages list.' "$1" >&2
377 # Retry with "get-package"; prevent looping with 'redo'
378 if [ "$2" != 'redo' ]; then
379 get_pkg "get-$1" 'redo'; exit 0
380 else
381 # Retry with extra-package
382 get_pkg_extra "${1#get-}"
383 exit 0
384 fi
385 fi
387 # We need package "$pkgfile" from "$rep" with "$pkgsum"
389 if [ -z "$nocache" ]; then
390 # Go to cache for "get-install" command; don't move for "get" command
391 debug "cd '$(get_cache_path "$rep")'"
392 cd "$(get_cache_path "$rep")"
393 fi
395 # Check if package already downloaded
396 if [ -f "$pkgfile" ]; then
397 [ -z "$nocache" -a -z "$quiet" ] && _ 'Package "%s" already in the cache' "$namever" >&2
399 # Check if downloading complete, resume if not complete
400 if ! tail -c 2k "$pkgfile" | fgrep -q '00000000TRAILER'; then
401 [ -z "$quiet" ] && _ 'Continuing package "%s" download' "$namever" >&2
402 download_from "$(cat "$rep/mirror")" "$pkgfile"
403 fi
404 else
405 # Package absent in the cache, "cold" downloading
406 download_from "$(cat "$rep/mirror")" "$pkgfile"
407 fi
409 # Make sure we downloaded what we want with checksum
411 if [ "$($CHECKSUM "$pkgfile" | cut -d' ' -f1)" != "$pkgsum" ]; then
412 _ 'Checksum error for "%s"' "$pkgfile" >&2
413 rm "$pkgfile"
415 # Recharge DBs and try again; prevent looping with 'redo'
416 if [ "$2" != 'redo' ]; then
417 tazpkg recharge >&2
418 get_pkg "$1" 'redo'; exit 0
419 else
420 # Give up
421 # TODO: try another mirror?
422 die 'Please wait until the mirror synchronization is complete and try again.'
423 fi
424 fi
426 # Output: path to downloaded package
427 printf '%s/%s\n' "$(pwd)" "$pkgfile"
428 }
431 # Get local package from /home/slitaz/packages to co-work with the cookutils
433 get_pkg_cookmode() {
434 # input: $1 package name (like 'advancecomp')
435 # action: find package in /home/slitaz/packages (or where defined in the cook.conf)
436 # output: full path to the found package
437 # ROOT NOT USED
439 local PKGS='/home/slitaz/packages' found='0'
440 [ -e "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
442 # Find local package
443 pi="$PKGS/packages.info"
444 [ -e "$pi" ] || pi="$PKGS/packages-$SLITAZ_ARCH.info"
446 namever="$(awk -F$'\t' -vpkg="$1" '{
447 if ($1 == pkg) { printf("%s-%s", $1, $2); exit; }
448 }' $pi)"
450 # Find local provided package
451 [ -n "$namever" ] ||
452 namever="$(awk -F$'\t' -vpkg="$1" '{
453 if (index(" " $10 " ", " " pkg " ")) { printf("%s-%s", $1, $2); exit; }
454 }' $pi)"
456 if [ -n "$namever" ]; then
457 pkgfile=$(find "$PKGS" -name "$namever-$SLITAZ_ARCH.tazpkg")
458 [ -n "$pkgfile" ] ||
459 pkgfile=$(find "$PKGS" -name "$namever-any.tazpkg")
460 fi
462 if [ -n "$pkgfile" -a -f "$pkgfile" ]; then
463 echo "$pkgfile"
464 else
465 # Proceed to get package as usual (non-local)
466 get_pkg "$1"
467 fi
468 }
473 # Command 'get-install' calls 'get', then 'install' modules. Check package presence here, on the
474 # first stage, if '--forced' option not given
475 if [ "$tazpkg_command" == 'get-install' ]; then
476 if grep -qs "^$1$" "$BLOCKED"; then
477 _ 'Package "%s" blocked.' "$1" >&2
478 exit 1
479 fi
481 if [ -z "$forced" ]; then
482 awk -F$'\t' -vpv="$1" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
483 if [ "$?" -eq 1 ]; then
484 [ -z "$quiet" ] && (
485 newline
486 _ '"%s" package is already installed.' "$(colorize 34 "$1")"
487 longline "$(_ 'You can use the --forced option to force installation.')"
488 newline
489 ) >&2
490 # Prevent execution 'install' stage:
491 exit 1
492 fi
493 fi
494 fi
496 if [ -n "$cookmode" ]; then
497 # When '--cookmode' option given, try to find package in the local cook repository,
498 # then, if fails, try to get the package as usual
499 get_pkg_cookmode "$1"
500 elif [ -n "$extra" ]; then
501 # When '--extra' option given, extra-package has priority over 'regular' packages
502 get_pkg_extra "$1"
503 else
504 # Try to get 'package', then 'get-package', then extra 'package'
505 get_pkg "$1"
506 fi