Debian Codegraph @

pkgkde-getbuildlogs

raw
1 #!/usr/bin/perl
2
3 # Copyright (C) 2010 Modestas Vainius <[email protected]>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>
17
18 use strict;
19 use warnings;
20
21 use Debian::PkgKde;
22 use Getopt::Long;
23 use File::Copy qw();
24 use File::Temp qw();
25 use File::Spec;
26 use IO::Uncompress::Inflate qw(inflate);
27 use IO::Uncompress::Gunzip qw(gunzip);
28
29 # Load extra modules (from libwww-perl and its dependencies)
30 INIT {
31 eval "use HTTP::Request";
32 if ($@) {
33 error "in order to use this utility, you have to install libwww-perl package";
34 }
35 eval "use URI; use URI::QueryParam; use URI::Escape;";
36 eval "use HTTP::Response; use LWP::UserAgent; use HTML::LinkExtor; use HTML::Parser;";
37 }
38
39 sub usage {
40 usageerr "[ -d destdir ] [ -v version ] [ -a arch ] [ -o ] [ package ] [ distribution ]";
41 }
42
43 sub construct_url {
44 my ($path, %params) = @_;
45
46 my $url = URI->new($path);
47 foreach my $param (keys %params) {
48 my @value = (ref $params{$param} eq "ARRAY") ?
49 @{$params{$param}} : ( $params{$param} );
50 $url->query_param_append($param, @value);
51 }
52 return $url->as_string();
53 }
54
55 sub as_array {
56 my $scalar = shift;
57 my @ret;
58 if (defined $scalar) {
59 if (ref($scalar) eq 'ARRAY') {
60 @ret = @$scalar;
61 } else {
62 push @ret, $scalar;
63 }
64 }
65 return @ret;
66 }
67
68 sub in_array {
69 my $value = shift;
70 foreach my $member (@_) {
71 return 1 if $member eq $value;
72 }
73 return 0;
74 }
75
76 sub get_command_output {
77 my @lines;
78 open(my $cmd, "-|", @_) or syserr("unable to execute command %s", $_[0]);
79 while (<$cmd>) {
80 chop;
81 push @lines, $_;
82 }
83 close $cmd;
84 return @lines;
85 }
86
87 sub get_rfc822_field_value {
88 my ($field, $input) = @_;
89 foreach my $line (@$input) {
90 if ($line =~ /^\Q$field\E:\s*(.*)$/) {
91 return "$1"
92 }
93 }
94 }
95
96 sub html2text {
97 my ($in, $out) = @_;
98 my $body;
99 my $parser = HTML::Parser->new( api_version => 3,
100 start_h => [ sub { if (shift() eq "body") { $body = 1 } }, "tagname" ],
101 end_h => [ sub { if (shift() eq "body") { $body = 0 } }, "tagname" ],
102 text_h => [ sub { if ($body) { print $out shift(); } }, "dtext" ]
103 );
104 $parser->ignore_elements("head", "a", "img");
105 return defined($parser->parse_file($in)) && defined($body);
106 }
107
108 sub fetch_log {
109 my ($browser, $uri, %opts) = @_;
110 my $ok = 0;
111 my @status;
112 my ($filename, $file);
113
114 # Construct log filename
115 $filename = sprintf("%s_%s_%s_%s.build",
116 $uri->query_param("pkg"),
117 $uri->query_param("ver"),
118 $uri->query_param("arch"),
119 $uri->query_param("stamp"));
120 $file = File::Spec->catfile($opts{destdir}, $filename)
121 if defined $opts{destdir};
122
123 # Check overwrite option
124 if (!$opts{overwrite} && -e $file) {
125 info "Not overwriting existing build log at $file ...";
126 } else {
127 # Create a temporary file
128 my %tmpfileopts = ( TEMPLATE => $filename . ".XXXXXX" );
129 $tmpfileopts{DIR} = $opts{destdir} if exists $opts{destdir};
130 my $tmpfile1 = File::Temp->new(%tmpfileopts);
131 my $tmpfile2 = File::Temp->new(%tmpfileopts);
132
133 info "Fetching build log to $filename ...";
134 my $request = HTTP::Request->new(GET => $uri);
135 $request->header("Accept-Encoding" => "gzip, deflate, identity");
136 $browser->show_progress(1);
137 $tmpfile1->close();
138 my $response = $browser->request($request, $tmpfile1->filename);
139 if ($response->is_success()) {
140 # Inflate/gunzip contents if needed
141 my $content_encoding = $response->header("Content-Encoding");
142 if (defined $content_encoding && $content_encoding ne "identity") {
143 my $uncompress_func;
144 if ($content_encoding eq "deflate") {
145 push @status, "decoded:deflate";
146 $uncompress_func = \&inflate;
147 } elsif ($content_encoding eq "gzip") {
148 push @status, "decoded:gzip";
149 $uncompress_func = \&gunzip;
150 } else {
151 push @status, "unsupported content encoding: $content_encoding";
152 }
153 if (defined $uncompress_func) {
154 if (&$uncompress_func($tmpfile1->filename => $tmpfile2, BinModeOut => 1)) {
155 $tmpfile2->close();
156 ($tmpfile1, $tmpfile2) = ($tmpfile2, $tmpfile1);
157 open($tmpfile2, ">:utf8", $tmpfile2->filename) or
158 syserr "unable to reopen temporary file";
159 $ok = 1;
160 }
161 }
162 } else {
163 # Identity encoding (no compression)
164 $ok = 1;
165 binmode($tmpfile2, ":utf8");
166 }
167 if ($ok) {
168 open($tmpfile1, "<:utf8", $tmpfile1->filename);
169 if ($ok = html2text($tmpfile1 => $tmpfile2)) {
170 $tmpfile1->close();
171 $tmpfile1 = $tmpfile2;
172 } else {
173 push @status, "html unstripped";
174 }
175 $tmpfile1->close();
176 $tmpfile2->close();
177 File::Copy::move($tmpfile1->filename, $file) or
178 error "unable to rename '%s' to '%s'", $tmpfile1->filename, $file;
179 }
180 }
181 }
182 return ($ok, [ $filename, @status ]);
183 }
184
185 sub download_logs {
186 my ($pkg, %opts) = @_;
187 my $distro = $opts{distro};
188 my @arches = as_array($opts{arch});
189 my @vers = as_array($opts{ver});
190 my $url;
191
192 # Construct index URL
193 if (defined $distro) {
194 $url = construct_url('https://buildd.debian.org/status/package.php',
195 p => $pkg, suite => $distro);
196 } elsif (defined $opts{ver}) {
197 if (scalar(@arches) == 1 && scalar(@vers) == 1) {
198 $url = construct_url('https://buildd.debian.org/build.php',
199 pkg => $pkg, ver => \@vers, arch => \@arches);
200 } else {
201 # Only single ver + single arch query is supported by build.php.
202 # For anything else fallback to url filtering (see below)
203 $url = construct_url('https://buildd.debian.org/build.php', pkg => $pkg);
204 }
205 } else {
206 error "neither version(s) nor distribution was specified";
207 }
208
209 # Download index document and extract links
210 info "Downloading build log index from $url ...";
211 my $browser = LWP::UserAgent->new(
212 agent => get_program_name(),
213 timeout => 10,
214 keep_alive => 1,
215 env_proxy => 1,
216 );
217 my $request = HTTP::Request->new(GET => $url);
218 if (my $response = $browser->request($request)) {
219 error "unable to access log index at URL $url: ".$response->status_line
220 unless $response->is_success();
221 my $urlbase = $url;
222 $urlbase =~ s,[^/]*$,,;
223 my $linkextor = HTML::LinkExtor->new(undef, $urlbase);
224 $linkextor->parse($response->content());
225 if (my @links = grep { $_->[0] eq "a" } $linkextor->links()) {
226 @links = map { shift @{$_}; +{ @{$_} }->{href} } @links;
227 my @ok;
228 my @failed;
229 my %latest_logs;
230
231 # Collect links we need to fetch
232 foreach my $link (@links) {
233 # Check if it is the link we need
234 if ($link =~ m,/fetch\.(cgi|php)(\?[^/]+)$,) {
235 # We might need to filter out links based on the arch and
236 # version we need
237 my $loguri = URI->new($link);
238 my $ver = $loguri->query_param("ver");
239 my $arch = $loguri->query_param("arch");
240 my $stamp = $loguri->query_param("stamp");
241 next if @vers && !in_array($ver, @vers);
242 next if @arches && !in_array($arch, @arches);
243
244 # Ensure that we are fetching the latest log for ver/arch
245 my $k = $ver . "_" . $arch;
246 if (!exists $latest_logs{$k} || $latest_logs{$k}{stamp} < $stamp) {
247 $latest_logs{$k} = { stamp => $stamp, uri => $loguri };
248 }
249 }
250 }
251
252 # Fetch logs
253 foreach my $k (sort keys %latest_logs) {
254 my $loguri = $latest_logs{$k}{uri};
255 my ($ok, $status) = fetch_log($browser, $loguri, %opts);
256 if ($ok) {
257 push @ok, $status;
258 } else {
259 push @failed, $status;
260 }
261 }
262 return (@ok || @failed) ? (\@ok, \@failed) : ();
263 }
264 return ();
265 } else {
266 error "unable to access log index URL $url";
267 }
268 }
269
270 sub print_summary {
271 my $logs = shift;
272 my $is_warning = shift;
273 my $msg = shift;
274 if (@$logs) {
275 info $msg, @_ unless $is_warning;
276 warning $msg, @_ if $is_warning;
277 foreach my $log_info (@$logs) {
278 my ($filename, @info) = @$log_info;
279 if (@info) {
280 printmsg " - %s [%s]", $filename, join(", ", @info);
281 } else {
282 printmsg " - %s", $filename;
283 }
284 }
285 }
286 }
287
288 my $opt_destdir;
289 my @opt_versions;
290 my @opt_archs;
291 my $opt_force;
292
293 # Get and verify options
294 unless (GetOptions(
295 "destdir|d=s" => \$opt_destdir,
296 "version|v=s" => \@opt_versions,
297 "arch|a=s" => \@opt_archs,
298 "force|f!" => \$opt_force))
299 {
300 usage();
301 }
302
303 my ($opt_package, $opt_distro) = @ARGV;
304 my @dpkg_parsechangelog;
305
306 if (!$opt_package && -f "debian/changelog") {
307 @dpkg_parsechangelog = get_command_output("dpkg-parsechangelog");
308 $opt_package = get_rfc822_field_value("Source", \@dpkg_parsechangelog);
309 }
310
311 if (!$opt_package) {
312 errormsg "source package was not specified and could not be autoguessed";
313 usage();
314 }
315
316 if ($opt_distro && @opt_versions) {
317 errormsg "version and distribution options are mutually exclusive";
318 usage();
319 }
320
321 if (!@opt_versions) {
322 if (!$opt_distro && -f "debian/changelog") {
323 @dpkg_parsechangelog = get_command_output("dpkg-parsechangelog") unless @dpkg_parsechangelog;
324 $opt_distro = get_rfc822_field_value("Distribution", \@dpkg_parsechangelog);
325 if ($opt_distro eq "UNRELEASED") {
326 # Get distro from the next to current entry
327 $opt_distro = get_rfc822_field_value("Distribution",
328 [ get_command_output("dpkg-parsechangelog", "-c1", "-o1") ]);
329 }
330 }
331 if (!$opt_distro) {
332 errormsg "neither distribution nor version(s) was specified and could not be autoguessed";
333 usage();
334 }
335 }
336
337 # Determine destination directory to store logs
338 unless ($opt_destdir) {
339 $opt_destdir = sprintf("%s_%s_logs", $opt_package,
340 ($opt_distro) ? $opt_distro : $opt_versions[0]);
341 }
342
343 info("Selected output directory for logs: %s/", $opt_destdir);
344 unless (-d $opt_destdir) {
345 mkdir $opt_destdir;
346 }
347
348 my ($ok_logs, $failed_logs) =
349 download_logs($opt_package, overwrite => $opt_force, destdir => $opt_destdir,
350 distro => $opt_distro, ver => \@opt_versions, arch => \@opt_archs);
351
352 if (defined $ok_logs) {
353 print_summary $ok_logs, 0, "Successfully downloaded build logs (stored to %s):", $opt_destdir;
354 print_summary $failed_logs, 1, "Failed to fetch/ignored the following build logs:";
355 } else {
356 error "no build logs referenced in the build log index";
357 }
358
359 END {
360 rmdir $opt_destdir if $opt_destdir && $opt_destdir ne ".";
361 }
362
363 exit 0
364
365 # vim: noexpandtab tabstop=8 shiftwidth=4