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