spk view spk-add @ rev 30

spk-add: some colors, improvments, usage
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 15 19:35:44 2012 +0200 (2012-05-15)
parents 1e166d1364d7
children ea3c01346d05
line source
1 #!/bin/sh
2 #
3 # Spk-add - Install SliTaz packages. Read the README before adding or
4 # modifing any code in spk!
5 #
6 # Copyright (C) SliTaz GNU/Linux - BSD License
7 # Author: See AUTHORS files
8 #
9 . /usr/lib/slitaz/libspk.sh
11 # Set to / for now until we add installing to chroot support
12 # Could we update tools so they do not need this?
13 ROOT=""
14 TMP_DIR="/tmp/$RANDOM"
16 #
17 # Functions
18 #
20 # Help and usage
21 usage() {
22 name=$(basename $0)
23 cat << EOT
25 $(boldify $(gettext "Usage:")) $name [packages|--options]
27 $(gettext "Install SliTaz Packages")
29 $(boldify $(gettext "Options:"))
30 --forced $(gettext "Force package reinstallation")
31 --root= $(gettext "Set the root file system path")
33 $(boldify $(gettext "Examples:"))
34 $name package1 package2 packageN
36 EOT
37 exit 0
38 }
40 # Update system databases
41 update_databases() {
42 if [ -f $ROOT/usr/bin/update-desktop-database ] && [ -n "$updatedesktopdb" ]; then
43 chroot "$ROOT/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
44 fi
45 # Mimetypes
46 if [ -f $ROOT/usr/bin/update-mime-database ] && [ -n "$updatemimedb" ]; then
47 chroot "$ROOT/" /usr/bin/update-mime-database /usr/share/mime
48 fi
49 # Icons
50 if [ -f $ROOT/usr/bin/gtk-update-icon-cache ] && [ -n "$updateicondb" ]; then
51 chroot "$ROOT/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
52 fi
53 # Glib schemas
54 if [ -f $ROOT/usr/bin/glib-compile-schemas ] && [ -n "$compile_schemas" ]; then
55 chroot "$ROOT/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
56 fi
57 # Kernel modules
58 if [ -f $ROOT/sbin/depmod ] && [ -n "$updatedepmod" ]; then
59 chroot "$ROOT/" /sbin/depmod -a
60 fi
61 }
63 # This function installs a package in the rootfs.
64 # Parameters: package_name package_file
65 install_package() {
66 local package_file=$1
68 # Never used I think!!!
69 # Set by receipt: pre_depends() DEPENDS SELF_INSTALL CONFIG_FILES post_install()
71 # Create package path early to avoid dependencies loop
72 mkdir -p $TMP_DIR
73 extract_receipt $TMP_DIR $package_file
74 source $TMP_DIR/receipt
76 local package_name=$PACKAGE
77 local package_dir="$installed/$package_name"
78 mkdir -p $package_dir
80 # Run pre_depends from receipt if it exists
81 if grep -q ^pre_depends $TMP_DIR/receipt; then
82 pre_depends $ROOT
83 fi
85 # Create modifiers and files.list if they do not exist
86 # Why ? If missing files.list it's meta packages.
87 #touch $package_dir/modifiers
88 #touch $package_dir/files.list
90 # Add package checksum to pkgsmd5
91 sed -i "/ $(basename $package_dir)$/d" $pkgsmd5 2> /dev/null
92 oldpwd=$(pwd)
93 cd $(dirname $package_file) || exit 1
94 $CHECKSUM $(basename $package_file) >> $pkgsmd5
95 cd $oldpwd
97 # Resolve package deps.
98 if missing_deps $package_name $DEPENDS; then
99 install_deps $package_name $DEPENDS
100 fi
102 newline
103 boldify $(gettext "Installation of :") $package_name
104 separator
105 eval_gettext "Copying \$package_name... "
106 cp $package_file $TMP_DIR
107 status
109 # Extract Package
110 cd $TMP_DIR || exit 1
111 rm receipt
112 spk-archive extract $package_file
113 cd - >/dev/null
115 # Get files to remove if upgrading
116 local files_to_remove
117 if [ -f $package_dir/files.list ]; then
118 for file in $($package_dir/files.list); do
119 grep -q "^$(echo $file | grepesc)$" $TMP_DIR/files.list && continue
120 local modifiers=$(cat $package_dir/modifiers 2> /dev/null;\
121 fgrep -sl $package_dir */modifiers | cut -d/ -f1)
122 for i in modifiers; do
123 grep -qs "^$(echo $file | grepesc)$" $i/files.list && continue 2
124 done
125 files_to_remove="$files_to_remove $file"
126 done
127 fi
129 local check=false
130 local file_list
131 # Create list of all possibly modified files
132 for i in $(fgrep -v [ $TMP_DIR/files.list); do
133 [ -e "$ROOT$i" ] || continue
134 [ -d "$ROOT$i" ] && continue
135 file_list="$file_list $i"
136 check=true
137 done
139 # Check possibly modified files against other packages file.list
140 if $check; then
141 for pkg in $INSTALLED/*; do
142 [ "$pkg" == "$package_name" ] && continue
143 [ -s $pkg/files.list ] || continue
145 for file in $file_list; do
146 # $package_name wants to install $file which is already
147 # Installed from $pkg
148 if grep -q ^$file$ $pkg/files.list; then
149 # Tell $pkg that $package_name is going to overwrite some of its files
150 if [ -s "$pkg/volatile.cpio.gz" ]; then
151 # We can modify backed up files without notice
152 zcat $pkg/volatile.cpio.gz | cpio -t --quiet | \
153 grep -q "^${file#/}$" && continue
154 fi
155 echo "$package_name" >> $pkg/modifiers
156 fi
157 done
158 done
159 fi
161 cd $TMP_DIR || exit 1
162 cp receipt files.list $package_dir
163 # Copy the description if found.
164 [ -f "description.txt" ] && cp description.txt $package_dir
166 # Pre install commands.
167 if grep -q ^pre_install $package_dir/receipt; then
168 pre_install $ROOT
169 fi
171 # Handle Config Files from receipt
172 if [ -n "$CONFIG_FILES" ]; then
173 cd $fs || exit 1
174 # save 'official' configuration files
175 eval_gettext "Saving configuration files for \$package_name... "
177 local confs
178 for i in $CONFIG_FILES; do
179 confs="$confs $(find ${i#/} -type f 2> /dev/null)"
180 done
182 echo $confs | cpio -o -H newc --quiet | gzip -9 > $package_dir/volatile.cpio.gz
184 # keep user configuration files
185 for conf_file in $confs; do
186 [ -e $conf_file ] || continue
187 cp -a $conf_file fs/$conf_file
188 done
189 status
190 cd - >/dev/null
191 fi
193 # Merge ROOT_FS with Package FS
194 eval_gettext "Installing \$package_name... "
195 cp -a fs/* $ROOT/
196 status
198 # Remove old config files
199 if [ -n $files_to_remove ]; then
200 eval_gettext "Removing old \$package_name... "
201 for file in $files_to_remove; do
202 remove_with_path $ROOT$file
203 done
204 status
205 fi
206 cd - >/dev/null
208 # Remove the temporary directory.
209 gettext "Removing all tmp files... "
210 rm -rf $TMP_DIR
211 status
213 # Post install commands.
214 if grep -q ^post_install $package_dir/receipt; then
215 post_install $ROOT
216 fi
218 # Update-desktop-database if needed.
219 if [ "$(fgrep .desktop $package_dir/files.list | fgrep /usr/share/applications/)" ]; then
220 updatedesktopdb=yes
221 fi
222 # Update-mime-database if needed.
223 if [ "$(fgrep /usr/share/mime $package_dir/files.list)" ]; then
224 updatemimedb=yes
225 fi
226 # Update-icon-database
227 if [ "$(fgrep /usr/share/icon/hicolor $package_dir/files.list)" ]; then
228 updateicondb=yes
229 fi
230 # Compile glib schemas if needed.
231 if [ "$(fgrep /usr/share/glib-2.0/schemas $package_dir/files.list)" ]; then
232 compile_schemas=yes
233 fi
234 # Update depmod list
235 if [ "$(fgrep /lib/modules $package_dir/files.list)" ]; then
236 updatedepmod=yes
237 fi
238 separator
239 eval_gettext "\$package_name (\$VERSION\$EXTRAVERSION) is installed."; newline
240 newline
241 }
243 # Install .tazpkg packages.
244 # Parameters: package_file
245 install_local() {
246 package_file="$1"
247 check_root
248 check_valid_tazpkg $package_file
250 # Check if forced install.
251 if ! [ "$forced" ]; then
252 check_installed $(package_name $package_file)
253 fi
255 install_package $package_file
256 update_databases
257 }
259 # Download and install a package. TODO: Handle Undigest Mirrors
260 # Parameters: package_name
261 install_web() {
262 local package_name="$1"
264 # Check if get-Package
265 if ! is_package_mirrored $package_name; then
266 package_name="get-$package_name"
267 AUTOEXEC=true
268 fi
270 # Check if package is mirrored
271 if ! is_package_mirrored $package_name; then
272 gettext "Could not find package on mirror:"; echo " $package_name"
273 exit 1
274 fi
276 # package_full=Package-Version
277 local package_full=$(full_package $package_name)
279 # Check if forced install.
280 if ! [ "$forced" ]; then
281 check_installed $package_name
282 fi
284 cd $CACHE_DIR > /dev/null
285 if [ -f "$package_full.tazpkg" ]; then
286 echo -n "$(colorize "$package_full" 34) "; echo ": $CACHE_DIR)"
287 # Check package download was finished
288 if ! tail -c 2k $package_full.tazpkg | fgrep -q 00000000TRAILER; then
289 eval_gettext "Continuing \$package_name download"; newline
290 download "$package_full.tazpkg"
291 fi
292 # Check that the package has the correct checksum
293 #if [ "$($CHECKSUM $package_full.tazpkg)" != "$(fgrep \" $package_full.tazpkg\" $pkgsmd5)" ]; then
294 # rm -f $package.tazpkg
295 # download "$package_full.tazpkg"
296 #fi
297 else
298 newline
299 download "$package_full.tazpkg"
300 fi
302 install_package "$CACHE_DIR/$package_full.tazpkg"
304 [ -n "$AUTOEXEC" ] && $package_name $ROOT
305 update_databases
306 }
308 # Install all missing deps. Auto install or ask user then install all missing
309 # deps from local dir, cdrom, media or from the mirror. In case we want to
310 # install packages from local, we need a packages.list to find the version.
311 # Parameters: package List of deps to install
312 install_deps() {
313 local package=$1
314 shift
315 local deps="$@"
317 gettext "Install all missing dependencies? "
319 # Print Yes/No and get result
320 if $AUTO_INSTALL_DEPS || confirm; then
321 for pkgorg in $deps; do
322 local pkg=$(equivalent_pkg $pkgorg)
323 # Check if package is not installed
324 if [ ! -d "$installed/$pkg" ]; then
325 if [ ! -f "$PKGS_DB/packages.list" ]; then
326 tazpkg recharge
327 fi
328 get-install $pkg
329 fi
330 done
331 else
332 newline
333 boldify $(gettext "Package:") $package
334 gettext \
335 "Dependencies unresolved. Installed but will probably not work."
336 newline && newline
337 fi
338 }
340 #
341 # Commands and exit
342 #
344 case "$1" in
345 ""|*usage|*help) usage ;;
346 esac
348 #
349 # Handle packages: package package.tazpkg ... packageN packageN.tazpkg
350 #
352 check_root
354 for pkg in $@
355 do
356 case "$pkg" in
357 *.tazpkg|*.spk)
358 [ "$forced" ] || check_installed $(package_name $package_file)
359 echo "Local package"
360 #install_local $pkg
361 ;;
362 --*) continue ;;
363 *)
364 [ "$forced" ] || check_installed $pkg
365 install_web $pkg ;;
366 esac
367 done
368 exit 0