tazpkg view modules/install @ rev 890

Module 'install': process option '--local' without using the databases
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu Dec 17 15:44:35 2015 +0200 (2015-12-17)
parents 66b0ea69ad69
children d034a2d99e3a
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'"
149 # "--nodeps" option prevents to install dependencies
150 if [ "$answer" -eq 0 -a -z "$nodeps" ]; then
151 debug " let's install missing packages"
152 for pkg in $missing_packages; do
153 debug " pkg='$pkg'"
154 if [ ! -d "$INSTALLED/$pkg" ]; then
155 # Package not installed
157 found='0'
158 # Prefer local packages
159 if [ -n "$local" ]; then
160 [ -z "$quiet" ] && _ 'Checking if package "%s" exists in local list...' "$pkg"
161 # Find local package
162 tempd="$(mktemp -d)"; cd "$tempd"
163 for pkgfile in $dir/$pkg-*.tazpkg; do
164 # Extract receipt from each matched package
165 cpio -F "$pkgfile" -i receipt >/dev/null 2>&1
166 name=$(. receipt; echo $PACKAGE)
167 rm receipt
168 if [ "$name" == "$pkg" ]; then
169 found='1'
170 # Install the first matched package: normally there is only one package
171 # with the $PACKAGE matched in the receipt
172 tazpkg install "$pkgfile"
173 fi
174 done
175 rm -r "$tempd"
176 fi
177 debug " found='$found'"
179 # Install package from the mirror
180 [ "$found" -eq 0 ] && tazpkg get-install "$pkg"
181 fi
182 done
183 else
184 # Answered 'No' to install dependencies, or '--nodeps' option given
185 newline
186 _ 'Leaving dependencies for package "%s" unresolved.' "$PACKAGE"
187 _ 'The package will be installed but will probably not work.'
188 newline
189 fi
190 }
193 # Extract a package with cpio and gzip/lzma.
195 extract_package() {
196 # input: $1 - path to package to be extracted; package should be in the current dir
197 # ROOT INDEPENDENT
198 action 'Extracting package...'
200 # Extract "outer layer": cpio; remove the original package file
201 cpio -idm --quiet < "$1" && rm -f "$1"
203 # "Inner layer" may vary
204 if [ -f fs.cpio.lzma ]; then
205 # "Plain" cpio.lzma
206 unlzma < fs.cpio.lzma | cpio -idm --quiet && rm fs.cpio.lzma
207 elif [ -f fs.cpio.gz ]; then
208 # "Fast" cpio.gz (used to pack-then-install process in most of get-packages)
209 zcat fs.cpio.gz | cpio -idm --quiet && rm fs.cpio.gz
210 fi
212 status
213 }
216 # Print short package description
218 print_short_description() {
219 # TODO: undigest repo support? priority...
220 # ROOT READY
221 local short_desc=''
223 # Try to find localized short description
224 for LC in $LANG ${LANG%_*}; do
225 [ -e "$PKGS_DB/packages-desc.$LC" ] &&
226 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $2; exit}' "$PKGS_DB/packages-desc.$LC")
227 done
229 # Try to find short description for mirrored package
230 [ -z "$short_desc" -a -s "$PKGS_DB/packages.info" ] &&
231 short_desc=$(awk -F$'\t' -vp="$1" '$1==p{print $4; exit}' "$PKGS_DB/packages.info")
233 [ -z "$short_desc" ] && short_desc="$SHORT_DESC"
235 longline "$short_desc"
236 }
239 grepesc() {
240 sed 's/\[/\\[/g'
241 }
246 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
248 # Block of receipt function callers
249 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
250 # Few receipts function should be patched now.
252 # Input: $1 = path to the receipt to be processed
254 # Pre-install commands
255 call_pre_install() {
256 local tmp
257 if grep -q '^pre_install()' "$1"; then
258 action 'Execute pre-install commands...'
259 tmp="$(mktemp)"
260 cp "$1" "$tmp"
261 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
262 ( . "$tmp"; pre_install "$root" )
263 status
264 rm "$tmp"
265 fi
267 }
268 # Post-install commands
269 call_post_install() {
270 local tmp
271 if grep -q '^post_install()' "$1"; then
272 action 'Execute post-install commands...'
273 tmp="$(mktemp)"
274 cp "$1" "$tmp"
275 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
276 ( . "$tmp"; post_install "$root" )
277 status
278 rm "$tmp"
279 fi
280 }
283 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
286 # This function installs a package in the rootfs.
288 install_package() {
289 # input: $1 = path to package to be installed
290 # dep: install_all_deps, print_short_description, extract_package, grepesc.
292 debug "\ninstall_package('$1')"
293 local dir
295 PACKAGE_FILE="$1"
296 TMP_DIR="$(mktemp -d)"
298 # Get receipt's variables and functions
299 { cd "$TMP_DIR"; cpio --quiet -i receipt >/dev/null 2>&1; } < "$PACKAGE_FILE"
300 # Why next code? "Bad" receipt sourcing can redefine some vital TazPkg variables.
301 (
302 . "$TMP_DIR/receipt"
303 cat > "$TMP_DIR/receipt.var" <<EOT
304 PACKAGE="$PACKAGE"
305 VERSION="$VERSION"
306 EXTRAVERSION="$EXTRAVERSION"
307 CATEGORY="$CATEGORY"
308 SHORT_DESC="$SHORT_DESC"
309 WEB_SITE="$WEB_SITE"
310 TAGS="$TAGS"
311 DEPENDS="$DEPENDS"
312 CONFIG_FILES="$CONFIG_FILES"
313 PACKED_SIZE="$PACKED_SIZE"
314 UNPACKED_SIZE="$UNPACKED_SIZE"
315 EOT
316 rm "$TMP_DIR/receipt"
317 )
318 . "$TMP_DIR/receipt.var"
321 # Make sure folder exists on new installs or upgrades
322 mkdir -p "$INSTALLED/$PACKAGE"
324 # Keep "modifiers" and "files.list" on upgrade
325 find "$INSTALLED/$PACKAGE" -type f \( ! -name modifiers ! -name files.list \) -delete
327 # Update "installed.md5"
328 # TODO: discontinue using 'installed.md5'
329 touch "$PKGS_DB/installed.$SUM"
330 sed -i "/ $(basename "$PACKAGE_FILE")$/d" "$PKGS_DB/installed.$SUM" 2>/dev/null
331 cd "$(dirname "$PACKAGE_FILE")"
332 $CHECKSUM "$(basename "$PACKAGE_FILE")" >> "$PKGS_DB/installed.$SUM"
334 # Resolve package dependencies before package installation
335 install_all_deps "$PACKAGE_FILE"
338 # TODO: why this list-processed in the $PKGS_DB?
339 #[ -n "$INSTALL_LIST" ] && echo "$PACKAGE_FILE" >> "$PKGS_DB/$INSTALL_LIST-processed"
341 if [ -n "$sequence" ]; then
342 title 'Installation of package "%s" (%s)' "$PACKAGE" "$sequence"
343 else
344 title 'Installation of package "%s"' "$PACKAGE"
345 fi
347 if [ -z "$quiet" ]; then
348 print_short_description "$PACKAGE"
349 separator '-'
350 fi
352 action 'Copying package...'
353 cp "$PACKAGE_FILE" "$TMP_DIR"
354 status
356 cd "$TMP_DIR"
357 extract_package "$(basename "$PACKAGE_FILE")"
359 # Include temporary receipt to get the right variables
360 . "$TMP_DIR/receipt.var"
362 cd "$INSTALLED"
365 # Get files to remove if upgrading
366 # IFS here modified temporarily for processing filenames with spaces
367 IFS=$'\n'
368 if [ -f "$PACKAGE/files.list" ]; then
369 while read file; do
370 grep -q "^$(echo "$file" | grepesc)$" "$TMP_DIR/files.list" && continue
371 for i in $(cat "$PACKAGE/modifiers" 2>/dev/null;
372 fgrep -sl "$PACKAGE" */modifiers | cut -d/ -f1); do
373 grep -qs "^$(echo "$file" | grepesc)$" "$i/files.list" && continue 2
374 done
375 echo "$file"
376 done < "$PACKAGE/files.list" > "$TMP_DIR/files2remove.list"
377 fi
378 unset IFS
381 # Remember modified packages
382 action 'Remember modified packages...'
383 {
384 check=false
385 # TODO: why '[' the special?
386 # FIXME: we have files with spaces in our packages!
387 for i in $(fgrep -v [ $TMP_DIR/files.list); do
388 [ -e "$root$i" ] || continue
389 [ -d "$root$i" ] && continue
390 echo "- $i"
391 check=true
392 done ;
393 $check && \
394 for i in *; do
395 [ "$i" == "$PACKAGE" ] && continue
396 [ -s "$i/files.list" ] || continue
397 awk "{ printf \"$i %s\\n\",\$1 }" < "$i/files.list"
398 done;
399 } | awk '
400 {
401 if ($1 == "-" || file[$2] != "") {
402 file[$2] = file[$2] " " $1
403 if ($1 != "-") {
404 if (pkg[$1] == "") all = all " " $1
405 pkg[$1] = pkg[$1] " " $2
406 }
407 }
408 }
409 END {
410 for (i = split(all, p, " "); i > 0; i--)
411 for (j = split(pkg[p[i]], f, " "); j > 0; j--)
412 printf "%s %s\n",p[i],f[j];
413 }
414 ' | while read dir file; do
415 if grep -qs "^$dir$" "$PACKAGE/modifiers"; then
416 # Do not overload an overloaded file !
417 rm "$TMP_DIR/$file" 2>/dev/null
418 continue
419 fi
420 grep -qs "^$PACKAGE$" "$dir/modifiers" && continue
421 if [ -s "$dir/volatile.cpio.gz" ]; then
422 # We can modify backed up files without notice
423 zcat "$dir/volatile.cpio.gz" | cpio -t --quiet | \
424 grep -q "^${file#/}$" && continue
425 fi
426 echo "$PACKAGE" >> "$dir/modifiers"
427 done
428 status
431 cd "$TMP_DIR"
432 # Copy receipt, etc.
433 for file in receipt files.list description.txt $CHECKSUM; do
434 [ -f "$file" ] && cp "$file" "$INSTALLED/$PACKAGE"
435 done
438 # Pre-install commands
439 call_pre_install "$INSTALLED/$PACKAGE/receipt"
442 if [ -n "$CONFIG_FILES" ]; then
443 # Save "official" configuration files
444 action 'Saving configuration files...'
445 debug "\n"
447 cd fs
448 local config_file
449 for config_file in $CONFIG_FILES; do
450 debug " config_file: '$config_file'"
451 find ${config_file#/} -type f 2>/dev/null
452 done | cpio -o -H newc --quiet | gzip -9 > "$INSTALLED/$PACKAGE/volatile.cpio.gz"
453 cd ..
455 if [ -z "$newconf" ]; then
456 debug " no '--newconf': clean official config files"
457 # Keep user configuration files: remove "official" from fs tree
458 for config_file in $CONFIG_FILES; do
459 for config_file_official in $(find "fs$config_file" ! -type d 2>/dev/null | sed 's|^fs||'); do
460 if [ -e "$root$config_file_official" ]; then
461 debug " official '$config_file_official' will be skipped"
462 rm "fs$config_file_official"
463 else
464 debug " official '$config_file_official' will be written"
465 fi
466 done
467 done
468 fi
469 # always '[ Done ]' status, unless '--newconf' is passed or not
470 :; status
471 fi
474 if [ -n "$(ls fs/* 2>/dev/null)" ]; then
475 action 'Installing package...'
477 debug '\n resolving destination links in source'
478 IFS=$'\n'
479 for dir in $(find fs -type d | sed 's|^fs||;/^$/d'); do
480 if ldir=$(readlink -n $root$dir); then
481 debug " * mv 'fs$dir'\n -> 'fs${dir%/*}/$ldir'"
482 mkdir -p "fs${dir%/*}/${ldir%/*}"
483 mv "fs$dir" "fs${dir%/*}/$ldir"
484 fi
485 done
486 unset IFS
488 debug ' copying folders and files to destination'
489 cp -af fs/* "$root/"
490 status
491 fi
494 if [ -s files2remove.list ]; then
495 action 'Removing old files...'
496 while read file; do
497 dir="$root$file"
498 # Remove specified file
499 rm -f "$dir"
500 # Recursive remove non-empty up-dirs
501 while [ "$dir" != "$root/" ]; do
502 dir=$(dirname "$dir")
503 rmdir "$dir" 2>/dev/null || break
504 done
505 done < files2remove.list
506 :; status
507 fi
510 # Remove the temporary random directory.
511 action "Removing all tmp files..."
512 cd ..; rm -rf "$TMP_DIR"
513 status
516 # Post install commands
517 call_post_install "$INSTALLED/$PACKAGE/receipt"
522 # Update system databases
523 # Updating DBs is important process, so not to hide such errors (localized):
524 # chroot: can't execute '/usr/bin/***': No such file or directory
526 local fl="$INSTALLED/$PACKAGE/files.list" upd=0 udesk umime uicon uschm ukrnl
528 fgrep /usr/share/applications/ "$fl" | fgrep -q .desktop && udesk='yes'
529 fgrep -q /usr/share/mime "$fl" && umime='yes'
530 fgrep -q /usr/share/icon/hicolor "$fl" && uicon='yes'
531 fgrep -q /usr/share/glib-2.0/schemas "$fl" && uschm='yes'
532 fgrep /usr/lib/gdk-pixbuf "$fl" | fgrep -q .so && upixb='yes'
533 fgrep -q /lib/modules "$fl" && ukrnl='yes'
535 if [ -n "$udesk$umime$uicon$uschm$upixb$ukrnl" ]; then
536 action 'Update system databases...'
537 upd=1
538 fi
540 # package 'desktop-file-utils'
541 [ -n "$udesk" ] && chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
542 # package 'shared-mime-info'
543 [ -n "$umime" ] && chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
544 # packages 'gtk+', 'gtk+3'
545 [ -n "$uicon" ] && chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
546 # package 'glib'
547 [ -n "$uschm" ] && chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
548 # package 'gdk-pixbuf'
549 [ -n "$upixb" ] && chroot "$root/" /usr/bin/gdk-pixbuf-query-loaders --update-cache
550 # packages 'busybox', 'kmod', 'depmod'
551 [ -n "$ukrnl" ] && grep '/lib/modules' "$fl" | cut -d'/' -f4 | uniq | xargs chroot "$root/" /sbin/depmod -a
553 [ "$upd" -eq 1 ] && status
558 # Update installed.info
559 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g')
560 # Remove newlines from some receipts
561 DEPENDS=$(echo $DEPENDS)
562 PKG_SUM="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.$SUM" | cut -d' ' -f1)"
563 ii="$PKGS_DB/installed.info"
564 # Remove old entry
565 sed -i "/^$PACKAGE /d" "$ii"
566 cat >> "$ii" <<EOT
567 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $PKG_SUM
568 EOT
569 #awk -F$'\t' -vp="$PACKAGE" '$1==p' "$PKGS_DB/packages.info" > $ii
570 TEMP_FILE="$(mktemp)"
571 sort "$ii" > "$TEMP_FILE"; mv -f "$TEMP_FILE" "$ii"; chmod a+r "$ii"; unset ii
573 cd "$CUR_DIR"
574 footer "$(_ 'Package "%s" (%s) is installed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
576 # Log this activity
577 log_pkg Installed
579 # Remove package from upgrade list
580 [ -s "$UP_LIST" ] && sed -i "/^$PACKAGE\$/d" "$UP_LIST"
581 }
586 #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
589 PACKAGE=$(
590 tmp_dir=$(mktemp -d); cd "$tmp_dir"
591 cpio --quiet -i receipt >/dev/null 2>&1
592 . receipt; echo $PACKAGE
593 rm -rf "$tmp_dir"
594 ) < "$1"
596 if grep -qs "^$PACKAGE$" "$BLOCKED"; then
597 _ 'Package "%s" blocked.' "$PACKAGE"
598 exit 1
599 fi
601 if [ -z "$forced" ]; then
602 # Check if a package is already installed
603 debug "\ncheck for installed package '$PACKAGE'"
605 awk -F$'\t' -vpv="$PACKAGE" '$1==pv { exit 1 }' "$PKGS_DB/installed.info"
607 if [ "$?" -eq 1 ]; then
608 if [ -z "$quiet" ]; then
609 newline
610 _ '"%s" package is already installed.' "$(colorize 34 "$PACKAGE")"
611 longline "$(_ 'You can use the --forced option to force installation.')"
612 newline
613 fi
614 exit 1
615 fi
616 fi
618 install_package "$(realpath "$1")"