spk view lib/libspk.sh @ rev 28

spk-add: rename some functions
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 15 18:56:21 2012 +0200 (2012-05-15)
parents eb8149264a44
children daf5adaaa87b
line source
1 #!/bin/sh
2 #
3 # Libspk - The Spk base function and internal variables used by almost all
4 # spk-tools. Read the README before adding or modifing any code in spk!
5 #
6 # Copyright (C) SliTaz GNU/Linux - BSD License
7 # Author: See AUTHORS files
8 #
9 . /lib/libtaz.sh
10 . /usr/lib/slitaz/libpkg.sh
11 . /etc/slitaz/slitaz.conf
13 # Internal variables.
14 mirrorurl="${root}${PKGS_DB}/mirror"
15 installed="${root}${PKGS_DB}/installed"
16 pkgsdesc="${root}${PKGS_DB}/packages.desc"
17 pkgsmd5="${root}${PKGS_DB}/packages.md5"
18 # ????do we need packages.equiv????
19 blocked="${root}${PKGS_DB}/blocked.list"
20 activity="${root}${PKGS_DB}/activity"
22 #
23 # Sanity checks
24 #
26 if [ ! -d "${root}${PKGS_DB}" ]; then
27 gettext "Can't find DB:"; echo " ${root}${PKGS_DB}"
28 exit 1
29 fi
31 #
32 # Functions
33 #
35 # Display receipt information. Expects a receipt to be sourced
36 receipt_info() {
37 cat << EOT
38 $(gettext "Version :") ${VERSION}${EXTRAVERSION}
39 $(gettext "Short desc :") $SHORT_DESC
40 $(gettext "Category :") $CATEGORY
41 EOT
42 }
44 # Extract receipt from tazpkg
45 # Parameters: result_dir package_file
46 extract_receipt() {
47 local dir="$1"
48 local file="$2"
49 cd "$dir"
50 { cpio --quiet -i receipt > /dev/null 2>&1; } < $file
51 cd - >/dev/null
52 }
54 # Used by: list
55 count_installed() {
56 local count=$(ls $installed | wc -l)
57 gettext "Installed :"; echo " $count"
58 }
60 # Used by: list
61 count_mirrored() {
62 local count=$(cat $pkgsmd5 | wc -l)
63 gettext "Mirrored :"; echo " $count"
64 }
66 is_package_mirrored() {
67 local name=$1
68 local occurance=$(cat $pkgsdesc | grep "$name ")
69 [ -n "$occurance" ]
70 }
72 # Download a file trying all mirrors
73 # Parameters: package/file
74 download() {
75 local package=$1
76 local mirror="$(cat $mirrorurl)"
77 case "$package" in
78 *.tazpkg)
79 echo "${mirror%/}/$package"
80 wget -c ${mirror%/}/$package ;;
81 esac
82 }
84 # Assume package name is valid
85 # There may be a more efficient way to do this...
86 full_package() {
87 local name=$1
88 local occurance=$(cat $pkgsdesc | grep "$name ")
89 local count=0
90 for i in $(echo $occurance | tr "|" "\n"); do
91 if [ $count -eq 1 ]; then
92 echo $name-$i && return
93 fi
94 count=$(($count+1))
95 done
96 }
98 # Check if a package is already installed.
99 # Usage: check_installed package
100 check_installed() {
101 local name="$1"
102 if [ -d "$installed/$name" ]; then
103 echo $(boldify "$name") $(gettext "package is already installed.")
104 continue
105 fi
106 }
108 # get an already installed package from packages.equiv TODO REDO!
109 equivalent_pkg() {
110 for i in $(grep -hs "^$1=" ${root}${PKGS_DB}/packages.equiv \
111 ${root}${PKGS_DB}/undigest/*/packages.equiv | sed "s/^$1=//")
112 do
113 if echo $i | fgrep -q : ; then
114 # format 'alternative:newname'
115 # if alternative is installed then substitute newname
116 if [ -f $installed/${i%:*}/receipt ]; then
117 # substitute package dependancy
118 echo ${i#*:}
119 return
120 fi
121 else
122 # if alternative is installed then nothing to install
123 if [ -f $installed/$i/receipt ]; then
124 # substitute installed package
125 echo $i
126 return
127 fi
128 fi
129 done
130 # if not found in packages.equiv then no substitution
131 echo $1
132 }
134 # Check for missing deps listed in a receipt packages.
135 # Parameters: package dependencies
136 missing_deps() {
137 local package="$1"
138 shift 1
139 local depends="$@"
141 local deps=0
142 local missing
144 # Calculate missing dependencies
145 for pkgorg in $depends; do
146 local pkg=$(equivalent_pkg $pkgorg)
147 if [ ! -d "$installed/$pkg" ]; then
148 gettext "Missing: \$pkg"; newline
149 deps=$(($deps+1))
150 elif [ ! -f "$installed/$pkg/receipt" ]; then
151 gettext "WARNING Dependency loop between \$package and \$pkg."; newline
152 fi
153 done
154 if [ $deps -gt 0 ]; then
155 echo $deps $(gettext "missing package(s) to install.")
156 fi
158 gettext "\$deps missing package(s) to install."; newline
160 # Return true if missing deps
161 [ "$deps" != "0" ]
162 }
164 grepesc() {
165 sed 's/\[/\\[/g'
166 }
168 # Check for ELF file
169 is_elf() {
170 [ "$(dd if=$1 bs=1 skip=1 count=3 2> /dev/null)" = "ELF" ]
171 }