Check that a package is really present in a distribution before putting
[bugscan.git] / scanlib.pm
1 #! /usr/bin/perl
2 # vim: ts=4 sw=4 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 #   %premature      - list of prematurely closed bugreports
10 #   %exclude        - list of bugreports to exclude from the report
11 #   %maintainer     - map from packagename to maintainer
12 #   %section        - map from packagename to section in the FTP-site
13 #   %packagelist    - map from packagename to bugreports
14
15 use lib qw(/org/bugs.debian.org/perl);
16 use LWP::UserAgent;
17 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
18 use Debbugs::Packages;
19 use Debbugs::Versions;
20 use Debbugs::Status;
21 use Fcntl qw(O_RDONLY);
22 use strict;
23 use warnings;
24 require bugcfg;
25 package scanlib;
26
27 our (%premature,%exclude,%maintainer,%section,%packagelist,%debbugssection,%bugs);
28
29
30 # Read the list of maintainer 
31 sub readmaintainers() {
32         my $pkg;                                        # Name of package
33         my $mnt;                                        # Maintainer name & email
34
35         open(M, $bugcfg::maintainerlist) or die "open $bugcfg::maintainerlist: $!\n";
36         while (<M>) {
37                 chomp;
38                 m/^(\S+)\s+(\S.*\S)\s*$/ or die "Maintainers: $_ ?";
39                 ($pkg, $mnt) = ($1, $2);
40                 $pkg =~ y/A-Z/a-z/;                     # Normalize package-name. why???
41                 $_=$mnt;
42                 if (not m/</) {
43                         $mnt="$2 <$1>" if ( m/(\S+)\s+\(([^)]+)\)/ );
44                 }
45                 $maintainer{$pkg}= $mnt;
46         }
47         close(M);
48 }
49
50
51 sub readsources() {
52         my $root;                                       # Root of archive we are scanning
53         my $archive;                            # Name of archive we are scanning
54         my $sect;                                       # Name of current section
55
56         $root=shift;
57         $archive=shift;
58         for $sect (@bugcfg::sections) {
59                 open(P, "zcat $root/$sect/source/Sources.gz|")
60                         or die open "open: $sect sourcelist: $!\n";
61                 while (<P>) {
62                         chomp;
63                         next unless m/^Package:\s/;
64                         s/^Package:\s*//;                       # Strip the fieldname
65                         $section{$_} = "$archive/$sect";
66                 }
67                 close (P);
68         }
69 }
70
71 sub readpackages() {
72         my $root;                                       # Root of archive we are scanning
73         my $archive;                            # Name of archive we are scanning
74         my $sect;                                       # Name of current section
75         my $arch;                                       # Name of current architecture
76
77         $root=shift;
78         $archive=shift;
79         for $arch ( @bugcfg::architectures ) {
80                 for $sect ( @bugcfg::sections) {
81                         open(P, "zcat $root/$sect/binary-$arch/Packages.gz|")
82                                 or die "open: $root/$sect/binary-$arch/Packages.gz: $!\n";
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                         }
89                         close(P);
90                 }
91         }
92 }
93
94 sub readdebbugssources() {
95         my $file;
96         my $archive;
97
98         $file=shift;
99         $archive=shift;
100         open(P, $file)
101                 or die "open: $file: $!\n";
102         while (<P>) {
103                 chomp;
104                 my ($host, $bin, $sect, $ver, $src) = split /\s+/;
105                 my $sectname = ($sect =~ /^\Q$archive/) ? $sect : "$archive/$sect";
106                 $debbugssection{$bin} = $sectname;
107                 $debbugssection{$src} = $sectname;
108         }
109         close(P);
110 }
111
112 sub readpseudopackages() {
113         open(P, $bugcfg::pseudolist) or die("open $bugcfg::pseudolist: $!\n");
114         while (<P>) {
115                 chomp;
116                 s/\s.*//;
117                 $section{$_} = "pseudo";
118         }
119         close(P);
120 }
121
122
123 sub scanspool() {
124         my @dirs;
125         my $dir;
126
127         chdir($bugcfg::spooldir) or die "chdir $bugcfg::spooldir: $!\n";
128
129         opendir(DIR, $bugcfg::spooldir) or die "opendir $bugcfg::spooldir: $!\n";
130         @dirs=grep(m/^\d+$/,readdir(DIR));
131         closedir(DIR);
132
133         for $dir (@dirs) {
134                 scanspooldir("$bugcfg::spooldir/$dir");
135         }
136
137 }
138
139 sub scanspooldir() {
140         my ($dir)               = @_;
141         my $f;                  # While we're currently processing
142         my @list;               # List of files to process
143         my $skip;               # Flow control
144         my $walk;               # index variable
145         my $taginfo;    # Tag info
146
147         chdir($dir) or die "chdir $dir: $!\n";
148
149         opendir(DIR, $dir) or die "opendir $dir: $!\n";
150         @list = grep { s/\.summary$// }
151                         grep { m/^\d+\.summary$/ } 
152                         readdir(DIR);
153         closedir(DIR);
154
155         for $f (@list) {
156                 next if $exclude{$f};                   # Check the list of bugs to skip
157         
158                 my $bug = Debbugs::Status::read_bug(summary => "$f.summary");
159                 next if (!defined($bug));
160                 
161                 $skip=1;
162                 for $walk (@bugcfg::priorities) {
163                         $skip=0 if $walk eq $bug->{'severity'};
164                 }
165
166                 my @tags = split(' ', $bug->{'keywords'});
167                 for my $tag (@tags) {
168                         for my $s (@bugcfg::skiptags) {
169                                 $skip=1 if $tag eq $s;
170                         }
171                 }
172                 next if $skip==1;
173         
174                 my %disttags = ();      
175                 $disttags{'oldstable'}    = grep(/^woody$/, @tags);
176                 $disttags{'stable'}       = grep(/^sarge$/, @tags);
177                 $disttags{'testing'}      = grep(/^etch$/, @tags);
178                 $disttags{'unstable'}     = grep(/^sid$/, @tags);
179                 $disttags{'experimental'} = grep(/^experimental$/, @tags);
180                         
181                 # default according to dondelelcaro 2006-11-11
182                 if (!$disttags{'oldstable'} && !$disttags{'stable'} && !$disttags{'testing'} && !$disttags{'unstable'} && !$disttags{'experimental'}) {
183                         $disttags{'testing'} = 1;
184                         $disttags{'unstable'} = 1;
185                         $disttags{'experimental'} = 1;
186                 }
187                 
188                 my $relinfo = "";
189                 if (defined($section{$bug->{'package'}}) && $section{$bug->{'package'}} eq 'pseudo') {
190                         # versioning information makes no sense for pseudo packages,
191                         # just use the tags
192                         for my $dist qw(oldstable stable testing unstable experimental) {
193                                 $relinfo .= uc(substr($dist, 0, 1)) if $disttags{$dist};
194                         }
195                         next if (length($bug->{'done'}));
196                 } else {
197                         # only bother to check the versioning status for the distributions indicated by the tags 
198                         for my $dist qw(oldstable stable testing unstable experimental) {
199                                 local $SIG{__WARN__} = sub {};
200
201                                 next if (!$disttags{$dist});
202
203                                 # This is needed for now
204                                 my $exists = 0;
205                                 for my $arch (@bugcfg::architectures) {
206                                         for my $pkg (split /[,\s]+/, $bug->{'package'}) {
207                                                 my @versions = Debbugs::Packages::getversions($pkg, $dist, $arch);
208                                                 $exists = 1 if (scalar @versions > 0);
209                                         }
210                                         last if $exists;
211                                 }
212
213                                 next if !$exists;
214
215                                 my $presence = Debbugs::Status::bug_presence(
216                                         bug => $f, 
217                                         status => $bug, 
218                                         dist => $dist, 
219                                         arch => \@bugcfg::architectures
220                                 );
221
222                                 # ignore bugs that are absent/fixed in this distribution, include everything
223                                 # else (that is, "found" which says that the bug is present, and undef, which
224                                 # indicates that no versioning information is present and it's not closed
225                                 # unversioned)
226                                 if (!defined($presence) || ($presence ne 'absent' && $presence ne 'fixed')) {
227                                         $relinfo .= uc(substr($dist, 0, 1));
228                                 }
229                         }
230                         
231                         next if $relinfo eq '' and not $premature{$f};
232                         $premature{$f}++ if $relinfo eq '';
233                 }
234
235                 $taginfo = "[";
236                 $taginfo .= ($bug->{'keywords'} =~ /\bpending\b/        ? "P" : " ");
237                 $taginfo .= ($bug->{'keywords'} =~ /\bpatch\b/          ? "+" : " ");
238                 $taginfo .= ($bug->{'keywords'} =~ /\bhelp\b/           ? "H" : " ");
239                 $taginfo .= ($bug->{'keywords'} =~ /\bmoreinfo\b/       ? "M" : " ");
240                 $taginfo .= ($bug->{'keywords'} =~ /\bunreproducible\b/ ? "R" : " ");
241                 $taginfo .= ($bug->{'keywords'} =~ /\bsecurity\b/       ? "S" : " ");
242                 $taginfo .= ($bug->{'keywords'} =~ /\bupstream\b/       ? "U" : " ");
243                 $taginfo .= ($bug->{'keywords'} =~ /\betch-ignore\b/    ? "I" : " ");
244                 $taginfo .= "]";
245
246                 if (length($bug->{'mergedwith'})) {
247                         my @merged = split(' ', $bug->{'mergedwith'});
248                         next if ($merged[0] < $f);
249                 }
250
251                 for my $package (split /[,\s]+/, $bug->{'package'}) {
252                         $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._a-z0-9]/;
253                         if (not defined $section{$_}) {
254                                 if (defined $debbugssection{$_}) {
255                                         $relinfo .= "X";
256                                 } else {
257                                         next;   # Skip unavailable packages
258                                 }
259                         }
260
261                         push @{$packagelist{$_}}, $f;
262                 }
263
264                 if ($relinfo eq "") { # or $relinfo eq "U" # confuses e.g. #210306
265                         $relinfo = "";
266                 } else {
267                         $relinfo = " [$relinfo]";
268                 }
269
270                 $bugs{$f} = "$f $taginfo$relinfo " . $bug->{'subject'};
271         }
272 }
273
274
275 sub readstatus() {
276         my $bug;                # Number of current bug
277         my $subject;    # Subject for current bug
278         my $pkg;                # Name of current package
279         my $file;               # Name of statusfile
280         my $sect;               # Section of current package
281         my $mnt;                # Maintainer of current package
282
283         $file=shift;
284         open(P, $file) or die "open $file: $!";
285         while (<P>) {
286                 chomp;
287                 if (m/^[0-9]+ \[/) {
288                         ($bug,$subject)=split(/ /, $_, 2);
289                         $bugs{$bug}=$subject;
290                         push @{$packagelist{$pkg}}, $bug;
291                 } else {
292                         ($pkg,$sect, $mnt)=split(/ /, $_, 3);
293                         next if (!defined($pkg));
294                         $section{$pkg}=$sect;
295                         $maintainer{$pkg}=$mnt;
296                 }
297         }
298         close P;
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 ($status) = @_;
334
335         if ($status =~ m/^\[[^]]*I/ or
336             $status =~ m/ \[[^]]*X/ or
337             $status !~ m/ \[[^]]*T/) {
338                 return 0;
339         }
340         return 1;
341 }
342
343 1;