spk view lib/libspk.sh @ rev 17

libspk.sh: set $root for all files/DB path, spk-rm: work with --root=
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 15 12:29:41 2012 +0200 (2012-05-15)
parents dcf6700bb045
children e5d4c5d3ccf4
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-packages.list"
20 activity="${root}${PKGS_DB}/activity"
22 #
23 # Functions
25 # Display receipt information. Expects a receipt to be sourced
26 receipt_info() {
27 cat << EOT
28 $(gettext "Version :") ${VERSION}${EXTRAVERSION}
29 $(gettext "Short desc :") $SHORT_DESC
30 $(gettext "Category :") $CATEGORY
31 EOT
32 }
34 # Extract receipt from tazpkg
35 # Parameters: result_dir package_file
36 extract_receipt() {
37 local dir="$1"
38 local file="$2"
39 pushd "$dir" > /dev/null
40 { cpio --quiet -i receipt > /dev/null 2>&1; } < $file
41 popd > /dev/null
42 }
44 # Used by: list
45 count_installed() {
46 local count=$(ls ${root}${installed} | wc -l)
47 gettext "Installed packages"; echo ": $count"
48 }
50 # Used by: list
51 count_mirrored() {
52 local count=$(cat $pkgsmd5 | wc -l)
53 gettext "Mirrored packages"; echo ": $count"
54 }
56 is_package_mirrored() {
57 local name=$1
58 local occurance=$(cat $pkgsdesc | grep "$name ")
59 [ -n "$occurance" ]
60 }
62 # Download a file trying all mirrors
63 # Parameters: package/file
64 download() {
65 local package=$1
66 local mirror="$(cat $mirrorurl)"
67 case "$package" in
68 *.tazpkg)
69 echo "${mirror%/}/$package"
70 wget -c ${mirror%/}/$package ;;
71 esac
72 }
74 # Assume package_name is valid
75 # There may be a more efficient way to do this...
76 full_package() {
77 local name=$1
78 local occurance=$(cat $pkgsdesc | grep "$name ")
79 local count=0
80 for i in $(echo $occurance | tr "|" "\n"); do
81 if [ $count -eq 1 ]; then
82 echo $name-$i && return
83 fi
84 count=$(($count+1))
85 done
86 }
88 # Check if a package is already installed.
89 # Parameters: package
90 check_for_installed_package() {
91 local name="$1"
92 if [ -d "${root}${installed}/$name" ]; then
93 newline
94 echo $name $(gettext "package is already installed.")
95 exit 0
96 fi
97 }
99 # get an already installed package from packages.equiv TODO REDO!
100 equivalent_pkg() {
101 for i in $(grep -hs "^$1=" ${root}${PKGS_DB}/packages.equiv \
102 ${root}${PKGS_DB}/undigest/*/packages.equiv | sed "s/^$1=//")
103 do
104 if echo $i | fgrep -q : ; then
105 # format 'alternative:newname'
106 # if alternative is installed then substitute newname
107 if [ -f ${root}${installed}/${i%:*}/receipt ]; then
108 # substitute package dependancy
109 echo ${i#*:}
110 return
111 fi
112 else
113 # if alternative is installed then nothing to install
114 if [ -f ${root}${installed}/$i/receipt ]; then
115 # substitute installed package
116 echo $i
117 return
118 fi
119 fi
120 done
121 # if not found in packages.equiv then no substitution
122 echo $1
123 }
125 # Check for missing deps listed in a receipt packages.
126 # Parameters: package dependencies
127 missing_deps() {
128 local package="$1"
129 shift 1
130 local depends="$@"
132 local deps=0
133 local missing
135 # Calculate missing dependencies
136 for pkgorg in $depends; do
137 local pkg=$(equivalent_pkg $pkgorg)
138 if [ ! -d "${root}${installed}/$pkg" ]; then
139 gettext "Missing: \$pkg"; newline
140 deps=$(($deps+1))
141 elif [ ! -f "${root}${installed}/$pkg/receipt" ]; then
142 gettext "WARNING Dependency loop between \$package and \$pkg."; newline
143 fi
144 done
145 if [ $deps -gt 0 ]; then
146 echo $deps $(gettext "missing package(s) to install.")
147 fi
149 gettext "\$deps missing package(s) to install."; newline
151 # Return true if missing deps
152 [ "$deps" != "0" ]
153 }
155 grepesc() {
156 sed 's/\[/\\[/g'
157 }