]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - update-geoip.sh
update-geoip: update git commit status display
[packages/rpm-build-tools.git] / update-geoip.sh
1 #!/bin/sh
2 # Update GeoIP packages to new version provided by MaxMind.
3 #
4 # Author: Elan Ruusamäe <glen@pld-linux.org>
5 #
6 # Changelog:
7 # 2012-07-04 Created initial version
8 # 2014-03-04 Rewritten to be smarter when checking for updates avoiding full download if no changes.
9 # 2014-06-06 Fix finding new versions if multiple previous archives were present
10 # 2015-08-25 Add auto commit support
11
12 set -e
13
14 update=false
15 status=false
16 commit=true
17 while [ $# -gt 0 ]; do
18         case "$1" in
19         update|-u|-update|--update)
20                 update=true
21                 shift
22                 ;;
23         status|-s|-status|--status)
24                 status=true
25                 shift
26                 ;;
27         *)
28                 break
29                 ;;
30         esac
31 done
32
33 # get file DATE in GMT timezone
34 filedate() {
35         local file="$1"
36         TZ=GMT stat -c '%y' "$file" | awk '{print $1}'
37 }
38
39 # get package, no sources
40 get_package() {
41         local pkg=$1 out
42         out=$(builder -g -ns $pkg 2>&1) || echo "$out"
43 }
44
45 get_urls() {
46         local specfile=$1 t url
47
48         t=$(mktemp)
49         builder -su $specfile 2>/dev/null > $t
50
51         while read url; do
52                 # skip non-archives
53                 case "$url" in
54                 *.zip|*.gz|*.xz)
55                         echo "$url"
56                         ;;
57                 esac
58         done < $t
59         rm -f $t
60 }
61
62 update_urls() {
63         local specfile=$1 url fn z
64
65         for url in "$@"; do
66                 # take output filename (anything after last slash)
67                 fn=${url##*/}
68                 # remove querystring for mtime match to work
69                 url=${url%\?*}
70                 test -e "$fn" && z= || unset z
71                 curl ${z+-z "$fn"} -o "$fn" "$url" -R -s
72         done
73 }
74
75 # set version to $version in $specfile and build the package
76 update_version() {
77         local specfile="$1" version="$2" out
78
79         # update version
80         sed -i -e "
81                 s/^\(Version:[ \t]\+\)[.0-9]\+\$/\1$version/
82                 s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/
83         " $specfile
84
85         # update md5
86         out=$(md5 -p1 $specfile 2>&1) || echo "$out"
87
88         # build it
89         out=$(builder -bb $specfile 2>&1) || echo "$out"
90 }
91
92 commit_vcs() {
93         local specfile="$1" version="$2"
94
95         git commit -m "updated to $version" $specfile
96 }
97
98 # get version from package files
99 # set $version variable
100 version_from_files() {
101         local pkg=$1 url fn dt d
102         shift
103
104         for url in "$@"; do
105                 # take output filename (anything after last slash)
106                 fn=${url##*/}
107                 # skip inexistent files
108                 test -e "$fn" || continue
109
110                 d=$(filedate "$fn")
111                 if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then
112                         dt=$d
113                 fi
114         done
115
116         case "$pkg" in
117         xtables-geoip)
118                 version=$(echo "$dt" | tr -d -)
119                 ;;
120         *)
121                 version=$(echo "$dt" | tr - .)
122                 ;;
123         esac
124 }
125
126 dir=$(dirname "$0")
127 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
128 PATH=$APPDIR:$PATH
129 cd "$dir"
130
131 pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip'
132 for pkg in ${*:-$pkgs}; do
133         pkg=${pkg%.spec}
134         $status && continue
135
136         get_package $pkg
137         cd $pkg
138         specfile=*.spec
139
140         urls=$(get_urls $specfile)
141         update_urls $urls
142         version_from_files $pkg $urls
143         oldvers=$(awk '/^Version:[      ]+/{print $NF}' $specfile)
144         if [ "$oldvers" != "$version" ]; then
145                 update_version $specfile $version
146                 if $commit; then
147                         commit_vcs $specfile $version
148                 fi
149         fi
150         cd ..
151 done
152
153 # report each package git status
154 for pkg in ${*:-$pkgs}; do
155         pkg=${pkg%.spec}
156         cd $pkg
157         out=$(
158         git status --porcelain
159         git status | grep ahead || :
160         )
161         test -n "$out" && echo "$pkg: $out"
162         cd ..
163 done
This page took 1.392519 seconds and 4 git commands to generate.