spk view spk-convert @ rev 131

Tiny edits
author Paul Issott <paul@slitaz.org>
date Mon Mar 03 16:06:46 2014 +0000 (2014-03-03)
parents 64165d8e4550
children a0bdc170c51e
line source
1 #!/bin/sh
2 #
3 # Spk-convert - Convert other distribution's packages to tazpkg form. Read the README before adding or
4 # modifying 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 "Convert SliTaz Packages")
24 $(boldify $(gettext "Options:"))
25 --install $(gettext "Install package after conversion")
26 --root= $(gettext "Set the root file system path")
27 --debug $(gettext "Display some useful debug information")
29 $(boldify $(gettext "Examples:"))
30 $name package1.rpm package2.tgz packageN.ipk
32 EOT
33 exit 0
34 }
36 # Move tazpkg pack to shared lib?
38 # generate a receipt based on standard receipt vars
39 # Usage: generate_receipt $receipt $base_package
40 generate_receipt() {
41 local receipt=$1
42 local base_package=$2
43 cat > $receipt <<EOT
44 # SliTaz package receipt.
45 # generated by tazpkg from package $(basename $base_package)
46 PACKAGE="$PACKAGE"
47 VERSION="$VERSION"
48 CATEGORY="misc"
49 SHORT_DESC="$SHORT_DESC"
50 WEB_SITE="$WEB_SITE"
51 MAINTAINER="$MAINTAINER"
52 DEPENDS="$DEPENDS"
53 CONFIG_FILES="$CONFIG_FILES"
54 EOT
56 }
58 # Create SliTaz package archive using cpio and lzma.
59 # TODO: Cook also packs packages, we should share code in libpkg.sh
60 # Usage: pack $package
61 pack() {
62 local package=$1
63 local dir="$2"
64 local fs="$dir/fs"
65 local receipt="$dir/receipt"
67 if [ ! -f "$receipt" ]; then
68 gettext "Receipt is missing. Please read the documentation."; newline
69 exit 0
70 else
71 # cd $dir
72 newline
73 echo $(boldify "Packing :") $package
74 separator
76 # Create files.list with redirecting find output.
77 gettext "Creating the list of files..."
78 cd $fs
79 find . -type f -print > $dir/files.list
80 find . -type l -print >> $dir/files.list
81 sed -i s/'^.'/''/ $dir/files.list
82 cd - /dev/null
84 status
85 gettext "Creating $CHECKSUM of files..."
86 local file
87 cd $fs
88 for file in $(cat $dir/files.list); do
89 [ -L "$file" ] && continue
90 [ -f "$file" ] || continue
91 case "$file" in
92 /lib/modules/*/modules.*|*.pyc) continue;;
93 esac
94 $CHECKSUM "$file" | sed 's/ fs/ /' >> $dir/$CHECKSUM
95 done
96 cd - /dev/null
98 status
99 UNPACKED_SIZE=$(du -chs $fs $receipt $dir/files.list $dir/$CHECKSUM \
100 $dir/description.txt 2> /dev/null | \
101 awk '{ sz=$1 } END { print sz }')
103 # Build cpio archives.
104 gettext "Compressing the fs... "
105 cd $dir
106 find fs | cpio -o -H newc --quiet | lzma e $fs.cpio.lzma -si
107 cd - /dev/null
108 rm -rf $fs
109 status
111 PACKED_SIZE=$(du -chs $fs.cpio.lzma $receipt $dir/files.list \
112 $dir/$CHECKSUM $dir/description.txt 2> /dev/null | \
113 awk '{ sz=$1 } END { print sz }')
115 gettext "Updating receipt sizes..."
116 sed -i s/^PACKED_SIZE.*$// $receipt
117 sed -i s/^UNPACKED_SIZE.*$// $receipt
118 sed -i "s/^PACKAGE=/PACKED_SIZE=\"$PACKED_SIZE\"\nUNPACKED_SIZE=\"$UNPACKED_SIZE\"\nPACKAGE=/" $receipt
119 status
121 gettext "Creating full cpio archive... "
122 cd $dir
123 find . -print | cpio -o -H newc --quiet > $dir.tazpkg
124 cd - /dev/null
125 status
127 gettext "Restoring original package tree... "
129 unlzma -c $fs.cpio.lzma | cpio -idm --quiet $dir
130 status
132 rm $fs.cpio.lzma
133 separator
134 eval_gettext "Package \$package compressed successfully."; newline
135 echo $(gettext "Size") : $(du -sh $dir.tazpkg)
136 newline
137 fi
138 }
142 # search dependencies for files
143 # Usage: find_depends $fs
144 find_depends() {
145 local fs=$1
147 local DEFAULT_DEPENDS="glibc-base gcc-lib-base"
149 for chkfile in $(find $fs -type f); do
150 is_elf $chkfile || continue
151 case "$chkfile" in
152 *.o|*.ko|*.ko.gz) continue;;
153 esac
155 ldd $chkfile | \
156 while read lib rem; do
157 case "$lib" in
158 statically|linux-gate.so*|ld-*.so|*/ld-*.so)
159 continue;;
160 esac
162 find $fs | grep -q /$lib$ && continue
164 for dep in $(fgrep $lib files.list | cut -d: -f1); do
165 case " $DEFAULT_DEPENDS " in
166 *\ $dep\ *) continue 2;;
167 esac
168 grep -qs "^$dep$" $tmpdir/depends && continue 2
169 done
171 if [ -n "$dep" ]; then
172 echo "$dep" >> $tmpdir/depends
173 else
174 grep -qs ^$lib$ $tmpdir/unresolved ||
175 echo "$lib" >> $tmpdir/unresolved
176 fi
177 done
178 done
179 unset spc
180 local spc
181 for file in $(cat $tmpdir/depends 2> /dev/null | sort | uniq); do
182 echo -n "$spc$file"
183 spc=" "
184 done
185 }
187 show_unresolved_lib() {
188 local receipt=$1
189 if [ -s $tmpdir/unresolved ]; then
190 echo -e "BUGS=\"$(gettext 'No dependency for')" >> $receipt
191 for lib in $(cat $tmpdir/unresolved | sort | uniq); do
192 eval_gettext "WARNING: unknown dependency for \$lib"; newline
193 echo -n " $lib" >> $receipt
194 done
195 echo "\"" >> $receipt
196 fi
197 }
201 # convert a .ipk package to .tazpkg
202 # Usage: convert_ipk $package_file
203 convert_ipk() {
204 local package_file=$1
206 mkdir -p $tmpdir
207 tar xOzf $package_file ./control.tar.gz | tar xzf - -C $tmpdir
209 local control="$tmpdir/control"
211 # filter ipk control file for variable
212 # Usage: filter_vars $search_var
213 filter_vars() {
214 local var=$1
215 echo "$(grep ^$var $control | sed 's/.*: //')"
216 }
218 # Load up receipt template variables
219 PACKAGE=$(filter_vars "Package")
220 VERSION=$(filter_vars "Version")
221 MAINTAINER=$(filter_vars "Maintainer")
222 SHORT_DESC=$(filter_vars "Description")
223 WEB_SITE="http://openwrt.org/"
225 target=$(filter_vars "Architecture")
226 case "$target" in
227 i386|all)
228 local file="$tmpdir/$PACKAGE-$VERSION"
229 local receipt="$file/receipt"
230 local fs="$file/fs"
232 mkdir -p $fs
233 tar xOzf $package_file ./data.tar.gz | tar xzf - -C $fs
235 unset CONFIG_FILES
236 [ -s $tmpdir/conffiles ] && $CONFIG_FILE=$(cat $tmpdir/conffiles)
240 DEPENDS=$(find_depends $fs)
241 generate_receipt $receipt $package_file
243 show_unresolved_lib $receipt
245 # Writes a ipk script to receipt function
246 # Usage: transpose_func $function_name $script
247 transpose_func() {
248 local func=$1
249 local script=$2
250 if [ -s $script ]; then
251 cat >> $receipt << EOT
252 $func()
253 {
254 $(cat $script)
255 }
256 EOT
257 fi
258 }
260 # read functions into receipt
261 transpose_func pre_install $tmpdir/preinst
262 transpose_func post_install $tmpdir/postinst
263 transpose_func pre_remove $tmpdir/prerm
264 transpose_func post_remove $tmpdir/postrm
266 [ -z "$SHORT_DESC" ] || echo $SHORT_DESC > $file/description.txt
268 pack $PACKAGE $file
270 mv $file.tazpkg .
271 ;;
272 *)
273 gettext "Invalid target: $target (expected i386)"; newline
274 ;;
275 esac
276 unset_receipt
277 rm -rf $tmpdir
278 }
280 # convert a .pkg.tar.gz/.apk package to .tazpkg
281 # Usage: convert_arch $package_file
282 convert_arch() {
283 local package_file=$1
285 # Extract
286 local fs="$tmpdir/fs"
287 mkdir -p $fs
289 case $package_file in
290 *.apk|*.pkg.tar.gz)
291 tar xzf $package_file -C $fs;;
292 *.pkg.tar.xz)
293 tar xf $package_file -C $fs;;
294 esac
296 local PKGINFO="$fs/.PKGINFO"
297 if [ -f $PKGINFO ]; then
299 # filter .PKGINFO file for variable
300 # Usage: filter_vars $search_var
301 filter_vars() {
302 local var=$1
303 echo $(grep ^$var $PKGINFO | sed 's/.*= //')
304 }
306 PACKAGE=$(filter_vars "pkgname")
307 VERSION=$(filter_vars "pkgver")
308 SHORT_DESC=$(filter_vars "pkgdesc")
309 WEB_SITE=$(filter_vars "url")
310 MAINTAINER=$(filter_vars "packager")
312 local file="$tmpdir/$PACKAGE-$VERSION"
313 local receipt="$file/receipt"
315 mkdir $file
316 mv $fs $file
318 DEPENDS=$(find_depends $fs)
319 generate_receipt $receipt $package_file
321 show_unresolved_lib $tmpdir/$file/receipt
322 rm -f $file/fs/.[A-Z]*
324 pack $PACKAGE $file
326 mv $file.tazpkg .
327 else
328 eval_gettext "\$package_file does not look like an Archlinux/Alpine package !"; newline
329 fi
330 unset_receipt
331 rm -rf $tmpdir
332 }
334 # convert a .deb package to .tazpkg
335 # Usage: convert_deb $package_file
336 convert_deb() {
337 local package_file="$1"
339 # Extract deb control
340 mkdir -p $tmpdir
341 dpkg-deb -e $package_file $tmpdir
344 # filter control file for variable
345 # Usage: filter_vars $search_var
346 filter_vars() {
347 local var=$1
348 local result=$(grep "^ *$var:" $tmpdir/control)
349 echo ${result##*:}
350 }
352 PACKAGE=$(filter_vars "Package")
353 VERSION=$(filter_vars "Version")
354 SHORT_DESC=$(filter_vars "Description")
355 WEB_SITE="http://packages.debian.org/search?keywords=$PACKAGE"
356 MAINTAINER="nobody@slitaz.org"
357 target="$(grep ^Architecture $tmpdir/control | sed 's/.*: //')"
359 case "$target" in
360 i386|all)
361 local file="$tmpdir/$PACKAGE-$VERSION"
362 local receipt="$file/receipt"
363 local fs="$file/fs"
365 # Extract deb contents
366 mkdir -p $fs
367 dpkg-deb -x $package_file $fs
369 DEPENDS=$(find_depends $fs)
370 generate_receipt $receipt $package_file
372 if [ -s $tmpdir/conffiles ]; then
373 cat >> $receipt <<EOT
374 CONFIG_FILES="$(cat $tmpdir/conffiles)"
375 EOT
376 fi
378 show_unresolved_lib $file/receipt
380 # Description.txt
381 [ -z "$SHORT_DESC" ] || echo "$SHORT_DESC" > $file/description.txt
383 pack $PACKAGE $file
385 mv $file.tazpkg .
386 ;;
387 *)
388 gettext "Invalid target: $target (expected i386 or all)"; newline
389 ;;
390 esac
391 rm -rf $tmpdir
392 }
394 # convert a .rpm package to .tazpkg
395 # Usage: convert_rpm $package_file
396 convert_rpm() {
397 local package_file=$1
399 # Move package_file to tmp
400 mkdir -p $tmpdir
401 cp $package_file $tmpdir
402 package_file=$tmpdir/$(basename $package_file)
404 # filter package_file for variable
405 # Usage: filter_vars $search_var
406 filter_vars() {
407 local var=$1
408 echo $(rpm -qip $package_file | grep "^$var*" | awk '{print $3}')
409 }
411 PACKAGE=$(filter_vars "Name")
412 VERSION=$(filter_vars "Version")
413 SHORT_DESC=$(filter_vars "Summary")
414 WEB_SITE=$(filter_vars "URL")
415 if [ -z "$WEB_SITE" ]; then
416 WEB_SITE="http://rpmfind.net/linux/rpm2html/search.php?query=$PACKAGE"
417 fi
418 CATEGORY="misc";
419 MAINTAINER="nobody@slitaz.org";
422 local file="$tmpdir/$PACKAGE-$VERSION"
423 local receipt="$file/receipt"
424 local fs="$file/fs"
426 mkdir -p $fs
428 rpm -qip $package_file | \
429 grep "^Description*" -A 100 | \
430 grep -v "^Description*" > \
431 $file/description.txt
433 cd $fs
434 rpm2cpio $package_file | cpio -idm --quiet
435 cd - > /dev/null
438 DEPENDS=$(find_depends $fs)
439 generate_receipt $receipt $package_file
441 show_unresolved_lib $receipt
443 pack $PACKAGE $file
445 mv $file.tazpkg .
447 unset_receipt
448 rm -rf $tmpdir
449 }
452 #
453 # Commands and exit
454 #
456 case "$1" in
457 ""|*usage|*help) usage ;;
458 esac
460 #
461 # Handle packages: package package.tazpkg ... packageN packageN.tazpkg
462 #
464 check_root
466 for pkg in $@
467 do
468 if [ "$(dd if=$pkg bs=8 count=1 skip=1 2> /dev/null)" == "debian-b" ];
469 then
470 convert_deb $pkg
471 else
472 case "$pkg" in
473 *.deb|*.udeb)
474 convert_deb $pkg;;
475 *.rpm)
476 convert_rpm $pkg;;
477 *.apk|*.pkg.tar.gz|*.pkg.tar.xz)
478 convert_arch $pkg;;
479 *.ipk|*.opk)
480 convert_ipk $pkg;;
481 *)
482 gettext "Unsupported format"; newline;;
483 esac
484 fi
485 done
487 exit 0