tazpkg view modules/install @ rev 916

Restore AUTO_INSTALL_DEPS behaviour
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Aug 01 16:25:14 2016 +0200 (2016-08-01)
parents 92509572ed28
children 9a88902937e5
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # install - TazPkg module
4 # Install packages
7 # Connect function libraries
8 . /lib/libtaz.sh
9 . /usr/lib/slitaz/libpkg.sh
12 # Get TazPkg working environment
13 . @@MODULES@@/getenv
14 # $CACHE_DIR will change, it based on unchanged value of $SAVE_CACHE_DIR
15 SAVE_CACHE_DIR="$CACHE_DIR"
18 . @@MODULES@@/find-depends
23 # Log TazPkg activity
25 log_pkg() {
26 debug "\nlog_pkg('$1')\n PACKAGE='$PACKAGE'\n VERSION='$VERSION'\n EXTRAVERSION='$EXTRAVERSION'"
28 local extra
30 [ "$1" == 'Installed' ] && \
31 extra=" - $(fgrep " $PACKAGE-$VERSION" "$PKGS_DB/installed.$SUM" | awk '{print $1}')"
32 debug " extra='$extra'"
34 [ -w "$LOG" ] &&
35 echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)$extra" >> $LOG
36 }
39 # get an already installed package from packages.equiv
41 equivalent_pkg() {
42 # input: $1 = package name (like "nano")
43 local i rep rules rule out
45 rules=$(for rep in $PRIORITY; do
46 grep -hs "^$1=" "$rep/packages.equiv"
47 done | sed "s|^$1=||")
48 debug " >rules='$rules'"
50 for rule in $rules; do
51 debug " >rule='$rule'"
52 case $rule in
53 *:*)
54 debug '-- x:x'
55 # format 'alternative:newname'
56 # if alternative is installed then substitute newname
57 out="${rule#*:}"
58 awk -F$'\t' -vp="${rule%:*}" '$1==p{exit 1}' "$PKGS_DB/installed.info" || break
59 debug '-- x:x /'
60 ;;
61 *)
62 debug '-- x'
63 # unconditional substitution
64 out="$rule"
65 awk -F$'\t' -vp="$rule" '$1==p{exit 1}' "$PKGS_DB/installed.info" || break
66 debug '-- x /'
67 ;;
68 esac
69 unset out
70 done
71 debug '--'
72 # if not found in packages.equiv then no substitution
73 echo "${out:-$1}"
74 }
77 # Check and install all missing deps.
78 # Auto install or ask user then install all missing deps from local dir, CD-ROM,
79 # media or from the mirror.
81 install_all_deps() {
82 # input: $1 = package file to check/install missing dependencies
83 # ROOT READY
84 # dep: equivalent_pkg.
86 debug "\ninstall_all_deps('$1')"
88 local TMP_DIR DEPENDS num missing_packages equiv pkg answer dir found pkgfile
90 # Check for missing deps listed in a receipt packages.
92 # Get the receipt's variable DEPENDS
93 DEPENDS=$(
94 TMP_DIR=$(mktemp -d); cd "$TMP_DIR"
95 cpio --quiet -i receipt >/dev/null 2>&1
96 . receipt; echo $DEPENDS
97 rm -rf "$TMP_DIR"
98 ) < "$1"
100 unset num missing_packages
101 for depend in $DEPENDS; do
102 debug " depend='$depend'"
103 equiv=$(equivalent_pkg $depend)
104 debug " equiv='$equiv'\n"
105 if [ ! -d "$INSTALLED/$equiv" ]; then
106 missing_packages="$missing_packages $equiv"
107 num=$((num+1))
108 elif [ ! -f "$INSTALLED/$equiv/receipt" ]; then
109 [ -z "$quiet" ] && _ 'WARNING! Dependency loop between "%s" and "%s".' "$PACKAGE" "$equiv"
110 fi
111 done
113 # Nothing to install, exit function
114 [ -z "$num" ] && return
117 title "$(_ 'Tracking dependencies for package "%s"' "$PACKAGE")"
119 # Individual messages for each missing package
120 [ -z "$quiet" ] && \
121 for pkg in $missing_packages; do
122 _ 'Missing package "%s"' "$pkg"
123 done
125 footer "$(_p \
126 '%s missing package to install.' \
127 '%s missing packages to install.' "$num" \
128 "$num")"
131 if [ "$AUTO_INSTALL_DEPS" == 'yes' ] || [ -n "$quiet" ]; then
132 # Quietly not display anything. Assume 'yes' unless '--noconfirm' is provided
133 answer=0
134 [ -n "$noconfirm" ] && answer=1
135 else
136 # Display question; wait for answer or print auto-answer
137 newline
138 confirm "$(_ 'Install all missing dependencies? (y/N)')"
139 answer=$?
140 newline
141 fi
142 debug " answer='$answer'"
144 dir="$(dirname "$1")"
145 debug " dir='$dir'"
147 # We can install packages from /home/boot/packages at a boot time
148 # Also we can prefer local packages over mirrored/cached using '--local' option
149 [ "$dir" == '/home/boot/packages' ] && local='yes'
150 debug " local='$local'"
152 # "--nodeps" option prevents to install dependencies
153 if [ "$answer" -eq 0 -a -z "$nodeps" ]; then
154 debug " let's install missing packages"
155 for pkg in $missing_packages; do
156 debug " pkg='$pkg'"
157 if [ ! -d "$INSTALLED/$pkg" ]; then
158 # Package not installed
160 found='0'
161 # Prefer local packages
162 if [ -n "$local" ]; then
163 [ -z "$quiet" ] && _ 'Checking if package "%s" exists in local list...' "$pkg"
164 # Find local package
165 tempd="$(mktemp -d)"; cd "$tempd"
166 for pkgfile in $dir/$pkg-*.tazpkg; do
167 # Extract receipt from each matched package
168 cpio -F "$pkgfile" -i receipt >/dev/null 2>&1
169 name=$(. receipt; echo $PACKAGE)
170 rm receipt
171 if [ "$name" == "$pkg" ]; then
172 found='1'
173 # Install the first matched package: normally there is only one package
174 # with the $PACKAGE matched in the receipt
175 tazpkg install "$pkgfile"
176 fi
177 done
178 rm -r "$tempd"
179 fi
180 debug " found='$found'"
182 # Install package from the mirror
183 [ "$found" -eq 0 ] && tazpkg get-install "$pkg"
184 fi
185 done
186 else
187 # Answered 'No' to install dependencies, or '--nodeps' option given
188 newline
189 _ 'Leaving dependencies for package "%s" unresolved.' "$PACKAGE"
190 _ 'The package will be installed but will probably not work.'
191 newline
192 fi
193 }
196 # Extract a package with cpio and gzip/lzma.
198 extract_package() {
199 # input: $1 - path to package to be extracted; package should be in the current dir
200 # ROOT INDEPENDENT
201 action 'Extracting package...'
203 # Extract "outer layer": cpio; remove the original package file
204 cpio -idm --quiet < "$1" && rm -f "$1"
206 # "Inner layer" may vary
207 if [ -f fs.cpio.lzma ]; then
208 # "Plain" cpio.lzma
209 unlzma < fs.cpio.lzma | cpio -idm --quiet && rm fs.cpio.lzma
210 elif [ -f fs.cpio.gz ]; then
211 # "Fast" cpio.gz (used to pack-then-install process in most of get-packages)
212 zcat fs.cpio.gz | cpio -idm --quiet && rm fs.cpio.gz
213 fi
215 status
216 }
219 # Print short package description
221 print_short_description() {
222 # TODO: undigest repo support? priority...
223 # ROOT READY
224 local short_desc=''
226 # Try to find localized short description
227 for LC in $LANG ${LANG%_*}; do
228 [ -e "$PKGS_DB/packages-desc.$LC" ] &&
229 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $2; exit}' "$PKGS_DB/packages-desc.$LC")
230 done
232 # Try to find short description for mirrored package
233 [ -z "$short_desc" -a -s "$PKGS_DB/packages.info" ] &&
234 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $4; exit}' "$PKGS_DB/packages.info")
236 [ -z "$short_desc" ] && short_desc="$SHORT_DESC"
238 longline "$short_desc"
239 }
242 grepesc() {
243 sed 's/\[/\\[/g'
244 }
249 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
251 # Block of receipt function callers
252 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
253 # Few receipts function should be patched now.
255 # Input: $1 = path to the receipt to be processed
257 # Pre-install commands
258 call_pre_install() {
259 local tmp
260 if grep -q '^pre_install()' "$1"; then
261 action 'Execute pre-install commands...'
262 tmp="$(mktemp)"
263 cp "$1" "$tmp"
264 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
265 ( . "$tmp"; pre_install "$root" )
266 status
267 rm "$tmp"
268 fi
270 }
271 # Post-install commands
272 call_post_install() {
273 local tmp
274 if grep -q '^post_install()' "$1"; then
275 action 'Execute post-install commands...'
276 tmp="$(mktemp)"
277 cp "$1" "$tmp"
278 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
279 ( . "$tmp"; post_install "$root" )
280 status
281 rm "$tmp"
282 fi
283 }
286 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
289 # This function installs a package in the rootfs.
291 install_package() {
292 # input: $1 = path to package to be installed
293 # dep: install_all_deps, print_short_description, extract_package, grepesc.
295 debug "\ninstall_package('$1')"
296 local dir
298 PACKAGE_FILE="$1"
299 TMP_DIR="$(mktemp -d)"
301 # Get receipt's variables and functions
302 { cd "$TMP_DIR"; cpio --quiet -i receipt >/dev/null 2>&1; } < "$PACKAGE_FILE"
303 # Why next code? "Bad" receipt sourcing can redefine some vital TazPkg variables.
304 (
305 . "$TMP_DIR/receipt"
306 cat > "$TMP_DIR/receipt.var" <<EOT
307 PACKAGE="$PACKAGE"
308 VERSION="$VERSION"
309 EXTRAVERSION="$EXTRAVERSION"
310 CATEGORY="$CATEGORY"
311 SHORT_DESC="$SHORT_DESC"
312 WEB_SITE="$WEB_SITE"
313 TAGS="$TAGS"
314 DEPENDS="$DEPENDS"
315 CONFIG_FILES="$CONFIG_FILES"
316 PACKED_SIZE="$PACKED_SIZE"
317 UNPACKED_SIZE="$UNPACKED_SIZE"
318 EOT
319 rm "$TMP_DIR/receipt"
320 )
321 . "$TMP_DIR/receipt.var"
324 # Make sure folder exists on new installs or upgrades
325 mkdir -p "$INSTALLED/$PACKAGE"
327 # Keep "modifiers" and "files.list" on upgrade
328 find "$INSTALLED/$PACKAGE" -type f \( ! -name modifiers ! -name files.list \) -delete
330 # Update "installed.md5"
331 # TODO: discontinue using 'installed.md5'
332 touch "$PKGS_DB/installed.$SUM"
333 sed -i "/ $(basename "$PACKAGE_FILE")$/d" "$PKGS_DB/installed.$SUM" 2>/dev/null
334 cd "$(dirname "$PACKAGE_FILE")"
335 $CHECKSUM "$(basename "$PACKAGE_FILE")" >> "$PKGS_DB/installed.$SUM"
337 # Resolve package dependencies before package installation
338 install_all_deps "$PACKAGE_FILE"
341 # TODO: why this list-processed in the $PKGS_DB?
342 #[ -n "$INSTALL_LIST" ] && echo "$PACKAGE_FILE" >> "$PKGS_DB/$INSTALL_LIST-processed"
344 if [ -n "$sequence" ]; then
345 title 'Installation of package "%s" (%s)' "$PACKAGE" "$sequence"
346 else
347 title 'Installation of package "%s"' "$PACKAGE"
348 fi
350 if [ -z "$quiet" ]; then
351 print_short_description "$PACKAGE"
352 separator '-'
353 fi
355 action 'Copying package...'
356 cp "$PACKAGE_FILE" "$TMP_DIR"
357 status
359 cd "$TMP_DIR"
360 extract_package "$(basename "$PACKAGE_FILE")"
362 # Include temporary receipt to get the right variables
363 . "$TMP_DIR/receipt.var"
365 cd "$INSTALLED"
368 # Get files to remove if upgrading
369 # IFS here modified temporarily for processing filenames with spaces
370 IFS=$'\n'
371 if [ -f "$PACKAGE/files.list" ]; then
372 while read file; do
373 grep -q "^$(echo "$file" | grepesc)$" "$TMP_DIR/files.list" && continue
374 for i in $(cat "$PACKAGE/modifiers" 2>/dev/null;
375 fgrep -sl "$PACKAGE" */modifiers | cut -d/ -f1); do
376 grep -qs "^$(echo "$file" | grepesc)$" "$i/files.list" && continue 2
377 done
378 echo "$file"
379 done < "$PACKAGE/files.list" > "$TMP_DIR/files2remove.list"
380 fi
381 unset IFS
384 # Remember modified packages
385 action 'Remember modified packages...'
386 {
387 check=false
388 # TODO: why '[' the special?
389 # FIXME: we have files with spaces in our packages!
390 for i in $(fgrep -v [ $TMP_DIR/files.list); do
391 [ -e "$root$i" ] || continue
392 [ -d "$root$i" ] && continue
393 echo "- $i"
394 check=true
395 done ;
396 $check && \
397 for i in *; do
398 [ "$i" == "$PACKAGE" ] && continue
399 [ -s "$i/files.list" ] || continue
400 awk "{ printf \"$i %s\\n\",\$1 }" < "$i/files.list"
401 done;
402 } | awk '
403 {
404 if ($1 == "-" || file[$2] != "") {
405 file[$2] = file[$2] " " $1
406 if ($1 != "-") {
407 if (pkg[$1] == "") all = all " " $1
408 pkg[$1] = pkg[$1] " " $2
409 }
410 }
411 }
412 END {
413 for (i = split(all, p, " "); i > 0; i--)
414 for (j = split(pkg[p[i]], f, " "); j > 0; j--)
415 printf "%s %s\n",p[i],f[j];
416 }
417 ' | while read dir file; do
418 if grep -qs "^$dir$" "$PACKAGE/modifiers"; then
419 # Do not overload an overloaded file !
420 rm "$TMP_DIR/$file" 2>/dev/null
421 continue
422 fi
423 grep -qs "^$PACKAGE$" "$dir/modifiers" && continue
424 if [ -s "$dir/volatile.cpio.gz" ]; then
425 # We can modify backed up files without notice
426 zcat "$dir/volatile.cpio.gz" | cpio -t --quiet | \
427 grep -q "^${file#/}$" && continue
428 fi
429 echo "$PACKAGE" >> "$dir/modifiers"
430 done
431 status
434 cd "$TMP_DIR"
435 # Copy receipt, etc.
436 for file in receipt files.list description.txt $CHECKSUM; do
437 [ -f "$file" ] && cp "$file" "$INSTALLED/$PACKAGE"
438 done
441 # Pre-install commands
442 call_pre_install "$INSTALLED/$PACKAGE/receipt"
445 if [ -n "$CONFIG_FILES" ]; then
446 # Save "official" configuration files
447 action 'Saving configuration files...'
448 debug "\n"
450 cd fs
451 local config_file
452 for config_file in $CONFIG_FILES; do
453 debug " config_file: '$config_file'"
454 find ${config_file#/} -type f 2>/dev/null
455 done | cpio -o -H newc --quiet | gzip -9 > "$INSTALLED/$PACKAGE/volatile.cpio.gz"
456 cd ..
458 if [ -z "$newconf" ]; then
459 debug " no '--newconf': clean official config files"
460 # Keep user configuration files: remove "official" from fs tree
461 for config_file in $CONFIG_FILES; do
462 for config_file_official in $(find "fs$config_file" ! -type d 2>/dev/null | sed 's|^fs||'); do
463 if [ -e "$root$config_file_official" ]; then
464 debug " official '$config_file_official' will be skipped"
465 rm "fs$config_file_official"
466 else
467 debug " official '$config_file_official' will be written"
468 fi
469 done
470 done
471 fi
472 # always '[ Done ]' status, unless '--newconf' is passed or not
473 :; status
474 fi
477 if [ -n "$(ls fs/* 2>/dev/null)" ]; then
478 action 'Installing package...'
480 debug '\n resolving destination links in source'
481 IFS=$'\n'
482 for dir in $(find fs -type d | sed 's|^fs||;/^$/d'); do
483 if ldir=$(readlink -n $root$dir); then
484 debug " * mv 'fs$dir'\n -> 'fs${dir%/*}/$ldir'"
485 mkdir -p "fs${dir%/*}/${ldir%/*}"
486 mv "fs$dir" "fs${dir%/*}/$ldir"
487 fi
488 done
489 unset IFS
491 debug ' copying folders and files to destination'
492 cp -af fs/* "$root/"
493 status
494 fi
497 if [ -s files2remove.list ]; then
498 action 'Removing old files...'
499 while read file; do
500 dir="$root$file"
501 # Remove specified file
502 rm -f "$dir"
503 # Recursive remove non-empty up-dirs
504 while [ "$dir" != "$root/" ]; do
505 dir=$(dirname "$dir")
506 rmdir "$dir" 2>/dev/null || break
507 done
508 done < files2remove.list
509 :; status
510 fi
513 # Remove the temporary random directory.
514 action "Removing all tmp files..."
515 cd ..; rm -rf "$TMP_DIR"
516 status
519 # Post install commands
520 call_post_install "$INSTALLED/$PACKAGE/receipt"
525 # Update system databases
526 # Updating DBs is important process, so not to hide such errors (localized):
527 # chroot: can't execute '/usr/bin/***': No such file or directory
529 local fl="$INSTALLED/$PACKAGE/files.list" upd=0 udesk umime uicon uschm ukrnl
531 fgrep /usr/share/applications/ "$fl" | fgrep -q .desktop && udesk='yes'
532 fgrep -q /usr/share/mime "$fl" && umime='yes'
533 fgrep -q /usr/share/icon/hicolor "$fl" && uicon='yes'
534 fgrep -q /usr/share/glib-2.0/schemas "$fl" && uschm='yes'
535 fgrep /usr/lib/gdk-pixbuf "$fl" | fgrep -q .so && upixb='yes'
536 fgrep -q /lib/modules "$fl" && ukrnl='yes'
538 if [ -n "$udesk$umime$uicon$uschm$upixb$ukrnl" ]; then
539 action 'Update system databases...'
540 upd=1
541 fi
543 # package 'desktop-file-utils'
544 [ -n "$udesk" ] && chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
545 # package 'shared-mime-info'
546 [ -n "$umime" ] && chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
547 # packages 'gtk+', 'gtk+3'
548 [ -n "$uicon" ] && chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
549 # package 'glib'
550 # hide messages like next because they are unresolved (we may to patch glib to hide them, almost the same)
551 # warning: Schema '*' has path '*'. Paths starting with '/apps/', '/desktop/' or '/system/' are deprecated.
552 [ -n "$uschm" ] && chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas 2>&1 | fgrep -v '/apps/'
553 # package 'gdk-pixbuf'
554 [ -n "$upixb" ] && chroot "$root/" /usr/bin/gdk-pixbuf-query-loaders --update-cache
555 # packages 'busybox', 'kmod', 'depmod'
556 [ -n "$ukrnl" ] && grep '/lib/modules' "$fl" | cut -d'/' -f4 | uniq | xargs chroot "$root/" /sbin/depmod -a
558 [ "$upd" -eq 1 ] && status
563 # Update installed.info
564 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g')
565 # Remove newlines from some receipts
566 DEPENDS=$(echo $DEPENDS)
567 PKG_SUM="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.$SUM" | cut -d' ' -f1)"
568 ii="$PKGS_DB/installed.info"
569 # Remove old entry
570 sed -i "/^$PACKAGE /d" "$ii"
571 cat >> "$ii" <<EOT
572 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $PKG_SUM
573 EOT
574 #awk -F$'\t' -vp="$PACKAGE" '$1==p' "$PKGS_DB/packages.info" > $ii
575 TEMP_FILE="$(mktemp)"
576 sort "$ii" > "$TEMP_FILE"; mv -f "$TEMP_FILE" "$ii"; chmod a+r "$ii"; unset ii
578 cd "$CUR_DIR"
579 footer "$(_ 'Package "%s" (%s) is installed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
581 # Log this activity
582 log_pkg Installed
584 # Remove package from upgrade list
585 [ -s "$UP_LIST" ] && sed -i "/^$PACKAGE\$/d" "$UP_LIST"
586 }
591 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
594 PACKAGE=$(
595 tmp_dir=$(mktemp -d); cd "$tmp_dir"
596 cpio --quiet -i receipt >/dev/null 2>&1
597 . receipt; echo $PACKAGE
598 rm -rf "$tmp_dir"
599 ) < "$1"
601 if grep -qs "^$PACKAGE$" "$BLOCKED"; then
602 _ 'Package "%s" blocked.' "$PACKAGE"
603 exit 1
604 fi
606 if [ -z "$forced" ]; then
607 # Check if a package is already installed
608 debug "\ncheck for installed package '$PACKAGE'"
610 awk -F$'\t' -vpv="$PACKAGE" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
612 if [ "$?" -eq 1 ]; then
613 if [ -z "$quiet" ]; then
614 newline
615 _ '"%s" package is already installed.' "$(colorize 34 "$PACKAGE")"
616 longline "$(_ 'You can use the --forced option to force installation.')"
617 newline
618 fi
619 exit 1
620 fi
621 fi
623 install_package "$(realpath "$1")"