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