spk view spk-up @ rev 81

Add spk-up: lets update the system or individual packages, priority work (unlike tazpkg)
author Christophe Lincoln <pankso@slitaz.org>
date Thu May 24 17:02:36 2012 +0200 (2012-05-24)
parents
children ee3163f781b3
line source
1 #!/bin/sh
2 #
3 # Spk-up - Update 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 #
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 "Update packages lists and upgrade the system")
24 $(boldify $(gettext "Options:"))
25 --list $(gettext "Recharges the packages lists")
26 --add $(gettext "Install upgrade automaticaly")
27 --forced $(gettext "Force recharging the lists")
28 --mirror= $(gettext "Specify a mirror to check")
30 $(boldify $(gettext "Examples:"))
31 $name package1 package2
32 $name --list --forced --mirror=main
34 EOT
35 exit 0
36 }
38 # Headers fo system or packages update
39 up_headers() {
40 newline
41 boldify $(gettext "Package") \
42 $(echo -n $(indent 28 $(gettext "Version"))) \
43 $(echo -n $(indent 48 $(gettext "Status"))) \
44 $(echo -n $(indent 68 $(gettext "Mirror")))
45 separator
46 }
48 # Recharges all list from one mirror or all mirrors
49 recharge_lists() {
50 check_root
51 newline
52 boldify $(gettext "Updating packages lists")
53 separator
54 check="$extradb/*/mirror $PKGS_DB/mirror"
55 if [ "$mirror" ]; then
56 check=$extradb/$mirror/mirror
57 [ "$mirror" == "main" ] && check=$PKGS_DB/mirror
58 fi
59 for mirror in $check; do
60 [ -f "$mirror" ] || continue
61 # Skip local mirror
62 [ ! $(readlink $(dirname $mirror)/packages.desc) ] || continue
63 # Get want a mirror name and download url
64 name=$(basename $(dirname $mirror))
65 url=$(cat $mirror)
66 lists="packages.list packages.md5 packages.desc packages.equiv files.list.lzma"
67 [ "$(dirname $mirror)" == "$PKGS_DB" ] && name="main"
68 [ "$quiet" ] && quiet="-q"
70 gettext "Checking mirror:"; colorize 34 " $name"
71 cd $(dirname $mirror)
73 # ID
74 [ -f "ID" ] || echo "$$" > ID
75 mv ID ID.bak
76 wget -q ${url%/}/ID
77 debug "ID: $(cat ID)"
78 debug "ID.bak: $(cat ID.bak)"
79 if [ $(cat ID) == $(cat ID.bak) ] && [ ! "$forced" ]; then
80 gettext "Mirror is up-to-date"; newline
81 continue
82 fi
84 # Backup and get all lists
85 for list in $lists
86 do
87 [ -f "$list" ] && cp -f $list $list.bak
88 debug "url: ${url%/}/$list"
89 rm -f $list
90 busybox wget $quiet ${url%/}/$list
91 done
92 done
93 separator
94 newline
95 }
97 # Repo priority: local, extras then official
98 priority() {
99 extras=$(ls $extradb | sed s"/local//")
100 for i in $extras; do
101 extras="$extradb/$i"
102 done
103 [ -d "$extradb/local" ] && localdb="$extradb/local"
104 echo "$localdb $extras $PKGS_DB"
105 }
107 # Types: blocked, new build or new version
108 up_type() {
109 # Jump to next repository if pkg doesn't exists in this one.
110 grep -q "^$PACKAGE |" $dbdesc || continue
112 echo -n "$PACKAGE"
113 echo -n $(indent 28 "$VERSION")
115 # Mirror name
116 case $repo in
117 $PKGS_DB) reponame=$(gettext "Official") ;;
118 *) reponame=$(gettext "Extra") ;;
119 esac
120 # Local have priority
121 if [ -d "$localdb" ] && $(grep -q "^$PACKAGE |" $extradb/local/packages.desc); then
122 #local_count=$(($local_count + 1))
123 reponame=$(gettext "Local")
124 fi
126 # Blocked
127 if $(grep -qs "^$PACKAGE" $blocked); then
128 blocked_count=$(($blocked_count + 1))
129 echo -n $(colorize 31 $(indent 48 $(gettext "Blocked")))
130 indent 68 "$reponame"
131 break
132 fi
134 new=$(grep "^$PACKAGE |" $dbdesc | awk '{print $3}')
136 if [ "$VERSION" == "$new" ]; then
137 build_count=$(($build_count + 1))
138 echo -n $(colorize 34 $(indent 48 $(gettext "New build")))
139 else
140 echo -n $(colorize 32 $(indent 48 $(gettext "New") $new))
141 fi
142 indent 68 "$reponame"
143 echo "$PACKAGE" >> $pkgsup
144 }
146 # Check if we have an upgrade for a package
147 check_pkgup() {
148 unset_receipt
149 . $pkg/receipt
150 sum=$(fgrep " $PACKAGE-${VERSION}$EXTRAVERSION.tazpkg" \
151 $installed.$SUM | awk '{print $1}')
153 # Skip up-to-date local packages
154 if [ -d "$extradb/local" ] && fgrep -q "$sum $PACKAGE-" $extradb/local/packages.$SUM; then
155 continue
156 fi
158 for repo in $(priority); do
159 dbdesc=$repo/packages.desc
160 dbsum=$repo/packages.$SUM
162 # Sum match or not ?
163 if ! fgrep -q "$sum $PACKAGE-" $dbsum; then
164 up_type
165 break
166 fi
167 done
168 }
170 # Log and install an upgradable package.
171 install_up() {
172 mirrored_pkg $pkg
173 vers=$(echo "$mirrored" | awk '{print $3}')
174 mkdir -p $logdir/$pkg
175 echo "$(date '+%Y-%m-%d %H:%M') : Upgrade to $vers" >> $logdir/$pkg/up.log
176 spk-add $pkg --forced
177 }
179 #
180 # Handle packages and --options
181 #
183 count=0
185 for arg in $@
186 do
187 case "$arg" in
188 *usage|*help) usage ;;
189 --list) recharge_lists && exit 0 ;;
190 --*) continue ;;
191 *)
192 pkg=$arg
193 system=no
194 [ "$count" == 0 ] && up_headers
195 if [ -f "$installed/$pkg/receipt" ]; then
196 count=$(($count +1))
197 cd $installed
198 check=$(check_pkgup)
199 if [ "$check" != "" ]; then
200 echo "$check"
201 [ "$add" ] && install_up
202 else
203 if $(grep -qs "^${pkg}$" $blocked); then
204 echo -n $(colorize 31 "$pkg $VERSION ")
205 gettext "is blocked"; newline
206 else
207 echo -n $(boldify "$pkg $VERSION ")
208 gettext "is up-to-date. Version"; newline
209 fi
210 fi
211 fi ;;
212 esac
213 done
215 # Skip system-wide upgrade if some packages was updated individually.
216 if [ "$system" ]; then
217 [ "$add" ] || newline
218 exit 0
219 fi
221 #
222 # Check all mirrors list and upgrade system.
223 #
225 time=$(date +%s)
226 build_count=0
227 blocked_count=0
229 up_headers
230 cd $installed
231 newline > $pkgsup
233 # Check all installed packages
234 for pkg in *
235 do
236 check_pkgup
237 done
239 # Remove empty line and count
240 sed -i /^$/d $pkgsup
241 upnb=$(cat $pkgsup | wc -l)
242 pkgs=$(ls | wc -l)
243 time=$(($(date +%s) - $time))
245 if [ "$upnb" == 0 ]; then
246 gettext "System is up-to-date..."; newline
247 fi
248 separator
249 echo -n "$pkgs "; gettext "installed packages scanned in"; echo " ${time}s"
250 newline
252 # Summary
253 boldify $(gettext "Packages upgrade summary")
254 separator
255 gettext "New version :"; colorize 32 " $(($upnb - $build_count))"
256 gettext "New build :"; colorize 34 " $build_count"
257 gettext "Blocked :"; colorize 31 " $blocked_count"
258 separator
259 newline
261 # Pkgs to upgrade ? Skip, let --add/--install them all or ask user
262 if [ "$upnb" -gt 0 ]; then
263 if [ "$add" ] || [ "$install" ]; then
264 continue
265 else
266 gettext "Do you wish to upgrade now"
267 if ! confirm; then
268 gettext "Upgrade cancelled"
269 echo -e "\n" && exit 0
270 fi
271 fi
272 # Install and log all upgrade
273 for pkg in $(cat $pkgsup)
274 do
275 install_up
276 done
277 # List is generated each time and must be cleaned so
278 # tazpkg-notify dont find upgrade anymore.
279 rm $pkgsup && touch $pkgsup
280 newline
281 gettext "Handeled upgrades:"; colorize 32 " $upnb"
282 newline
283 fi
285 exit 0