ed9fd37dd022f2ccfeb856dbc1f142b0ce60c7e0
[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                                 my $presence = Debbugs::Status::bug_presence(
204                                         bug => $f, 
205                                         status => $bug, 
206                                         dist => $dist, 
207                                         arch => \@bugcfg::architectures
208                                 );
209
210                                 # ignore bugs that are absent/fixed in this distribution, include everything
211                                 # else (that is, "found" which says that the bug is present, and undef, which
212                                 # indicates that no versioning information is present and it's not closed
213                                 # unversioned)
214                                 if (!defined($presence) || ($presence ne 'absent' && $presence ne 'fixed')) {
215                                         $relinfo .= uc(substr($dist, 0, 1));
216                                 }
217                         }
218                         
219                         next if $relinfo eq '' and not $premature{$f};
220                         $premature{$f}++ if $relinfo eq '';
221                 }
222
223                 $taginfo = "[";
224                 $taginfo .= ($bug->{'keywords'} =~ /\bpending\b/        ? "P" : " ");
225                 $taginfo .= ($bug->{'keywords'} =~ /\bpatch\b/          ? "+" : " ");
226                 $taginfo .= ($bug->{'keywords'} =~ /\bhelp\b/           ? "H" : " ");
227                 $taginfo .= ($bug->{'keywords'} =~ /\bmoreinfo\b/       ? "M" : " ");
228                 $taginfo .= ($bug->{'keywords'} =~ /\bunreproducible\b/ ? "R" : " ");
229                 $taginfo .= ($bug->{'keywords'} =~ /\bsecurity\b/       ? "S" : " ");
230                 $taginfo .= ($bug->{'keywords'} =~ /\bupstream\b/       ? "U" : " ");
231                 $taginfo .= ($bug->{'keywords'} =~ /\betch-ignore\b/    ? "I" : " ");
232                 $taginfo .= "]";
233
234                 if (length($bug->{'mergedwith'})) {
235                         my @merged = split(' ', $bug->{'mergedwith'});
236                         next if ($merged[0] < $f);
237                 }
238
239                 for my $package (split /[,\s]+/, $bug->{'package'}) {
240                         $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._a-z0-9]/;
241                         push @{$packagelist{$_}}, $f;
242                 }
243
244                 if ($relinfo eq "") { # or $relinfo eq "U" # confuses e.g. #210306
245                         $relinfo = "";
246                 } else {
247                         $relinfo = " [$relinfo]";
248                 }
249
250                 $bugs{$f} = "$f $taginfo$relinfo " . $bug->{'subject'};
251         }
252 }
253
254
255 sub readstatus() {
256         my $bug;                # Number of current bug
257         my $subject;    # Subject for current bug
258         my $pkg;                # Name of current package
259         my $file;               # Name of statusfile
260         my $sect;               # Section of current package
261         my $mnt;                # Maintainer of current package
262
263         $file=shift;
264         open(P, $file) or die "open $file: $!";
265         while (<P>) {
266                 chomp;
267                 if (m/^[0-9]+ \[/) {
268                         ($bug,$subject)=split(/ /, $_, 2);
269                         $bugs{$bug}=$subject;
270                         push @{$packagelist{$pkg}}, $bug;
271                 } else {
272                         ($pkg,$sect, $mnt)=split(/ /, $_, 3);
273                         next if (!defined($pkg));
274                         $section{$pkg}=$sect;
275                         $maintainer{$pkg}=$mnt;
276                 }
277         }
278         close P;
279 }
280
281
282 sub urlsanit {
283         my $url = shift;
284         $url =~ s/%/%25/g;
285         $url =~ s/\+/%2b/g;
286         my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
287         $url =~ s/([<>&"])/\&$saniarray{$1};/g;
288         return $url;
289 }
290
291 sub htmlsanit {
292     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
293     my $in = shift || "";
294     $in =~ s/([<>&"])/\&$saniarray{$1};/g;
295     return $in;
296 }
297
298 sub wwwnumber() {
299         my $number = shift;             # Number of bug to html-ize
300
301         "<A HREF=\"http://bugs.debian.org/cgi-bin/bugreport.cgi?archive=no&amp;bug=" .
302                 urlsanit($number) . '">' . htmlsanit($number) . '</A>';
303 }
304
305 sub wwwname() {
306         my $name = shift;                       # Name of package
307
308         "<A HREF=\"http://bugs.debian.org/cgi-bin/pkgreport.cgi?archive=no&amp;pkg=" .
309                 urlsanit($name) . '">' . htmlsanit($name) . '</A>';
310 }
311
312 sub check_worry {
313         my ($status) = @_;
314
315         if ($status =~ m/^\[[^]]*I/ or
316             $status !~ m/ \[[^]]*T/) {
317                 return 0;
318         }
319         return 1;
320 }
321
322 sub check_worry_stable {
323         my ($status) = @_;
324
325         if ($status !~ m/ \[[^]]*S/) {
326                 return 0;
327         }
328         return 1;
329 }
330
331 1;