spk view spk-add @ rev 60
Bunch of improvment, new extract_fileslist function in libspk.sh
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Thu May 17 23:40:23 2012 +0200 (2012-05-17) |
parents | 85543a2e118c |
children | d0fa46ea46bd |
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 #
12 # Functions
13 #
15 # Help and usage
16 usage() {
17 name=$(basename $0)
18 cat << EOT
20 $(boldify $(gettext "Usage:")) $name [packages|--options]
22 $(gettext "Install SliTaz Packages")
24 $(boldify $(gettext "Options:"))
25 --forced $(gettext "Force package reinstallation")
26 --root= $(gettext "Set the root file system path")
27 --debug $(gettext "Display some usefull debug information")
29 $(boldify $(gettext "Examples:"))
30 $name package1 package2 packageN
32 EOT
33 exit 0
34 }
36 # Log install messages
37 log_install() {
38 debug "log_install: $logdir/$pkg/install.log"
39 mkdir -p $logdir/$pkg
40 tee $logdir/$pkg/install.log
41 }
43 # Update system databases
44 update_databases() {
45 if [ -f $root/usr/bin/update-desktop-database ] && [ -n "$updatedesktopdb" ]; then
46 chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
47 fi
48 # Mimetypes
49 if [ -f $root/usr/bin/update-mime-database ] && [ -n "$updatemimedb" ]; then
50 chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
51 fi
52 # Icons
53 if [ -f $root/usr/bin/gtk-update-icon-cache ] && [ -n "$updateicondb" ]; then
54 chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
55 fi
56 # Glib schemas
57 if [ -f $root/usr/bin/glib-compile-schemas ] && [ -n "$compile_schemas" ]; then
58 chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
59 fi
60 # Kernel modules
61 if [ -f $root/sbin/depmod ] && [ -n "$updatedepmod" ]; then
62 chroot "$root/" /sbin/depmod -a
63 fi
64 }
66 # This function installs a package in the rootfs.
67 # Parameters: package_name package_file
68 install_package() {
69 local package_file=$1
71 # Set by receipt: pre_depends() DEPENDS SELF_INSTALL CONFIG_FILES post_install()
73 local package_name=$PACKAGE
74 local package_dir="$installed/$package_name"
75 mkdir -p $package_dir || exit 1
77 # Run pre_depends from receipt if it exists
78 if grep -q ^pre_depends $tmpdir/receipt; then
79 pre_depends $root
80 fi
82 # Resolve package deps.
83 if missing_deps $package_name $DEPENDS; then
84 if [ "$confirm" ]; then
85 gettext "Install missing dependencies"
86 if ! confirm; then
87 gettext "WARNING: Any dependencies installed"; newline
88 else
89 install_deps $package_name $DEPENDS
90 fi
91 else
92 install_deps $package_name $DEPENDS
93 fi
94 else
95 newline
96 fi
98 boldify $(gettext "Adding"; echo " $package_name")
99 separator
100 gettext "Copying"; echo -n " $package_name..."
101 cp $package_file $tmpdir
102 status
104 # Add package checksum to $installed.$SUM
105 if [ "$verbose" ]; then
106 gettext "Incrementing:"; echo -n " $(basename $installed.$SUM)"
107 fi
108 sed -i "/$(basename $package_file)/"d $installed.$SUM 2>/dev/null
109 $CHECKSUM $(basename $package_file) >> $installed.$SUM
110 [ "$verbose" ] && status
112 # Extract Package
113 cd $tmpdir || exit 1
114 rm receipt
115 spk-archive extract $package_file
116 cd - >/dev/null
118 # Get files to remove if upgrading
119 local files_to_remove
120 if [ -f $package_dir/files.list ]; then
121 for file in $(cat $package_dir/files.list)
122 do
123 grep -q "^$(echo $file | grepesc)$" $tmpdir/files.list && continue
124 local modifiers=$(cat $package_dir/modifiers 2> /dev/null; \
125 fgrep -sl $package_dir */modifiers | cut -d/ -f1)
126 for i in modifiers; do
127 grep -qs "^$(echo $file | grepesc)$" $i/files.list && continue 2
128 done
129 files_to_remove="$files_to_remove $file"
130 done
131 fi
132 debug "file_to_remove: $files_to_remove"
134 # Create list of all possibly modified files
135 local file_list
136 for i in $(fgrep -v [ $tmpdir/files.list); do
137 [ -e "${root}${i}" ] || continue
138 [ -d "${root}${i}" ] && continue
139 file_list="$file_list $i"
140 done
142 debug "file_list is set"
144 # Check possibly modified files against other packages files.list
145 debug "check modified files"
146 for file in $file_list
147 do
148 local filegrep=$(egrep ^${file}$ $installed/*/files.list)
149 if [ "$filegrep" ]; then
150 local list=$(echo $filegrep | cut -d ":" -f 1)
151 local count=0
152 for pkg_file in $list; do
153 local name=$(basename $(dirname $pkg_file))
154 [ "$package_name" == "$name" ] && continue 2
155 count=$(($count +1))
156 [ "$count" == "1" ] && gettext "Modified package:"; \
157 colorize " $name" 31
158 gettext "Overwriting file:"; echo " $file"
159 # If confirm is set, ask to remove. Do we want that ?
160 # If gawk is installed for example we will never remove Busybox
161 # Use CONFLICTS receipt variable to avoid
162 if [ "$confirm" ]; then
163 echo -n "NOTE: confirm: spk-rm $name ???"; read
164 fi
165 done
166 fi
167 done
169 cd $tmpdir || exit 1
170 cp receipt files.list $package_dir || exit 1
172 # Copy the description if found.
173 [ -f "description.txt" ] && cp description.txt $package_dir
175 # Pre install commands.
176 if grep -q ^pre_install $package_dir/receipt; then
177 pre_install $root
178 fi
180 # Handle Config Files from receipt
181 if [ -n "$CONFIG_FILES" ]; then
182 cd $fs || exit 1
183 # save 'official' configuration files
184 eval_gettext "Saving configuration files for \$package_name... "
186 local confs
187 for i in $CONFIG_FILES; do
188 confs="$confs $(find ${i#/} -type f 2> /dev/null)"
189 done
191 echo $confs | cpio -o -H newc --quiet | gzip -9 > \
192 $package_dir/volatile.cpio.gz
194 # keep user configuration files
195 for configfile in $confs; do
196 [ -e $configfile ] || continue
197 cp -a $configfile fs/$configfile
198 done
199 status
200 cd - >/dev/null
201 fi
203 # Merge ROOT_FS with Package FS
204 gettext "Installing in:"; echo -n " $root/"
205 cp -a fs/* $root/
206 status
208 # Remove old config files
209 if [ "$files_to_remove" ]; then
210 gettext "Removing old"; echo -n " $package_name..."
211 for file in $files_to_remove; do
212 if [ "$verbose" ]; then
213 gettext "Removing:"; echo " ${root}${file}"
214 fi
215 remove_with_path ${root}${file}
216 done
217 status
218 fi
219 cd - >/dev/null
221 # Remove the temporary directory.
222 gettext "Removing all tmp files... "
223 rm -rf $tmpdir
224 status
226 # Post install commands.
227 if grep -q ^post_install $package_dir/receipt; then
228 post_install $root
229 fi
231 # Update-desktop-database if needed.
232 if [ "$(fgrep .desktop $package_dir/files.list | fgrep /usr/share/applications/)" ]; then
233 updatedesktopdb=yes
234 fi
235 # Update-mime-database if needed.
236 if [ "$(fgrep /usr/share/mime $package_dir/files.list)" ]; then
237 updatemimedb=yes
238 fi
239 # Update-icon-database
240 if [ "$(fgrep /usr/share/icon/hicolor $package_dir/files.list)" ]; then
241 updateicondb=yes
242 fi
243 # Compile glib schemas if needed.
244 if [ "$(fgrep /usr/share/glib-2.0/schemas $package_dir/files.list)" ]; then
245 compile_schemas=yes
246 fi
247 # Update depmod list
248 if [ "$(fgrep /lib/modules $package_dir/files.list)" ]; then
249 updatedepmod=yes
250 fi
251 separator
252 echo -n "$package_name ${VERSION}${EXTRAVERSION} "
253 gettext "is installed"; newline
254 newline
255 }
257 # Install .tazpkg packages.
258 # Parameters: package_file
259 install_local() {
260 package_file="$1"
261 #check_valid_tazpkg $package_file
262 if [ -f "$package_file" ]; then
263 if [ $(dirname $package_file) == "." ]; then
264 package_file=$(pwd)/$package_file
265 fi
266 debug "package file: $package_file"
267 else
268 gettext "Unable to find:"; echo " $package_file"
269 exit 1
270 fi
272 # Get package name now to check if installed
273 mkdir -p $tmpdir
274 extract_receipt $tmpdir $package_file
275 source $tmpdir/receipt
276 [ "$forced" ] || check_installed $PACKAGE
277 [ "$count" == "1" ] && newline
278 install_package $package_file
279 update_databases
280 }
282 # Download and install a package. TODO: Handle Undigest Mirrors
283 # Parameters: package_name
284 install_web() {
285 local package_name="$1"
287 # Check if get-Package
288 if ! is_package_mirrored $package_name; then
289 package_name="get-$package_name"
290 AUTOEXEC=true
291 fi
293 # Check if package is mirrored
294 if ! is_package_mirrored $package_name; then
295 gettext "Could not find package on mirror:"; echo " $package_name"
296 exit 1
297 fi
299 # package_full=Package-Version
300 local package_full=$(full_package $package_name)
302 cd $CACHE_DIR > /dev/null
303 if [ -f "$package_full.tazpkg" ]; then
304 [ "$count" == 1 ] && newline
305 gettext "Using cache:"; echo " $(colorize "$package_full" 34)"
306 # Check package download was finished
307 if ! tail -c 2k $package_full.tazpkg | fgrep -q 00000000TRAILER; then
308 gettext "Continuing download of:"; echo " $package_name"
309 download "$package_full.tazpkg"
310 fi
311 # Check that the package has the correct checksum
312 #if [ "$($CHECKSUM $package_full.tazpkg)" != "$(fgrep \" $package_full.tazpkg\" $pkgsmd5)" ]; then
313 # rm -f $package.tazpkg
314 # download "$package_full.tazpkg"
315 #fi
316 else
317 newline && download "$package_full.tazpkg"
318 fi
320 # Create package path early to avoid dependencies loop
321 mkdir -p $tmpdir
322 extract_receipt $tmpdir "$CACHE_DIR/$package_full.tazpkg"
323 source $tmpdir/receipt
325 install_package "$CACHE_DIR/$package_full.tazpkg"
327 #[ "$AUTOEXEC" ] && chroot $root/ $package_name
328 [ "$AUTOEXEC" ] && $package_name $root
329 update_databases
330 }
332 # Install all missing deps of a package.
333 # Usage: install_deps package DEPENDS
334 install_deps() {
335 local package=$1
336 shift
337 local deps="$@"
338 for pkgorg in $deps; do
339 local pkg=$(equivalent_pkg $pkgorg)
340 # Check if package is not installed
341 if [ ! -d "$installed/$pkg" ]; then
342 if [ ! -f "$PKGS_DB/packages.list" ]; then
343 #spk-up --recharge-only
344 tazpkg recharge
345 fi
346 spk-add $pkg
347 fi
348 done
349 }
351 #
352 # Commands and exit
353 #
355 case "$1" in
356 ""|*usage|*help) usage ;;
357 esac
359 #
360 # Handle packages: package package.tazpkg ... packageN packageN.tazpkg
361 #
363 : ${count=0}
364 check_root
366 for pkg in $@
367 do
368 case "$pkg" in
369 *.tazpkg|*.spk)
370 count=$(($count + 1))
371 install_local $pkg | log_install ;;
372 *.deb|.rmp)
373 echo "TODO: spk-convert then install" ;;
374 *.*) gettext "WARNING: Not a valid package:"; echo " $pkg" ;;
375 --*) continue ;;
376 *)
377 [ "$forced" ] || check_installed $pkg
378 count=$(($count + 1))
379 install_web $pkg | log_install ;;
380 esac
381 done
383 # Show all new counted packages in verbose mode
384 if [ "$verbose" ]; then
385 gettext "New installed packages:"; colorize " $count" 34
386 fi
388 exit 0