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