spk view spk-up @ rev 94

spk-up: check_root
author Christophe Lincoln <pankso@slitaz.org>
date Fri May 25 16:35:18 2012 +0200 (2012-05-25)
parents 51329da37c52
children c74dc687b9a3
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 newline
51 boldify $(gettext "Updating packages lists")
52 separator
53 check="$extradb/*/mirror $PKGS_DB/mirror"
54 if [ "$mirror" ]; then
55 check=$extradb/$mirror/mirror
56 [ "$mirror" == "main" ] && check=$PKGS_DB/mirror
57 fi
58 for mirror in $check; do
59 [ -f "$mirror" ] || continue
60 # Skip local mirror
61 [ ! $(readlink $(dirname $mirror)/packages.desc) ] || continue
62 # Get want a mirror name and download url
63 name=$(basename $(dirname $mirror))
64 url=$(cat $mirror)
65 lists="packages.list packages.md5 packages.desc packages.equiv files.list.lzma"
66 [ "$(dirname $mirror)" == "$PKGS_DB" ] && name="main"
67 [ "$quiet" ] && quiet="-q"
69 gettext "Checking mirror:"; colorize 34 " $name"
70 cd $(dirname $mirror)
72 # ID
73 [ -f "ID" ] || echo "$$" > ID
74 mv ID ID.bak
75 wget -q ${url%/}/ID
76 debug "ID: $(cat ID)"
77 debug "ID.bak: $(cat ID.bak)"
78 if [ $(cat ID) == $(cat ID.bak) ] && [ ! "$forced" ]; then
79 gettext "Mirror is up-to-date"; newline
80 continue
81 fi
83 # Backup and get all lists
84 for list in $lists
85 do
86 [ -f "$list" ] && cp -f $list $list.bak
87 debug "url: ${url%/}/$list"
88 rm -f $list
89 busybox wget $quiet ${url%/}/$list
90 done
91 done
92 separator
93 }
95 # Repo priority: local, extras then official
96 priority() {
97 extras=$(ls $extradb | sed s"/local//")
98 for i in $extras; do
99 extras="$extradb/$i"
100 done
101 [ -d "$extradb/local" ] && local="$extradb/local"
102 echo "$local $extras $PKGS_DB"
103 }
105 # Types: blocked, new build or new version
106 up_type() {
107 # Jump to next repository if pkg doesn't exists in this one.
108 grep -q "^$PACKAGE |" $dbdesc || continue
110 echo -n "$PACKAGE"
111 echo -n $(indent 28 "$VERSION")
113 # Blocked
114 if $(grep -qs "^$PACKAGE" $blocked); then
115 blocked_count=$(($blocked_count + 1))
116 echo -n $(colorize 31 $(indent 48 $(gettext "Blocked")))
117 indent 68 "$reponame"
118 break
119 fi
121 new=$(grep "^$PACKAGE |" $dbdesc | awk '{print $3}')
123 if [ "$VERSION" == "$new" ]; then
124 build_count=$(($build_count + 1))
125 echo -n $(colorize 34 $(indent 48 $(gettext "New build")))
126 else
127 echo -n $(colorize 32 $(indent 48 $(gettext "New") $new))
128 fi
129 indent 68 "$reponame"
130 echo "$PACKAGE" >> $pkgsup
131 }
133 # Check if we have an upgrade for a package
134 check_pkgup() {
135 unset_receipt
136 . $pkg/receipt
137 localdb=$extradb/local
138 sum=$(fgrep " $PACKAGE-${VERSION}$EXTRAVERSION.tazpkg" \
139 $installed.$SUM | awk '{print $1}')
141 # Skip up-to-date local packages
142 if [ -d "$localdb" ] && fgrep -q "$sum $PACKAGE-" $localdb/packages.$SUM; then
143 reponame=$(gettext "Local")
144 return 0
145 fi
147 for repo in $(priority); do
148 dbdesc=$repo/packages.desc
149 dbsum=$repo/packages.$SUM
151 # Mirror name
152 case $repo in
153 $PKGS_DB) reponame=$(gettext "Official") ;;
154 *) reponame=$(gettext "Extra") ;;
155 esac
156 # Overwrite: Local have priority
157 if [ -d "$localdb" ] && fgrep -q "$sum $PACKAGE-" $localdb/packages.$SUM; then
158 reponame=$(gettext "Local")
159 fi
161 # Sum match or not ?
162 if ! fgrep -q "$sum $PACKAGE-" $dbsum; then
163 up_type
164 break
165 fi
166 done
167 }
169 # Log and install an upgradable package.
170 install_up() {
171 mirrored_pkg $pkg
172 vers=$(echo "$mirrored" | awk '{print $3}')
173 mkdir -p $logdir/$pkg
174 echo "$(date '+%Y-%m-%d %H:%M') : Upgrade to $vers" >> $logdir/$pkg/up.log
175 spk-add $pkg --forced
176 }
178 #
179 # Handle packages and --options
180 #
182 count=0
184 for arg in $@
185 do
186 case "$arg" in
187 *usage|*help) usage ;;
188 --list)
189 check_root
190 recharge_lists
191 newline && exit 0 ;;
192 --*) continue ;;
193 *)
194 pkg="$arg"
195 system=no
196 check_root
197 [ "$count" == 0 ] && up_headers
198 if [ -f "$installed/$pkg/receipt" ]; then
199 count=$(($count +1))
200 cd $installed
201 . $pkg/receipt
202 check=$(check_pkgup)
203 if [ "$check" != "" ]; then
204 echo "$check"
205 [ "$add" ] && install_up
206 else
207 echo -n "$pkg"
208 echo -n $(indent 28 "$VERSION")
209 echo -n $(colorize 32 $(indent 48 $(gettext "up-to-date")))
210 check_pkgup
211 indent 68 "$reponame"
212 fi
213 fi ;;
214 esac
215 done
217 # Skip system-wide upgrade if some packages was updated individually.
218 if [ "$system" ]; then
219 [ "$add" ] || newline
220 exit 0
221 fi
223 #
224 # Check all mirrors list and upgrade system.
225 #
227 time=$(date +%s)
228 build_count=0
229 blocked_count=0
231 check_root
232 recharge_lists
233 up_headers
234 cd $installed
235 newline > $pkgsup
237 # Check all installed packages
238 for pkg in *
239 do
240 check_pkgup
241 done
243 # Remove empty line and count
244 sed -i /^$/d $pkgsup
245 upnb=$(cat $pkgsup | wc -l)
246 pkgs=$(ls | wc -l)
247 time=$(($(date +%s) - $time))
249 if [ "$upnb" == 0 ] && [ "$blocked_count" == 0 ]; then
250 gettext "System is up-to-date..."; newline
251 fi
252 separator
253 echo -n "$pkgs "; gettext "installed packages scanned in"; echo " ${time}s"
254 newline
256 # Summary
257 boldify $(gettext "Packages upgrade summary")
258 separator
259 gettext "New version :"; colorize 32 " $(($upnb - $build_count))"
260 gettext "New build :"; colorize 34 " $build_count"
261 gettext "Blocked :"; colorize 31 " $blocked_count"
262 separator
263 newline
265 # Pkgs to upgrade ? Skip, let --add/--install them all or ask user
266 if [ "$upnb" -gt 0 ]; then
267 if [ "$add" ] || [ "$install" ]; then
268 continue
269 else
270 gettext "Do you wish to upgrade now"
271 if ! confirm; then
272 gettext "Upgrade cancelled"
273 echo -e "\n" && exit 0
274 fi
275 fi
276 # Install and log all upgrade
277 for pkg in $(cat $pkgsup)
278 do
279 install_up
280 done
281 # List is generated each time and must be cleaned so
282 # tazpkg-notify dont find upgrade anymore.
283 rm $pkgsup && touch $pkgsup
284 newline
285 gettext "Handeled upgrades:"; colorize 32 " $upnb"
286 newline
287 fi
289 exit 0