tazpkg view tazpkg @ rev 952

Do not hard-code year in docs
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Oct 07 23:53:39 2017 +0200 (2017-10-07)
parents 0b78aae43c97
children 7454f6bf99e5
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() {
94 if [ -n "$debug" ]; then
95 colorize 036 "$@" >&2
96 echo -e "$(date +%f) $@" >> "${LOG/.log/.debug}"
97 fi
98 }
101 # Check if dir exists
103 check_dir() {
104 if [ ! -d "$1" ]; then
105 action 'Creating folder "%s"...' "$1"
106 mkdir -p "$1"
107 status
108 return 1
109 fi
110 }
113 # Check if the directories and files used by TazPkg exist.
114 # If not and user is root we create them.
116 check_base_dir() {
117 if [ "$(id -u)" == '0' ]; then
118 check_dir $1$CACHE_DIR
119 check_dir $1$INSTALLED
120 check_dir $1$SLITAZ_LOGS
121 if [ ! -f "$1$PKGS_DB/mirror" ]; then
122 echo "${DEFAULT_MIRROR%/}/" > $1$PKGS_DB/mirror
123 [ -n "$1" ] && cp $PKGS_DB/packages.* $1$PKGS_DB/
124 fi
125 fi
126 }
127 check_base_dir
132 # Check commandline for tazpkg syntax
134 check_cmd() {
135 for i in $@; do
136 case $i in
137 su)
138 check_root "$COMMAND"; continue;;
139 pkg)
140 [ -n "$PACKAGE" -o -n "$list" ] && continue
141 newline; _ 'Please specify a package name on the command line.';;
142 list)
143 [ -n "$PACKAGE" ] && continue
144 newline; _ 'Please specify a list name on the command line.';;
145 flavor)
146 [ -n "$PACKAGE" ] && continue
147 newline; _ 'Please specify a flavor name on the command line.';;
148 release)
149 [ -n "$PACKAGE" ] && continue
150 newline; _ 'Please specify a release name on the command line.';;
151 file)
152 [ -f "$PACKAGE_FILE" ] && continue
153 newline; _ 'Unable to find file "%s"' "$PACKAGE_FILE";;
154 dir)
155 [ -d "$TARGET_DIR" ] && continue
156 newline; _ 'Please specify an existing folder name on the command line.';;
157 pattern)
158 [ -n "$PACKAGE" ] && continue
159 newline; _ 'Please specify a pattern to search for.';;
160 receipt)
161 [ -f "$INSTALLED/$PACKAGE/receipt" ] && continue
162 newline; _ 'Unable to find the receipt "%s"' "$INSTALLED/$PACKAGE/receipt";;
163 esac
164 tazpkg -h "$COMMAND"
165 exit 1
166 done
167 }
170 # List support for getting/installing packages listed in the file
171 process_list() {
172 debug "\nprocess_list()\n list='$list'"
173 local tmp_list pkg
175 [ -z "$list" ] && return
177 tmp_list="$(mktemp)"
178 cp "$list" "$tmp_list"
179 debug " tmp_list='$tmp_list'"
181 # Upgrade tazpkg first. It may handle new features/formats...
182 # then upgrade essential packages early
183 debug ' process important packages...'
184 for pkg in busybox-pam busybox gcc-lib-base glibc-base slitaz-base-files tazpkg; do
185 debug " pkg='$pkg'"
186 pkg=$(grep "^$pkg" "$tmp_list")
187 [ -z "$pkg" ] && continue
188 # Specify here empty list to prevent looping while recursive calls
189 debug " tazpkg $COMMAND '$pkg' --list=''"
190 tazpkg $COMMAND "$pkg" --list=''
191 sed -i "/^$pkg$/d" "$tmp_list"
192 done
194 # Process the rest of the list
195 debug ' process the rest...'
196 for pkg in $(cat "$tmp_list"); do
197 debug "tazpkg $COMMAND '$pkg' --list=''"
198 tazpkg $COMMAND "$pkg" --list=''
199 done
201 # Clean
202 rm "$tmp_list"
203 debug ' END: process_list()'
204 }
209 # Shared functions
210 # ----------------
212 # Log TazPkg activity
214 log_pkg() {
215 local extra
217 [ "$1" == 'Installed' ] && \
218 extra=" - $(fgrep " $PACKAGE-$VERSION" $PKGS_DB/installed.$SUM | awk '{ print $1 }')"
220 [ -e "$LOG" ] || touch $LOG
222 [ -w "$LOG" ] &&
223 echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)$extra" >> $LOG
224 }
227 # Extract a package with cpio and gzip/lzma.
229 extract_package() {
230 action 'Extracting package...'
231 cpio -idm --quiet < "${PACKAGE_FILE##*/}" && rm -f "${PACKAGE_FILE##*/}"
232 if [ -f fs.cpio.lzma ]; then
233 unlzma < fs.cpio.lzma | cpio -idm --quiet && rm fs.cpio.lzma
234 elif [ -f fs.cpio.gz ]; then
235 zcat fs.cpio.gz | cpio -idm --quiet && rm fs.cpio.gz
236 fi
237 status
238 }
241 # Translate category names (must be last in line)
243 translate_category() {
244 sed "s|base-system$|$(_ base-system)|g; s|x-window$|$(_ x-window)|g;
245 s|utilities$|$(_ utilities)|g; s|network$|$(_ network)|g;
246 s|graphics$|$(_ graphics)|g; s|multimedia$|$(_ multimedia)|g;
247 s|office$|$(_ office)|g; s|development$|$(_ development)|g;
248 s|system-tools$|$(_ system-tools)|g; s|security$|$(_ security)|g;
249 s|games$|$(_ games)|g; s|misc$|$(_ misc)|g; s|meta$|$(_ meta)|g;
250 s|non-free$|$(_ non-free)|g"
251 }
254 # If category is not one of those translated in native language, keep it
255 # untranslated. This allows both native and English language support.
256 # This also supports custom categories.
257 # And now we support spaces in translated categories
259 reverse_translate_category() {
260 echo "$cat_i18n" | awk "BEGIN{FS=\" \"}{if (/^$@ /) a=\$2}END{if (a==\"\") a=\"$@\"; print a}"
261 }
263 # END: Shared functions
268 ###################
269 # TazPkg commands #
270 ###################
272 case "$COMMAND" in
273 call)
274 # Call internal tazpkg function from external tazpkg module or other script.
275 # Useful for functions sharing.
276 shift
277 # Check to call only tazpkg functions
278 fgrep -q "$1()" "$0" && $@
279 ;;
282 list|-l)
283 # Various lists
284 shift
285 case $1 in
286 b|blocked) @@MODULES@@/list blocked;;
287 c|cat|categories) @@MODULES@@/list categories;;
288 l|linked) @@MODULES@@/list linked;;
289 '') @@MODULES@@/list installed;;
290 *) @@MODULES@@/list installed_of_category "$@";;
291 esac
292 ;;
293 -lb) @@MODULES@@/list blocked;;
294 -lc) @@MODULES@@/list categories;;
295 -ll) @@MODULES@@/list linked;;
297 -lm|list-mirror) @@MODULES@@/list mirrored;;
298 -lf|list-files) check_cmd pkg; @@MODULES@@/list installed_files "$PACKAGE";;
299 -a|activity|log) @@MODULES@@/list activity;;
300 list-config) @@MODULES@@/list config_files "$2";;
301 list-suggested) @@MODULES@@/list suggested;;
304 # Information about package
305 info)
306 check_cmd pkg; @@MODULES@@/info "$2";;
309 desc|-d)
310 # Display package description
311 check_cmd pkg; @@MODULES@@/description "$2";;
314 search|-s|-si|-sl)
315 # Search for a package by pattern or name.
316 check_cmd pattern
318 # Extend short options to long analogs
319 for i in $@; do
320 case "$i" in
321 -i|-si) export installed='yes';;
322 -l|-sl) export list='yes';;
323 -m) export mirror='yes';;
324 esac
325 done
327 @@MODULES@@/search pkg "$2"
328 ;;
331 search-file|-sf)
332 # Search for a file by pattern or name in all files.list.
333 check_cmd pattern; @@MODULES@@/search file "$2";;
336 search-pkgname|-sp)
337 # Search for a package name
338 check_cmd pattern; @@MODULES@@/search file2 "$2";;
341 add-flavor)
342 # Install a set of packages from a flavor.
343 check_cmd su flavor; shift; @@MODULES@@/flavor $@;;
344 install-flavor)
345 # Install a set of packages from a flavor and purge other ones.
346 check_cmd su flavor; shift; purge='yes' @@MODULES@@/flavor $@;;
349 set-release)
350 # Change current release and upgrade packages.
351 check_cmd su release
352 @@MODULES@@/set-release "$2";;
355 remove|-r)
356 # Remove packages.
357 check_cmd su pkg; shift; @@MODULES@@/remove $@;;
358 # TODO: remove multiple packages
361 extract|-e)
362 # Extract .tazpkg cpio archive into a directory.
363 check_cmd pkg file; shift; @@MODULES@@/extract $@;;
366 recompress)
367 # Recompress .tazpkg cpio archive with lzma.
368 check_cmd su pkg file; @@MODULES@@/recompress "$PACKAGE_FILE";;
371 repack-config)
372 # Create SliTaz package archive from configuration files.
373 check_cmd su; @@MODULES@@/repack-config;;
376 repack)
377 # Create SliTaz package archive from an installed package.
378 check_cmd pkg receipt; shift; @@MODULES@@/repack $@;;
381 pack)
382 # Create SliTaz package archive using cpio and lzma.
383 # TODO: Cook also packs packages, we should share code in libpkg.sh
384 check_cmd pkg; shift; @@MODULES@@/pack $@;;
387 recharge)
388 # Recharge packages databases from a mirror.
389 #
390 # WARNING: The 'mirrors' file has all SliTaz mirrors but 'mirror'
391 # must have only the chosen main mirror.
392 #
393 check_cmd su; shift; @@MODULES@@/recharge $@;;
396 up|upgrade)
397 # This is the new way to upgrade packages making 'upgrade' and
398 # upgradeable out-of-date. This new way is much, much more faster!
399 # Look into installed packages and get data from receipt, it is fast
400 # and easy to handle vars after using only md5sum to compare packages
401 #
402 check_cmd su
403 for opt in $@; do
404 case "$opt" in
405 -i) export install='yes';;
406 -c) export check='yes';;
407 esac
408 done
410 @@MODULES@@/upgrade
411 ;;
414 bugs)
415 # Show known bugs in package(s)
416 shift; @@MODULES@@/bugs $@;;
419 check)
420 # Check installed packages set.
421 shift; @@MODULES@@/check $@;;
424 block|-b|unblock|-u|chblock)
425 # Add/remove a pkg name to the list of blocked packages.
426 check_cmd su pkg; @@MODULES@@/block $@;;
429 get|-g)
430 # Download a package with wget.
431 check_cmd pkg; shift
432 # Get all the packages given on command line
433 export nocache='yes'
434 for i in $@; do
435 pkg="$(@@MODULES@@/get $i)" && _ 'Done: %s' "${pkg##*/}"
436 done
437 # Get all the packages listed in the file
438 process_list
439 ;;
442 install|-i)
443 # Install .tazpkg packages.
444 check_cmd su pkg file; shift
445 for i in $@; do
446 @@MODULES@@/install $i
447 done
448 # Install all the packages listed in the file
449 process_list
450 ;;
453 get-install|-gi)
454 # Download and install a package.
455 check_cmd su pkg; shift
456 export tazpkg_command='get-install'
457 # Get and install all the packages given on command line
458 for i in $@; do
459 pkg="$(@@MODULES@@/get $i)" && @@MODULES@@/install "$pkg"
460 done
461 # Get and install all the packages listed in the file
462 process_list
463 ;;
466 get-list|install-list|get-install-list)
467 # Install a set of packages from a list.
468 check_cmd su list file
469 COMMAND=${COMMAND%-list}
470 export list="$2"
471 process_list
472 ;;
475 clean-cache|-cc)
476 # Remove all downloaded packages.
477 check_cmd su; @@MODULES@@/cache clean;;
478 list-cache)
479 # List of packages in the cache.
480 @@MODULES@@/cache list;;
483 list-undigest)
484 # List undigest mirrors
485 @@MODULES@@/mirror list;;
486 remove-undigest)
487 # Remove undigest mirror
488 check_cmd su; shift; @@MODULES@@/mirror remove $@;;
489 add-undigest|setup-undigest)
490 # Add undigest mirror
491 check_cmd su; shift; @@MODULES@@/mirror add $@;;
492 setup-mirror|-sm)
493 # Change main mirror
494 check_cmd su; shift; @@MODULES@@/mirror setup $@;;
497 reconfigure)
498 # Replay post_install from receipt
499 check_cmd su pkg receipt; @@MODULES@@/reconfigure "$2";;
502 shell)
503 # TazPkg SHell
504 if [ "$(id -u)" -eq 0 ]; then
505 PROMPT="\\033[1;33mtazpkg\\033[0;39m# "
506 else
507 PROMPT="\\033[1;33mtazpkg\\033[0;39m> "
508 fi
509 if [ -z "$noheader" ]; then
510 clear
511 title 'TazPkg SHell.'
512 _ "Type 'usage' to list all available commands or 'quit' or 'q' to exit."
513 newline
514 fi
515 while true; do
516 echo -en "$PROMPT"; read cmd
517 case $cmd in
518 q|quit)
519 break ;;
520 shell)
521 _ 'You are already running a TazPkg SHell.' ;;
522 su)
523 su -c 'exec tazpkg shell --noheader' && break ;;
524 "")
525 continue ;;
526 *)
527 tazpkg $cmd ;;
528 esac
529 done ;;
532 depends)
533 # Display dependencies tree
534 check_cmd pkg; shift; @@MODULES@@/depends depends $@;;
535 rdepends)
536 # Display reverse dependencies tree
537 check_cmd pkg; shift; @@MODULES@@/depends rdepends $@;;
540 convert|-c)
541 # convert misc package format to .tazpkg
542 check_cmd file; shift; @@MODULES@@/convert $@;;
545 link)
546 # link a package from another SliTaz installation
547 check_cmd su pkg dir; shift; @@MODULES@@/link $@;;
550 help|-h)
551 # TazPkg help system
552 shift; @@MODULES@@/help $@
553 ;;
556 mkdb)
557 # Make TazPkg database
558 shift; @@MODULES@@/mkdb $@
559 ;;
562 '')
563 # Default to summary
564 @@MODULES@@/summary
565 ;;
568 usage|*)
569 # Print a short help or give usage for an unknown or empty command.
570 @@MODULES@@/help
571 ;;
572 esac
574 exit 0