spk annotate lib/libspk.sh @ rev 131

Tiny edits
author Paul Issott <paul@slitaz.org>
date Mon Mar 03 16:06:46 2014 +0000 (2014-03-03)
parents ee5a53b16e51
children db947fd832be
rev   line source
pankso@5 1 #!/bin/sh
pankso@5 2 #
paul@12 3 # Libspk - The Spk base function and internal variables used by almost all
paul@131 4 # spk-tools. Read the README before adding or modifying any code in spk!
pankso@5 5 #
pankso@5 6 # Copyright (C) SliTaz GNU/Linux - BSD License
pankso@5 7 # Author: See AUTHORS files
pankso@5 8 #
pankso@5 9 . /lib/libtaz.sh
pankso@5 10 . /usr/lib/slitaz/libpkg.sh
pankso@5 11 . /etc/slitaz/slitaz.conf
pankso@5 12
pankso@5 13 # Internal variables.
pankso@17 14 mirrorurl="${root}${PKGS_DB}/mirror"
pankso@59 15 mirrors="${root}${PKGS_DB}/mirrors"
pankso@17 16 installed="${root}${PKGS_DB}/installed"
pankso@17 17 pkgsdesc="${root}${PKGS_DB}/packages.desc"
pankso@32 18 pkgsmd5="${root}${PKGS_DB}/packages.$SUM"
pankso@60 19 pkgsequiv="${root}${PKGS_DB}/packages.equiv"
pankso@80 20 pkgsup="${root}${PKGS_DB}/packages.up"
pankso@19 21 blocked="${root}${PKGS_DB}/blocked.list"
pankso@17 22 activity="${root}${PKGS_DB}/activity"
pankso@57 23 logdir="${root}/var/log/spk"
pankso@59 24 extradb="${root}${PKGS_DB}/extra"
pankso@60 25 tmpdir="/tmp/spk/$RANDOM"
pankso@5 26
pankso@5 27 #
pankso@19 28 # Sanity checks
pankso@19 29 #
pankso@19 30
pankso@19 31 if [ ! -d "${root}${PKGS_DB}" ]; then
pankso@19 32 gettext "Can't find DB:"; echo " ${root}${PKGS_DB}"
pankso@19 33 exit 1
pankso@19 34 fi
pankso@19 35
pankso@80 36 if [ ! -d "${root}${extradb}" ]; then
pankso@80 37 mkdir -p ${root}${extradb}
pankso@80 38 fi
pankso@80 39
pankso@93 40 if [ ! -d "${root}${CACHE_DIR}" ]; then
pankso@93 41 mkdir -p ${root}${CACHE_DIR}
pankso@93 42 fi
pankso@93 43
pankso@19 44 #
pankso@5 45 # Functions
pankso@18 46 #
meshca@11 47
pankso@14 48 # Display receipt information. Expects a receipt to be sourced
pankso@5 49 receipt_info() {
pankso@5 50 cat << EOT
pankso@5 51 $(gettext "Version :") ${VERSION}${EXTRAVERSION}
pankso@5 52 $(gettext "Short desc :") $SHORT_DESC
pankso@5 53 $(gettext "Category :") $CATEGORY
pankso@5 54 EOT
pankso@5 55 }
pankso@5 56
pankso@62 57 # Display package info from a packages.desc list
pankso@62 58 # Usage: read_pkgsdesc /path/to/packages.desc
pankso@62 59 read_pkgsdesc() {
pankso@62 60 local list="$1"
pankso@62 61 IFS="|"
pankso@62 62 cat $list | while read package version desc category
pankso@62 63 do
pankso@62 64 if [ "$short" ]; then
meshca@101 65 echo $(colorize 32 "$package") $(indent 28 " $version")
pankso@62 66 else
pankso@62 67 newline
meshca@69 68 gettext "Package :"; colorize 32 " $package"
pankso@62 69 gettext "Version :"; echo "$version"
pankso@62 70 gettext "Short desc :"; echo "$desc"
pankso@62 71 fi
pankso@62 72 done && unset IFS
pankso@62 73 }
pankso@62 74
meshca@9 75 # Extract receipt from tazpkg
meshca@9 76 # Parameters: result_dir package_file
meshca@9 77 extract_receipt() {
meshca@9 78 local dir="$1"
meshca@9 79 local file="$2"
pankso@26 80 cd "$dir"
meshca@9 81 { cpio --quiet -i receipt > /dev/null 2>&1; } < $file
pankso@26 82 cd - >/dev/null
meshca@9 83 }
meshca@9 84
pankso@60 85 # Extract files.list from tazpkg
pankso@60 86 # Parameters: result_dir package_file
pankso@60 87 extract_fileslist() {
pankso@60 88 local dir="$1"
pankso@60 89 local file="$2"
pankso@60 90 cd "$dir"
pankso@60 91 { cpio --quiet -i files.list > /dev/null 2>&1; } < $file
pankso@60 92 cd - >/dev/null
pankso@60 93 }
pankso@60 94
slaxemulator@107 95 # Extract library.list from tazpkg
slaxemulator@107 96 # Parameters: result_dir package_file
slaxemulator@107 97 extract_librarylist() {
slaxemulator@107 98 local dir="$1"
slaxemulator@107 99 local file="$2"
slaxemulator@107 100 cd "$dir"
slaxemulator@107 101 { cpio --quiet -i library.list > /dev/null 2>&1; } < $file
slaxemulator@107 102 cd - >/dev/null
slaxemulator@107 103 }
slaxemulator@107 104
meshca@70 105 is_package_installed() {
slaxemulator@72 106 [ -f "$installed/$1/receipt" ]
meshca@70 107 }
meshca@70 108
pankso@5 109 # Used by: list
pankso@5 110 count_installed() {
pankso@18 111 local count=$(ls $installed | wc -l)
pankso@60 112 gettext "Installed :"; echo " $count"
pankso@5 113 }
pankso@5 114
pankso@5 115 # Used by: list
pankso@5 116 count_mirrored() {
pankso@62 117 [ -f "$pkgsmd5" ] || return
meshca@9 118 local count=$(cat $pkgsmd5 | wc -l)
pankso@60 119 gettext "Mirrored :"; echo " $count"
pankso@5 120 }
meshca@9 121
pankso@63 122 # Check if package is on main or extra mirror.
pankso@63 123 mirrored_pkg() {
pankso@14 124 local name=$1
pankso@67 125 #local find=$(grep "^$name |" $pkgsdesc $extradb/*/*.desc 2>/dev/null)
pankso@67 126 for desc in $(find $extradb $pkgsdesc -name packages.desc); do
pankso@67 127 if grep -q "^$name |" $desc; then
pankso@67 128 db=$(dirname $desc)
pankso@67 129 mirrored=$(grep "^$name |" $desc)
pankso@67 130 mirror=$(cat $db/mirror)
pankso@67 131 break
pankso@67 132 fi
pankso@67 133 done
meshca@11 134 }
meshca@11 135
pankso@68 136 # Check if the download was sane
pankso@68 137 check_download() {
pankso@68 138 debug "check_download: $file"
pankso@68 139 if ! tail -c 2k $file | fgrep -q 00000000TRAILER; then
pankso@68 140 gettext "Continuing download of:"; echo " $file"
pankso@68 141 download "$file" $mirror
pankso@68 142 fi
pankso@68 143 # Check that the package has the correct checksum
pankso@68 144 local msum=$(fgrep " $package_full" $pkgsmd5)
pankso@68 145 local sum=$($CHECKSUM $file)
pankso@68 146 debug "mirror $SUM : $msum"
pankso@68 147 debug "local $SUM : $sum"
pankso@68 148 if [ "$sum" != "$msum" ]; then
pankso@68 149 rm -f $file && download "$file" $mirror
pankso@68 150 fi
pankso@68 151 }
pankso@68 152
meshca@11 153 # Download a file trying all mirrors
pankso@67 154 # Usage: file [url|path]
pankso@65 155 #
paul@98 156 # Priority to extra is done by mirrored_pkg which tries first to find the
pankso@67 157 # packages in extra mirror, then on official.
pankso@65 158 #
meshca@11 159 download() {
pankso@65 160 local file=$1
pankso@67 161 local uri="${2%/}"
pankso@67 162 local pwd=$(pwd)
pankso@65 163 [ "$quiet" ] && local quiet="-q"
pankso@67 164 [ "$cache" ] && local pwd=$CACHE_DIR
pankso@67 165 [ "$forced" ] && rm -f $pwd/$file
pankso@67 166 debug "download file: $file"
pankso@67 167 debug "DB: $db"
paul@98 168 # Local mirror ? End by cd to cache we may be installed in. If --get
pankso@67 169 # was used we dl/copy in the current dir.
pankso@67 170 if [ -f "$uri/$file" ]; then
pankso@68 171 [ "$verbose" ] && echo "URI: $uri/"
pankso@68 172 gettext "Using local mirror:"; boldify " $file"
meshca@69 173 [ "$verbose" ] && (gettext "Copying file to:"; colorize 34 " $pwd")
pankso@67 174 cp -f $uri/$file $pwd
pankso@67 175 cd $pwd && return 0
pankso@67 176 fi
pankso@67 177 # In cache ? Root can use --cache to set destdir.
pankso@67 178 if [ -f "$CACHE_DIR/$file" ]; then
meshca@69 179 gettext "Using cache:"; colorize 34 " ${file%.tazpkg}"
pankso@67 180 return 0
pankso@67 181 else
pankso@68 182 [ "$verbose" ] && echo "URL: $uri/"
pankso@68 183 if [ "$db" == "$PKGS_DB" ]; then
pankso@68 184 gettext "Using official mirror:"
pankso@68 185 else
pankso@68 186 gettext "Using extra mirror:"
pankso@68 187 fi
pankso@68 188 boldify " $file"
meshca@69 189 [ "$verbose" ] && (gettext "Destination:"; colorize 34 " $pwd")
pankso@67 190 if [ -f "$pwd/$file" ]; then
paul@98 191 echo "File exists: $pwd/$file" && return 0
pankso@67 192 fi
pankso@68 193 # TODO: be a spider with wget -s to check if package is on mirror,
pankso@68 194 # if not try all official mirrors ?
pankso@126 195 busybox wget $quiet -c $uri/$file -O $CACHE_DIR/$file
pankso@68 196 cd $CACHE_DIR && check_download
pankso@67 197 fi
pankso@67 198 # Be sure the file was fetched.
pankso@127 199 if [ ! -f "$CACHE_DIR/$file" ] && [ ! -f "$pwd/$file" ]; then
pankso@127 200 gettext "ERROR: Missing file:"; colorize 31 " $CACHE_DIR/$file"
pankso@67 201 newline && exit 1
pankso@67 202 fi
meshca@11 203 }
meshca@11 204
meshca@71 205 # Extract .tazpkg cpio archive into a directory.
meshca@71 206 # Parameters: package_file results_directory
meshca@71 207 extract_package() {
meshca@71 208 local package_file=$1
meshca@71 209 local target_dir=$2
meshca@71 210
meshca@71 211 # Validate the file
pankso@78 212 #check_valid_tazpkg $package_file
meshca@71 213
meshca@71 214 # Find the package name
meshca@71 215 local package_name=$(package_name $package_file)
meshca@71 216
pankso@78 217 # Create destination directory and copy package
meshca@71 218 local dest_dir=$(pwd)/$package_name
meshca@71 219 [ -n "$target_dir" ] && dest_dir=$target_dir/$package_name
meshca@71 220 mkdir -p $dest_dir
pankso@78 221 cp $package_file $dest_dir
meshca@71 222
pankso@78 223 cd $dest_dir
meshca@73 224 size=$(du -sh $package_file | awk '{print $1}')
meshca@71 225 echo -n $(gettext "Extracting archive"): $size
meshca@71 226 cpio -idm --quiet < ${package_file##*/}
meshca@71 227 rm -f ${package_file##*/}
meshca@71 228 unlzma -c fs.cpio.lzma | cpio -idm --quiet
meshca@71 229 rm fs.cpio.lzma
meshca@71 230 status
meshca@71 231 cd - > /dev/null
meshca@71 232 }
meshca@71 233
paul@131 234 # Unset var set by mirrored_pkg
pankso@68 235 unset_mirrored() {
pankso@68 236 unset mirrored mirror db pwd
pankso@68 237 }
pankso@68 238
pankso@66 239 # Return the full package name, search in all packages.desc and break when
paul@131 240 # first occurrence is found: Usage: full_package pkgname
meshca@11 241 full_package() {
pankso@66 242 for desc in $(find $extradb $pkgsdesc -name packages.desc); do
pankso@66 243 local line="$(grep "^$1 |" $desc)"
pankso@66 244 if grep -q "^$1 |" $desc; then
pankso@66 245 IFS="|"
pankso@66 246 echo $line | busybox awk '{print $1 "-" $2 ".tazpkg"}'
pankso@66 247 unset IFS && break
pankso@66 248 fi
pankso@66 249 done
meshca@11 250 }
meshca@11 251
meshca@11 252 # Check if a package is already installed.
pankso@27 253 # Usage: check_installed package
pankso@27 254 check_installed() {
pankso@14 255 local name="$1"
meshca@104 256 if is_package_installed $name; then
meshca@101 257 echo $(boldify "$name") $(gettext "package is already installed")
pankso@55 258 [ "$forced" ] || rm -rf $tmpdir
pankso@28 259 continue
meshca@11 260 fi
meshca@11 261 }
meshca@11 262
meshca@11 263 # get an already installed package from packages.equiv TODO REDO!
meshca@9 264 equivalent_pkg() {
pankso@66 265 for i in $(grep -hs "^$1=" $pkgsequiv $extradb/*/*.equiv | sed "s/^$1=//")
meshca@9 266 do
meshca@9 267 if echo $i | fgrep -q : ; then
meshca@9 268 # format 'alternative:newname'
meshca@9 269 # if alternative is installed then substitute newname
meshca@104 270 if is_package_installed ${i%:*}; then
paul@131 271 # substitute package dependency
meshca@9 272 echo ${i#*:}
meshca@9 273 return
meshca@9 274 fi
meshca@9 275 else
meshca@9 276 # if alternative is installed then nothing to install
meshca@104 277 if is_package_installed $i/receipt; then
meshca@9 278 # substitute installed package
meshca@9 279 echo $i
meshca@9 280 return
meshca@9 281 fi
meshca@9 282 fi
meshca@9 283 done
meshca@9 284 # if not found in packages.equiv then no substitution
meshca@9 285 echo $1
meshca@9 286 }
meshca@9 287
meshca@9 288 # Check for missing deps listed in a receipt packages.
meshca@9 289 # Parameters: package dependencies
meshca@9 290 missing_deps() {
meshca@9 291 local package="$1"
meshca@9 292 shift 1
meshca@9 293 local depends="$@"
pankso@10 294
meshca@9 295 local deps=0
meshca@9 296 local missing
pankso@10 297
paul@12 298 # Calculate missing dependencies
meshca@9 299 for pkgorg in $depends; do
meshca@9 300 local pkg=$(equivalent_pkg $pkgorg)
pankso@18 301 if [ ! -d "$installed/$pkg" ]; then
pankso@30 302 gettext "Missing:"; echo " $pkg"
meshca@9 303 deps=$(($deps+1))
meshca@106 304 elif [ ! -f "$installed/$pkg/receipt" ]; then
pankso@56 305 gettext "WARNING: Dependency loop between:"; newline
pankso@56 306 echo " $package --> $pkg"
meshca@9 307 fi
meshca@9 308 done
pankso@14 309
meshca@99 310 gettext "Missing dependencies:"; colorize 34 " $deps"
pankso@10 311
meshca@9 312 # Return true if missing deps
meshca@9 313 [ "$deps" != "0" ]
meshca@9 314 }
meshca@11 315
meshca@11 316 grepesc() {
meshca@11 317 sed 's/\[/\\[/g'
meshca@11 318 }
meshca@11 319
pankso@27 320 # Check for ELF file
pankso@27 321 is_elf() {
pankso@27 322 [ "$(dd if=$1 bs=1 skip=1 count=3 2> /dev/null)" = "ELF" ]
pankso@27 323 }
slaxemulator@122 324
slaxemulator@122 325 # Avoid dirname errors by checking for argument and then remove file and
slaxemulator@122 326 # empty directory. Usage: remove_file file
slaxemulator@122 327 remove_file() {
slaxemulator@122 328 [ "$1" ] || return
slaxemulator@122 329 local dir
slaxemulator@122 330 rm -f $1 2>/dev/null
slaxemulator@122 331 dir="$1"
slaxemulator@122 332 while [ "$dir" != "/" ]; do
slaxemulator@122 333 dir="$(dirname $dir)"
slaxemulator@122 334 rmdir $dir 2> /dev/null || break
slaxemulator@122 335 done
slaxemulator@122 336 }