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