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