A bit less repeated code, replaced by a for loop.
[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 %disttags = ();      
214                 $disttags{'oldstable'}    = grep(/^woody$/, @tags);
215                 $disttags{'stable'}       = grep(/^sarge$/, @tags);
216                 $disttags{'testing'}      = grep(/^etch$/, @tags);
217                 $disttags{'unstable'}     = grep(/^sid$/, @tags);
218                 $disttags{'experimental'} = grep(/^experimental$/, @tags);
219
220                 # default according to dondelelcaro 2006-11-11
221                 if (!$disttags{'oldstable'} && !$disttags{'stable'} && !$disttags{'testing'} && !$disttags{'unstable'} && !$disttags{'experimental'}) {
222                         $disttags{'testing'} = 1;
223                         $disttags{'unstable'} = 1;
224                         $disttags{'experimental'} = 1;
225                 }
226
227                 # only bother to check the versioning status for the distributions indicated by the tags 
228                 my $relinfo = "";
229                 for my $dist qw(oldstable stable testing unstable experimental) {
230                         next if (!$disttags{$dist});
231                         if (Debbugs::Status::check_bug_presence(bug => $f, status => $bug, dist => $dist) eq 'pending') {
232                                 $relinfo .= uc(substr($dist, 0, 1));
233                         }
234                 }
235                 
236                 next if $relinfo eq '' and not $premature{$f};
237                 $premature{$f}++ if $relinfo eq '';
238
239                 $taginfo = "[";
240                 $taginfo .= ($bug->{'keywords'} =~ /\bpending\b/        ? "P" : " ");
241                 $taginfo .= ($bug->{'keywords'} =~ /\bpatch\b/          ? "+" : " ");
242                 $taginfo .= ($bug->{'keywords'} =~ /\bhelp\b/           ? "H" : " ");
243                 $taginfo .= ($bug->{'keywords'} =~ /\bmoreinfo\b/       ? "M" : " ");
244                 $taginfo .= ($bug->{'keywords'} =~ /\bunreproducible\b/ ? "R" : " ");
245                 $taginfo .= ($bug->{'keywords'} =~ /\bsecurity\b/       ? "S" : " ");
246                 $taginfo .= ($bug->{'keywords'} =~ /\bupstream\b/       ? "U" : " ");
247                 $taginfo .= ($bug->{'keywords'} =~ /\betch-ignore\b/    ? "I" : " ");
248                 $taginfo .= "]";
249
250                 if (length($bug->{'mergedwith'})) {
251                         my @merged = split(' ', $bug->{'mergedwith'});
252                         next if ($merged[0] < $f);
253                 }
254
255                 for my $package (split /[,\s]+/, $bug->{'package'}) {
256                         $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._a-z0-9]/;
257                         if (not defined $section{$_}) {
258                                 if (defined $debbugssection{$_}) {
259                                         $relinfo .= "X";
260                                 } else {
261                                         next;   # Skip unavailable packages
262                                 }
263                         }
264
265                         push @{$packagelist{$_}}, $f;
266                 }
267
268                 if ($relinfo eq "") { # or $relinfo eq "U" # confuses e.g. #210306
269                         $relinfo = "";
270                 } else {
271                         $relinfo = " [$relinfo]";
272                 }
273
274                 $bugs{$f} = "$f $taginfo$relinfo " . $bug->{'subject'};
275         }
276 }
277
278
279 sub readstatus() {
280         my $bug;                # Number of current bug
281         my $subject;    # Subject for current bug
282         my $pkg;                # Name of current package
283         my $file;               # Name of statusfile
284         my $sect;               # Section of current package
285         my $mnt;                # Maintainer of current package
286
287         $file=shift;
288         open(P, $file) or die "open $file: $!";
289         while (<P>) {
290                 chomp;
291                 if (m/^[0-9]+ \[/) {
292                         ($bug,$subject)=split(/ /, $_, 2);
293                         $bugs{$bug}=$subject;
294                         push @{$packagelist{$pkg}}, $bug;
295                 } else {
296                         ($pkg,$sect, $mnt)=split(/ /, $_, 3);
297                         next if (!defined($pkg));
298                         $section{$pkg}=$sect;
299                         $maintainer{$pkg}=$mnt;
300                 }
301         }
302         close P;
303 }
304
305
306 sub readNMUstatus() {
307         my $bug;       # Number of current bug
308         my $source;    # Source upload which closes this bug.
309         my $version;   # Version where this bug was closed.
310         my $flag;      # Whether this paragraph has been processed.
311         my ($field, $value);
312
313         for (split /\n/, LWP::UserAgent->new->request(HTTP::Request->new(GET => shift))->content) {
314                 chomp;
315                 if (m/^$/) {
316                         $NMU{$bug} = 1;
317                         $NMU{$bug, "source"} = $source;
318                         $NMU{$bug, "version"} = $version;
319 #                       $comments{$bug} .= "[FIXED] Fixed package $source is in Incoming\n";
320                         $flag = 0;
321                 } else {
322                         ($field, $value) = split(/: /, $_, 2);
323                         $bug = $value if($field =~ /bug/i);
324                         $source = $value if($field =~ /source/i);
325                         $version = $value if($field =~ /version/i);
326                         $flag = 1;
327                 }
328         }
329         if ($flag) {
330                 $NMU{$bug} = 1;
331                 $NMU{$bug, "source"} = $source;
332                 $NMU{$bug, "version"} = $version;
333 #               $comments{$bug} .= "[FIXED] Fixed package $source in in Incoming\n";
334         }
335         close P;
336 }
337
338
339 sub urlsanit {
340         my $url = shift;
341         $url =~ s/%/%25/g;
342         $url =~ s/\+/%2b/g;
343         my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
344         $url =~ s/([<>&"])/\&$saniarray{$1};/g;
345         return $url;
346 }
347
348 sub htmlsanit {
349     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
350     my $in = shift || "";
351     $in =~ s/([<>&"])/\&$saniarray{$1};/g;
352     return $in;
353 }
354
355 sub wwwnumber() {
356         my $number = shift;             # Number of bug to html-ize
357 #       my $section);                           # Section for the bug
358
359         "<A HREF=\"http://bugs.debian.org/cgi-bin/bugreport.cgi?archive=no&amp;bug=" .
360                 urlsanit($number) . '">' . htmlsanit($number) . '</A>';
361 #       ($section=$number) =~ s/([0-9]{2}).*/$1/;
362 #       "<A HREF=\"${btsURL}/db/$section/$number.html\">$number</A>";
363 }
364
365 sub wwwname() {
366         my $name = shift;                       # Name of package
367
368         "<A HREF=\"http://bugs.debian.org/cgi-bin/pkgreport.cgi?archive=no&amp;pkg=" .
369                 urlsanit($name) . '">' . htmlsanit($name) . '</A>';
370 #       "<A HREF=\"${btsURL}/db/pa/l$name.html\">$name</A>";
371 }
372
373 sub check_worry {
374         my ($status) = @_;
375
376         if ($status =~ m/^\[[^]]*I/ or
377             $status =~ m/ \[[^]]*X/ or
378             ($status =~ m/ \[[^]]*[OSUE]/ and $status !~ m/ \[[^]]*T/)) {
379                 return 0;
380         }
381         return 1;
382 }
383
384 1;