tazpkg view modules/get @ rev 898

Module 'get': fix temp dir; module 'find-depends': faster search, add debug messages
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Dec 29 22:00:47 2015 +0200 (2015-12-29)
parents ce0db4329d29
children f16a2aef84b1
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 'BEGIN{RS="\r"}$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 echo "${1:-Abort $PACKAGE.}"
114 exit 1
115 }
118 # Get extra-package file to the cache
120 get_pkg_extra() {
121 # input: $1 extra-package name (like 'dropbox')
122 # $2 (internal): empty or 'redo' (when recursive calling)
123 # action: download stuff and make package
124 # output: full path to created package
126 debug "\nget_pkg_extra('$1', '$2')"
128 local mirror extra_cache converted tmp_dir script
130 # Check extra.list
131 if [ ! -s "$PKGS_DB/extra.list" ]; then
132 # Empty extra.list
133 if [ "$2" != 'redo' ]; then
134 tazpkg recharge >&2
135 get_pkg_extra "$1" 'redo'; exit 0
136 else
137 # Give up
138 _ 'File "%s" empty.' 'extra.list' >&2
139 die 'Unable to find package "%s" in the extra packages list.' "$(boldify $1)"
140 fi
141 fi
143 # Extra.list exists and not empty
144 if ! grep -q "^$1|" "$PKGS_DB/extra.list"; then
145 die 'Unable to find package "%s" in the extra packages list.' "$(boldify $1)"
146 fi
148 # Extra-package found in extra.list
150 if [ -z "$nocache" ]; then
151 # Go to cache for "get-install" command; don't move for "get" command
152 extra_cache="$(get_cache_path "$PKGS_DB" 'extra')"
153 debug "cd '$extra_cache'"
154 cd "$extra_cache"
156 # Extra-cache should contain packages DB files (packages.info, etc.)
157 # to list extra-packages contained in the extra-cache
158 [ ! -f 'packages.info' ] && tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
160 if [ -f 'packages.info' ]; then
161 awk -F$'\t' -vp="$1" '$1==p{exit 1}' packages.info
162 if [ "$?" -eq 1 ]; then
163 [ -z "$quiet" ] && _ 'Package "%s" already in the cache' "$1" >&2
164 echo -n "$(pwd)/"
165 awk -F$'\t' -vp="$1" '$1==p{printf "%s-%s.tazpkg\n", $1, $2; exit}' packages.info
166 exit 0
167 fi
168 fi
169 fi
171 mirror="$(cat "$PKGS_DB/mirror")"
172 debug "mirror='$mirror'"
175 # Try converted extra-packages first
176 # FIXME: Workaround to get packages filenames (even better to have names and versions separate)
177 converted="$(wget -O - http://mirror1.slitaz.org/packages/extra/ 2>/dev/null | \
178 tr \'\" $'\n' | grep "$1-.*\.tazpkg$" | sort -u)"
179 debug "converted='$converted'"
180 if [ -n "$converted" ]; then
181 case "$mirror" in
182 http://*|https://*|ftp://*)
183 # Default 'http://mirror.slitaz.org/packages/cooking/'
184 # -> 'http://mirror.slitaz.org/packages/extra/'
185 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/extra/$converted'"
186 q=''; [ -n "$quiet" ] && q='-q'
187 wget $q -T 30 -U "$UA" "${mirror%packages/*}packages/extra/$converted" \
188 2>&1 | awk 'BEGIN{RS="\r"}$0~"%"{printf "%s\r",$0}' >&2;;
189 esac
190 if [ -f "$converted" ]; then
191 echo "$extra_cache/$converted"; exit 0
192 fi
193 fi
196 # Fails to download converted extra-package; now try to download and execute get-script
197 cd ..
198 case "$mirror" in
199 http://*|https://*|ftp://*)
200 # Default 'http://mirror.slitaz.org/packages/cooking/'
201 # -> 'http://mirror.slitaz.org/packages/get/'
202 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/get/$1'"
203 q=''; [ -n "$quiet" ] && q='-q'
204 wget $q -T 30 -U "$UA" "${mirror%packages/*}packages/get/$1" \
205 2>&1 | awk 'BEGIN{RS="\r"}$0~"%"{printf "%s\r",$0}' >&2;;
206 esac
208 if [ ! -f "$1" ]; then
209 # TODO: extra package or extra package script? :) Too complicated...
210 die 'Unable to download extra package "%s".' "$(boldify $1)"
211 fi
213 # Extra-package script downloaded in the /var/cache/tazpkg/extra/
215 unset_receipt
216 PACKAGE="$1"
217 script="$(realpath $1)"
218 tmp_dir="$(mktemp -d)"; cd "$tmp_dir"
219 # Run script
220 set -e
221 . "$script"
222 set +e
224 if [ ! -d "$PACKAGE-$VERSION" ]; then
225 abort_package 'Could not download "%s" from "%s". Exiting.' "${TARBALL:-$PACKAGE}" "${WGET_URL:-$WEB_SITE}"
226 fi
228 if [ ! -s "$PACKAGE-$VERSION/receipt" ]; then
229 # Create receipt (if script not created it early) using variables from script
230 cat > "$PACKAGE-$VERSION/receipt" <<EOT
231 # SliTaz package receipt.
233 PACKAGE="$PACKAGE"
234 VERSION="${VERSION:-unknown}"
235 CATEGORY="${CATEGORY:-non-free}"
236 WEB_SITE="$WEB_SITE"
237 SHORT_DESC="${SHORT_DESC:-$PACKAGE}"
238 MAINTAINER="${MAINTAINER:-nobody@slitaz.org}"
239 LICENSE="$LICENSE"
240 TARBALL="$TARBALL"
241 WGET_URL="$WGET_URL"
242 CONFIG_FILES="$CONFIG_FILES"
243 SUGGESTED="$SUGGESTED"
244 PROVIDE="$PROVIDE"
245 DEPENDS="$DEPENDS"
246 HOST_ARCH="$HOST_ARCH"
247 TAGS="$TAGS"
248 EXTRA_SOURCE_FILES="$EXTRA_SOURCE_FILES"
249 EOT
250 fi
252 # Add dependencies which was found to already defined dependencies
253 DEPENDS="$(unset DEPENDS; . "$PACKAGE-$VERSION/receipt"; echo $DEPENDS)"
254 TMP_DIR="$tmp_dir"
255 for i in $(find_depends "$PACKAGE-$VERSION/fs"); do
256 case " $DEPENDS " in
257 *\ $i\ *) continue;;
258 esac
259 sed -i "s/^DEPENDS=\"/&$i /" "$PACKAGE-$VERSION/receipt"
260 done
262 # Remove empty variables
263 sed -i '/=""$/d' "$PACKAGE-$VERSION/receipt"
265 tazpkg pack "$PACKAGE-$VERSION" gzip >&2
267 if [ -z "nocache" ]; then
268 # move package to the extra-cache for "get-install" command
269 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$extra_cache"
270 # Re-make extra packages database
271 tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
272 else
273 # move package to directory where "tazpkg get" command invoked
274 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$CUR_DIR"
275 fi
277 # Clean
278 rm -rf "$tmp_dir"
280 # Function output: path to package
281 echo "$CUR_DIR/$PACKAGE-$VERSION.tazpkg"
282 }
285 # return possible name for a virtual package name
287 virtual_pkg() {
288 # input: $1 virtual package name
289 # $2 repository db directory
290 # output: display possible package name
292 debug "\nvirtual_pkg('$1', '$2')"
293 local i
294 unset IFS
295 for i in $(grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"); do
296 if echo $i | fgrep -q : ; then
297 # format 'alternative:newname'
298 # if alternative is installed then substitute newname
299 if [ -f $INSTALLED/${i%:*}/receipt ]; then
300 # substitute package dependency
301 echo ${i#*:}
302 return
303 fi
304 elif ! grep -q "^$1 " "$2/packages.info" || [ -f "$INSTALLED/$i/receipt" ]; then
305 # unconditional substitution
306 echo $i
307 return
308 fi
309 done
310 # the real package name
311 echo $1
312 }
315 # Download package file to the cache
317 get_pkg() {
318 # input: $1 package name either "name" or "name-version"
319 # $2 (internal): empty or 'redo' (when recursive calling)
320 # action: download package from mirror to the cache directory
321 # output: full path to the downloaded package
323 debug "\nget_pkg('$1', '$2')"
324 local repo line namever pkgsum
326 IFS=$'\n'
327 for rep in $PRIORITY; do
328 [ ! -f "$rep/packages.info" ] && continue
329 # If found, output string "<name>-<ver>:<sum>"
330 line=$(awk -F$'\t' -vpkg="$(virtual_pkg "$1" "$rep")" \
331 '$1==pkg || $1"-"$2==pkg {printf "%s-%s:%s", $1, $2, $9; exit}' "$rep/packages.info")
332 if [ -n "$line" ]; then
333 namever=${line%:*}; pkgsum=${line#*:}
334 break
335 fi
336 done
337 unset IFS
339 debug " rep='$rep'\n namever='$namever'\n pkgsum='$pkgsum'"
341 if [ -z "$line" ]; then
342 _ 'Unable to find package "%s" in the mirrored packages list.' "$1" >&2
343 # Retry with "get-package"; prevent looping with 'redo'
344 if [ "$2" != 'redo' ]; then
345 get_pkg "get-$1" 'redo'; exit 0
346 else
347 # Retry with extra-package
348 get_pkg_extra "${1#get-}"
349 exit 0
350 fi
351 fi
353 # We need package "$namever.tazpkg" from "$rep" with "$pkgsum"
355 if [ -z "$nocache" ]; then
356 # Go to cache for "get-install" command; don't move for "get" command
357 debug "cd '$(get_cache_path "$rep")'"
358 cd "$(get_cache_path "$rep")"
359 fi
361 # Check if package already downloaded
362 if [ -f "$namever.tazpkg" ]; then
363 [ -z "$nocache" -a -z "$quiet" ] && _ 'Package "%s" already in the cache' "$namever" >&2
365 # Check if downloading complete, resume it not complete
366 if ! tail -c 2k "$namever.tazpkg" | fgrep -q '00000000TRAILER'; then
367 [ -z "$quiet" ] && _ 'Continuing package "%s" download' "$namever" >&2
368 download_from "$(cat "$rep/mirror")" "$namever.tazpkg"
369 fi
370 else
371 # Package absent in the cache, "cold" downloading
372 download_from "$(cat "$rep/mirror")" "$namever.tazpkg"
373 fi
375 # Make sure we downloaded what we want with checksum
377 if [ "$($CHECKSUM "$namever.tazpkg" | cut -d' ' -f1)" != "$pkgsum" ]; then
378 _ 'Checksum error for "%s"' "$namever.tazpkg" >&2
379 rm "$namever.tazpkg"
381 # Recharge DBs and try again; prevent looping with 'redo'
382 if [ "$2" != 'redo' ]; then
383 tazpkg recharge >&2
384 get_pkg "$1" 'redo'; exit 0
385 else
386 # Give up
387 # TODO: try another mirror?
388 die 'Please wait until the mirror synchronization is complete and try again.'
389 fi
390 fi
392 # Output: path to downloaded package
393 printf '%s/%s.tazpkg\n' "$(pwd)" "$namever"
394 }
399 # Command 'get-install' calls 'get', then 'install' modules. Check package presence here, on the
400 # first stage, if '--forced' option not given
401 if [ "$tazpkg_command" == 'get-install' ]; then
402 if grep -qs "^$1$" "$BLOCKED"; then
403 _ 'Package "%s" blocked.' "$1" >&2
404 exit 1
405 fi
407 if [ -z "$forced" ]; then
408 awk -F$'\t' -vpv="$1" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
409 if [ "$?" -eq 1 ]; then
410 [ -z "$quiet" ] && (
411 newline
412 _ '"%s" package is already installed.' "$(colorize 34 "$1")"
413 longline "$(_ 'You can use the --forced option to force installation.')"
414 newline
415 ) >&2
416 # Prevent execution 'install' stage:
417 exit 1
418 fi
419 fi
420 fi
422 if [ -n "$extra" ]; then
423 # When '--extra' option given, extra-package has priority over 'regular' packages
424 get_pkg_extra "$1"
425 else
426 # Try to get 'package', then 'get-package', then extra 'package'
427 get_pkg "$1"
428 fi