rev |
line source |
al@844
|
1 #!/bin/sh
|
al@844
|
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
|
al@844
|
3 # get - TazPkg module
|
al@844
|
4 # Get package to the cache directory
|
al@844
|
5
|
al@844
|
6
|
al@844
|
7 # 1. "Plain packages" - compiled and packed on cook.slitaz.org using receipts.
|
al@844
|
8 #
|
al@844
|
9 # Recently added other type of packages: packages that can't be compiled due to the closed sources
|
al@844
|
10 # or due to the giant size of that sources. In this case special script: a) downloads package
|
al@844
|
11 # compiled for other Linux from official web site, b) converts this package to the TazPkg format,
|
al@844
|
12 # c) installs this package as "plain" package.
|
al@844
|
13 #
|
al@844
|
14 # 2. "Get-packages" - package contains only one special script for downloading/converting.
|
al@844
|
15 # Sometimes get-package can get some specified version of the package, sometimes - different
|
al@844
|
16 # (latest) version.
|
al@844
|
17 #
|
al@844
|
18 # Packages 1. and 2. can be found in the wok repository: http://hg.slitaz.org/wok/
|
al@844
|
19 #
|
al@844
|
20 # 3. "Extra" get-scripts. Next addition - only script, without packaging.
|
al@844
|
21 # Extra get-scripts repository: http://hg.slitaz.org/get-scripts/
|
al@844
|
22 # Extra get-scripts mirror: http://mirror.slitaz.org/packages/get/
|
al@844
|
23 #
|
al@844
|
24 # 4. Converted "extra" packages. Next addition - some packages like LibreOffice / OpenOffice are
|
al@844
|
25 # big to compile them like _1. "Plain packages"_ as well as big to convert them on the user side
|
al@844
|
26 # (you need a lot of time, CPU, and RAM) and sometimes it is unable on the weak user machines.
|
al@844
|
27 # So, some selected and free packages are converted on the SliTaz server side, packaged and
|
al@844
|
28 # are ready to download by any user: http://mirror.slitaz.org/packages/extra/
|
al@844
|
29 #
|
al@844
|
30 #
|
al@844
|
31 # Algorithm to get package:
|
al@844
|
32 # - search package in question in the mirrored packages list, download if exists;
|
al@844
|
33 # - search "get-package" in the mirrored packages list, download if exists;
|
al@844
|
34 # - search package in the "extra" get-scripts list, download if exists and run it, get the package.
|
al@844
|
35 #
|
al@844
|
36 # Note, here doubling. Imagine, you may wanted "palemoon" package.
|
al@844
|
37 # 1. No "plain package";
|
al@844
|
38 # 2. Existing "get-palemoon" package;
|
al@844
|
39 # 3. Existing "palemoon" extra get-script;
|
al@844
|
40 # 4. Existing "palemoon" extra converted package.
|
al@844
|
41 # User will get 2. He can specify "--extra" option to get 3.
|
al@844
|
42 # FIXME: do something with doubling.
|
al@844
|
43
|
al@844
|
44
|
al@844
|
45
|
al@844
|
46
|
al@844
|
47 # Connect function libraries
|
al@844
|
48 . /lib/libtaz.sh
|
al@844
|
49 . /usr/lib/slitaz/libpkg.sh
|
al@844
|
50
|
al@844
|
51 # Get TazPkg working environment
|
al@844
|
52 . @@MODULES@@/getenv
|
al@844
|
53
|
al@844
|
54 . @@MODULES@@/find-depends
|
al@844
|
55
|
al@844
|
56
|
al@844
|
57
|
al@844
|
58
|
al@844
|
59 # Get cache directory path
|
al@844
|
60
|
al@844
|
61 get_cache_path() {
|
al@844
|
62 # input: $1 = DB folder such as "$PKGS_DB" or "$PKGS_DB/undigest/*"
|
al@844
|
63 # $2 (optional): 'extra' for cache for extra-package scripts
|
al@844
|
64 # output: path to cache directory
|
al@844
|
65
|
al@844
|
66 local cache_dir
|
al@844
|
67
|
al@844
|
68 if [ "$2" == 'extra' ]; then
|
al@844
|
69 cache_dir="$SAVE_CACHE_DIR/extra/packages"
|
al@844
|
70 elif [ "$1" == "$PKGS_DB" ]; then
|
al@844
|
71 # Main repository
|
al@844
|
72 cache_dir="$SAVE_CACHE_DIR/$SLITAZ_RELEASE/packages"
|
al@844
|
73 else
|
al@844
|
74 # Undigest repository
|
al@844
|
75 cache_dir="$SAVE_CACHE_DIR/${1##*/}/packages"
|
al@844
|
76 fi
|
al@844
|
77
|
al@844
|
78 # Make cache directory if absent
|
al@844
|
79 mkdir -p "$cache_dir"
|
al@844
|
80 chmod a+w "$cache_dir"
|
al@844
|
81
|
al@844
|
82 echo "$cache_dir"
|
al@844
|
83 }
|
al@844
|
84
|
al@844
|
85
|
al@844
|
86 # Download a file from mirror
|
al@844
|
87
|
al@844
|
88 download_from() {
|
al@844
|
89 # input: "<mirror_url>" "<package_name>-<version>.tazpkg"
|
al@844
|
90
|
al@844
|
91 debug "\ndownload_from('$1', '$2')"
|
al@844
|
92
|
al@844
|
93 case "$1" in
|
al@844
|
94 # Mirror URL can have a trailing slash or not.
|
al@844
|
95 http://* | https://* | ftp://*)
|
al@862
|
96 debug " wget -c -T 30 -U $UA ${1%/}/$2"
|
al@864
|
97 # Display abridged wget status (skip info about connections)
|
al@864
|
98 wget -c -T 30 -U $UA ${1%/}/$2 2>&1 | awk 'BEGIN{RS="\r"}$0~"%"{printf "%s\r",$0}' >&2
|
al@844
|
99 ;;
|
al@844
|
100 *)
|
al@863
|
101 debug " cp ${1%/}/$2 ."
|
al@863
|
102 cp ${1%/}/$2 .;;
|
al@844
|
103 esac
|
al@844
|
104 }
|
al@844
|
105
|
al@844
|
106
|
al@844
|
107 # This function may be called by a get script.
|
al@844
|
108
|
al@844
|
109 abort_package() {
|
al@844
|
110 cd "$CUR_DIR"
|
al@844
|
111 rm -rf "$tmp_dir"
|
al@844
|
112 echo "${1:-Abort $PACKAGE.}"
|
al@844
|
113 exit 1
|
al@844
|
114 }
|
al@844
|
115
|
al@844
|
116
|
al@844
|
117 # Get extra-package file to the cache
|
al@844
|
118
|
al@844
|
119 get_pkg_extra() {
|
al@844
|
120 # input: $1 extra-package name (like 'dropbox')
|
al@844
|
121 # $2 (internal): empty or 'redo' (when recursive calling)
|
al@844
|
122 # action: download stuff and make package
|
al@844
|
123 # output: full path to created package
|
al@844
|
124
|
al@844
|
125 debug "\nget_pkg_extra('$1', '$2')"
|
al@844
|
126
|
al@844
|
127 local mirror extra_cache converted tmp_dir script
|
al@844
|
128
|
al@844
|
129 # Check extra.list
|
al@844
|
130 if [ ! -s "$PKGS_DB/extra.list" ]; then
|
al@844
|
131 # Empty extra.list
|
al@844
|
132 if [ "$2" != 'redo' ]; then
|
al@844
|
133 tazpkg recharge >&2
|
al@844
|
134 get_pkg_extra "$1" 'redo'; exit 0
|
al@844
|
135 else
|
al@844
|
136 # Give up
|
al@844
|
137 _ 'File "%s" empty.' 'extra.list' >&2
|
al@844
|
138 die 'Unable to find package "%s" in the extra packages list.' "$(boldify $1)"
|
al@844
|
139 fi
|
al@844
|
140 fi
|
al@844
|
141
|
al@844
|
142 # Extra.list exists and not empty
|
al@844
|
143 if ! grep -q "^$1|" "$PKGS_DB/extra.list"; then
|
al@844
|
144 die 'Unable to find package "%s" in the extra packages list.' "$(boldify $1)"
|
al@844
|
145 fi
|
al@844
|
146
|
al@844
|
147 # Extra-package found in extra.list
|
al@844
|
148
|
al@844
|
149 if [ -z "$nocache" ]; then
|
al@844
|
150 # Go to cache for "get-install" command; don't move for "get" command
|
al@844
|
151 extra_cache="$(get_cache_path "$PKGS_DB" 'extra')"
|
al@844
|
152 debug "cd '$extra_cache'"
|
al@844
|
153 cd "$extra_cache"
|
al@844
|
154
|
al@844
|
155 # Extra-cache should contain packages DB files (packages.info, etc.)
|
al@844
|
156 # to list extra-packages contained in the extra-cache
|
al@844
|
157 [ ! -f 'packages.info' ] && tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
|
al@844
|
158
|
al@844
|
159 if [ -f 'packages.info' ]; then
|
al@844
|
160 awk -F$'\t' -vp="$1" '$1==p{exit 1}' packages.info
|
al@844
|
161 if [ "$?" -eq 1 ]; then
|
al@844
|
162 _ 'Package "%s" already in the cache' "$1" >&2
|
al@844
|
163 echo -n "$(pwd)/"
|
al@844
|
164 awk -F$'\t' -vp="$1" '$1==p{printf "%s-%s.tazpkg\n", $1, $2; exit}' packages.info
|
al@844
|
165 exit 0
|
al@844
|
166 fi
|
al@844
|
167 fi
|
al@844
|
168 fi
|
al@844
|
169
|
al@844
|
170 mirror="$(cat "$PKGS_DB/mirror")"
|
al@844
|
171 debug "mirror='$mirror'"
|
al@844
|
172
|
al@844
|
173
|
al@844
|
174 # Try converted extra-packages first
|
al@844
|
175 # FIXME: Workaround to get packages filenames (even better to have names and versions separate)
|
al@846
|
176 converted="$(wget -O - http://mirror1.slitaz.org/packages/extra/ 2>/dev/null | \
|
al@844
|
177 tr \'\" $'\n' | grep "$1-.*\.tazpkg$" | sort -u)"
|
al@844
|
178 debug "converted='$converted'"
|
al@844
|
179 if [ -n "$converted" ]; then
|
al@844
|
180 case "$mirror" in
|
al@844
|
181 http://*|https://*|ftp://*)
|
al@844
|
182 # Default 'http://mirror.slitaz.org/packages/cooking/'
|
al@844
|
183 # -> 'http://mirror.slitaz.org/packages/extra/'
|
al@862
|
184 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/extra/$converted'"
|
al@864
|
185 wget -T 30 -U "$UA" "${mirror%packages/*}packages/extra/$converted" \
|
al@864
|
186 2>&1 | awk 'BEGIN{RS="\r"}$0~"%"{printf "%s\r",$0}' >&2;;
|
al@844
|
187 esac
|
al@844
|
188 if [ -f "$converted" ]; then
|
al@844
|
189 echo "$extra_cache/$converted"; exit 0
|
al@844
|
190 fi
|
al@844
|
191 fi
|
al@844
|
192
|
al@844
|
193
|
al@844
|
194 # Fails to download converted extra-package; now try to download and execute get-script
|
al@844
|
195 cd ..
|
al@844
|
196 case "$mirror" in
|
al@844
|
197 http://*|https://*|ftp://*)
|
al@844
|
198 # Default 'http://mirror.slitaz.org/packages/cooking/'
|
al@844
|
199 # -> 'http://mirror.slitaz.org/packages/get/'
|
al@862
|
200 debug "wget -T 30 -U '$UA' '${mirror%packages/*}packages/get/$1'"
|
al@864
|
201 wget -T 30 -U "$UA" "${mirror%packages/*}packages/get/$1" \
|
al@864
|
202 2>&1 | awk 'BEGIN{RS="\r"}$0~"%"{printf "%s\r",$0}' >&2;;
|
al@844
|
203 esac
|
al@844
|
204
|
al@844
|
205 if [ ! -f "$1" ]; then
|
al@844
|
206 # TODO: extra package or extra package script? :) Too complicated...
|
al@844
|
207 die 'Unable to download extra package "%s".' "$(boldify $1)"
|
al@844
|
208 fi
|
al@844
|
209
|
al@844
|
210 # Extra-package script downloaded in the /var/cache/tazpkg/extra/
|
al@844
|
211
|
al@844
|
212 unset_receipt
|
al@844
|
213 PACKAGE="$1"
|
al@844
|
214 script="$(realpath $1)"
|
al@844
|
215 tmp_dir="$(mktemp -d)"; cd "$tmp_dir"
|
al@844
|
216 # Run script
|
al@844
|
217 set -e
|
al@844
|
218 . "$script"
|
al@844
|
219 set +e
|
al@844
|
220
|
al@844
|
221 if [ ! -d "$PACKAGE-$VERSION" ]; then
|
al@844
|
222 abort_package 'Could not download "%s" from "%s". Exiting.' "${TARBALL:-$PACKAGE}" "${WGET_URL:-$WEB_SITE}"
|
al@844
|
223 fi
|
al@844
|
224
|
al@844
|
225 if [ ! -s "$PACKAGE-$VERSION/receipt" ]; then
|
al@844
|
226 # Create receipt (if script not created it early) using variables from script
|
al@844
|
227 cat > "$PACKAGE-$VERSION/receipt" <<EOT
|
al@844
|
228 # SliTaz package receipt.
|
al@844
|
229
|
al@844
|
230 PACKAGE="$PACKAGE"
|
al@844
|
231 VERSION="${VERSION:-unknown}"
|
al@844
|
232 CATEGORY="${CATEGORY:-non-free}"
|
al@844
|
233 WEB_SITE="$WEB_SITE"
|
al@844
|
234 SHORT_DESC="${SHORT_DESC:-$PACKAGE}"
|
al@844
|
235 MAINTAINER="${MAINTAINER:-nobody@slitaz.org}"
|
al@844
|
236 LICENSE="$LICENSE"
|
al@844
|
237 TARBALL="$TARBALL"
|
al@844
|
238 WGET_URL="$WGET_URL"
|
al@844
|
239 CONFIG_FILES="$CONFIG_FILES"
|
al@844
|
240 SUGGESTED="$SUGGESTED"
|
al@844
|
241 PROVIDE="$PROVIDE"
|
al@844
|
242 DEPENDS="$DEPENDS"
|
al@844
|
243 HOST_ARCH="$HOST_ARCH"
|
al@844
|
244 TAGS="$TAGS"
|
al@844
|
245 EXTRA_SOURCE_FILES="$EXTRA_SOURCE_FILES"
|
al@844
|
246 EOT
|
al@844
|
247 fi
|
al@844
|
248
|
al@844
|
249 # Add dependencies which was found to already defined dependencies
|
al@844
|
250 DEPENDS="$(unset DEPENDS; . "$PACKAGE-$VERSION/receipt"; echo $DEPENDS)"
|
al@844
|
251 for i in $(find_depends "$PACKAGE-$VERSION/fs"); do
|
al@844
|
252 case " $DEPENDS " in
|
al@844
|
253 *\ $i\ *) continue;;
|
al@844
|
254 esac
|
al@844
|
255 sed -i "s/^DEPENDS=\"/&$i /" "$PACKAGE-$VERSION/receipt"
|
al@844
|
256 done
|
al@844
|
257
|
al@844
|
258 # Remove empty variables
|
al@844
|
259 sed -i '/=""$/d' "$PACKAGE-$VERSION/receipt"
|
al@844
|
260
|
al@844
|
261 tazpkg pack "$PACKAGE-$VERSION" gzip >&2
|
al@844
|
262
|
al@844
|
263 if [ -z "nocache" ]; then
|
al@844
|
264 # move package to the extra-cache for "get-install" command
|
al@844
|
265 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$extra_cache"
|
al@844
|
266 # Re-make extra packages database
|
al@844
|
267 tazpkg mkdb "$extra_cache" --root='' --forced >/dev/null
|
al@844
|
268 else
|
al@844
|
269 # move package to directory where "tazpkg get" command invoked
|
al@844
|
270 mv -f "$tmp_dir/$PACKAGE-$VERSION.tazpkg" "$CUR_DIR"
|
al@844
|
271 fi
|
al@844
|
272
|
al@844
|
273 # Clean
|
al@844
|
274 rm -rf "$tmp_dir"
|
al@844
|
275
|
al@844
|
276 # Function output: path to package
|
al@844
|
277 echo "$CUR_DIR/$PACKAGE-$VERSION.tazpkg"
|
al@844
|
278 }
|
al@844
|
279
|
al@844
|
280
|
pascal@873
|
281 # return possible name for a virtual package name
|
pascal@873
|
282
|
pascal@873
|
283 virtual_pkg() {
|
pascal@873
|
284 # input: $1 virtual package name
|
pascal@873
|
285 # $2 repository db directory
|
pascal@873
|
286 # outout: display possible package name
|
pascal@873
|
287
|
pascal@873
|
288 local i
|
pascal@873
|
289 for i in $(grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"); do
|
pascal@873
|
290 if echo $i | fgrep -q : ; then
|
pascal@873
|
291 # format 'alternative:newname'
|
pascal@873
|
292 # if alternative is installed then substitute newname
|
pascal@874
|
293 if [ -f $INSTALLED/${i%:*}/receipt ]; then
|
pascal@873
|
294 # substitute package dependency
|
pascal@873
|
295 echo ${i#*:}
|
pascal@873
|
296 return
|
pascal@873
|
297 fi
|
pascal@873
|
298 else
|
pascal@873
|
299 # unconditional substitution
|
pascal@873
|
300 echo $i
|
pascal@873
|
301 return
|
pascal@873
|
302 fi
|
pascal@873
|
303 done
|
pascal@873
|
304 }
|
pascal@873
|
305
|
al@844
|
306 # Download package file to the cache
|
al@844
|
307
|
al@844
|
308 get_pkg() {
|
al@844
|
309 # input: $1 package name either "name" or "name-version"
|
al@844
|
310 # $2 (internal): empty or 'redo' (when recursive calling)
|
al@844
|
311 # action: download package from mirror to the cache directory
|
al@844
|
312 # output: full path to the downloaded package
|
al@844
|
313
|
al@844
|
314 debug "\nget_pkg('$1', '$2')"
|
al@844
|
315 local repo line namever pkgsum
|
al@844
|
316
|
al@844
|
317 IFS=$'\n'
|
al@844
|
318 for rep in $PRIORITY; do
|
al@844
|
319 [ ! -f "$rep/packages.info" ] && continue
|
al@844
|
320 # If found, output string "<name>-<ver>:<sum>"
|
pascal@873
|
321 line=$(awk -F$'\t' -vpkg="$1" -vvpkg="$(virtual_pkg "$1" "$rep")" \
|
pascal@873
|
322 '$1==pkg || $1"-"$2==pkg || $1==vpkg || $1"-"$2==vpkg {printf "%s-%s:%s", $1, $2, $9; exit}' "$rep/packages.info")
|
al@844
|
323 if [ -n "$line" ]; then
|
al@844
|
324 namever=${line%:*}; pkgsum=${line#*:}
|
al@844
|
325 break
|
al@844
|
326 fi
|
al@844
|
327 done
|
al@844
|
328 unset IFS
|
al@844
|
329
|
al@844
|
330 debug " rep='$rep'\n namever='$namever'\n pkgsum='$pkgsum'"
|
al@844
|
331
|
al@844
|
332 if [ -z "$line" ]; then
|
al@844
|
333 _ 'Unable to find package "%s" in the mirrored packages list.' "$(boldify $1)" >&2
|
al@844
|
334 # Retry with "get-package"; prevent looping with 'redo'
|
al@844
|
335 if [ "$2" != 'redo' ]; then
|
al@844
|
336 get_pkg "get-$1" 'redo'; exit 0
|
al@844
|
337 else
|
al@844
|
338 # Retry with extra-package
|
al@844
|
339 get_pkg_extra "${1#get-}"
|
al@844
|
340 exit 0
|
al@844
|
341 fi
|
al@844
|
342 fi
|
al@844
|
343
|
al@844
|
344 # We need package "$namever.tazpkg" from "$rep" with "$pkgsum"
|
al@844
|
345
|
al@844
|
346 if [ -z "$nocache" ]; then
|
al@844
|
347 # Go to cache for "get-install" command; don't move for "get" command
|
al@844
|
348 debug "cd '$(get_cache_path "$rep")'"
|
al@844
|
349 cd "$(get_cache_path "$rep")"
|
al@844
|
350 fi
|
al@844
|
351
|
al@844
|
352 # Check if package already downloaded
|
al@844
|
353 if [ -f "$namever.tazpkg" ]; then
|
al@844
|
354 [ -z "$nocache" ] && _ 'Package "%s" already in the cache' "$namever" >&2
|
al@844
|
355
|
al@844
|
356 # Check if downloading complete, resume it not complete
|
al@844
|
357 if ! tail -c 2k "$namever.tazpkg" | fgrep -q '00000000TRAILER'; then
|
al@844
|
358 _ 'Continuing package "%s" download' "$namever" >&2
|
al@844
|
359 download_from "$(cat "$rep/mirror")" "$namever.tazpkg"
|
al@844
|
360 fi
|
al@844
|
361 else
|
al@844
|
362 # Package absent in the cache, "cold" downloading
|
al@844
|
363 download_from "$(cat "$rep/mirror")" "$namever.tazpkg"
|
al@844
|
364 fi
|
al@844
|
365
|
al@844
|
366 # Make sure we downloaded what we want with checksum
|
al@844
|
367
|
al@844
|
368 if [ "$($CHECKSUM "$namever.tazpkg" | cut -d' ' -f1)" != "$pkgsum" ]; then
|
al@845
|
369 _ 'Checksum error for "%s"' "$namever.tazpkg" >&2
|
al@844
|
370 rm "$namever.tazpkg"
|
al@844
|
371
|
al@844
|
372 # Recharge DBs and try again; prevent looping with 'redo'
|
al@844
|
373 if [ "$2" != 'redo' ]; then
|
al@844
|
374 tazpkg recharge >&2
|
al@844
|
375 get_pkg "$1" 'redo'; exit 0
|
al@844
|
376 else
|
al@844
|
377 # Give up
|
al@844
|
378 # TODO: try another mirror?
|
al@844
|
379 die 'Please wait until the mirror synchronization is complete and try again.'
|
al@844
|
380 fi
|
al@844
|
381 fi
|
al@844
|
382
|
al@844
|
383 # Output: path to downloaded package
|
al@844
|
384 printf '%s/%s.tazpkg\n' "$(pwd)" "$namever"
|
al@844
|
385 }
|
al@844
|
386
|
al@844
|
387
|
al@844
|
388
|
al@844
|
389
|
al@864
|
390 # Command 'get-install' calls 'get', then 'install' modules. Check package presence here, on the
|
al@864
|
391 # first stage, if '--forced' option not given
|
al@866
|
392 if [ "$tazpkg_command" == 'get-install' ]; then
|
al@866
|
393 if grep -qs "^$1$" "$BLOCKED"; then
|
al@866
|
394 _ 'Package "%s" blocked.' "$1" >&2
|
al@864
|
395 exit 1
|
al@864
|
396 fi
|
al@866
|
397
|
al@866
|
398 if [ -z "$forced" ]; then
|
al@866
|
399 awk -F$'\t' -vpv="$1" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
|
al@866
|
400 if [ "$?" -eq 1 ]; then
|
al@866
|
401 (
|
al@866
|
402 newline
|
al@866
|
403 _ '"%s" package is already installed.' "$(colorize 34 "$1")"
|
al@866
|
404 longline "$(_ 'You can use the --forced option to force installation.')"
|
al@866
|
405 newline
|
al@866
|
406 ) >&2
|
al@866
|
407 # Prevent execution 'install' stage:
|
al@866
|
408 exit 1
|
al@866
|
409 fi
|
al@866
|
410 fi
|
al@864
|
411 fi
|
al@864
|
412
|
al@844
|
413 if [ -n "$extra" ]; then
|
al@844
|
414 # When '--extra' option given, extra-package has priority over 'regular' packages
|
al@844
|
415 get_pkg_extra "$1"
|
al@844
|
416 else
|
al@844
|
417 # Try to get 'package', then 'get-package', then extra 'package'
|
al@844
|
418 get_pkg "$1"
|
al@844
|
419 fi
|