tazpkg view modules/install @ rev 913

Modules install, remove: improve compatibility with auto-answer options --yes and --noconfirm.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Jul 22 09:57:30 2016 +0300 (2016-07-22)
parents d034a2d99e3a
children 07ef2e1b4273
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 [ -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 [ "$AUTO_INSTALL_DEPS" == 'yes' ] && yes='yes'
138 # Display question; wait for answer or print auto-answer
139 newline
140 confirm "$(_ 'Install all missing dependencies? (y/N)')"
141 answer=$?
142 newline
143 fi
144 debug " answer='$answer'"
146 dir="$(dirname "$1")"
147 debug " dir='$dir'"
149 # We can install packages from /home/boot/packages at a boot time
150 # Also we can prefer local packages over mirrored/cached using '--local' option
151 [ "$dir" == '/home/boot/packages' ] && local='yes'
152 debug " local='$local'"
154 # "--nodeps" option prevents to install dependencies
155 if [ "$answer" -eq 0 -a -z "$nodeps" ]; then
156 debug " let's install missing packages"
157 for pkg in $missing_packages; do
158 debug " pkg='$pkg'"
159 if [ ! -d "$INSTALLED/$pkg" ]; then
160 # Package not installed
162 found='0'
163 # Prefer local packages
164 if [ -n "$local" ]; then
165 [ -z "$quiet" ] && _ 'Checking if package "%s" exists in local list...' "$pkg"
166 # Find local package
167 tempd="$(mktemp -d)"; cd "$tempd"
168 for pkgfile in $dir/$pkg-*.tazpkg; do
169 # Extract receipt from each matched package
170 cpio -F "$pkgfile" -i receipt >/dev/null 2>&1
171 name=$(. receipt; echo $PACKAGE)
172 rm receipt
173 if [ "$name" == "$pkg" ]; then
174 found='1'
175 # Install the first matched package: normally there is only one package
176 # with the $PACKAGE matched in the receipt
177 tazpkg install "$pkgfile"
178 fi
179 done
180 rm -r "$tempd"
181 fi
182 debug " found='$found'"
184 # Install package from the mirror
185 [ "$found" -eq 0 ] && tazpkg get-install "$pkg"
186 fi
187 done
188 else
189 # Answered 'No' to install dependencies, or '--nodeps' option given
190 newline
191 _ 'Leaving dependencies for package "%s" unresolved.' "$PACKAGE"
192 _ 'The package will be installed but will probably not work.'
193 newline
194 fi
195 }
198 # Extract a package with cpio and gzip/lzma.
200 extract_package() {
201 # input: $1 - path to package to be extracted; package should be in the current dir
202 # ROOT INDEPENDENT
203 action 'Extracting package...'
205 # Extract "outer layer": cpio; remove the original package file
206 cpio -idm --quiet < "$1" && rm -f "$1"
208 # "Inner layer" may vary
209 if [ -f fs.cpio.lzma ]; then
210 # "Plain" cpio.lzma
211 unlzma < fs.cpio.lzma | cpio -idm --quiet && rm fs.cpio.lzma
212 elif [ -f fs.cpio.gz ]; then
213 # "Fast" cpio.gz (used to pack-then-install process in most of get-packages)
214 zcat fs.cpio.gz | cpio -idm --quiet && rm fs.cpio.gz
215 fi
217 status
218 }
221 # Print short package description
223 print_short_description() {
224 # TODO: undigest repo support? priority...
225 # ROOT READY
226 local short_desc=''
228 # Try to find localized short description
229 for LC in $LANG ${LANG%_*}; do
230 [ -e "$PKGS_DB/packages-desc.$LC" ] &&
231 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $2; exit}' "$PKGS_DB/packages-desc.$LC")
232 done
234 # Try to find short description for mirrored package
235 [ -z "$short_desc" -a -s "$PKGS_DB/packages.info" ] &&
236 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $4; exit}' "$PKGS_DB/packages.info")
238 [ -z "$short_desc" ] && short_desc="$SHORT_DESC"
240 longline "$short_desc"
241 }
244 grepesc() {
245 sed 's/\[/\\[/g'
246 }
251 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
253 # Block of receipt function callers
254 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
255 # Few receipts function should be patched now.
257 # Input: $1 = path to the receipt to be processed
259 # Pre-install commands
260 call_pre_install() {
261 local tmp
262 if grep -q '^pre_install()' "$1"; then
263 action 'Execute pre-install commands...'
264 tmp="$(mktemp)"
265 cp "$1" "$tmp"
266 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
267 ( . "$tmp"; pre_install "$root" )
268 status
269 rm "$tmp"
270 fi
272 }
273 # Post-install commands
274 call_post_install() {
275 local tmp
276 if grep -q '^post_install()' "$1"; then
277 action 'Execute post-install commands...'
278 tmp="$(mktemp)"
279 cp "$1" "$tmp"
280 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
281 ( . "$tmp"; post_install "$root" )
282 status
283 rm "$tmp"
284 fi
285 }
288 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
291 # This function installs a package in the rootfs.
293 install_package() {
294 # input: $1 = path to package to be installed
295 # dep: install_all_deps, print_short_description, extract_package, grepesc.
297 debug "\ninstall_package('$1')"
298 local dir
300 PACKAGE_FILE="$1"
301 TMP_DIR="$(mktemp -d)"
303 # Get receipt's variables and functions
304 { cd "$TMP_DIR"; cpio --quiet -i receipt >/dev/null 2>&1; } < "$PACKAGE_FILE"
305 # Why next code? "Bad" receipt sourcing can redefine some vital TazPkg variables.
306 (
307 . "$TMP_DIR/receipt"
308 cat > "$TMP_DIR/receipt.var" <<EOT
309 PACKAGE="$PACKAGE"
310 VERSION="$VERSION"
311 EXTRAVERSION="$EXTRAVERSION"
312 CATEGORY="$CATEGORY"
313 SHORT_DESC="$SHORT_DESC"
314 WEB_SITE="$WEB_SITE"
315 TAGS="$TAGS"
316 DEPENDS="$DEPENDS"
317 CONFIG_FILES="$CONFIG_FILES"
318 PACKED_SIZE="$PACKED_SIZE"
319 UNPACKED_SIZE="$UNPACKED_SIZE"
320 EOT
321 rm "$TMP_DIR/receipt"
322 )
323 . "$TMP_DIR/receipt.var"
326 # Make sure folder exists on new installs or upgrades
327 mkdir -p "$INSTALLED/$PACKAGE"
329 # Keep "modifiers" and "files.list" on upgrade
330 find "$INSTALLED/$PACKAGE" -type f \( ! -name modifiers ! -name files.list \) -delete
332 # Update "installed.md5"
333 # TODO: discontinue using 'installed.md5'
334 touch "$PKGS_DB/installed.$SUM"
335 sed -i "/ $(basename "$PACKAGE_FILE")$/d" "$PKGS_DB/installed.$SUM" 2>/dev/null
336 cd "$(dirname "$PACKAGE_FILE")"
337 $CHECKSUM "$(basename "$PACKAGE_FILE")" >> "$PKGS_DB/installed.$SUM"
339 # Resolve package dependencies before package installation
340 install_all_deps "$PACKAGE_FILE"
343 # TODO: why this list-processed in the $PKGS_DB?
344 #[ -n "$INSTALL_LIST" ] && echo "$PACKAGE_FILE" >> "$PKGS_DB/$INSTALL_LIST-processed"
346 if [ -n "$sequence" ]; then
347 title 'Installation of package "%s" (%s)' "$PACKAGE" "$sequence"
348 else
349 title 'Installation of package "%s"' "$PACKAGE"
350 fi
352 if [ -z "$quiet" ]; then
353 print_short_description "$PACKAGE"
354 separator '-'
355 fi
357 action 'Copying package...'
358 cp "$PACKAGE_FILE" "$TMP_DIR"
359 status
361 cd "$TMP_DIR"
362 extract_package "$(basename "$PACKAGE_FILE")"
364 # Include temporary receipt to get the right variables
365 . "$TMP_DIR/receipt.var"
367 cd "$INSTALLED"
370 # Get files to remove if upgrading
371 # IFS here modified temporarily for processing filenames with spaces
372 IFS=$'\n'
373 if [ -f "$PACKAGE/files.list" ]; then
374 while read file; do
375 grep -q "^$(echo "$file" | grepesc)$" "$TMP_DIR/files.list" && continue
376 for i in $(cat "$PACKAGE/modifiers" 2>/dev/null;
377 fgrep -sl "$PACKAGE" */modifiers | cut -d/ -f1); do
378 grep -qs "^$(echo "$file" | grepesc)$" "$i/files.list" && continue 2
379 done
380 echo "$file"
381 done < "$PACKAGE/files.list" > "$TMP_DIR/files2remove.list"
382 fi
383 unset IFS
386 # Remember modified packages
387 action 'Remember modified packages...'
388 {
389 check=false
390 # TODO: why '[' the special?
391 # FIXME: we have files with spaces in our packages!
392 for i in $(fgrep -v [ $TMP_DIR/files.list); do
393 [ -e "$root$i" ] || continue
394 [ -d "$root$i" ] && continue
395 echo "- $i"
396 check=true
397 done ;
398 $check && \
399 for i in *; do
400 [ "$i" == "$PACKAGE" ] && continue
401 [ -s "$i/files.list" ] || continue
402 awk "{ printf \"$i %s\\n\",\$1 }" < "$i/files.list"
403 done;
404 } | awk '
405 {
406 if ($1 == "-" || file[$2] != "") {
407 file[$2] = file[$2] " " $1
408 if ($1 != "-") {
409 if (pkg[$1] == "") all = all " " $1
410 pkg[$1] = pkg[$1] " " $2
411 }
412 }
413 }
414 END {
415 for (i = split(all, p, " "); i > 0; i--)
416 for (j = split(pkg[p[i]], f, " "); j > 0; j--)
417 printf "%s %s\n",p[i],f[j];
418 }
419 ' | while read dir file; do
420 if grep -qs "^$dir$" "$PACKAGE/modifiers"; then
421 # Do not overload an overloaded file !
422 rm "$TMP_DIR/$file" 2>/dev/null
423 continue
424 fi
425 grep -qs "^$PACKAGE$" "$dir/modifiers" && continue
426 if [ -s "$dir/volatile.cpio.gz" ]; then
427 # We can modify backed up files without notice
428 zcat "$dir/volatile.cpio.gz" | cpio -t --quiet | \
429 grep -q "^${file#/}$" && continue
430 fi
431 echo "$PACKAGE" >> "$dir/modifiers"
432 done
433 status
436 cd "$TMP_DIR"
437 # Copy receipt, etc.
438 for file in receipt files.list description.txt $CHECKSUM; do
439 [ -f "$file" ] && cp "$file" "$INSTALLED/$PACKAGE"
440 done
443 # Pre-install commands
444 call_pre_install "$INSTALLED/$PACKAGE/receipt"
447 if [ -n "$CONFIG_FILES" ]; then
448 # Save "official" configuration files
449 action 'Saving configuration files...'
450 debug "\n"
452 cd fs
453 local config_file
454 for config_file in $CONFIG_FILES; do
455 debug " config_file: '$config_file'"
456 find ${config_file#/} -type f 2>/dev/null
457 done | cpio -o -H newc --quiet | gzip -9 > "$INSTALLED/$PACKAGE/volatile.cpio.gz"
458 cd ..
460 if [ -z "$newconf" ]; then
461 debug " no '--newconf': clean official config files"
462 # Keep user configuration files: remove "official" from fs tree
463 for config_file in $CONFIG_FILES; do
464 for config_file_official in $(find "fs$config_file" ! -type d 2>/dev/null | sed 's|^fs||'); do
465 if [ -e "$root$config_file_official" ]; then
466 debug " official '$config_file_official' will be skipped"
467 rm "fs$config_file_official"
468 else
469 debug " official '$config_file_official' will be written"
470 fi
471 done
472 done
473 fi
474 # always '[ Done ]' status, unless '--newconf' is passed or not
475 :; status
476 fi
479 if [ -n "$(ls fs/* 2>/dev/null)" ]; then
480 action 'Installing package...'
482 debug '\n resolving destination links in source'
483 IFS=$'\n'
484 for dir in $(find fs -type d | sed 's|^fs||;/^$/d'); do
485 if ldir=$(readlink -n $root$dir); then
486 debug " * mv 'fs$dir'\n -> 'fs${dir%/*}/$ldir'"
487 mkdir -p "fs${dir%/*}/${ldir%/*}"
488 mv "fs$dir" "fs${dir%/*}/$ldir"
489 fi
490 done
491 unset IFS
493 debug ' copying folders and files to destination'
494 cp -af fs/* "$root/"
495 status
496 fi
499 if [ -s files2remove.list ]; then
500 action 'Removing old files...'
501 while read file; do
502 dir="$root$file"
503 # Remove specified file
504 rm -f "$dir"
505 # Recursive remove non-empty up-dirs
506 while [ "$dir" != "$root/" ]; do
507 dir=$(dirname "$dir")
508 rmdir "$dir" 2>/dev/null || break
509 done
510 done < files2remove.list
511 :; status
512 fi
515 # Remove the temporary random directory.
516 action "Removing all tmp files..."
517 cd ..; rm -rf "$TMP_DIR"
518 status
521 # Post install commands
522 call_post_install "$INSTALLED/$PACKAGE/receipt"
527 # Update system databases
528 # Updating DBs is important process, so not to hide such errors (localized):
529 # chroot: can't execute '/usr/bin/***': No such file or directory
531 local fl="$INSTALLED/$PACKAGE/files.list" upd=0 udesk umime uicon uschm ukrnl
533 fgrep /usr/share/applications/ "$fl" | fgrep -q .desktop && udesk='yes'
534 fgrep -q /usr/share/mime "$fl" && umime='yes'
535 fgrep -q /usr/share/icon/hicolor "$fl" && uicon='yes'
536 fgrep -q /usr/share/glib-2.0/schemas "$fl" && uschm='yes'
537 fgrep /usr/lib/gdk-pixbuf "$fl" | fgrep -q .so && upixb='yes'
538 fgrep -q /lib/modules "$fl" && ukrnl='yes'
540 if [ -n "$udesk$umime$uicon$uschm$upixb$ukrnl" ]; then
541 action 'Update system databases...'
542 upd=1
543 fi
545 # package 'desktop-file-utils'
546 [ -n "$udesk" ] && chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
547 # package 'shared-mime-info'
548 [ -n "$umime" ] && chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
549 # packages 'gtk+', 'gtk+3'
550 [ -n "$uicon" ] && chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
551 # package 'glib'
552 # hide messages like next because they are unresolved (we may to patch glib to hide them, almost the same)
553 # warning: Schema '*' has path '*'. Paths starting with '/apps/', '/desktop/' or '/system/' are deprecated.
554 [ -n "$uschm" ] && chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas 2>&1 | fgrep -v '/apps/'
555 # package 'gdk-pixbuf'
556 [ -n "$upixb" ] && chroot "$root/" /usr/bin/gdk-pixbuf-query-loaders --update-cache
557 # packages 'busybox', 'kmod', 'depmod'
558 [ -n "$ukrnl" ] && grep '/lib/modules' "$fl" | cut -d'/' -f4 | uniq | xargs chroot "$root/" /sbin/depmod -a
560 [ "$upd" -eq 1 ] && status
565 # Update installed.info
566 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g')
567 # Remove newlines from some receipts
568 DEPENDS=$(echo $DEPENDS)
569 PKG_SUM="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.$SUM" | cut -d' ' -f1)"
570 ii="$PKGS_DB/installed.info"
571 # Remove old entry
572 sed -i "/^$PACKAGE /d" "$ii"
573 cat >> "$ii" <<EOT
574 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $PKG_SUM
575 EOT
576 #awk -F$'\t' -vp="$PACKAGE" '$1==p' "$PKGS_DB/packages.info" > $ii
577 TEMP_FILE="$(mktemp)"
578 sort "$ii" > "$TEMP_FILE"; mv -f "$TEMP_FILE" "$ii"; chmod a+r "$ii"; unset ii
580 cd "$CUR_DIR"
581 footer "$(_ 'Package "%s" (%s) is installed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
583 # Log this activity
584 log_pkg Installed
586 # Remove package from upgrade list
587 [ -s "$UP_LIST" ] && sed -i "/^$PACKAGE\$/d" "$UP_LIST"
588 }
593 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
596 PACKAGE=$(
597 tmp_dir=$(mktemp -d); cd "$tmp_dir"
598 cpio --quiet -i receipt >/dev/null 2>&1
599 . receipt; echo $PACKAGE
600 rm -rf "$tmp_dir"
601 ) < "$1"
603 if grep -qs "^$PACKAGE$" "$BLOCKED"; then
604 _ 'Package "%s" blocked.' "$PACKAGE"
605 exit 1
606 fi
608 if [ -z "$forced" ]; then
609 # Check if a package is already installed
610 debug "\ncheck for installed package '$PACKAGE'"
612 awk -F$'\t' -vpv="$PACKAGE" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
614 if [ "$?" -eq 1 ]; then
615 if [ -z "$quiet" ]; then
616 newline
617 _ '"%s" package is already installed.' "$(colorize 34 "$PACKAGE")"
618 longline "$(_ 'You can use the --forced option to force installation.')"
619 newline
620 fi
621 exit 1
622 fi
623 fi
625 install_package "$(realpath "$1")"