]> pd.if.org Git - zpackage/blob - zpm-update
c3c65ed4d9c8b6423c6f4f197b59b96620d6e022
[zpackage] / zpm-update
1 #!/bin/sh
2
3 warn() {
4         echo $* 1>&2
5 }
6
7 run_ldconfig() {
8         if [ $(id -u) -eq 0 ]; then
9                 if [ ! -d $1/etc ]; then
10                         warn "no etc directory in $1, skipping ldconfig"
11                 elif [ -f $1/sbin/ldconfig ]; then
12                         $1/sbin/ldconfig -r ${1:-/}
13                 elif [ -f /sbin/ldconfig ]; then
14                         /sbin/ldconfig -r ${1:-/}
15                 else
16                         true
17                 fi
18         fi
19 }
20
21 changeall() {
22         for pkgid in $(zpm list -s $1); do
23                 zpm pkg $pkgid status=$2
24         done
25 }
26
27 die() {
28         if [ $# -gt 0 ]; then
29                 echo $* 1>&2
30         fi
31         zpm log -i -a 'aborted install' -t "$pkgid" "$*"
32         changeall installing
33         changeall removing installed
34         changeall updating installed
35         exit 1
36 }
37
38 current_version() (
39         zpm list -s installed $(zpm parse -n $1)
40 )
41
42 maybe_run() (
43         phase=$1
44         pkgid=$2
45         run=${3:-0}
46         current=$4
47
48         if [ $run -gt 0 ]; then
49                 zpm script -p $phase $pkgid $current
50                 rv=$?
51                 if [ $rv -ne 0 ]; then
52                         warn "$phase script for $pkgid failed"
53                         exit $rv
54                 fi
55         else
56                 if zpm script -Fql -p $phase $pkgid; then
57                         zpm note -e -p $pkgid -m "$phase script not run"
58                 fi
59         fi
60 )
61
62 validity_check() {
63         rv=0
64         # check if we're installing something already
65         var=$(zpm list -s installing | wc -l)
66         if [ $var -gt 0 ]; then
67                 zpm list -v -f $localdb -s installing 
68                 warn "already ($localdb) installing $var package(s)"
69                 rv=1
70         fi
71         var=$(zpm list -s removing | wc -l)
72         if [ $var -gt 0 ]; then
73                 zpm list -v -f $localdb -s removing 
74                 warn "already ($localdb) removing $var package(s)"
75                 rv=1
76         fi
77         var=$(zpm list -s updating | wc -l)
78         if [ $var -gt 0 ]; then
79                 zpm list -v -f $localdb -s updating 
80                 warn "already ($localdb) updating $var package(s)"
81                 rv=1
82         fi
83         if [ $rv -ne 0 ]; then
84                 die "aborting update"
85         fi
86 }
87
88 dryrun=0
89 verbose=0
90 runscripts=1
91 runconfigure=1
92 absorb=0
93 overwrite=0
94 syncopts=''
95
96 packagepath="/var/lib/zpm/packages"
97 to_install=
98 to_remove=
99 to_update=
100 install_for_libs=''
101
102 backup=0
103 ignorelibdeps=0
104 justlist=0
105 pullrepos=1
106
107 # treat un-adorned packages as uninstalls
108 uninstall=0
109
110 # TODO option to attempt to resume an ongoing install
111 # zpm-install [-SCn] [ -d localdb ] [ -f pkgfile ] [ -R installroot ] pkgstr ...
112 while getopts f:d:R:nSCvAObBLUz opt; do
113         case $opt in
114                 f) pkgfile="$OPTARG" ;;
115                 d) localdb="$OPTARG" ;;
116                 P) packagepath="$OPTARG" ;;
117                 D) packagepath="$OPTARG:$packagepath" ;;
118                 R) rootdir="$OPTARG" ;;
119                 S) runscripts=0 ;;
120                 C) runconfigure=0 ;;
121                 A) absorb=1; syncopts="${syncopts} -A" ;;
122                 O) overwrite=1; syncopts="${syncopts} -O" ;;
123                 n) dryrun=1 ;;
124                 v) verbose=1 ;;
125                 b) backup=1 ;;
126                 B) backup=0 ;;
127                 L) ignorelibdeps=1 ;;
128                 l) justlist=1 ;;
129                 U) uninstall=1 ;;
130                 z) pullrepos=0 ;;
131                 *) die "usage ..." ;;
132         esac
133 done
134 shift $(( OPTIND - 1))
135
136 ZPMDB=${localdb:-${ZPMDB:-/var/lib/zpm/local.db}}
137 export ZPMDB
138 # create the localdb if needed
139 zpm init $ZPMDB
140
141 zpm test -v "$ZPMDB" || die "$ZPMDB is not a zpm database"
142
143 validity_check
144
145 for op in "$@"; do
146         case "$op" in
147                 -*) pkgid=$(zpm findpkg -I "${op#-}")
148                         if [ -n "$pkgid" ]; then
149                                 to_remove="$to_remove $pkgid"
150                         else
151                                 die "$op not installed"
152                         fi
153                         ;;
154                 +*) pkg=${op#+}
155                         search="$search $pkg"
156                         ;;
157                 *:*) pkg=${op%:*}
158                         file=${op#*:}
159                         pkgid=$(zpm findpkg -f $file $pkg)
160                         if [ -n $pkgid ]; then
161                                 merge="$merge $pkgid:$file"
162                         else
163                                 die "can't find $pkg in $file"
164                         fi
165                         ;;
166                 *)
167                         if [ $uninstall -eq 0 ]; then
168                                 search="$search $op"
169                         else
170                                 pkgid=$(zpm findpkg -I "${op}")
171                                 if [ -n "$pkgid" ]; then
172                                         to_remove="$to_remove $pkgid"
173                                 else
174                                         die "$op not installed"
175                                 fi
176                         fi
177                         ;;
178         esac
179 done
180
181 search=$(echo $search)
182
183 ebail=0
184
185 if [ $pullrepos -eq 1 ]; then
186         echo pulling repos
187         zpm repo pull
188 fi
189
190 if [ $ignorelibdeps -eq 1 ]; then
191         merge=$(zpm search -iIO $search)
192 else
193         merge=$(zpm search -iIO -l $search)
194 fi
195
196 if [ $? -ne 0 ]; then
197         warn "unmet packages or dependencies, or errors"
198         exit 1
199 fi
200
201 installset=
202 for mergepkg in $merge; do
203         pkgid=${mergepkg%%:*}
204         installset="$installset $pkgid"
205 done
206 installset=$(printf "%s\n" $installset | sort -u)
207
208 # TODO check for downgrade
209
210 # fixup package
211 nlist=
212 nfound=
213 baddl=
214 for mergepkg in $merge; do
215         pkgid=${mergepkg%:*}
216         repofile=${mergepkg#*:}
217         if [ ${repofile##*.} = 'repo' ]; then
218                 status=$(zpm pkg -f $repofile $pkgid status)
219                 reponame=$(basename $repofile .repo)
220                 if [ "$status" != 'merged' ]; then
221                         repodir=$(dirname $repofile)
222                         zpmfile=$repodir/$reponame/$pkgid.zpm
223                         if [ -f "$zpmfile" ]; then
224                                 nlist="$nlist $pkgid:$zpmfile"
225                                 continue
226                         else
227                                 echo downloading $zpmfile
228                                 zpm repo fetch $reponame $pkgid
229                                 if [ -f "$zpmfile" ]; then
230                                         nlist="$nlist $pkgid:$zpmfile"
231                                 else
232                                         baddl="$baddl $reponame:$pkgid"
233                                 fi
234                         fi
235                 fi
236         else
237                 nlist="$nlist $mergepkg"
238         fi
239 done
240 merge=${nlist#' '}
241 baddl=${baddl#' '}
242
243 if [ -n "$baddl" ]; then
244         printf 'unable to download: %s\n' $baddl
245         die
246 fi
247
248 for package in $merge; do
249         pkgid=${package%:*}
250         installlist="$installlist $pkgid"
251 done
252 installlist=${installlist#' '}
253
254 if [ $justlist -eq 1 ]; then
255         for package in $merge; do
256                 pkgid=${package%:*}
257                 printf 'install %s\n' $pkgid
258         done
259         for pkgid in $to_remove; do
260                 printf 'remove %s\n' $pkgid
261         done
262         exit 0
263 fi
264
265 if [ -z "$merge" ] && [ -z "$to_remove" ]; then
266         echo nothing to do
267         exit 0
268 fi
269
270 test -n "$merge" && echo "install: $installlist"
271 test -n "$to_remove" && echo "remove: $to_remove"
272
273 # finally, merge everything
274 for mergepkg in $merge; do
275         pkgid=${mergepkg%:*}
276         pkgfile=${mergepkg#*:}
277
278         # we could have found the package already in the localdb
279         # in which case, we don't need to merge it
280         # TODO check for symlinks?
281         if [ $pkgfile != $ZPMDB ]; then
282                 echo merging $pkgfile $pkgid
283                 zpm merge -Fuv -f "$pkgfile" $pkgid
284                 if [ $? -ne 0 ]; then
285                         die "unable to merge $pkgfile"
286                 fi
287         fi
288         to_install="$to_install $pkgid"
289 done
290
291 to_install=${to_install#' '}
292
293 if [ -n "$to_install" ]; then
294         echo "installing: $to_install"
295 fi
296
297 # absorb anything we're updating or removing, if absorb flag set
298
299 if [ -n "$to_install" ]; then
300         for pkgid in $to_install; do
301                 current=$(current_version $pkgid)
302                 maybe_run pre-install $pkgid $runscripts $current || die
303         done
304 fi
305
306 if [ -n "$to_remove" ]; then
307         for pkgid in $to_remove; do
308                 maybe_run pre-remove $pkgid $runscripts || die
309         done
310 fi
311
312 if [ -n "$to_install" ]; then
313         for pkgid in $to_install; do
314                 zpm pkg $pkgid status=installing
315         done
316         zpm shell "$ZPMDB" "update packages set status='updating' where status='installed' and package in (select package from packages where status = 'installing')"
317 fi
318
319 if [ -n "$to_remove" ]; then
320         for pkgid in $to_remove; do
321                 zpm pkg $pkgid status=removing
322         done
323 fi
324
325 if [ -n "$rootdir" ]; then
326         ZPM_ROOT_DIR=$rootdir
327         export ZPM_ROOT_DIR
328         : rootdir=${rootdir%%/}
329 fi
330
331 if [ $dryrun -gt 0 ]; then
332         runscripts=0 # we don't want to run post scripts on a dry-run
333         syncopts="$syncopts -n"
334 fi
335
336 if [ $verbose -gt 0 ]; then
337         syncopts="${syncopts} -v"
338 else
339         # force -v during development
340         syncopts="${syncopts} -v"
341 fi
342
343 if [ $dryrun -eq 0 ] && [ $backup -eq 1 ]; then
344         true
345         #zpm absorb -s updating
346         #zpm absorb -s removing
347 fi
348
349 zpm syncfs $syncopts || die 'zpm-syncfs failed';
350
351 if [ -n "$to_remove" ]; then
352         for pkgid in $to_remove; do
353                 maybe_run post-remove $pkgid $runscripts
354         done
355 fi
356
357 changeall removing removed
358 changeall updating updated
359
360 echo running ldconfig
361 run_ldconfig $rootdir
362
363 if [ -n "$to_install" ]; then
364         for pkgid in $to_install; do
365                 package=$(zpm parse -n $pkgid)
366                 current=$(zpm list -s installed "$package")
367
368                 maybe_run post-install $pkgid $runscripts $current
369
370                 if [ -n "$current" ]; then
371                         zpm pkg $pkgid status=installed :$current status=updated
372                 else
373                         zpm pkg $pkgid status=installed
374                 fi
375
376                 maybe_run configure $pkgid $runconfigure
377         done
378 fi
379 echo install complete