use open_compresed file and give more output when scanlib fails
[bugscan.git] / scanlib.pm
1 #! /usr/bin/perl
2 # vim: ts=8 sw=8 nowrap
3 #
4 # General functions for scanning the BTS-database.
5 # Based on bugscan, written by Richard Braakman <dark@debian.org>,
6 # which was based on an unknown other script.
7 #
8 # Global variables:
9 #   %maintainer     - map from packagename to maintainer
10 #   %section        - map from packagename to section in the FTP-site
11 #   %packagelist    - map from packagename to bugreports
12
13 use warnings;
14 use strict;
15
16 use lib qw(/org/bugs.debian.org/perl);
17 use LWP::UserAgent;
18 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
19 use Debbugs::Packages;
20 use Debbugs::Versions;
21 use Debbugs::Status;
22 use Debbugs::Common qw(open_compressed_file);
23 use Fcntl qw(O_RDONLY);
24
25 use File::Basename;
26 use lib dirname(__FILE__);
27 use bugcfg;
28
29 package scanlib;
30
31 our (%maintainer,%section,%packagelist,%debbugssection,%bugs);
32
33
34 # Read the list of maintainer 
35 sub readmaintainers() {
36         my $pkg;                                        # Name of package
37         my $mnt;                                        # Maintainer name & email
38
39         open(M, $bugcfg::maintainerlist) or die "open $bugcfg::maintainerlist: $!\n";
40         while (<M>) {
41                 chomp;
42                 m/^(\S+)\s+(\S.*\S)\s*$/ or die "Maintainers: $_ ?";
43                 ($pkg, $mnt) = ($1, $2);
44                 $pkg =~ y/A-Z/a-z/;                     # Normalize package-name. why???
45                 $_=$mnt;
46                 if (not m/</) {
47                         $mnt="$2 <$1>" if ( m/(\S+)\s+\(([^)]+)\)/ );
48                 }
49                 $maintainer{$pkg}= $mnt;
50         }
51         close(M);
52 }
53
54 sub glob_compressed_fh {
55     my ($fn) = @_;
56     my @fn = grep { -f $_ } glob $fn;
57     if (not @fn) {
58         die "No files exist which match glob '$fn'";
59     }
60     my $fh = open_compressed_file($fn[0]) or
61         die "Unable to open $fn for reading: $!";
62     return $fh;
63 }
64
65
66 sub readsources {
67     my ($root,$archive) = @_;
68
69         for my $sect (@bugcfg::sections) {
70         my $p = glob_compressed_fh("$root/$sect/source/Sources.*");
71                 while (<$p>) {
72                         chomp;
73                         next unless m/^Package:\s/;
74                         s/^Package:\s*//;                       # Strip the fieldname
75                         $section{$_} = "$archive/$sect";
76                 }
77                 close ($p);
78         }
79 }
80
81 sub readpackages {
82     my ($root,$archive) = @_;
83         for my $arch ( @bugcfg::architectures ) {
84                 for my $sect ( @bugcfg::sections) {
85             my $p = glob_compressed_fh("$root/$sect/binary-$arch/Packages.*");
86                         while (<$p>) {
87                                 chomp;
88                                 next unless m/^Package:\s/;     # We're only interested in the packagenames
89                                 s/^Package:\s*//;                       # Strip the fieldname
90                                 $section{$_} = "$archive/$sect";
91                                 print "$root/$sect/binary-$arch/Packages.gz\n" if ($_ eq 'xtla');
92                         }
93                         close($p);
94                 }
95         }
96     # handle the source packages
97     for my $sect (@bugcfg::sections) {
98         my $fh = glob_compressed_fh("$root/$sect/source/Sources.*");
99         while (<$fh>) {
100             chomp;
101             next unless m/^Package:\s/; # We're only interested in the packagenames
102             s/^Package:\s*//;                   # Strip the fieldname
103             $section{$_} = "$archive/$sect";
104         }
105     }
106 }
107
108 sub readdebbugssources {
109     my ($file,$archive) = @_;
110
111         open(P, $file)
112                 or die "open: $file: $!\n";
113         while (<P>) {
114                 chomp;
115                 my ($host, $bin, $sect, $ver, $src) = split /\s+/;
116                 my $sectname = ($sect =~ /^\Q$archive/) ? $sect : "$archive/$sect";
117                 $debbugssection{$bin} = $sectname;
118                 $debbugssection{$src} = $sectname;
119         }
120         close(P);
121 }
122
123 sub readpseudopackages() {
124         open(P, $bugcfg::pseudolist) or die("open $bugcfg::pseudolist: $!\n");
125         while (<P>) {
126                 chomp;
127                 s/\s.*//;
128                 $section{$_} = "pseudo";
129         }
130         close(P);
131 }
132
133
134 sub scanspool() {
135         my @dirs;
136         my $dir;
137
138         chdir($bugcfg::spooldir) or die "chdir $bugcfg::spooldir: $!\n";
139
140         opendir(DIR, $bugcfg::spooldir) or die "opendir $bugcfg::spooldir: $!\n";
141         @dirs=grep(m/^\d+$/,readdir(DIR));
142         closedir(DIR);
143
144         for $dir (@dirs) {
145                 scanspooldir("$bugcfg::spooldir/$dir");
146         }
147
148 }
149
150 sub scanspooldir {
151         my ($dir)               = @_;
152         my $f;                  # While we're currently processing
153         my @list;               # List of files to process
154         my $skip;               # Flow control
155         my $walk;               # index variable
156         my $taginfo;    # Tag info
157                                         
158         my @archs_with_source = ( @bugcfg::architectures, 'source' );
159
160         chdir($dir) or die "chdir $dir: $!\n";
161
162         opendir(DIR, $dir) or die "opendir $dir: $!\n";
163         @list = grep { s/\.summary$// }
164                         grep { m/^\d+\.summary$/ } 
165                         readdir(DIR);
166         closedir(DIR);
167
168         for $f (@list) {
169                 my $bug = Debbugs::Status::read_bug(summary => "$f.summary");
170                 next if (!defined($bug));
171                 
172                 my $bi = {
173                         number => $f,
174                         subject => $bug->{'subject'},
175                         package => $bug->{'package'}
176                 };
177                 
178                 $skip=1;
179                 for $walk (@bugcfg::priorities) {
180                         $skip=0 if $walk eq $bug->{'severity'};
181                 }
182
183                 my @tags = split(' ', $bug->{'keywords'});
184                 for my $tag (@tags) {
185                         for my $s (@bugcfg::skiptags) {
186                                 $skip=1 if $tag eq $s;
187                         }
188                 }
189                 next if $skip==1;
190         
191                 my %disttags = ();
192         for my $release (qw(oldstable stable testing unstable)) {
193             $disttags{$release}    = grep(/^$bugcfg::debian_releases->{$release}$/, @tags);
194         }
195                 $disttags{'experimental'} = grep(/^experimental$/, @tags);
196                         
197                 # default according to vorlon 2007-06-17
198                 if (!$disttags{'oldstable'} && !$disttags{'stable'} && !$disttags{'testing'} && !$disttags{'unstable'} && !$disttags{'experimental'}) {
199                         $disttags{'stable'} = 1;
200                         $disttags{'testing'} = 1;
201                         $disttags{'unstable'} = 1;
202                         $disttags{'experimental'} = 1;
203                 }
204                 
205                 if (defined($section{$bug->{'package'}}) && $section{$bug->{'package'}} eq 'pseudo') {
206                         # versioning information makes no sense for pseudo packages,
207                         # just use the tags
208                         for my $dist (qw(oldstable stable testing unstable experimental)) {
209                                 $bi->{$dist} = $disttags{$dist};
210                         }
211                         next if (length($bug->{'done'}));
212                 } else {
213                         my $affects_any = 0;
214                 
215                         # only bother to check the versioning status for the distributions indicated by the tags 
216                         for my $dist (qw(oldstable stable testing unstable experimental)) {
217                                 local $SIG{__WARN__} = sub {};
218
219                                 $bi->{$dist} = 0;
220                                 next if (!$disttags{$dist});
221
222                                 my $presence = Debbugs::Status::bug_presence(
223                                         bug => $f, 
224                                         status => $bug, 
225                                         dist => $dist, 
226                                         arch => \@archs_with_source
227                                 );
228
229                                 # ignore bugs that are absent/fixed in this distribution, include everything
230                                 # else (that is, "found" which says that the bug is present, and undef, which
231                                 # indicates that no versioning information is present and it's not closed
232                                 # unversioned)
233                                 if (!defined($presence) || ($presence ne 'absent' && $presence ne 'fixed')) {
234                                         $bi->{$dist} = 1;
235                                         $affects_any = 1;
236                                 }
237                         }
238                         
239                         next if !$affects_any;
240                 }
241
242                 for my $keyword (qw(pending patch help moreinfo unreproducible security upstream),
243                          map {$bugcfg::debian_releases->{$_}.'-ignore'} keys %{$bugcfg::debian_releases}) {
244                         $bi->{$keyword} = grep(/^$keyword$/, @tags);
245                 }
246
247                 if (length($bug->{'mergedwith'})) {
248                         my @merged = split(' ', $bug->{'mergedwith'});
249                         next if ($merged[0] < $f);
250                 }
251
252                 for my $package (split /[,\s]+/, $bug->{'package'}) {
253                         $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._:a-z0-9]/;
254                         push @{$packagelist{$_}}, $f;
255                 }
256
257                 my $taginfo = get_taginfo($bi);
258                 my $relinfo = get_relinfo($bi);
259
260                 $bugs{$f} = $bi;
261         }
262 }
263
264
265 sub readstatus {
266     my $filename = shift;
267         open STATUS, "<", $filename
268                 or die "$filename: $!";
269
270     while (1) {
271                 chomp (my $type = <STATUS>);
272                 if ($type eq 'package') {
273                         chomp (my $package = <STATUS>);
274                         chomp (my $section = <STATUS>);
275                         chomp (my $maintainer = <STATUS>);
276                         my $blank = <STATUS>;
277
278                         $section{$package} = $section;
279                         $maintainer{$package} = $maintainer;
280                 }
281                 if ($type eq 'bug') {
282                         my $bug = {};
283                         while (1) {
284                                 my $line = <STATUS>;
285                                 last if ($line !~ /^(.*?)=(.*)$/);
286
287                                 $bug->{$1} = $2;                                
288                         }
289                         $bugs{$bug->{'number'}} = $bug;
290
291                         for my $package (split /[,\s]+/, $bug->{'package'}) {
292                                 $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._:a-z0-9]/;
293                                 push @{$packagelist{$_}}, $bug->{'number'};
294                         }
295                 }
296                 last if ($type eq 'end');
297         }
298         close(STATUS);
299 }
300
301
302 sub urlsanit {
303         my $url = shift;
304         $url =~ s/%/%25/g;
305         $url =~ s/\+/%2b/g;
306         my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
307         $url =~ s/([<>&"])/\&$saniarray{$1};/g;
308         return $url;
309 }
310
311 sub htmlsanit {
312     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
313     my $in = shift || "";
314     $in =~ s/([<>&"])/\&$saniarray{$1};/g;
315     return $in;
316 }
317
318 sub wwwnumber {
319         my $number = shift;             # Number of bug to html-ize
320
321         "<A HREF=\"http://bugs.debian.org/cgi-bin/bugreport.cgi?archive=no&amp;bug=" .
322                 urlsanit($number) . '">' . htmlsanit($number) . '</A>';
323 }
324
325 sub wwwname {
326         my $name = shift;                       # Name of package
327
328         "<A HREF=\"http://bugs.debian.org/cgi-bin/pkgreport.cgi?archive=no&amp;pkg=" .
329                 urlsanit($name) . '">' . htmlsanit($name) . '</A>';
330 }
331
332 sub check_worry {
333         my ($bi,$dist) = @_;
334     $dist = 'testing' if not defined $dist;
335
336         return ($bi->{$dist} && !$bi->{$bugcfg::debian_releases->{$dist}.'-ignore'});
337 }
338
339 sub check_worry_testing {
340     return check_worry($_[0],'testing');
341 }
342 sub check_worry_stable {
343     return check_worry($_[0],'stable');
344 }
345 sub check_worry_oldstable {
346     return check_worry($_[0],'oldstable');
347 }
348
349 sub check_worry_unstable {
350         my ($bi) = @_;
351
352         return ($bi->{'unstable'});
353 }
354
355 sub get_taginfo {
356     my $bi = shift;
357
358         my $taginfo = "";
359         $taginfo .= $bi->{'pending'}        ? "P" : " ";
360         $taginfo .= $bi->{'patch'}          ? "+" : " ";
361         $taginfo .= $bi->{'help'}           ? "H" : " ";
362         $taginfo .= $bi->{'moreinfo'}       ? "M" : " ";
363         $taginfo .= $bi->{'unreproducible'} ? "R" : " ";
364         $taginfo .= $bi->{'security'}       ? "S" : " ";
365         $taginfo .= $bi->{'upstream'}       ? "U" : " ";
366         $taginfo .= ($bi->{$bugcfg::debian_releases->{stable}.'-ignore'} || $bi->{$bugcfg::debian_releases->{testing}.'-ignore'}) ? "I" : " ";
367
368         return $taginfo;
369 }
370
371 sub get_relinfo {
372     my $bi = shift;
373
374     my $relinfo = "";
375         for my $dist (qw(oldstable stable testing unstable experimental)) {
376             $relinfo .= uc(substr($dist, 0, 1)) if $bi->{$dist};
377         }
378
379         return $relinfo;
380 }
381
382
383 1;