There's no need for %packagelist to be a space-delimited list of bug
[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 #   %comments       - map from bugnumber to bug description
10 #   %premature      - list of prematurely closed bugreports
11 #   %exclude        - list of bugreports to exclude from the report
12 #   %maintainer     - map from packagename to maintainer
13 #   %section        - map from packagename to section in the FTP-site
14 #   %packagelist    - map from packagename to bugreports
15 #   %NMU            - map with NMU information
16
17 use lib qw(/org/bugs.debian.org/perl/);
18 use LWP::UserAgent;
19 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
20 use Debbugs::Packages;
21 use Debbugs::Versions;
22 use Debbugs::Status;
23 use Fcntl qw(O_RDONLY);
24 use strict;
25 use warnings;
26 require bugcfg;
27 package scanlib;
28
29 our (%comments,%premature,%exclude,%maintainer,%section,%packagelist,%NMU,%debbugssection,%bugs);
30
31 sub readcomments() {
32 # Read bug commentary 
33 # It is in paragraph format, with the first line of each paragraph being
34 # the bug number or package name to which the comment applies.
35 # Prefix a bug number with a * to force it to be listed even if it's closed.
36 # (This deals with prematurely closed bugs)
37
38         my $index;                                      # Bug-number for current comment
39         my $file;                                       # Name of comments-file
40
41         %comments = ();                                 # Initialize our data
42         %premature = ();
43         %exclude = ();
44         $file=shift;
45         open(C, $file) or die "open $file: $!\n";
46         while (<C>) {
47                 chomp;
48                 if (m/^\s*$/) {                         # Check for paragraph-breaks
49                         undef $index;
50                 } elsif (defined $index) {
51                         $comments{$index} .= $_ . "\n";
52                 } else {
53                         if (s/^\*//) {                  # Test & remove initial *
54                                 $premature{$_} = 1;
55                         }
56                         if (s/\s+EXCLUDE\s*//) {        # Test & remove EXCLUDE
57                                 $exclude{$_} = 1;
58                                 next;
59                         }
60                         $index = $_;
61                         $comments{$index} = ''; # New comment, initialize data
62                 }
63         }
64         close(C);
65 }
66
67
68 # Read the list of maintainer 
69 sub readmaintainers() {
70         my $pkg;                                        # Name of package
71         my $mnt;                                        # Maintainer name & email
72
73         open(M, $bugcfg::maintainerlist) or die "open $bugcfg::maintainerlist: $!\n";
74         while (<M>) {
75                 chomp;
76                 m/^(\S+)\s+(\S.*\S)\s*$/ or die "Maintainers: $_ ?";
77                 ($pkg, $mnt) = ($1, $2);
78                 $pkg =~ y/A-Z/a-z/;                     # Normalize package-name. why???
79                 $_=$mnt;
80                 if (not m/</) {
81                         $mnt="$2 <$1>" if ( m/(\S+)\s+\(([^)]+)\)/ );
82                 }
83                 $maintainer{$pkg}= $mnt;
84         }
85         close(M);
86 }
87
88
89 sub readsources() {
90         my $root;                                       # Root of archive we are scanning
91         my $archive;                            # Name of archive we are scanning
92         my $sect;                                       # Name of current section
93
94         $root=shift;
95         $archive=shift;
96         for $sect (@bugcfg::sections) {
97                 open(P, "zcat $root/$sect/source/Sources.gz|")
98                         or die open "open: $sect sourcelist: $!\n";
99                 while (<P>) {
100                         chomp;
101                         next unless m/^Package:\s/;
102                         s/^Package:\s*//;                       # Strip the fieldname
103                         $section{$_} = "$archive/$sect";
104                 }
105                 close (P);
106         }
107 }
108
109 sub readpackages() {
110         my $root;                                       # Root of archive we are scanning
111         my $archive;                            # Name of archive we are scanning
112         my $sect;                                       # Name of current section
113         my $arch;                                       # Name of current architecture
114
115         $root=shift;
116         $archive=shift;
117         for $arch ( @bugcfg::architectures ) {
118                 for $sect ( @bugcfg::sections) {
119                         open(P, "zcat $root/$sect/binary-$arch/Packages.gz|")
120                                 or die "open: $root/$sect/binary-$arch/Packages.gz: $!\n";
121                         while (<P>) {
122                                 chomp;
123                                 next unless m/^Package:\s/;     # We're only interested in the packagenames
124                                 s/^Package:\s*//;                       # Strip the fieldname
125                                 $section{$_} = "$archive/$sect";
126                         }
127                         close(P);
128                 }
129         }
130 }
131
132 sub readdebbugssources() {
133         my $file;
134         my $archive;
135
136         $file=shift;
137         $archive=shift;
138         open(P, $file)
139                 or die "open: $file: $!\n";
140         while (<P>) {
141                 chomp;
142                 my ($host, $bin, $sect, $ver, $src) = split /\s+/;
143                 my $sectname = ($sect =~ /^\Q$archive/) ? $sect : "$archive/$sect";
144                 $debbugssection{$bin} = $sectname;
145                 $debbugssection{$src} = $sectname;
146         }
147         close(P);
148 }
149
150 sub readpseudopackages() {
151         open(P, $bugcfg::pseudolist) or die("open $bugcfg::pseudolist: $!\n");
152         while (<P>) {
153                 chomp;
154                 s/\s.*//;
155                 $section{$_} = "pseudo";
156         }
157         close(P);
158 }
159
160
161 sub scanspool() {
162         my @dirs;
163         my $dir;
164
165         chdir($bugcfg::spooldir) or die "chdir $bugcfg::spooldir: $!\n";
166
167         opendir(DIR, $bugcfg::spooldir) or die "opendir $bugcfg::spooldir: $!\n";
168         @dirs=grep(m/^\d+$/,readdir(DIR));
169         closedir(DIR);
170
171         for $dir (@dirs) {
172                 scanspooldir("$bugcfg::spooldir/$dir");
173         }
174
175 }
176
177 sub scanspooldir() {
178         my ($dir)               = @_;
179         my $f;                  # While we're currently processing
180         my @list;               # List of files to process
181         my $skip;               # Flow control
182         my $walk;               # index variable
183         my $taginfo;    # Tag info
184
185         chdir($dir) or die "chdir $dir: $!\n";
186
187         opendir(DIR, $dir) or die "opendir $dir: $!\n";
188         @list = grep { s/\.summary$// }
189                         grep { m/^\d+\.summary$/ } 
190                         readdir(DIR);
191         closedir(DIR);
192
193         for $f (@list) {
194                 next if $exclude{$f};                   # Check the list of bugs to skip
195         
196                 my $bug = Debbugs::Status::read_bug(summary => "$f.summary");
197                 next if (!defined($bug));
198                 
199                 $skip=1;
200                 for $walk (@bugcfg::priorities) {
201                         $skip=0 if $walk eq $bug->{'severity'};
202                 }
203
204                 my @tags = split(' ', $bug->{'keywords'});
205                 for my $tag (@tags) {
206                         for my $s (@bugcfg::skiptags) {
207                                 $skip=1 if $tag eq $s;
208                         }
209                 }
210                 next if $skip==1;
211                 
212                 my $oldstable_tag    = grep(/^woody$/, @tags);
213                 my $stable_tag       = grep(/^sarge$/, @tags);
214                 my $testing_tag      = grep(/^etch$/, @tags);
215                 my $unstable_tag     = grep(/^sid$/, @tags);
216                 my $experimental_tag = grep(/^experimental$/, @tags);
217
218                 # default according to dondelelcaro 2006-11-11
219                 if (!$oldstable_tag && !$stable_tag && !$testing_tag && !$unstable_tag && !$experimental_tag) {
220                         $testing_tag = 1;
221                         $unstable_tag = 1;
222                         $experimental_tag = 1;
223                 }
224
225                 # only bother to check the versioning status for the distributions indicated by the tags 
226                 my $status_oldstable    = get_status($f, $bug, 'oldstable')    if ($oldstable_tag);
227                 my $status_stable       = get_status($f, $bug, 'stable')       if ($stable_tag);
228                 my $status_testing      = get_status($f, $bug, 'testing')      if ($testing_tag);
229                 my $status_unstable     = get_status($f, $bug, 'unstable')     if ($unstable_tag);
230                 my $status_experimental = get_status($f, $bug, 'experimental') if ($experimental_tag);
231
232                 my $relinfo = "";
233                 $relinfo .= (($oldstable_tag    && $status_oldstable    eq 'pending') ? "O" : "");
234                 $relinfo .= (($stable_tag       && $status_stable       eq 'pending') ? "S" : "");
235                 $relinfo .= (($testing_tag      && $status_testing      eq 'pending') ? "T" : "");
236                 $relinfo .= (($unstable_tag     && $status_unstable     eq 'pending') ? "U" : "");
237                 $relinfo .= (($experimental_tag && $status_experimental eq 'pending') ? "E" : "");
238                 
239                 next if $relinfo eq '' and not $premature{$f};
240                 $premature{$f}++ if $relinfo eq '';
241
242                 $taginfo = "[";
243                 $taginfo .= ($bug->{'keywords'} =~ /\bpending\b/        ? "P" : " ");
244                 $taginfo .= ($bug->{'keywords'} =~ /\bpatch\b/          ? "+" : " ");
245                 $taginfo .= ($bug->{'keywords'} =~ /\bhelp\b/           ? "H" : " ");
246                 $taginfo .= ($bug->{'keywords'} =~ /\bmoreinfo\b/       ? "M" : " ");
247                 $taginfo .= ($bug->{'keywords'} =~ /\bunreproducible\b/ ? "R" : " ");
248                 $taginfo .= ($bug->{'keywords'} =~ /\bsecurity\b/       ? "S" : " ");
249                 $taginfo .= ($bug->{'keywords'} =~ /\bupstream\b/       ? "U" : " ");
250                 $taginfo .= ($bug->{'keywords'} =~ /\betch-ignore\b/    ? "I" : " ");
251                 $taginfo .= "]";
252
253                 if (length($bug->{'mergedwith'})) {
254                         my @merged = split(' ', $bug->{'mergedwith'});
255                         next if ($merged[0] < $f);
256                 }
257
258                 for my $package (split /[,\s]+/, $bug->{'package'}) {
259                         $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._a-z0-9]/;
260                         if (not defined $section{$_}) {
261                                 if (defined $debbugssection{$_}) {
262                                         $relinfo .= "X";
263                                 } else {
264                                         next;   # Skip unavailable packages
265                                 }
266                         }
267
268                         push @{$packagelist{$_}}, $f;
269                 }
270
271                 if ($relinfo eq "") { # or $relinfo eq "U" # confuses e.g. #210306
272                         $relinfo = "";
273                 } else {
274                         $relinfo = " [$relinfo]";
275                 }
276
277                 $bugs{$f} = "$f $taginfo$relinfo " . $bug->{'subject'};
278         }
279 }
280
281
282 sub readstatus() {
283         my $bug;                # Number of current bug
284         my $subject;    # Subject for current bug
285         my $pkg;                # Name of current package
286         my $file;               # Name of statusfile
287         my $sect;               # Section of current package
288         my $mnt;                # Maintainer of current package
289
290         $file=shift;
291         open(P, $file) or die "open $file: $!";
292         while (<P>) {
293                 chomp;
294                 if (m/^[0-9]+ \[/) {
295                         ($bug,$subject)=split(/ /, $_, 2);
296                         $bugs{$bug}=$subject;
297                         push @{$packagelist{$pkg}}, $bug;
298                 } else {
299                         ($pkg,$sect, $mnt)=split(/ /, $_, 3);
300                         $section{$pkg}=$sect;
301                         $maintainer{$pkg}=$mnt;
302                 }
303         }
304         close P;
305 }
306
307
308 sub readNMUstatus() {
309         my $bug;       # Number of current bug
310         my $source;    # Source upload which closes this bug.
311         my $version;   # Version where this bug was closed.
312         my $flag;      # Whether this paragraph has been processed.
313         my ($field, $value);
314
315         for (split /\n/, LWP::UserAgent->new->request(HTTP::Request->new(GET => shift))->content) {
316                 chomp;
317                 if (m/^$/) {
318                         $NMU{$bug} = 1;
319                         $NMU{$bug, "source"} = $source;
320                         $NMU{$bug, "version"} = $version;
321 #                       $comments{$bug} .= "[FIXED] Fixed package $source is in Incoming\n";
322                         $flag = 0;
323                 } else {
324                         ($field, $value) = split(/: /, $_, 2);
325                         $bug = $value if($field =~ /bug/i);
326                         $source = $value if($field =~ /source/i);
327                         $version = $value if($field =~ /version/i);
328                         $flag = 1;
329                 }
330         }
331         if ($flag) {
332                 $NMU{$bug} = 1;
333                 $NMU{$bug, "source"} = $source;
334                 $NMU{$bug, "version"} = $version;
335 #               $comments{$bug} .= "[FIXED] Fixed package $source in in Incoming\n";
336         }
337         close P;
338 }
339
340
341 sub urlsanit {
342         my $url = shift;
343         $url =~ s/%/%25/g;
344         $url =~ s/\+/%2b/g;
345         my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
346         $url =~ s/([<>&"])/\&$saniarray{$1};/g;
347         return $url;
348 }
349
350 sub htmlsanit {
351     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
352     my $in = shift || "";
353     $in =~ s/([<>&"])/\&$saniarray{$1};/g;
354     return $in;
355 }
356
357 sub wwwnumber() {
358         my $number = shift;             # Number of bug to html-ize
359 #       my $section);                           # Section for the bug
360
361         "<A HREF=\"http://bugs.debian.org/cgi-bin/bugreport.cgi?archive=no&amp;bug=" .
362                 urlsanit($number) . '">' . htmlsanit($number) . '</A>';
363 #       ($section=$number) =~ s/([0-9]{2}).*/$1/;
364 #       "<A HREF=\"${btsURL}/db/$section/$number.html\">$number</A>";
365 }
366
367 sub wwwname() {
368         my $name = shift;                       # Name of package
369
370         "<A HREF=\"http://bugs.debian.org/cgi-bin/pkgreport.cgi?archive=no&amp;pkg=" .
371                 urlsanit($name) . '">' . htmlsanit($name) . '</A>';
372 #       "<A HREF=\"${btsURL}/db/pa/l$name.html\">$name</A>";
373 }
374
375 my $_version_cache = {};
376 sub get_status() {
377         my ($bugnr, $bug, $dist) = @_;
378
379         my @versions = Debbugs::Status::getversions($bug->{'package'}, $dist, undef);
380         my @sourceversions = Debbugs::Status::makesourceversions($bug->{'package'}, undef, @versions);
381
382         if (length($bug->{'done'}) and
383             (not @sourceversions or not @{$bug->{'fixed_versions'}})) {
384                 return 'done';
385         }
386         if (@sourceversions) {
387                 my $max_buggy = Debbugs::Status::max_buggy(bug => $bugnr,
388                          sourceversions => \@sourceversions,
389                          found => $bug->{'found_versions'},
390                          fixed => $bug->{'fixed_versions'},
391                          version_cache => $_version_cache,
392                          package => $bug->{'package'});
393                 if ($max_buggy eq 'absent' || $max_buggy eq 'fixed') {
394                         return $max_buggy;
395                 }
396         }
397
398         return 'pending';
399 }
400
401 sub check_worry {
402         my ($status) = @_;
403
404         if ($status =~ m/^\[[^]]*I/ or
405             $status =~ m/ \[[^]]*X/ or
406             ($status =~ m/ \[[^]]*[OSUE]/ and $status !~ m/ \[[^]]*T/)) {
407                 return 0;
408         }
409         return 1;
410 }