c52291eb18cfa989b6bc9cbc0cae619f06a73dc7
[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 $bi = {};
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                                 $bi->{$dist} = $disttags{$dist};
194                         }
195                         next if (length($bug->{'done'}));
196                 } else {
197                         my $affects_any = 0;
198                 
199                         # only bother to check the versioning status for the distributions indicated by the tags 
200                         for my $dist qw(oldstable stable testing unstable experimental) {
201                                 local $SIG{__WARN__} = sub {};
202
203                                 $bi->{$dist} = 0;
204                                 next if (!$disttags{$dist});
205
206                                 my $presence = Debbugs::Status::bug_presence(
207                                         bug => $f, 
208                                         status => $bug, 
209                                         dist => $dist, 
210                                         arch => \@bugcfg::architectures
211                                 );
212
213                                 # ignore bugs that are absent/fixed in this distribution, include everything
214                                 # else (that is, "found" which says that the bug is present, and undef, which
215                                 # indicates that no versioning information is present and it's not closed
216                                 # unversioned)
217                                 if (!defined($presence) || ($presence ne 'absent' && $presence ne 'fixed')) {
218                                         $bi->{$dist} = 1;
219                                         $affects_any = 1;
220                                 }
221                         }
222                         
223                         next if !$affects_any and not $premature{$f};
224                         $premature{$f}++ if !$affects_any;
225                 }
226
227                 for my $keyword qw(pending patch help moreinfo unreproducible security upstream etch-ignore) {
228                         $bi->{$keyword} = ($bug->{'keywords'} =~ /\b$keyword\b/) ? 1 : 0;
229                 }
230
231                 if (length($bug->{'mergedwith'})) {
232                         my @merged = split(' ', $bug->{'mergedwith'});
233                         next if ($merged[0] < $f);
234                 }
235
236                 for my $package (split /[,\s]+/, $bug->{'package'}) {
237                         $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._a-z0-9]/;
238                         push @{$packagelist{$_}}, $f;
239                 }
240
241                 my $taginfo = get_taginfo($bi);
242                 my $relinfo = get_relinfo($bi);
243
244                 $bugs{$f} = "$f [$taginfo] [$relinfo] " . $bug->{'subject'};
245         }
246 }
247
248
249 sub readstatus() {
250         my $bug;                # Number of current bug
251         my $subject;    # Subject for current bug
252         my $pkg;                # Name of current package
253         my $file;               # Name of statusfile
254         my $sect;               # Section of current package
255         my $mnt;                # Maintainer of current package
256
257         $file=shift;
258         open(P, $file) or die "open $file: $!";
259         while (<P>) {
260                 chomp;
261                 if (m/^[0-9]+ \[/) {
262                         ($bug,$subject)=split(/ /, $_, 2);
263                         $bugs{$bug}=$subject;
264                         push @{$packagelist{$pkg}}, $bug;
265                 } else {
266                         ($pkg,$sect, $mnt)=split(/ /, $_, 3);
267                         next if (!defined($pkg));
268                         $section{$pkg}=$sect;
269                         $maintainer{$pkg}=$mnt;
270                 }
271         }
272         close P;
273 }
274
275
276 sub urlsanit {
277         my $url = shift;
278         $url =~ s/%/%25/g;
279         $url =~ s/\+/%2b/g;
280         my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
281         $url =~ s/([<>&"])/\&$saniarray{$1};/g;
282         return $url;
283 }
284
285 sub htmlsanit {
286     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
287     my $in = shift || "";
288     $in =~ s/([<>&"])/\&$saniarray{$1};/g;
289     return $in;
290 }
291
292 sub wwwnumber() {
293         my $number = shift;             # Number of bug to html-ize
294
295         "<A HREF=\"http://bugs.debian.org/cgi-bin/bugreport.cgi?archive=no&amp;bug=" .
296                 urlsanit($number) . '">' . htmlsanit($number) . '</A>';
297 }
298
299 sub wwwname() {
300         my $name = shift;                       # Name of package
301
302         "<A HREF=\"http://bugs.debian.org/cgi-bin/pkgreport.cgi?archive=no&amp;pkg=" .
303                 urlsanit($name) . '">' . htmlsanit($name) . '</A>';
304 }
305
306 sub check_worry {
307         my ($status) = @_;
308
309         if ($status =~ m/^\[[^]]*I/ or
310             $status !~ m/ \[[^]]*T/) {
311                 return 0;
312         }
313         return 1;
314 }
315
316 sub check_worry_stable {
317         my ($status) = @_;
318
319         if ($status !~ m/ \[[^]]*S/) {
320                 return 0;
321         }
322         return 1;
323 }
324
325 sub get_taginfo {
326     my $bi = shift;
327
328         my $taginfo = "";
329         $taginfo .= $bi->{'pending'}        ? "P" : " ";
330         $taginfo .= $bi->{'patch'}          ? "+" : " ";
331         $taginfo .= $bi->{'help'}           ? "H" : " ";
332         $taginfo .= $bi->{'moreinfo'}       ? "M" : " ";
333         $taginfo .= $bi->{'unreproducible'} ? "R" : " ";
334         $taginfo .= $bi->{'security'}       ? "S" : " ";
335         $taginfo .= $bi->{'upstream'}       ? "U" : " ";
336         $taginfo .= $bi->{'etch-ignore'}    ? "I" : " ";
337
338         return $taginfo;
339 }
340
341 sub get_relinfo {
342     my $bi = shift;
343
344     my $relinfo = "";
345         for my $dist qw(oldstable stable testing unstable experimental) {
346             $relinfo .= uc(substr($dist, 0, 1)) if $bi->{$dist};
347         }
348
349         return $relinfo;
350 }
351
352
353 1;