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