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