tazpkg view tazpkg @ rev 845

install: rebuild fs/ tree respecting destination symlinks before installing files and folders to filesystem
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Oct 06 15:46:41 2015 +0300 (2015-10-06)
parents 0560ba4306a1
children 8a73a58ed3cb
line source
1 #!/bin/sh
2 #
3 # TazPkg - Tiny autonomous zone packages manager.
4 #
5 # This is a lightweight packages manager for *.tazpkg files written in SHell
6 # script. It works well with Busybox ash shell and bash. TazPkg lets you
7 # list, install, remove, download or get information about a package. You
8 # can use 'tazpkg usage' to get a list of commands with short descriptions.
9 # TazPkg also resolves dependencies and can upgrade packages from a mirror.
10 #
11 # (C) 2007-2015 SliTaz - GNU General Public License v3.
12 #
13 # Authors: See the AUTHORS files
14 #
17 ####################
18 # Script variables #
19 ####################
21 . /etc/slitaz/slitaz.conf
22 . /etc/slitaz/tazpkg.conf
24 . /lib/libtaz.sh
25 . /usr/lib/slitaz/libpkg.sh
26 . @@MODULES@@/find-depends
28 # Internationalization.
29 export TEXTDOMAIN='tazpkg'
30 _() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; echo; }
31 _n() { local T="$1"; shift; printf "$(gettext "$T")" "$@"; }
32 _p() {
33 local S="$1" P="$2" N="$3"; shift 3;
34 printf "$(ngettext "$S" "$P" "$N")" "$@"; }
39 # Remove all --parameters from cmdline
40 #-------------------------------------
41 # (thanks to libtaz.sh all --parameters are already set to variables)
43 IFS=$'\n'
44 set -- $(echo "$*" | sed '/^--/d')
45 unset IFS
48 # Initialize some variables to use words rather than numbers for functions
49 # and actions.
51 COMMAND="$1"
52 PACKAGE="${2%/}"
53 PACKAGE_DIR="$(cd $(dirname "$PACKAGE" 2>/dev/null) 2>/dev/null; pwd)"
54 [ -n "$PACKAGE" ] && PACKAGE_FILE="$PACKAGE_DIR/${PACKAGE##*/}"
55 if [ -f "$PACKAGE" ]; then
56 # Set pkg basename for install, extract
57 PACKAGE="$(basename "$PACKAGE" .tazpkg 2>/dev/null)"
58 else
59 # Pkg name for remove, search and all other cmds
60 PACKAGE="${PACKAGE%.tazpkg}"
61 fi
62 TARGET_DIR="$3"
63 export TOP_DIR="$(pwd)"
64 TMP_DIR="/tmp/$RANDOM"
65 INSTALL_LIST=''
66 SAVE_CACHE_DIR="$CACHE_DIR"
68 # Path to tazpkg used dir and configuration files
69 MIRROR="$PKGS_DB/mirror"
70 BLOCKED="$PKGS_DB/blocked-packages.list"
71 UP_LIST="$PKGS_DB/packages.up"
72 DEFAULT_MIRROR="$ONLINE_PKGS"
74 # TazPkg version
75 export VERSION=$(awk -F$'\t' '$1=="tazpkg"{print $2}' "$PKGS_DB/installed.info")
76 # User Agent
77 export UA="TazPkg-$VERSION"
82 ####################
83 # Script functions #
84 ####################
87 # Interactive mode
89 im() { tty -s; }
92 # Show debug messages
93 debug() { if [ -n "$debug" ]; then colorize 036 "$@" >&2; fi; }
96 # Check if dir exists
98 check_dir() {
99 if [ ! -d "$1" ]; then
100 action 'Creating folder "%s"...' "$1"
101 mkdir -p "$1"
102 status
103 return 1
104 fi
105 }
108 # Check if the directories and files used by TazPkg exist.
109 # If not and user is root we create them.
111 check_base_dir() {
112 if [ "$(id -u)" == '0' ]; then
113 check_dir $1$CACHE_DIR
114 check_dir $1$INSTALLED
115 check_dir $1$SLITAZ_LOGS
116 if [ ! -f "$1$PKGS_DB/mirror" ]; then
117 echo "${DEFAULT_MIRROR%/}/" > $1$PKGS_DB/mirror
118 [ -n "$1" ] && cp $PKGS_DB/packages.* $1$PKGS_DB/
119 fi
120 fi
121 }
122 check_base_dir
127 # Check commandline for tazpkg syntax
129 check_cmd() {
130 for i in $@; do
131 case $i in
132 su)
133 check_root "$COMMAND"; continue;;
134 pkg)
135 [ -n "$PACKAGE" -o -n "$list" ] && continue
136 newline; _ 'Please specify a package name on the command line.';;
137 list)
138 [ -n "$PACKAGE" ] && continue
139 newline; _ 'Please specify a list name on the command line.';;
140 flavor)
141 [ -n "$PACKAGE" ] && continue
142 newline; _ 'Please specify a flavor name on the command line.';;
143 release)
144 [ -n "$PACKAGE" ] && continue
145 newline; _ 'Please specify a release name on the command line.';;
146 file)
147 [ -f "$PACKAGE_FILE" ] && continue
148 newline; _ 'Unable to find file "%s"' "$PACKAGE_FILE";;
149 dir)
150 [ -d "$TARGET_DIR" ] && continue
151 newline; _ 'Please specify an existing folder name on the command line.';;
152 pattern)
153 [ -n "$PACKAGE" ] && continue
154 newline; _ 'Please specify a pattern to search for.';;
155 receipt)
156 [ -f "$INSTALLED/$PACKAGE/receipt" ] && continue
157 newline; _ 'Unable to find the receipt "%s"' "$INSTALLED/$PACKAGE/receipt";;
158 esac
159 tazpkg -h "$COMMAND"
160 exit 1
161 done
162 }
165 # List support for getting/installing packages listed in the file
166 process_list() {
167 debug "\nprocess_list()\n list='$list'"
168 local tmp_list pkg
170 [ -z "$list" ] && return
172 tmp_list="$(mktemp)"
173 cp "$list" "$tmp_list"
174 debug " tmp_list='$tmp_list'"
176 # Upgrade tazpkg first. It may handle new features/formats...
177 # then upgrade essential packages early
178 debug ' process important packages...'
179 for pkg in busybox-pam busybox gcc-lib-base glibc-base slitaz-base-files tazpkg; do
180 debug " pkg='$pkg'"
181 pkg=$(grep "^$pkg" "$tmp_list")
182 [ -z "$pkg" ] && continue
183 # Specify here empty list to prevent looping while recursive calls
184 debug " tazpkg $COMMAND '$pkg' --list=''"
185 tazpkg $COMMAND "$pkg" --list=''
186 sed -i "/^$pkg$/d" "$tmp_list"
187 done
189 # Process the rest of the list
190 debug ' process the rest...'
191 for pkg in $(cat "$tmp_list"); do
192 debug "tazpkg $COMMAND '$pkg' --list=''"
193 tazpkg $COMMAND "$pkg" --list=''
194 done
196 # Clean
197 rm "$tmp_list"
198 debug ' END: process_list()'
199 }
204 # Shared functions
205 # ----------------
207 # Log TazPkg activity
209 log_pkg() {
210 local extra
212 [ "$1" == 'Installed' ] && \
213 extra=" - $(fgrep " $PACKAGE-$VERSION" $PKGS_DB/installed.$SUM | awk '{ print $1 }')"
215 [ -e "$LOG" ] || touch $LOG
217 [ -w "$LOG" ] &&
218 echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)$extra" >> $LOG
219 }
222 # Extract a package with cpio and gzip/lzma.
224 extract_package() {
225 action 'Extracting package...'
226 cpio -idm --quiet < "${PACKAGE_FILE##*/}" && rm -f "${PACKAGE_FILE##*/}"
227 if [ -f fs.cpio.lzma ]; then
228 unlzma < fs.cpio.lzma | cpio -idm --quiet && rm fs.cpio.lzma
229 elif [ -f fs.cpio.gz ]; then
230 zcat fs.cpio.gz | cpio -idm --quiet && rm fs.cpio.gz
231 fi
232 status
233 }
236 # Translate category names (must be last in line)
238 translate_category() {
239 sed "s|base-system$|$(_ base-system)|g; s|x-window$|$(_ x-window)|g;
240 s|utilities$|$(_ utilities)|g; s|network$|$(_ network)|g;
241 s|graphics$|$(_ graphics)|g; s|multimedia$|$(_ multimedia)|g;
242 s|office$|$(_ office)|g; s|development$|$(_ development)|g;
243 s|system-tools$|$(_ system-tools)|g; s|security$|$(_ security)|g;
244 s|games$|$(_ games)|g; s|misc$|$(_ misc)|g; s|meta$|$(_ meta)|g;
245 s|non-free$|$(_ non-free)|g"
246 }
249 # If category is not one of those translated in native language, keep it
250 # untranslated. This allows both native and English language support.
251 # This also supports custom categories.
252 # And now we support spaces in translated categories
254 reverse_translate_category() {
255 echo "$cat_i18n" | awk "BEGIN{FS=\" \"}{if (/^$@ /) a=\$2}END{if (a==\"\") a=\"$@\"; print a}"
256 }
258 # END: Shared functions
263 ###################
264 # TazPkg commands #
265 ###################
267 case "$COMMAND" in
268 call)
269 # Call internal tazpkg function from external tazpkg module or other script.
270 # Useful for functions sharing.
271 shift
272 # Check to call only tazpkg functions
273 fgrep -q "$1()" "$0" && $@
274 ;;
277 list|-l)
278 # Various lists
279 shift
280 case $1 in
281 b|blocked) @@MODULES@@/list blocked;;
282 c|cat|categories) @@MODULES@@/list categories;;
283 l|linked) @@MODULES@@/list linked;;
284 '') @@MODULES@@/list installed;;
285 *) @@MODULES@@/list installed_of_category "$@";;
286 esac
287 ;;
288 -lb) @@MODULES@@/list blocked;;
289 -lc) @@MODULES@@/list categories;;
290 -ll) @@MODULES@@/list linked;;
292 -lm|list-mirror) @@MODULES@@/list mirrored;;
293 -lf|list-files) check_cmd pkg; @@MODULES@@/list installed_files "$PACKAGE";;
294 -a|activity|log) @@MODULES@@/list activity;;
295 list-config) @@MODULES@@/list config_files "$2";;
296 list-suggested) @@MODULES@@/list suggested;;
299 # Information about package
300 info)
301 check_cmd pkg; @@MODULES@@/info "$2";;
304 desc|-d)
305 # Display package description
306 check_cmd pkg; @@MODULES@@/description "$2";;
309 search|-s|-si|-sl|-sm)
310 # Search for a package by pattern or name.
311 check_cmd pattern
313 # Extend short options to long analogs
314 for i in $@; do
315 case "$i" in
316 -i|-si) export installed='yes';;
317 -l|-sl) export list='yes';;
318 -m|-sm) export mirror='yes';;
319 esac
320 done
322 @@MODULES@@/search pkg "$2"
323 ;;
326 search-file|-sf)
327 # Search for a file by pattern or name in all files.list.
328 check_cmd pattern; @@MODULES@@/search file "$2";;
331 search-pkgname|-sp)
332 # Search for a package name
333 check_cmd pattern; @@MODULES@@/search file2 "$2";;
336 add-flavor)
337 # Install a set of packages from a flavor.
338 check_cmd su flavor; shift; @@MODULES@@/flavor $@;;
339 install-flavor)
340 # Install a set of packages from a flavor and purge other ones.
341 check_cmd su flavor; shift; purge='yes' @@MODULES@@/flavor $@;;
344 set-release)
345 # Change current release and upgrade packages.
346 check_cmd su release
347 @@MODULES@@/set-release "$2";;
350 remove|-r)
351 # Remove packages.
352 check_cmd su pkg; shift; @@MODULES@@/remove $@;;
353 # TODO: remove multiple packages
356 extract|-e)
357 # Extract .tazpkg cpio archive into a directory.
358 check_cmd pkg file; shift; @@MODULES@@/extract $@;;
361 recompress)
362 # Recompress .tazpkg cpio archive with lzma.
363 check_cmd su pkg file; @@MODULES@@/recompress "$PACKAGE_FILE";;
366 repack-config)
367 # Create SliTaz package archive from configuration files.
368 check_cmd su; @@MODULES@@/repack-config;;
371 repack)
372 # Create SliTaz package archive from an installed package.
373 check_cmd pkg receipt; shift; @@MODULES@@/repack $@;;
376 pack)
377 # Create SliTaz package archive using cpio and lzma.
378 # TODO: Cook also pack packages, we should share code in libpkg.sh
379 check_cmd pkg; shift; @@MODULES@@/pack $@;;
382 recharge)
383 # Recharge packages databases from a mirror.
384 #
385 # WARNING: The 'mirrors' file has all SliTaz mirrors but 'mirror'
386 # must have only the chosen main mirror.
387 #
388 check_cmd su; shift; @@MODULES@@/recharge $@;;
391 up|upgrade)
392 # This is the new way to upgrade packages making 'upgrade' and
393 # upgradeable out-of-date. This new way is much, much more faster!
394 # Look into installed packages and get data from receipt, it is fast
395 # and easy to handle vars after using only md5sum to compare packages
396 #
397 check_cmd su
398 for opt in $@; do
399 case "$opt" in
400 -i) export install='yes';;
401 -c) export check='yes';;
402 esac
403 done
405 @@MODULES@@/upgrade
406 ;;
409 bugs)
410 # Show known bugs in package(s)
411 shift; @@MODULES@@/bugs $@;;
414 check)
415 # Check installed packages set.
416 shift; @@MODULES@@/check $@;;
419 block|-b|unblock|-u|chblock)
420 # Add/remove a pkg name to the list of blocked packages.
421 check_cmd su pkg; @@MODULES@@/block $@;;
424 get|-g)
425 # Download a package with wget.
426 check_cmd pkg; shift
427 # Get all the packages given on command line
428 export nocache='yes'
429 for i in $@; do
430 pkg="$(@@MODULES@@/get $i)" && _ 'Done: %s' "${pkg##*/}"
431 done
432 # Get all the packages listed in the file
433 process_list
434 ;;
437 install|-i)
438 # Install .tazpkg packages.
439 check_cmd su pkg file; shift
440 for i in $@; do
441 @@MODULES@@/install $i
442 done
443 # Install all the packages listed in the file
444 process_list
445 ;;
448 get-install|-gi)
449 # Download and install a package.
450 check_cmd su pkg; shift
451 # Get and install all the packages given on command line
452 for i in $@; do
453 pkg="$(@@MODULES@@/get $i)" && @@MODULES@@/install "$pkg"
454 done
455 # Get and install all the packages listed in the file
456 process_list
457 ;;
460 get-list|install-list|get-install-list)
461 # Install a set of packages from a list.
462 check_cmd su list file
463 COMMAND=${COMMAND%-list}
464 export list="$2"
465 process_list
466 ;;
469 clean-cache|-cc)
470 # Remove all downloaded packages.
471 check_cmd su; @@MODULES@@/cache clean;;
474 list-undigest)
475 # List undigest mirrors
476 @@MODULES@@/mirror list;;
477 remove-undigest)
478 # Remove undigest mirror
479 check_cmd su; shift; @@MODULES@@/mirror remove $@;;
480 add-undigest|setup-undigest)
481 # Add undigest mirror
482 check_cmd su; shift; @@MODULES@@/mirror add $@;;
483 setup-mirror|-sm)
484 # Change main mirror
485 check_cmd su; shift; @@MODULES@@/mirror setup $@;;
488 reconfigure)
489 # Replay post_install from receipt
490 check_cmd su pkg receipt; @@MODULES@@/reconfigure "$2";;
493 shell)
494 # TazPkg SHell
495 if [ "$(id -u)" -eq 0 ]; then
496 PROMPT="\\033[1;33mtazpkg\\033[0;39m# "
497 else
498 PROMPT="\\033[1;33mtazpkg\\033[0;39m> "
499 fi
500 if [ -z "$noheader" ]; then
501 clear
502 title 'TazPkg SHell.'
503 _ "Type 'usage' to list all available commands or 'quit' or 'q' to exit."
504 newline
505 fi
506 while true; do
507 echo -en "$PROMPT"; read cmd
508 case $cmd in
509 q|quit)
510 break ;;
511 shell)
512 _ 'You are already running a TazPkg SHell.' ;;
513 su)
514 su -c 'exec tazpkg shell --noheader' && break ;;
515 "")
516 continue ;;
517 *)
518 tazpkg $cmd ;;
519 esac
520 done ;;
523 depends)
524 # Display dependencies tree
525 check_cmd pkg; shift; @@MODULES@@/depends depends $@;;
526 rdepends)
527 # Display reverse dependencies tree
528 check_cmd pkg; shift; @@MODULES@@/depends rdepends $@;;
531 convert|-c)
532 # convert misc package format to .tazpkg
533 check_cmd file; shift; @@MODULES@@/convert $@;;
536 link)
537 # link a package from another SliTaz installation
538 check_cmd su pkg dir; shift; @@MODULES@@/link $@;;
541 help|-h)
542 # TazPkg help system
543 shift; @@MODULES@@/help $@
544 ;;
547 mkdb)
548 # Make TazPkg database
549 shift; @@MODULES@@/mkdb $@
550 ;;
553 '')
554 # Default to summary
555 @@MODULES@@/summary
556 ;;
559 usage|*)
560 # Print a short help or give usage for an unknown or empty command.
561 @@MODULES@@/help
562 ;;
563 esac
565 exit 0