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