spk view spk-add @ rev 25

spk-add: is NOT busbox compatible (bash is a huge package with readline deps for an base-system at 8Mb)
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 15 17:52:37 2012 +0200 (2012-05-15)
parents 2eefb6700d4b
children b341e8872256
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 pushd $(dirname $package_file) > /dev/null
66 $CHECKSUM $(basename $package_file) >> $pkgsmd5
67 popd > /dev/null
69 # Resolve package deps.
70 if missing_deps $package_name $DEPENDS; then
71 install_deps $package_name $DEPENDS
72 fi
74 newline
75 boldify $(gettext "Installation of :") $package_name
76 separator
77 eval_gettext "Copying \$package_name... "
78 cp $package_file $TMP_DIR
79 status
81 # Extract Package
82 pushd $TMP_DIR > /dev/null
83 rm receipt
84 spk-archive extract $package_file
85 popd > /dev/null
87 # Get files to remove if upgrading
88 local files_to_remove
89 if [ -f $package_dir/files.list ]; then
90 for file in $($package_dir/files.list); do
91 grep -q "^$(echo $file | grepesc)$" $TMP_DIR/files.list && continue
92 local modifiers=$(cat $package_dir/modifiers 2> /dev/null;\
93 fgrep -sl $package_dir */modifiers | cut -d/ -f1)
94 for i in modifiers; do
95 grep -qs "^$(echo $file | grepesc)$" $i/files.list && continue 2
96 done
97 files_to_remove="$files_to_remove $file"
98 done
99 fi
101 local check=false
102 local file_list
103 # Create list of all possibly modified files
104 for i in $(fgrep -v [ $TMP_DIR/files.list); do
105 [ -e "$ROOT$i" ] || continue
106 [ -d "$ROOT$i" ] && continue
107 file_list="$file_list $i"
108 check=true
109 done
111 # Check possibly modified files against other packages file.list
112 if $check; then
113 for pkg in $INSTALLED/*; do
114 [ "$pkg" == "$package_name" ] && continue
115 [ -s $pkg/files.list ] || continue
117 for file in $file_list; do
118 # $package_name wants to install $file which is already
119 # Installed from $pkg
120 if grep -q ^$file$ $pkg/files.list; then
121 # Tell $pkg that $package_name is going to overwrite some of its files
122 if [ -s "$pkg/volatile.cpio.gz" ]; then
123 # We can modify backed up files without notice
124 zcat $pkg/volatile.cpio.gz | cpio -t --quiet | \
125 grep -q "^${file#/}$" && continue
126 fi
127 echo "$package_name" >> $pkg/modifiers
128 fi
129 done
130 done
131 fi
133 pushd $TMP_DIR > /dev/null
134 cp receipt files.list $package_dir
135 # Copy the description if found.
136 [ -f "description.txt" ] && cp description.txt $package_dir
138 # Pre install commands.
139 if grep -q ^pre_install $package_dir/receipt; then
140 pre_install $ROOT
141 fi
143 # Handle Config Files from receipt
144 if [ -n "$CONFIG_FILES" ]; then
145 pushd $fs > /dev/null
146 # save 'official' configuration files
147 eval_gettext "Saving configuration files for \$package_name... "
149 local confs
150 for i in $CONFIG_FILES; do
151 confs="$confs $(find ${i#/} -type f 2> /dev/null)"
152 done
154 echo $confs | cpio -o -H newc --quiet | gzip -9 > $package_dir/volatile.cpio.gz
156 # keep user configuration files
157 for conf_file in $confs; do
158 [ -e $conf_file ] || continue
159 cp -a $conf_file fs/$conf_file
160 done
161 status
162 popd > /dev/null
163 fi
165 # Merge ROOT_FS with Package FS
166 eval_gettext "Installing \$package_name... "
167 cp -a fs/* $ROOT/
168 status
170 # Remove old config files
171 if [ -n $files_to_remove ]; then
172 eval_gettext "Removing old \$package_name... "
173 for file in $files_to_remove; do
174 remove_with_path $ROOT$file
175 done
176 status
177 fi
178 popd > /dev/null
180 # Remove the temporary directory.
181 gettext "Removing all tmp files... "
182 rm -rf $TMP_DIR
183 status
185 # Post install commands.
186 if grep -q ^post_install $package_dir/receipt; then
187 post_install $ROOT
188 fi
190 # Update-desktop-database if needed.
191 if [ "$(fgrep .desktop $package_dir/files.list | fgrep /usr/share/applications/)" ]; then
192 updatedesktopdb=yes
193 fi
194 # Update-mime-database if needed.
195 if [ "$(fgrep /usr/share/mime $package_dir/files.list)" ]; then
196 updatemimedb=yes
197 fi
198 # Update-icon-database
199 if [ "$(fgrep /usr/share/icon/hicolor $package_dir/files.list)" ]; then
200 updateicondb=yes
201 fi
202 # Compile glib schemas if needed.
203 if [ "$(fgrep /usr/share/glib-2.0/schemas $package_dir/files.list)" ]; then
204 compile_schemas=yes
205 fi
206 # Update depmod list
207 if [ "$(fgrep /lib/modules $package_dir/files.list)" ]; then
208 updatedepmod=yes
209 fi
211 separator
212 eval_gettext "\$package_name (\$VERSION\$EXTRAVERSION) is installed."; newline
213 newline
214 }
217 # Install .tazpkg packages.
218 # Parameters: package_file
219 install_pkg() {
220 package_file="$1"
222 check_root
223 check_valid_tazpkg $package_file
225 # Check if forced install.
226 if ! [ "$forced" ]; then
227 check_for_installed_package $(package_name $package_file)
228 fi
230 install_package $package_file
232 update_desktop_database
233 update_mime_database
234 update_icon_database
235 compile_glib_schemas
236 }
238 # Download and install a package. TODO: Handle Undigest Mirrors
239 # Parameters: package_name
240 get_install() {
241 local package_name="$1"
243 check_root
245 # Check if get-Package
246 if ! is_package_mirrored $package_name; then
247 package_name="get-$package_name"
248 AUTOEXEC=true
249 fi
251 # Check if package is mirrored
252 if ! is_package_mirrored $package_name; then
253 gettext "Could not find \$package_name in repositories"; newline
254 exit 1
255 fi
257 # package_full=Package-Version
258 local package_full=$(full_package $package_name)
260 # Check if forced install.
261 if ! [ "$forced" ]; then
262 check_for_installed_package $package_name
263 fi
265 pushd $CACHE_DIR > /dev/null
266 if [ -f "$package_full.tazpkg" ]; then
267 eval_gettext "\$package_full is already in the cache : \$CACHE_DIR"; newline
268 # Check package download was finished
269 if ! tail -c 2k $package_full.tazpkg | fgrep -q 00000000TRAILER; then
270 eval_gettext "Continuing \$package_name download"; newline
271 download "$package_full.tazpkg"
272 fi
274 # Check that the package has the correct checksum
275 # if [ "$($CHECKSUM $package_full.tazpkg)" != "$(fgrep \" $package_full.tazpkg\" $pkgsmd5)" ]; then
276 # rm -f $package.tazpkg
277 # download "$package_full.tazpkg"
278 # fi
279 else
280 newline
281 download "$package_full.tazpkg"
282 fi
283 popd > /dev/null
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