spk view spk-add @ rev 26

spk-add: make id busybox ash compatible and fix libspk.sh as well
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 15 18:17:27 2012 +0200 (2012-05-15)
parents 3cca155b72a7
children eb8149264a44
line source
1 #!/bin/busybox 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:"))
31 $(boldify $(gettext "Examples:"))
32 $name package1 package2 packageN
34 EOT
35 exit 0
36 }
38 # This function installs a package in the rootfs.
39 # Parameters: package_name package_file
40 install_package() {
41 local package_file=$1
43 # Set by receipt: pre_depends() DEPENDS SELF_INSTALL CONFIG_FILES post_install()
45 # Create package path early to avoid dependencies loop
46 mkdir -p $TMP_DIR
47 extract_receipt $TMP_DIR $package_file
48 source $TMP_DIR/receipt
50 local package_name=$PACKAGE
51 local package_dir="$installed/$package_name"
52 mkdir -p $package_dir
54 # Run pre_depends from receipt if it exists
55 if grep -q ^pre_depends $TMP_DIR/receipt; then
56 pre_depends $ROOT
57 fi
59 # Create modifiers and files.list if they do not exist
60 # touch $package_dir/modifiers
61 # touch $package_dir/files.list
63 # Add package checksum to pkgsmd5
64 sed -i "/ $(basename $package_dir)$/d" $pkgsmd5 2> /dev/null
65 oldpwd=$(pwd)
66 cd $(dirname $package_file) || exit 1
67 $CHECKSUM $(basename $package_file) >> $pkgsmd5
68 cd $oldpwd
70 # Resolve package deps.
71 if missing_deps $package_name $DEPENDS; then
72 install_deps $package_name $DEPENDS
73 fi
75 newline
76 boldify $(gettext "Installation of :") $package_name
77 separator
78 eval_gettext "Copying \$package_name... "
79 cp $package_file $TMP_DIR
80 status
82 # Extract Package
83 cd $TMP_DIR || exit 1
84 rm receipt
85 spk-archive extract $package_file
86 cd - >/dev/null
88 # Get files to remove if upgrading
89 local files_to_remove
90 if [ -f $package_dir/files.list ]; then
91 for file in $($package_dir/files.list); do
92 grep -q "^$(echo $file | grepesc)$" $TMP_DIR/files.list && continue
93 local modifiers=$(cat $package_dir/modifiers 2> /dev/null;\
94 fgrep -sl $package_dir */modifiers | cut -d/ -f1)
95 for i in modifiers; do
96 grep -qs "^$(echo $file | grepesc)$" $i/files.list && continue 2
97 done
98 files_to_remove="$files_to_remove $file"
99 done
100 fi
102 local check=false
103 local file_list
104 # Create list of all possibly modified files
105 for i in $(fgrep -v [ $TMP_DIR/files.list); do
106 [ -e "$ROOT$i" ] || continue
107 [ -d "$ROOT$i" ] && continue
108 file_list="$file_list $i"
109 check=true
110 done
112 # Check possibly modified files against other packages file.list
113 if $check; then
114 for pkg in $INSTALLED/*; do
115 [ "$pkg" == "$package_name" ] && continue
116 [ -s $pkg/files.list ] || continue
118 for file in $file_list; do
119 # $package_name wants to install $file which is already
120 # Installed from $pkg
121 if grep -q ^$file$ $pkg/files.list; then
122 # Tell $pkg that $package_name is going to overwrite some of its files
123 if [ -s "$pkg/volatile.cpio.gz" ]; then
124 # We can modify backed up files without notice
125 zcat $pkg/volatile.cpio.gz | cpio -t --quiet | \
126 grep -q "^${file#/}$" && continue
127 fi
128 echo "$package_name" >> $pkg/modifiers
129 fi
130 done
131 done
132 fi
134 cd $TMP_DIR || exit 1
135 cp receipt files.list $package_dir
136 # Copy the description if found.
137 [ -f "description.txt" ] && cp description.txt $package_dir
139 # Pre install commands.
140 if grep -q ^pre_install $package_dir/receipt; then
141 pre_install $ROOT
142 fi
144 # Handle Config Files from receipt
145 if [ -n "$CONFIG_FILES" ]; then
146 cd $fs || exit 1
147 # save 'official' configuration files
148 eval_gettext "Saving configuration files for \$package_name... "
150 local confs
151 for i in $CONFIG_FILES; do
152 confs="$confs $(find ${i#/} -type f 2> /dev/null)"
153 done
155 echo $confs | cpio -o -H newc --quiet | gzip -9 > $package_dir/volatile.cpio.gz
157 # keep user configuration files
158 for conf_file in $confs; do
159 [ -e $conf_file ] || continue
160 cp -a $conf_file fs/$conf_file
161 done
162 status
163 cd - >/dev/null
164 fi
166 # Merge ROOT_FS with Package FS
167 eval_gettext "Installing \$package_name... "
168 cp -a fs/* $ROOT/
169 status
171 # Remove old config files
172 if [ -n $files_to_remove ]; then
173 eval_gettext "Removing old \$package_name... "
174 for file in $files_to_remove; do
175 remove_with_path $ROOT$file
176 done
177 status
178 fi
179 cd - >/dev/null
181 # Remove the temporary directory.
182 gettext "Removing all tmp files... "
183 rm -rf $TMP_DIR
184 status
186 # Post install commands.
187 if grep -q ^post_install $package_dir/receipt; then
188 post_install $ROOT
189 fi
191 # Update-desktop-database if needed.
192 if [ "$(fgrep .desktop $package_dir/files.list | fgrep /usr/share/applications/)" ]; then
193 updatedesktopdb=yes
194 fi
195 # Update-mime-database if needed.
196 if [ "$(fgrep /usr/share/mime $package_dir/files.list)" ]; then
197 updatemimedb=yes
198 fi
199 # Update-icon-database
200 if [ "$(fgrep /usr/share/icon/hicolor $package_dir/files.list)" ]; then
201 updateicondb=yes
202 fi
203 # Compile glib schemas if needed.
204 if [ "$(fgrep /usr/share/glib-2.0/schemas $package_dir/files.list)" ]; then
205 compile_schemas=yes
206 fi
207 # Update depmod list
208 if [ "$(fgrep /lib/modules $package_dir/files.list)" ]; then
209 updatedepmod=yes
210 fi
212 separator
213 eval_gettext "\$package_name (\$VERSION\$EXTRAVERSION) is installed."; newline
214 newline
215 }
218 # Install .tazpkg packages.
219 # Parameters: package_file
220 install_pkg() {
221 package_file="$1"
223 check_root
224 check_valid_tazpkg $package_file
226 # Check if forced install.
227 if ! [ "$forced" ]; then
228 check_for_installed_package $(package_name $package_file)
229 fi
231 install_package $package_file
233 update_desktop_database
234 update_mime_database
235 update_icon_database
236 compile_glib_schemas
237 }
239 # Download and install a package. TODO: Handle Undigest Mirrors
240 # Parameters: package_name
241 get_install() {
242 local package_name="$1"
244 check_root
246 # Check if get-Package
247 if ! is_package_mirrored $package_name; then
248 package_name="get-$package_name"
249 AUTOEXEC=true
250 fi
252 # Check if package is mirrored
253 if ! is_package_mirrored $package_name; then
254 gettext "Could not find package on mirror:"; echo " $package_name"
255 exit 1
256 fi
258 # package_full=Package-Version
259 local package_full=$(full_package $package_name)
261 # Check if forced install.
262 if ! [ "$forced" ]; then
263 check_for_installed_package $package_name
264 fi
266 cd $CACHE_DIR > /dev/null
267 if [ -f "$package_full.tazpkg" ]; then
268 eval_gettext "\$package_full is already in the cache : \$CACHE_DIR"; newline
269 # Check package download was finished
270 if ! tail -c 2k $package_full.tazpkg | fgrep -q 00000000TRAILER; then
271 eval_gettext "Continuing \$package_name download"; newline
272 download "$package_full.tazpkg"
273 fi
275 # Check that the package has the correct checksum
276 # if [ "$($CHECKSUM $package_full.tazpkg)" != "$(fgrep \" $package_full.tazpkg\" $pkgsmd5)" ]; then
277 # rm -f $package.tazpkg
278 # download "$package_full.tazpkg"
279 # fi
280 else
281 newline
282 download "$package_full.tazpkg"
283 fi
285 install_package "$CACHE_DIR/$package_full.tazpkg"
287 [ -n "$AUTOEXEC" ] && $package_name $ROOT
288 update_desktop_database
289 update_mime_database
290 }
292 # Install all missing deps. Auto install or ask user then install all missing
293 # deps from local dir, cdrom, media or from the mirror. In case we want to
294 # install packages from local, we need a packages.list to find the version.
295 # Parameters: package List of deps to install
296 install_deps() {
297 local package=$1
298 shift
299 local deps="$@"
301 gettext "Install all missing dependencies? "
303 # Print Yes/No and get result
304 if $AUTO_INSTALL_DEPS || confirm; then
305 for pkgorg in $deps; do
306 local pkg=$(equivalent_pkg $pkgorg)
307 # Check if package is not installed
308 if [ ! -d "$installed/$pkg" ]; then
309 if [ ! -f "$PKGS_DB/packages.list" ]; then
310 tazpkg recharge
311 fi
312 get-install $pkg
313 fi
314 done
315 else
316 newline
317 eval_gettext \
318 "Leaving dependencies for \$package unresolved. The package is installed but
319 will probably not work."; newline
320 newline
321 fi
322 }
324 # Check for ELF file
325 is_elf() {
326 [ "$(dd if=$1 bs=1 skip=1 count=3 2> /dev/null)" = "ELF" ]
327 }
329 # Print shared library dependencies: we have /usr/bin/ldd
330 ldd() {
331 LD_TRACE_LOADED_OBJECTS=1 /lib/ld*.so $1 2> /dev/null
332 }
335 update_desktop_database() {
336 if [ -f $ROOT/usr/bin/update-desktop-database ] && [ -n "$updatedesktopdb" ]; then
337 chroot "$ROOT/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
338 fi
339 }
341 update_mime_database() {
342 if [ -f $ROOT/usr/bin/update-mime-database ] && [ -n "$updatemimedb" ]; then
343 chroot "$ROOT/" /usr/bin/update-mime-database /usr/share/mime
344 fi
345 }
347 update_icon_database() {
348 if [ -f $ROOT/usr/bin/gtk-update-icon-cache ] && [ -n "$updateicondb" ]; then
349 chroot "$ROOT/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
350 fi
351 }
353 compile_glib_schemas() {
354 if [ -f $ROOT/usr/bin/glib-compile-schemas ] && [ -n "$compile_schemas" ]; then
355 chroot "$ROOT/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
356 fi
357 }
359 update_kernel_modules() {
360 if [ -f $ROOT/sbin/depmod ] && [ -n "$updatedepmod" ]; then
361 chroot "$ROOT/" /sbin/depmod -a
362 fi
363 }
365 # Old style
366 case $1 in
367 install|-i)
368 install_pkg $2 $3
369 exit 0 ;;
370 get-install|-gi)
371 get_install $2
372 exit 0 ;;
373 "") usage ;;
374 esac
376 # Usage: spk-add package ... packageN
377 for pkg in $@
378 do
379 [ -d "$installed/$pkg" ] && continue
380 echo "Adding: $pkg"
381 done
382 exit 0