1#!/bin/sh
2#
3# getbuildlog: download package build logs from Debian auto-builders
4#
5# Copyright © 2008 Frank S. Thomas <fst@debian.org>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20
21set -e
22
23PROGNAME=`basename $0`
24
25usage() {
26    cat <<EOT
27Usage: $PROGNAME <package> [<version-pattern>] [<architecture-pattern>]
28  Downloads build logs of <package> from Debian auto-builders.
29  If <version-pattern> or <architecture-pattern> are given, only build logs
30  whose versions and architectures, respectively, matches the given patterns
31  are downloaded.
32
33  If <version-pattern> is "last" then only the logs for the most recent
34  version of <package> found on buildd.debian.org will be downloaded.
35
36  If <version-pattern> is "last-all" then the logs for the most recent
37  version found on each build log index will be downloaded.
38Options:
39  -h, --help        Show this help message.
40  -V, --version     Show version and copyright information.
41Examples:
42  # Download amd64 build log for hello version 2.2-1:
43  $PROGNAME hello 2\.2-1 amd64
44
45  # Download mips(el) build logs of all glibc versions:
46  $PROGNAME glibc "" mips.*
47
48  # Download all build logs of backported wesnoth versions:
49  $PROGNAME wesnoth .*bpo.*
50EOT
51}
52
53version() {
54    cat <<EOT
55This is $PROGNAME, from the Debian devscripts package, version ###VERSION###
56This code is copyright 2008 by Frank S. Thomas, all rights reserved.
57This program comes with ABSOLUTELY NO WARRANTY.
58You are free to redistribute this code under the terms of the
59GNU General Public License, version 2 or later.
60EOT
61}
62
63[ "$1" = "-h" ] || [ "$1" = "--help" ] && usage && exit 0
64[ "$1" = "-V" ] || [ "$1" = "--version" ] && version && exit 0
65
66[ $# -ge 1 ] && [ $# -le 3 ] || { usage && exit 1; }
67
68if ! which wget >/dev/null 2>&1; then
69    echo "$PROGNAME: this program requires the wget package to be installed";
70    exit 1
71fi
72
73PACKAGE=$1
74VERSION=${2:-[:~+.[:alnum:]-]+}
75ARCH=${3:-[[:alnum:]-]+}
76ESCAPED_PACKAGE=`echo "$PACKAGE" | sed -e 's/\+/\\\+/g'`
77
78GET_LAST_VERSION=no
79if [ "$VERSION" = "last" ]; then
80    GET_LAST_VERSION=yes
81    VERSION=[:~+.[:alnum:]-]+
82elif [ "$VERSION" = "last-all" ]; then
83    GET_LAST_VERSION=all
84    VERSION=[:~+.[:alnum:]-]+
85fi
86
87PATTERN="fetch\.(cgi|php)\?pkg=$ESCAPED_PACKAGE&arch=$ARCH&ver=$VERSION&\
88stamp=[[:digit:]]+"
89
90getbuildlog() {
91    BASE=$1
92    ALL_LOGS=$(mktemp --tmpdir getbuildlog.tmp.XXXXXXXXXX)
93
94    trap 'rm -f "$ALL_LOGS"' EXIT
95
96    wget -q -O $ALL_LOGS "$BASE/status/logs.php?pkg=$PACKAGE"
97
98    # Put each href in $ALL_LOGS on a separate line so that $PATTERN
99    # matches only one href. This is required because grep is greedy.
100    sed -i -e "s/href=\"/\nhref=\"/g" $ALL_LOGS
101    # Quick-and-dirty unescaping
102    sed -i -e "s/&amp;/\&/g" -e "s/%2B/\+/g" -e "s/%3A/:/g" -e "s/%7E/~/g" $ALL_LOGS
103
104    # If only the last version was requested, extract and sort
105    # the listed versions and determine the highest
106    if [ "$GET_LAST_VERSION" != "no" ]; then
107	LASTVERSION=$( \
108	    for match in `grep -E -o "$PATTERN" $ALL_LOGS`; do
109		ver=${match##*ver=}
110		echo ${ver%%&*}
111	    done | perl -e '
112		use Devscripts::Versort;
113		while (<>) { push @versions, [$_]; }
114		@versions = Devscripts::Versort::versort(@versions);
115		print $versions[0][0]; ' | sed -e "s/\+/\\\+/g"
116	)
117
118	NEWPATTERN="fetch\.(cgi|php)\?pkg=$ESCAPED_PACKAGE&\
119arch=$ARCH&ver=$LASTVERSION&stamp=[[:digit:]]+"
120    else
121	NEWPATTERN=$PATTERN
122    fi
123
124    for match in `grep -E -o "$NEWPATTERN" $ALL_LOGS`; do
125	ver=${match##*ver=}
126	ver=${ver%%&*}
127	arch=${match##*arch=}
128	arch=${arch%%&*}
129	match=`echo $match | sed -e 's/\+/%2B/g'`
130	# Mimic wget's behaviour, using a numerical suffix if needed,
131	# to support downloading several logs for a given tuple
132	# (unfortunately, -nc and -O means only the first file gets
133	# downloaded):
134	filename="${PACKAGE}_${ver}_${arch}.log"
135	if [ -f "$filename" ]; then
136	    suffix=1
137	    while [ -f "$filename.$suffix" ]; do suffix=$((suffix+1)); done
138	    filename="$filename.$suffix"
139	fi
140	wget -O "$filename" "$BASE/status/$match&raw=1"
141    done
142
143    rm -f $ALL_LOGS
144
145    if [ "$GET_LAST_VERSION" = "yes" ]; then
146	PATTERN=$NEWPATTERN
147	GET_LAST_VERSION=no
148    fi
149}
150
151getbuildlog https://buildd.debian.org
152