Be slightly more consistent about quoting.
[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($s_originator, $s_date, $s_subject, $s_msgid, $s_package, $s_keywords);
171         local($s_done, $s_forwarded, $s_mergedwith, $s_severity);
172         local($skip);           # Flow control
173         local($walk);           # index variable
174         local($taginfo);        # Tag info
175
176         chdir($dir) or die "chdir $dir: $!\n";
177
178         opendir(DIR, $dir) or die "opendir $dir: $!\n";
179         @list = grep { s/\.status$// }
180                         grep { m/^\d+\.status$/ } 
181                         readdir(DIR);
182         closedir(DIR);
183
184         for $f (@list) {
185                 next if $exclude{$f};                   # Check the list of bugs to skip
186                 next if (!open(S,"$f.status")); # Check bugs without a status (?)
187
188                 chomp($s_originator = <S>);
189                 chomp($s_date = <S>);
190                 chomp($s_subject = <S>);
191                 chomp($s_msgid = <S>);
192                 chomp($s_package = <S>);
193                 chomp($s_tags = <S>);
194                 chomp($s_done = <S>);
195                 chomp($s_forwarded = <S>);
196                 chomp($s_mergedwith = <S>);
197                 chomp($s_severity = <S>);
198                 close(S);
199
200                 next if length($s_done) and not $premature{$f};
201                 $premature{$f}++ if length($s_done);
202
203                 $s_severity =~ y/A-Z/a-z/;
204                 $s_tags =~ y/A-Z/a-z/;
205
206                 $skip=1;
207                 for $walk (@priorities) {
208                         $skip=0 if $walk eq $s_severity;
209                 }
210
211                 for $tag (split(' ', $s_tags)) {
212                         for $s (@skiptags) {
213                                 $skip=1 if $tag eq $s;
214                         }
215                 }
216                 next if $skip==1;
217
218                 $relinfo = "";
219                 $relinfo .= ($s_tags =~ /\bwoody\b/         ? "O" : "");
220                 $relinfo .= ($s_tags =~ /\bsarge(|\s.*)%/   ? "S" : "");
221                 $relinfo .= ($s_tags =~ /\betch(|\s.*)$/    ? "T" : "");
222                         # etch-ignore matches \betch\b :(
223                 $relinfo .= ($s_tags =~ /\bsid\b/           ? "U" : "");
224                 $relinfo .= ($s_tags =~ /\bexperimental\b/  ? "E" : "");
225
226                 $taginfo = "[";
227                 $taginfo .= ($s_tags =~ /\bpending\b/        ? "P" : " ");
228                 $taginfo .= ($s_tags =~ /\bpatch\b/          ? "+" : " ");
229                 $taginfo .= ($s_tags =~ /\bhelp\b/           ? "H" : " ");
230                 $taginfo .= ($s_tags =~ /\bmoreinfo\b/       ? "M" : " ");
231                 $taginfo .= ($s_tags =~ /\bunreproducible\b/ ? "R" : " ");
232                 $taginfo .= ($s_tags =~ /\bsecurity\b/       ? "S" : " ");
233                 $taginfo .= ($s_tags =~ /\bupstream\b/       ? "U" : " ");
234                 $taginfo .= ($s_tags =~ /\betch-ignore\b/    ? "I" : " ");
235                 $taginfo .= "]";
236
237                 if ($s_mergedwith) {                    # Only show the first package if things are merged
238                         my @merged = split(' ', $s_mergedwith);
239                         next if ($merged[0] < $f);
240                 }
241
242                 for $package (split /[,\s]+/, $s_package) {
243                         $_= $package; y/A-Z/a-z/; $_= $` if m/[^-+._a-z0-9]/;
244                         if (not defined $section{$_}) {
245                                 if (defined $debbugssection{$_}) {
246                                         $relinfo .= "X";
247                                 } else {
248                                         next;   # Skip unavailable packages
249                                 }
250                         }
251
252                         $packagelist{$_} .= " $f";
253                 }
254
255                 if ($relinfo eq "") { # or $relinfo eq "U" # confuses e.g. #210306
256                         $relinfo = "";
257                 } else {
258                         $relinfo = " [$relinfo]";
259                 }
260
261                 $bugs{$f} = "$f $taginfo$relinfo $s_subject";
262         }
263 }
264
265
266 sub readstatus() {
267         local ($bug);           # Number of current bug
268         local ($subject);       # Subject for current bug
269         local ($pkg);           # Name of current package
270         local ($file);          # Name of statusfile
271         local ($sect);          # Section of current package
272         local ($mnt);           # Maintainer of current package
273
274         $file=shift;
275         open(P, $file) or die "open $file: $!";
276         while (<P>) {
277                 chomp;
278                 if (m/^[0-9]+ \[/) {
279                         ($bug,$subject)=split(/ /, $_, 2);
280                         $bugs{$bug}=$subject;
281                         $packagelist{$pkg} .= "$bug ";
282                 } else {
283                         ($pkg,$sect, $mnt)=split(/ /, $_, 3);
284                         $section{$pkg}=$sect;
285                         $maintainer{$pkg}=$mnt;
286                 }
287         }
288         close P;
289 }
290
291
292 sub readNMUstatus() {
293         local ($bug);       # Number of current bug
294         local ($source);    # Source upload which closes this bug.
295         local ($version);   # Version where this bug was closed.
296         local ($flag);      # Whether this paragraph has been processed.
297         local ($field, $value);
298
299         for (split /\n/, LWP::UserAgent->new->request(HTTP::Request->new(GET => shift))->content) {
300                 chomp;
301                 if (m/^$/) {
302                         $NMU{$bug} = 1;
303                         $NMU{$bug, "source"} = $source;
304                         $NMU{$bug, "version"} = $version;
305 #                       $comments{$bug} .= "[FIXED] Fixed package $source is in Incoming\n";
306                         $flag = 0;
307                 } else {
308                         ($field, $value) = split(/: /, $_, 2);
309                         $bug = $value if($field =~ /bug/i);
310                         $source = $value if($field =~ /source/i);
311                         $version = $value if($field =~ /version/i);
312                         $flag = 1;
313                 }
314         }
315         if ($flag) {
316                 $NMU{$bug} = 1;
317                 $NMU{$bug, "source"} = $source;
318                 $NMU{$bug, "version"} = $version;
319 #               $comments{$bug} .= "[FIXED] Fixed package $source in in Incoming\n";
320         }
321         close P;
322 }
323
324
325 sub urlsanit {
326         my $url = shift;
327         $url =~ s/%/%25/g;
328         $url =~ s/\+/%2b/g;
329         my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
330         $url =~ s/([<>&"])/\&$saniarray{$1};/g;
331         return $url;
332 }
333
334 sub htmlsanit {
335     my %saniarray = ('<','lt', '>','gt', '&','amp', '"','quot');
336     my $in = shift || "";
337     $in =~ s/([<>&"])/\&$saniarray{$1};/g;
338     return $in;
339 }
340
341 sub wwwnumber() {
342         local ($number) = shift;                # Number of bug to html-ize
343 #       local ($section);                               # Section for the bug
344
345         "<A HREF=\"http://bugs.debian.org/cgi-bin/bugreport.cgi?archive=no&amp;bug=" .
346                 urlsanit($number) . '">' . htmlsanit($number) . '</A>';
347 #       ($section=$number) =~ s/([0-9]{2}).*/$1/;
348 #       "<A HREF=\"${btsURL}/db/$section/$number.html\">$number</A>";
349 }
350
351 sub wwwname() {
352         local ($name) = shift;                  # Name of package
353
354         "<A HREF=\"http://bugs.debian.org/cgi-bin/pkgreport.cgi?archive=no&amp;pkg=" .
355                 urlsanit($name) . '">' . htmlsanit($name) . '</A>';
356 #       "<A HREF=\"${btsURL}/db/pa/l$name.html\">$name</A>";
357 }
358
359 1;
360