]> pd.if.org Git - zpackage/blob - zpm-repo
add range search to findpkg
[zpackage] / zpm-repo
1 #!/bin/sh
2
3 # control repositories
4
5 # repo ...
6 # add name <url>
7 # del name
8 # pri name <n>
9 # list [-v ] [ name ... ]
10 # note name <note>
11 # url name [url]
12 # packages [ name ... ] -- like zpm list
13 # contents [ name ... ] -- like zpm contents
14 # search [ name ] -- packages
15 # push name ckagefile ...
16 # pull name package ...
17 # extract -- extract metadata from repo
18 # json - extract metadata as json
19 # setpkgurl packageid url
20 # update - pull info from remote
21 # gc [name] - run zpm gc on repo file
22
23 chroot=1
24 pkgroot=
25
26 while getopts :R:d: opt; do
27         case $opt in
28                 R) pkgroot="$OPTARG" ;;
29                 d) ZPMDB="$OPTARG" ;;
30         esac
31 done
32
33 shift $((OPTIND - 1))
34
35 for cf in /etc/zpmrc ~/.zpmrc ./.zpmrc; do
36         test -r $cf && . $cf
37 done
38
39 pkgroot=${pkgroot%/}
40
41 : ${ZPMDB:=$pkgroot/var/lib/zpm/local.db}
42
43 repodir=/var/lib/zpm/repo
44 mkdir -p $repodir
45
46 export ZPMDB
47
48 warn() {
49         printf '%s:' "$0" 1>&2
50         printf ' %s' $* 1>&2
51         printf '\n' 1>&2
52         exit 1
53 }
54
55 die() {
56         printf '%s:' "$0" 1>&2
57         printf ' %s' $* 1>&2
58         printf '\n' 1>&2
59         exit 1
60 }
61
62 set -e
63 zpm test -v $ZPMDB
64
65 if [ $# -gt 0 ]; then
66         action=$1
67         shift
68 else
69         action=list
70 fi
71
72 select() (
73         name=$(zpm quote "$1")
74         cols=$2
75         if [ -n "$name" ]; then
76                 where="where name = '$name'"
77         fi
78         zpm shell -separator ' ' $ZPMDB "select $cols from repository $where order by priority,name"
79 )
80
81 update() (
82         name=$(zpm quote "$1")
83         col=$2
84         val=$(zpm quote "$3")
85         if [ -n "$name" ]; then
86                 where="where name = '$name'"
87         fi
88         zpm shell $ZPMDB "update repository set $col = '$val' $where"
89 )
90
91 refresh() {
92         zpm shell $ZPMDB "update repository set refreshed = strftime('%s', 'now') where name = '$1'"
93 }
94
95 update_info() {
96         if [ $# -eq 0 ]; then
97                 set $(select '' name)
98         fi
99         for repo in "$@"; do
100                 url=$(select "$repo" url)
101                 rf="$repodir/$repo.repo"
102                 if [ -z "$url" ]; then
103                         check_for_repo "$repo" || warn "no url for repo $repo"
104                         return 1
105                 fi
106                 if [ -f "$rf" ]; then
107                         # TODO merge in so packages aren't deleted
108                         zpm fetchurl -fpn -z "$rf" -o "$rf.tmp" "$url"
109                         rv=$?
110                 else
111                         zpm fetchurl -fpn -o "$rf.tmp" "$url" && refresh "$repo"
112                         rv=$?
113                 fi
114                 if [ $rv -eq 0 ]; then
115                         test -f "$rf.tmp" &&
116                         zpm test "$rf.tmp" &&
117                         mv "$rf.tmp" "$rf"
118                         refresh "$repo"
119                 fi
120         done
121 }
122
123 head_response_code() {
124         zpm fetchurl -S -z "$2" "$1"
125 }
126
127 download() {
128         repo=$1
129         shift
130         url=$(select "$repo" url)
131         base=${url%/*}
132         for package in "$@"; do
133                 pkgid=$(zpm findpkg -f $repodir/$repo.repo $package)
134                 if [ -z "$pkgid" ]; then
135                         warn "no package found for $package"
136                 else
137                         mkdir -p "$repodir/$repo"
138                         dest="$repodir/$repo/$pkgid.zpm"
139                         fetch="$base/packages/$pkgid.zpm"
140
141                         if [ -f "$dest" ]; then
142                                 rc=$(head_response_code $fetch $dest)
143                                 if [ $rc = 304 ]; then
144                                         echo "have $pkgid"
145                                         continue
146                                 fi
147                                 echo downloading $pkgid
148                                 zpm fetchurl -f '-#' -z "$dest" -o "$dest.tmp" $fetch
149                                 mv "$dest.tmp" "$dest"
150                         else
151                                 echo downloading $pkgid
152                                 zpm fetchurl -f '-#' -o "$dest.tmp" $fetch
153                                 mv "$dest.tmp" "$dest"
154                         fi
155                 fi
156         done
157 }
158
159 purge() {
160         repo=$1
161         shift
162         url=$(select "$repo" url)
163         base=${url%/*}
164         for package in "$@"; do
165                 pkgid=$(zpm findpkg -f $repodir/$repo.repo $package)
166                 if [ -z "$pkgid" ]; then
167                         warn "no package found for $package"
168                 else
169                         mkdir -p "$repodir/$repo"
170                         dest="$repodir/$repo/$pkgid.zpm"
171
172                         if [ -f "$dest" ]; then
173                                 rm $dest
174                         fi
175                         zpm rmpackage -f $repodir/$repo.repo $pkgid
176                 fi
177         done
178 }
179
180 check_for_repo() {
181         rn=$(select "$1" name)
182         if [ -z "$rn" ]; then
183                 warn "no repository $1"
184                 return 1
185         fi
186         return 0
187 }
188
189 case $action in
190         list)
191                 select '' priority,name,url,refreshed ;;
192         add)
193                 name=$(zpm quote "$1")
194                 url=$(zpm quote "$2")
195                 zpm shell $ZPMDB "insert into repository (name,url) values ('$name', '$url')"
196                 ;;
197         del)
198                 name=$(zpm quote "$1")
199                 zpm shell $ZPMDB "delete from repository where name = '$name'"
200                 rm -f $repodir/$1.repo
201                 test -d "$repodir/$repo" && rm -r "$repodir/$repo"
202                 ;;
203         url)
204                 if [ -n "$2" ]; then
205                         update "$1" url "$2"
206                 else
207                         select "$1" url
208                 fi
209                 ;;
210         pri*)
211                 if [ -n "$2" ]; then
212                         update "$1" priority "$2"
213                 else
214                         select "$1" priority
215                 fi
216                 ;;
217         fetch|download)
218                 download "$@"
219                 ;;
220         update|pull)
221                 update_info "$@"
222                 ;;
223         packages)
224                 repo=$1
225                 shift
226                 zpm list -f $repodir/$repo.repo "$@"
227                 ;;
228         contents)
229                 repo=$1
230                 shift
231                 zpm contents -f $repodir/$repo.repo "$@"
232                 ;;
233         purge)
234                 purge "$@"
235                 ;;
236
237         *)
238                 echo unknown action
239                 exit 1
240                 ;;
241 esac