tazpkg view modules/install @ rev 888

Module install: official config file may be absent
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Dec 11 01:41:00 2015 +0200 (2015-12-11)
parents 1362693564d1
children ae9ace90449c
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' -o -n "$quiet" ]; then
132 answer=0
133 else
134 newline
135 confirm "$(_ 'Install all missing dependencies? (y/N)')"
136 answer=$?
137 newline
138 fi
139 debug " answer='$answer'"
141 dir="$(dirname "$1")"
142 debug " dir='$dir'"
144 # We can install packages from /home/boot/packages at a boot time
145 # Also we can prefer local packages over mirrored/cached using '--local' option
146 [ "$dir" == '/home/boot/packages' ] && local='yes'
147 debug " local='$local'"
148 [ -n "$local" ] && tazpkg mkdb "$dir" --forced >/dev/null
151 # "--nodeps" option prevents to install dependencies
152 if [ "$answer" -eq 0 -a -z "$nodeps" ]; then
153 debug " let's install missing packages"
154 for pkg in $missing_packages; do
155 debug " pkg='$pkg'"
156 if [ ! -d "$INSTALLED/$pkg" ]; then
157 # Package not installed
159 found='0'
160 # Prefer local packages
161 if [ -n "$local" ]; then
162 _ 'Checking if package "%s" exists in local list...' "$pkg"
163 pkgfile=$(awk -F$'\t' -vp="$pkg" '
164 $1==p{printf "%s-%s.tazpkg", $1, $2; exit 1}
165 ' "$dir/packages.info")
166 if [ -n "$pkgfile" ]; then
167 found='1'
168 tazpkg install "$dir/$pkgfile"
169 fi
170 fi
171 debug " found='$found'"
173 # Install package from the mirror
174 [ "$found" -eq 0 ] && tazpkg get-install "$pkg"
175 fi
176 done
177 else
178 # Answered 'No' to install dependencies, or '--nodeps' option given
179 newline
180 _ 'Leaving dependencies for package "%s" unresolved.' "$PACKAGE"
181 _ 'The package will be installed but will probably not work.'
182 newline
183 fi
184 }
187 # Extract a package with cpio and gzip/lzma.
189 extract_package() {
190 # input: $1 - path to package to be extracted; package should be in the current dir
191 # ROOT INDEPENDENT
192 action 'Extracting package...'
194 # Extract "outer layer": cpio; remove the original package file
195 cpio -idm --quiet < "$1" && rm -f "$1"
197 # "Inner layer" may vary
198 if [ -f fs.cpio.lzma ]; then
199 # "Plain" cpio.lzma
200 unlzma < fs.cpio.lzma | cpio -idm --quiet && rm fs.cpio.lzma
201 elif [ -f fs.cpio.gz ]; then
202 # "Fast" cpio.gz (used to pack-then-install process in most of get-packages)
203 zcat fs.cpio.gz | cpio -idm --quiet && rm fs.cpio.gz
204 fi
206 status
207 }
210 # Print short package description
212 print_short_description() {
213 # TODO: undigest repo support? priority...
214 # ROOT READY
215 local short_desc=''
217 # Try to find localized short description
218 for LC in $LANG ${LANG%_*}; do
219 [ -e "$PKGS_DB/packages-desc.$LC" ] &&
220 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $2; exit}' "$PKGS_DB/packages-desc.$LC")
221 done
223 # Try to find short description for mirrored package
224 [ -z "$short_desc" -a -s "$PKGS_DB/packages.info" ] &&
225 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $4; exit}' "$PKGS_DB/packages.info")
227 [ -z "$short_desc" ] && short_desc="$SHORT_DESC"
229 longline "$short_desc"
230 }
233 grepesc() {
234 sed 's/\[/\\[/g'
235 }
240 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
242 # Block of receipt function callers
243 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
244 # Few receipts function should be patched now.
246 # Input: $1 = path to the receipt to be processed
248 # Pre-install commands
249 call_pre_install() {
250 local tmp
251 if grep -q '^pre_install()' "$1"; then
252 action 'Execute pre-install commands...'
253 tmp="$(mktemp)"
254 cp "$1" "$tmp"
255 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
256 ( . "$tmp"; pre_install "$root" )
257 status
258 rm "$tmp"
259 fi
261 }
262 # Post-install commands
263 call_post_install() {
264 local tmp
265 if grep -q '^post_install()' "$1"; then
266 action 'Execute post-install commands...'
267 tmp="$(mktemp)"
268 cp "$1" "$tmp"
269 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
270 ( . "$tmp"; post_install "$root" )
271 status
272 rm "$tmp"
273 fi
274 }
277 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
280 # This function installs a package in the rootfs.
282 install_package() {
283 # input: $1 = path to package to be installed
284 # dep: install_all_deps, print_short_description, extract_package, grepesc.
286 debug "\ninstall_package('$1')"
287 local dir
289 PACKAGE_FILE="$1"
290 TMP_DIR="$(mktemp -d)"
292 # Get receipt's variables and functions
293 { cd "$TMP_DIR"; cpio --quiet -i receipt >/dev/null 2>&1; } < "$PACKAGE_FILE"
294 # Why next code? "Bad" receipt sourcing can redefine some vital TazPkg variables.
295 (
296 . "$TMP_DIR/receipt"
297 cat > "$TMP_DIR/receipt.var" <<EOT
298 PACKAGE="$PACKAGE"
299 VERSION="$VERSION"
300 EXTRAVERSION="$EXTRAVERSION"
301 CATEGORY="$CATEGORY"
302 SHORT_DESC="$SHORT_DESC"
303 WEB_SITE="$WEB_SITE"
304 TAGS="$TAGS"
305 DEPENDS="$DEPENDS"
306 CONFIG_FILES="$CONFIG_FILES"
307 PACKED_SIZE="$PACKED_SIZE"
308 UNPACKED_SIZE="$UNPACKED_SIZE"
309 EOT
310 rm "$TMP_DIR/receipt"
311 )
312 . "$TMP_DIR/receipt.var"
315 # Make sure folder exists on new installs or upgrades
316 mkdir -p "$INSTALLED/$PACKAGE"
318 # Keep "modifiers" and "files.list" on upgrade
319 find "$INSTALLED/$PACKAGE" -type f \( ! -name modifiers ! -name files.list \) -delete
321 # Update "installed.md5"
322 # TODO: discontinue using 'installed.md5'
323 touch "$PKGS_DB/installed.$SUM"
324 sed -i "/ $(basename "$PACKAGE_FILE")$/d" "$PKGS_DB/installed.$SUM" 2>/dev/null
325 cd "$(dirname "$PACKAGE_FILE")"
326 $CHECKSUM "$(basename "$PACKAGE_FILE")" >> "$PKGS_DB/installed.$SUM"
328 # Resolve package dependencies before package installation
329 install_all_deps "$PACKAGE_FILE"
332 # TODO: why this list-processed in the $PKGS_DB?
333 #[ -n "$INSTALL_LIST" ] && echo "$PACKAGE_FILE" >> "$PKGS_DB/$INSTALL_LIST-processed"
335 if [ -n "$sequence" ]; then
336 title 'Installation of package "%s" (%s)' "$PACKAGE" "$sequence"
337 else
338 title 'Installation of package "%s"' "$PACKAGE"
339 fi
341 if [ -z "$quiet" ]; then
342 print_short_description "$PACKAGE"
343 separator '-'
344 fi
346 action 'Copying package...'
347 cp "$PACKAGE_FILE" "$TMP_DIR"
348 status
350 cd "$TMP_DIR"
351 extract_package "$(basename "$PACKAGE_FILE")"
353 # Include temporary receipt to get the right variables
354 . "$TMP_DIR/receipt.var"
356 cd "$INSTALLED"
359 # Get files to remove if upgrading
360 # IFS here modified temporarily for processing filenames with spaces
361 IFS=$'\n'
362 if [ -f "$PACKAGE/files.list" ]; then
363 while read file; do
364 grep -q "^$(echo "$file" | grepesc)$" "$TMP_DIR/files.list" && continue
365 for i in $(cat "$PACKAGE/modifiers" 2>/dev/null;
366 fgrep -sl "$PACKAGE" */modifiers | cut -d/ -f1); do
367 grep -qs "^$(echo "$file" | grepesc)$" "$i/files.list" && continue 2
368 done
369 echo "$file"
370 done < "$PACKAGE/files.list" > "$TMP_DIR/files2remove.list"
371 fi
372 unset IFS
375 # Remember modified packages
376 action 'Remember modified packages...'
377 {
378 check=false
379 # TODO: why '[' the special?
380 # FIXME: we have files with spaces in our packages!
381 for i in $(fgrep -v [ $TMP_DIR/files.list); do
382 [ -e "$root$i" ] || continue
383 [ -d "$root$i" ] && continue
384 echo "- $i"
385 check=true
386 done ;
387 $check && \
388 for i in *; do
389 [ "$i" == "$PACKAGE" ] && continue
390 [ -s "$i/files.list" ] || continue
391 awk "{ printf \"$i %s\\n\",\$1 }" < "$i/files.list"
392 done;
393 } | awk '
394 {
395 if ($1 == "-" || file[$2] != "") {
396 file[$2] = file[$2] " " $1
397 if ($1 != "-") {
398 if (pkg[$1] == "") all = all " " $1
399 pkg[$1] = pkg[$1] " " $2
400 }
401 }
402 }
403 END {
404 for (i = split(all, p, " "); i > 0; i--)
405 for (j = split(pkg[p[i]], f, " "); j > 0; j--)
406 printf "%s %s\n",p[i],f[j];
407 }
408 ' | while read dir file; do
409 if grep -qs "^$dir$" "$PACKAGE/modifiers"; then
410 # Do not overload an overloaded file !
411 rm "$TMP_DIR/$file" 2>/dev/null
412 continue
413 fi
414 grep -qs "^$PACKAGE$" "$dir/modifiers" && continue
415 if [ -s "$dir/volatile.cpio.gz" ]; then
416 # We can modify backed up files without notice
417 zcat "$dir/volatile.cpio.gz" | cpio -t --quiet | \
418 grep -q "^${file#/}$" && continue
419 fi
420 echo "$PACKAGE" >> "$dir/modifiers"
421 done
422 status
425 cd "$TMP_DIR"
426 # Copy receipt, etc.
427 for file in receipt files.list description.txt $CHECKSUM; do
428 [ -f "$file" ] && cp "$file" "$INSTALLED/$PACKAGE"
429 done
432 # Pre-install commands
433 call_pre_install "$INSTALLED/$PACKAGE/receipt"
436 if [ -n "$CONFIG_FILES" ]; then
437 # Save "official" configuration files
438 action 'Saving configuration files...'
439 debug "\n"
441 cd fs
442 local config_file
443 for config_file in $CONFIG_FILES; do
444 debug " config_file: '$config_file'"
445 find ${config_file#/} -type f 2>/dev/null
446 done | cpio -o -H newc --quiet | gzip -9 > "$INSTALLED/$PACKAGE/volatile.cpio.gz"
447 cd ..
449 if [ -z "$newconf" ]; then
450 debug " no '--newconf': clean official config files"
451 # Keep user configuration files: remove "official" from fs tree
452 for config_file in $CONFIG_FILES; do
453 for config_file_official in $(find "fs$config_file" ! -type d 2>/dev/null | sed 's|^fs||'); do
454 if [ -e "$root$config_file_official" ]; then
455 debug " official '$config_file_official' will be skipped"
456 rm "fs$config_file_official"
457 else
458 debug " official '$config_file_official' will be written"
459 fi
460 done
461 done
462 fi
463 # always '[ Done ]' status, unless '--newconf' is passed or not
464 :; status
465 fi
468 if [ -n "$(ls fs/* 2>/dev/null)" ]; then
469 action 'Installing package...'
471 debug '\n resolving destination links in source'
472 IFS=$'\n'
473 for dir in $(find fs -type d | sed 's|^fs||;/^$/d'); do
474 if ldir=$(readlink -n $root$dir); then
475 debug " * mv 'fs$dir'\n -> 'fs${dir%/*}/$ldir'"
476 mkdir -p "fs${dir%/*}/${ldir%/*}"
477 mv "fs$dir" "fs${dir%/*}/$ldir"
478 fi
479 done
480 unset IFS
482 debug ' copying folders and files to destination'
483 cp -af fs/* "$root/"
484 status
485 fi
488 if [ -s files2remove.list ]; then
489 action 'Removing old files...'
490 while read file; do
491 dir="$root$file"
492 # Remove specified file
493 rm -f "$dir"
494 # Recursive remove non-empty up-dirs
495 while [ "$dir" != "$root/" ]; do
496 dir=$(dirname "$dir")
497 rmdir "$dir" 2>/dev/null || break
498 done
499 done < files2remove.list
500 :; status
501 fi
504 # Remove the temporary random directory.
505 action "Removing all tmp files..."
506 cd ..; rm -rf "$TMP_DIR"
507 status
510 # Post install commands
511 call_post_install "$INSTALLED/$PACKAGE/receipt"
516 # Update system databases
517 # Updating DBs is important process, so not to hide such errors (localized):
518 # chroot: can't execute '/usr/bin/***': No such file or directory
520 local fl="$INSTALLED/$PACKAGE/files.list" upd=0 udesk umime uicon uschm ukrnl
522 fgrep /usr/share/applications/ "$fl" | fgrep -q .desktop && udesk='yes'
523 fgrep -q /usr/share/mime "$fl" && umime='yes'
524 fgrep -q /usr/share/icon/hicolor "$fl" && uicon='yes'
525 fgrep -q /usr/share/glib-2.0/schemas "$fl" && uschm='yes'
526 fgrep /usr/lib/gdk-pixbuf "$fl" | fgrep -q .so && upixb='yes'
527 fgrep -q /lib/modules "$fl" && ukrnl='yes'
529 if [ -n "$udesk$umime$uicon$uschm$upixb$ukrnl" ]; then
530 action 'Update system databases...'
531 upd=1
532 fi
534 # package 'desktop-file-utils'
535 [ -n "$udesk" ] && chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
536 # package 'shared-mime-info'
537 [ -n "$umime" ] && chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
538 # packages 'gtk+', 'gtk+3'
539 [ -n "$uicon" ] && chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
540 # package 'glib'
541 [ -n "$uschm" ] && chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
542 # package 'gdk-pixbuf'
543 [ -n "$upixb" ] && chroot "$root/" /usr/bin/gdk-pixbuf-query-loaders --update-cache
544 # packages 'busybox', 'kmod', 'depmod'
545 [ -n "$ukrnl" ] && grep '/lib/modules' "$fl" | cut -d'/' -f4 | uniq | xargs chroot "$root/" /sbin/depmod -a
547 [ "$upd" -eq 1 ] && status
552 # Update installed.info
553 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g')
554 # Remove newlines from some receipts
555 DEPENDS=$(echo $DEPENDS)
556 PKG_SUM="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.$SUM" | cut -d' ' -f1)"
557 ii="$PKGS_DB/installed.info"
558 # Remove old entry
559 sed -i "/^$PACKAGE /d" "$ii"
560 cat >> "$ii" <<EOT
561 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $PKG_SUM
562 EOT
563 #awk -F$'\t' -vp="$PACKAGE" '$1==p' "$PKGS_DB/packages.info" > $ii
564 TEMP_FILE="$(mktemp)"
565 sort "$ii" > "$TEMP_FILE"; mv -f "$TEMP_FILE" "$ii"; chmod a+r "$ii"; unset ii
567 cd "$CUR_DIR"
568 footer "$(_ 'Package "%s" (%s) is installed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
570 # Log this activity
571 log_pkg Installed
573 # Remove package from upgrade list
574 [ -s "$UP_LIST" ] && sed -i "/^$PACKAGE\$/d" "$UP_LIST"
575 }
580 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
583 PACKAGE=$(
584 tmp_dir=$(mktemp -d); cd "$tmp_dir"
585 cpio --quiet -i receipt >/dev/null 2>&1
586 . receipt; echo $PACKAGE
587 rm -rf "$tmp_dir"
588 ) < "$1"
590 if grep -qs "^$PACKAGE$" "$BLOCKED"; then
591 _ 'Package "%s" blocked.' "$PACKAGE"
592 exit 1
593 fi
595 if [ -z "$forced" ]; then
596 # Check if a package is already installed
597 debug "\ncheck for installed package '$PACKAGE'"
599 awk -F$'\t' -vpv="$PACKAGE" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
601 if [ "$?" -eq 1 ]; then
602 if [ -z "$quiet" ]; then
603 newline
604 _ '"%s" package is already installed.' "$(colorize 34 "$PACKAGE")"
605 longline "$(_ 'You can use the --forced option to force installation.')"
606 newline
607 fi
608 exit 1
609 fi
610 fi
612 install_package "$(realpath "$1")"