]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame_incremental - update-geoip.sh
prevent filenames globbing in
[packages/rpm-build-tools.git] / update-geoip.sh
... / ...
CommitLineData
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
12set -e
13
14update=false
15status=false
16commit=true
17while [ $# -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
31done
32
33# get file DATE in GMT timezone
34filedate() {
35 local file="$1"
36 TZ=GMT stat -c '%y' "$file" | awk '{print $1}'
37}
38
39# get package, no sources
40get_package() {
41 local pkg=$1 out
42 out=$(builder -g -ns $pkg 2>&1) || echo "$out"
43}
44
45get_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
62update_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
76update_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
92commit_vcs() {
93 local specfile="$1" version="$2"
94
95 git commit -m "updated to $version" $specfile
96 git push || echo push failed
97}
98
99# get version from package files
100# set $version variable
101version_from_files() {
102 local pkg=$1 url fn dt d
103 shift
104
105 for url in "$@"; do
106 # take output filename (anything after last slash)
107 fn=${url##*/}
108 # skip inexistent files
109 test -e "$fn" || continue
110
111 d=$(filedate "$fn")
112 if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then
113 dt=$d
114 fi
115 done
116
117 case "$pkg" in
118 xtables-geoip)
119 version=$(echo "$dt" | tr -d -)
120 ;;
121 *)
122 version=$(echo "$dt" | tr - .)
123 ;;
124 esac
125}
126
127dir=$(dirname "$0")
128APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
129PATH=$APPDIR:$PATH
130cd "$dir"
131
132pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip'
133for pkg in ${*:-$pkgs}; do
134 pkg=${pkg%.spec}
135 $status && continue
136
137 get_package $pkg
138 cd $pkg
139 specfile=*.spec
140
141 urls=$(get_urls $specfile)
142 update_urls $urls
143 version_from_files $pkg $urls
144 oldvers=$(awk '/^Version:[ ]+/{print $NF}' $specfile)
145 if [ "$oldvers" != "$version" ]; then
146 update_version $specfile $version
147 if $commit; then
148 commit_vcs $specfile $version
149 fi
150 fi
151 cd ..
152done
153
154# report each package git status
155for pkg in ${*:-$pkgs}; do
156 pkg=${pkg%.spec}
157 cd $pkg
158 out=$(
159 git status --porcelain
160 git status | grep ahead || :
161 )
162 test -n "$out" && echo "$pkg: $out"
163 cd ..
164done
This page took 0.177489 seconds and 4 git commands to generate.