spk view spk-add @ rev 27

spk-add: some minor change and improvments
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 15 18:33:55 2012 +0200 (2012-05-15)
parents b341e8872256
children 19f1b93fd7d8
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:"))
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 # Never used I think!!!
44 # Set by receipt: pre_depends() DEPENDS SELF_INSTALL CONFIG_FILES post_install()
46 # Create package path early to avoid dependencies loop
47 mkdir -p $TMP_DIR
48 extract_receipt $TMP_DIR $package_file
49 source $TMP_DIR/receipt
51 local package_name=$PACKAGE
52 local package_dir="$installed/$package_name"
53 mkdir -p $package_dir
55 # Run pre_depends from receipt if it exists
56 if grep -q ^pre_depends $TMP_DIR/receipt; then
57 pre_depends $ROOT
58 fi
60 # Create modifiers and files.list if they do not exist
61 # Why ? If missing files.list it's meta packages.
62 #touch $package_dir/modifiers
63 #touch $package_dir/files.list
65 # Add package checksum to pkgsmd5
66 sed -i "/ $(basename $package_dir)$/d" $pkgsmd5 2> /dev/null
67 oldpwd=$(pwd)
68 cd $(dirname $package_file) || exit 1
69 $CHECKSUM $(basename $package_file) >> $pkgsmd5
70 cd $oldpwd
72 # Resolve package deps.
73 if missing_deps $package_name $DEPENDS; then
74 install_deps $package_name $DEPENDS
75 fi
77 newline
78 boldify $(gettext "Installation of :") $package_name
79 separator
80 eval_gettext "Copying \$package_name... "
81 cp $package_file $TMP_DIR
82 status
84 # Extract Package
85 cd $TMP_DIR || exit 1
86 rm receipt
87 spk-archive extract $package_file
88 cd - >/dev/null
90 # Get files to remove if upgrading
91 local files_to_remove
92 if [ -f $package_dir/files.list ]; then
93 for file in $($package_dir/files.list); do
94 grep -q "^$(echo $file | grepesc)$" $TMP_DIR/files.list && continue
95 local modifiers=$(cat $package_dir/modifiers 2> /dev/null;\
96 fgrep -sl $package_dir */modifiers | cut -d/ -f1)
97 for i in modifiers; do
98 grep -qs "^$(echo $file | grepesc)$" $i/files.list && continue 2
99 done
100 files_to_remove="$files_to_remove $file"
101 done
102 fi
104 local check=false
105 local file_list
106 # Create list of all possibly modified files
107 for i in $(fgrep -v [ $TMP_DIR/files.list); do
108 [ -e "$ROOT$i" ] || continue
109 [ -d "$ROOT$i" ] && continue
110 file_list="$file_list $i"
111 check=true
112 done
114 # Check possibly modified files against other packages file.list
115 if $check; then
116 for pkg in $INSTALLED/*; do
117 [ "$pkg" == "$package_name" ] && continue
118 [ -s $pkg/files.list ] || continue
120 for file in $file_list; do
121 # $package_name wants to install $file which is already
122 # Installed from $pkg
123 if grep -q ^$file$ $pkg/files.list; then
124 # Tell $pkg that $package_name is going to overwrite some of its files
125 if [ -s "$pkg/volatile.cpio.gz" ]; then
126 # We can modify backed up files without notice
127 zcat $pkg/volatile.cpio.gz | cpio -t --quiet | \
128 grep -q "^${file#/}$" && continue
129 fi
130 echo "$package_name" >> $pkg/modifiers
131 fi
132 done
133 done
134 fi
136 cd $TMP_DIR || exit 1
137 cp receipt files.list $package_dir
138 # Copy the description if found.
139 [ -f "description.txt" ] && cp description.txt $package_dir
141 # Pre install commands.
142 if grep -q ^pre_install $package_dir/receipt; then
143 pre_install $ROOT
144 fi
146 # Handle Config Files from receipt
147 if [ -n "$CONFIG_FILES" ]; then
148 cd $fs || exit 1
149 # save 'official' configuration files
150 eval_gettext "Saving configuration files for \$package_name... "
152 local confs
153 for i in $CONFIG_FILES; do
154 confs="$confs $(find ${i#/} -type f 2> /dev/null)"
155 done
157 echo $confs | cpio -o -H newc --quiet | gzip -9 > $package_dir/volatile.cpio.gz
159 # keep user configuration files
160 for conf_file in $confs; do
161 [ -e $conf_file ] || continue
162 cp -a $conf_file fs/$conf_file
163 done
164 status
165 cd - >/dev/null
166 fi
168 # Merge ROOT_FS with Package FS
169 eval_gettext "Installing \$package_name... "
170 cp -a fs/* $ROOT/
171 status
173 # Remove old config files
174 if [ -n $files_to_remove ]; then
175 eval_gettext "Removing old \$package_name... "
176 for file in $files_to_remove; do
177 remove_with_path $ROOT$file
178 done
179 status
180 fi
181 cd - >/dev/null
183 # Remove the temporary directory.
184 gettext "Removing all tmp files... "
185 rm -rf $TMP_DIR
186 status
188 # Post install commands.
189 if grep -q ^post_install $package_dir/receipt; then
190 post_install $ROOT
191 fi
193 # Update-desktop-database if needed.
194 if [ "$(fgrep .desktop $package_dir/files.list | fgrep /usr/share/applications/)" ]; then
195 updatedesktopdb=yes
196 fi
197 # Update-mime-database if needed.
198 if [ "$(fgrep /usr/share/mime $package_dir/files.list)" ]; then
199 updatemimedb=yes
200 fi
201 # Update-icon-database
202 if [ "$(fgrep /usr/share/icon/hicolor $package_dir/files.list)" ]; then
203 updateicondb=yes
204 fi
205 # Compile glib schemas if needed.
206 if [ "$(fgrep /usr/share/glib-2.0/schemas $package_dir/files.list)" ]; then
207 compile_schemas=yes
208 fi
209 # Update depmod list
210 if [ "$(fgrep /lib/modules $package_dir/files.list)" ]; then
211 updatedepmod=yes
212 fi
214 separator
215 eval_gettext "\$package_name (\$VERSION\$EXTRAVERSION) is installed."; newline
216 newline
217 }
219 # Install .tazpkg packages.
220 # Parameters: package_file
221 install_pkg() {
222 package_file="$1"
223 check_root
224 check_valid_tazpkg $package_file
226 # Check if forced install.
227 if ! [ "$forced" ]; then
228 check_installed $(package_name $package_file)
229 fi
231 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 on mirror:"; echo " $package_name"
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_installed $package_name
263 fi
265 cd $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
284 install_package "$CACHE_DIR/$package_full.tazpkg"
286 [ -n "$AUTOEXEC" ] && $package_name $ROOT
287 update_desktop_database
288 update_mime_database
289 }
291 # Install all missing deps. Auto install or ask user then install all missing
292 # deps from local dir, cdrom, media or from the mirror. In case we want to
293 # install packages from local, we need a packages.list to find the version.
294 # Parameters: package List of deps to install
295 install_deps() {
296 local package=$1
297 shift
298 local deps="$@"
300 gettext "Install all missing dependencies? "
302 # Print Yes/No and get result
303 if $AUTO_INSTALL_DEPS || confirm; then
304 for pkgorg in $deps; do
305 local pkg=$(equivalent_pkg $pkgorg)
306 # Check if package is not installed
307 if [ ! -d "$installed/$pkg" ]; then
308 if [ ! -f "$PKGS_DB/packages.list" ]; then
309 tazpkg recharge
310 fi
311 get-install $pkg
312 fi
313 done
314 else
315 newline
316 boldify $(gettext "Package:") $package
317 gettext \
318 "Dependencies unresolved. Installed but will probably not work."
319 newline && newline
320 fi
321 }
323 # Update system databases
324 update_desktop_database() {
325 if [ -f $ROOT/usr/bin/update-desktop-database ] && [ -n "$updatedesktopdb" ]; then
326 chroot "$ROOT/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
327 fi
328 }
330 update_mime_database() {
331 if [ -f $ROOT/usr/bin/update-mime-database ] && [ -n "$updatemimedb" ]; then
332 chroot "$ROOT/" /usr/bin/update-mime-database /usr/share/mime
333 fi
334 }
336 update_icon_database() {
337 if [ -f $ROOT/usr/bin/gtk-update-icon-cache ] && [ -n "$updateicondb" ]; then
338 chroot "$ROOT/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
339 fi
340 }
342 compile_glib_schemas() {
343 if [ -f $ROOT/usr/bin/glib-compile-schemas ] && [ -n "$compile_schemas" ]; then
344 chroot "$ROOT/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
345 fi
346 }
348 update_kernel_modules() {
349 if [ -f $ROOT/sbin/depmod ] && [ -n "$updatedepmod" ]; then
350 chroot "$ROOT/" /sbin/depmod -a
351 fi
352 }
354 # Old style
355 case $1 in
356 install|-i)
357 install_pkg $2 $3
358 exit 0 ;;
359 get-install|-gi)
360 get_install $2
361 exit 0 ;;
362 "") usage ;;
363 esac
365 # Usage: spk-add package ... packageN
366 for pkg in $@
367 do
368 [ -d "$installed/$pkg" ] && continue
369 echo "Adding: $pkg"
370 done
371 exit 0