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