spk view spk-add @ rev 55

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