tazpkg view modules/get @ rev 918

get: set default VERSION & TARBALL
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Aug 02 11:12:52 2016 +0200 (2016-08-02)
parents d36dc9257f15
children 251d9accc1f9
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 [ -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 [ ! -f 'packages.info' ] && tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
162 if [ -f 'packages.info' ]; then
163 awk -F$'\t' -vp="$1" '$1==p{exit 1}' packages.info
164 if [ "$?" -eq 1 ]; then
165 [ -z "$quiet" ] && _ 'Package "%s" already in the cache' "$1" >&2
166 echo -n "$(pwd)/"
167 awk -F$'\t' -vp="$1" '$1==p{printf "%s-%s.tazpkg\n", $1, $2; exit}' packages.info
168 exit 0
169 fi
170 fi
171 fi
173 mirror="$(cat "$PKGS_DB/mirror")"
174 debug "mirror='$mirror'"
177 # Try converted extra-packages first
178 # FIXME: Workaround to get packages filenames (even better to have names and versions separate)
179 converted="$(wget -O - http://mirror1.slitaz.org/packages/extra/ 2>/dev/null | \
180 tr \'\" $'\n' | grep "$1-.*\.tazpkg$" | sort -u)"
181 debug "converted='$converted'"
182 if [ -n "$converted" ]; then
183 case "$mirror" in
184 http://*|https://*|ftp://*)
185 # Default 'http://mirror.slitaz.org/packages/cooking/'
186 # -> 'http://mirror.slitaz.org/packages/extra/'
187 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/extra/$converted'"
188 q=''; [ -n "$quiet" ] && q='-q'
189 wget $q -T 30 -U "$UA" "${mirror%packages/*}packages/extra/$converted" \
190 2>&1 | awk 'BEGIN{RS="\r"}$0~"%"{printf "%s\r",$0}' >&2;;
191 esac
192 if [ -f "$converted" ]; then
193 echo "$extra_cache/$converted"; exit 0
194 fi
195 fi
198 # Fails to download converted extra-package; now try to download and execute get-script
199 cd ..
200 case "$mirror" in
201 http://*|https://*|ftp://*)
202 # Default 'http://mirror.slitaz.org/packages/cooking/'
203 # -> 'http://mirror.slitaz.org/packages/get/'
204 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/get/$1'"
205 q=''; [ -n "$quiet" ] && q='-q'
206 wget $q -T 30 -U "$UA" "${mirror%packages/*}packages/get/$1" \
207 2>&1 | awk 'BEGIN{RS="\r"}$0~"%"{printf "%s\r",$0}' >&2;;
208 esac
210 if [ ! -f "$1" ]; then
211 # TODO: extra package or extra package script? :) Too complicated...
212 die 'Unable to download extra package "%s".' "$(boldify $1)"
213 fi
215 # Extra-package script downloaded in the /var/cache/tazpkg/extra/
217 unset_receipt
218 PACKAGE="$1"
219 script="$(realpath $1)"
220 tmp_dir="$(mktemp -d)"; cd "$tmp_dir"
221 # Run script
222 set -e
223 . "$script"
224 set +e
226 [ "$PWD" != "$tmp_dir" ] && mv $PACKAGE* $tmp_dir
227 cd $tmp_dir
228 [ -n "$VERSION" ] || VERSION="$(date +%Y%m%d)"
229 [ -d "$PACKAGE-$VERSION" ] || mv $PACKAGE $PACKAGE-$VERSION || abort_package
230 [ -n "$TARBALL" ] || TARBALL="$(basename $WGET_URL)"
232 if [ ! -s "$PACKAGE-$VERSION/receipt" ]; then
233 # Create receipt (if script not created it early) using variables from script
234 cat > "$PACKAGE-$VERSION/receipt" <<EOT
235 # SliTaz package receipt.
237 PACKAGE="$PACKAGE"
238 VERSION="$VERSION"
239 CATEGORY="${CATEGORY:-non-free}"
240 WEB_SITE="$WEB_SITE"
241 SHORT_DESC="${SHORT_DESC:-The $PACKAGE software package of SliTaz}"
242 MAINTAINER="${MAINTAINER:-nobody@slitaz.org}"
243 LICENSE="${LICENSE:-unknown}"
244 TARBALL="$TARBALL"
245 WGET_URL="$WGET_URL"
246 CONFIG_FILES="$CONFIG_FILES"
247 SUGGESTED="$SUGGESTED"
248 PROVIDE="$PROVIDE"
249 DEPENDS="$DEPENDS"
250 HOST_ARCH="$HOST_ARCH"
251 TAGS="$TAGS"
252 EXTRA_SOURCE_FILES="$EXTRA_SOURCE_FILES"
253 EOT
254 fi
256 # Add dependencies which was found to already defined dependencies
257 DEPENDS="$(unset DEPENDS; . "$PACKAGE-$VERSION/receipt"; echo $DEPENDS)"
258 TMP_DIR="$tmp_dir"
259 for i in $(find_depends "$PACKAGE-$VERSION/fs"); do
260 case " $DEPENDS " in
261 *\ $i\ *) continue;;
262 esac
263 sed -i "s/^DEPENDS=\"/&$i /" "$PACKAGE-$VERSION/receipt"
264 done
266 # Remove empty variables
267 sed -i '/=""$/d' "$PACKAGE-$VERSION/receipt"
269 tazpkg pack "$PACKAGE-$VERSION" gzip >&2
271 if [ -z "nocache" ]; then
272 # move package to the extra-cache for "get-install" command
273 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$extra_cache"
274 # Re-make extra packages database
275 tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
276 else
277 # move package to directory where "tazpkg get" command invoked
278 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$CUR_DIR"
279 fi
281 # Clean
282 rm -rf "$tmp_dir"
284 # Function output: path to package
285 echo "$CUR_DIR/$PACKAGE-$VERSION.tazpkg"
286 }
289 # return possible name for a virtual package name
291 virtual_pkg() {
292 # input: $1 virtual package name
293 # $2 repository db directory
294 # output: display possible package name
296 debug "\nvirtual_pkg('$1', '$2')"
298 if [ "$tazpkg_command" != 'get-install' ]; then
299 # 'get' command: download any package
300 if [ -z "$(awk -F$'\t' -vp="$1" '{if ($1 == p) print p}' "$2/packages.info")" ]; then
301 # This package not exists in the list, it may be virtual package
302 grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"
303 else
304 echo $1
305 fi
306 return
307 fi
309 local i
310 unset IFS
311 for i in $(grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"); do
312 if echo $i | fgrep -q : ; then
313 # format 'alternative:newname'
314 # if alternative is installed then substitute newname
315 if [ -f $INSTALLED/${i%:*}/receipt ]; then
316 # substitute package dependency
317 echo ${i#*:}
318 return
319 fi
320 elif ! grep -q "^$1 " "$2/packages.info" || [ -f "$INSTALLED/$i/receipt" ]; then
321 # unconditional substitution
322 echo $i
323 return
324 fi
325 done
326 # the real package name
327 echo $1
328 }
331 # Download package file to the cache
333 get_pkg() {
334 # input: $1 package name either "name" or "name-version"
335 # $2 (internal): empty or 'redo' (when recursive calling)
336 # action: download package from mirror to the cache directory
337 # output: full path to the downloaded package
339 debug "\nget_pkg('$1', '$2')"
340 local repo line namever pkgsum
342 IFS=$'\n'
343 for rep in $PRIORITY; do
344 [ ! -f "$rep/packages.info" ] && continue
345 # If found, output string "<name>-<ver>:<sum>"
346 line=$(awk -F$'\t' -vpkg="$(virtual_pkg "$1" "$rep")" \
347 '$1==pkg || $1"-"$2==pkg {printf "%s-%s:%s", $1, $2, $9; exit}' "$rep/packages.info")
348 if [ -n "$line" ]; then
349 namever=${line%:*}; pkgsum=${line#*:}
350 break
351 fi
352 done
353 unset IFS
355 debug " rep='$rep'\n namever='$namever'\n pkgsum='$pkgsum'"
357 if [ -z "$line" ]; then
358 _ 'Unable to find package "%s" in the mirrored packages list.' "$1" >&2
359 # Retry with "get-package"; prevent looping with 'redo'
360 if [ "$2" != 'redo' ]; then
361 get_pkg "get-$1" 'redo'; exit 0
362 else
363 # Retry with extra-package
364 get_pkg_extra "${1#get-}"
365 exit 0
366 fi
367 fi
369 # We need package "$namever.tazpkg" from "$rep" with "$pkgsum"
371 if [ -z "$nocache" ]; then
372 # Go to cache for "get-install" command; don't move for "get" command
373 debug "cd '$(get_cache_path "$rep")'"
374 cd "$(get_cache_path "$rep")"
375 fi
377 # Check if package already downloaded
378 if [ -f "$namever.tazpkg" ]; then
379 [ -z "$nocache" -a -z "$quiet" ] && _ 'Package "%s" already in the cache' "$namever" >&2
381 # Check if downloading complete, resume it not complete
382 if ! tail -c 2k "$namever.tazpkg" | fgrep -q '00000000TRAILER'; then
383 [ -z "$quiet" ] && _ 'Continuing package "%s" download' "$namever" >&2
384 download_from "$(cat "$rep/mirror")" "$namever.tazpkg"
385 fi
386 else
387 # Package absent in the cache, "cold" downloading
388 download_from "$(cat "$rep/mirror")" "$namever.tazpkg"
389 fi
391 # Make sure we downloaded what we want with checksum
393 if [ "$($CHECKSUM "$namever.tazpkg" | cut -d' ' -f1)" != "$pkgsum" ]; then
394 _ 'Checksum error for "%s"' "$namever.tazpkg" >&2
395 rm "$namever.tazpkg"
397 # Recharge DBs and try again; prevent looping with 'redo'
398 if [ "$2" != 'redo' ]; then
399 tazpkg recharge >&2
400 get_pkg "$1" 'redo'; exit 0
401 else
402 # Give up
403 # TODO: try another mirror?
404 die 'Please wait until the mirror synchronization is complete and try again.'
405 fi
406 fi
408 # Output: path to downloaded package
409 printf '%s/%s.tazpkg\n' "$(pwd)" "$namever"
410 }
415 # Command 'get-install' calls 'get', then 'install' modules. Check package presence here, on the
416 # first stage, if '--forced' option not given
417 if [ "$tazpkg_command" == 'get-install' ]; then
418 if grep -qs "^$1$" "$BLOCKED"; then
419 _ 'Package "%s" blocked.' "$1" >&2
420 exit 1
421 fi
423 if [ -z "$forced" ]; then
424 awk -F$'\t' -vpv="$1" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
425 if [ "$?" -eq 1 ]; then
426 [ -z "$quiet" ] && (
427 newline
428 _ '"%s" package is already installed.' "$(colorize 34 "$1")"
429 longline "$(_ 'You can use the --forced option to force installation.')"
430 newline
431 ) >&2
432 # Prevent execution 'install' stage:
433 exit 1
434 fi
435 fi
436 fi
438 if [ -n "$extra" ]; then
439 # When '--extra' option given, extra-package has priority over 'regular' packages
440 get_pkg_extra "$1"
441 else
442 # Try to get 'package', then 'get-package', then extra 'package'
443 get_pkg "$1"
444 fi