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