1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
6 # Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
12 Debbugs::Bugs -- Bug selection routines for debbugs
16 use Debbugs::Bugs qw(get_bugs);
21 This module is a replacement for all of the various methods of
22 selecting different types of bugs.
24 It implements a single function, get_bugs, which defines the master
25 interface for selecting bugs.
27 It attempts to use subsidiary functions to actually do the selection,
28 in the order specified in the configuration files. [Unless you're
29 insane, they should be in order from fastest (and often most
30 incomplete) to slowest (and most complete).]
40 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
41 use base qw(Exporter);
45 $DEBUG = 0 unless defined $DEBUG;
49 @EXPORT_OK = (qw(get_bugs count_bugs));
50 $EXPORT_TAGS{all} = [@EXPORT_OK];
53 use Debbugs::Config qw(:config);
54 use Params::Validate qw(validate_with :types);
56 use Debbugs::Status qw(splitpackages);
57 use Debbugs::Packages qw(getsrcpkgs);
58 use Debbugs::Common qw(getparsedaddrs getmaintainers getmaintainers_reverse);
59 use Fcntl qw(O_RDONLY);
60 use MLDBM qw(DB_File Storable);
68 The following parameters can either be a single scalar or a reference
69 to an array. The parameters are ANDed together, and the elements of
70 arrayrefs are a parameter are ORed. Future versions of this may allow
71 for limited regular expressions, and/or more complex expressions.
75 =item package -- name of the binary package
77 =item src -- name of the source package
79 =item maint -- address of the maintainer
81 =item submitter -- address of the submitter
83 =item severity -- severity of the bug
85 =item status -- status of the bug
89 =item owner -- owner of the bug
91 =item dist -- distribution (I don't know about this one yet)
93 =item bugs -- list of bugs to search within
95 =item function -- see description below
99 =head3 Special options
101 The following options are special options used to modulate how the
102 searches are performed.
106 =item archive -- whether to search archived bugs or normal bugs;
109 =item usertags -- set of usertags and the bugs they are applied to
114 =head3 Subsidiary routines
116 All subsidiary routines get passed exactly the same set of options as
117 get_bugs. If for some reason they are unable to handle the options
118 passed (for example, they don't have the right type of index for the
119 type of selection) they should die as early as possible. [Using
120 Params::Validate and/or die when files don't exist makes this fairly
123 This function will then immediately move on to the next subroutine,
124 giving it the same arguments.
128 This option allows you to provide an arbitrary function which will be
129 given the information in the index.db file. This will be super, super
130 slow, so only do this if there's no other way to write the search.
132 You'll be given a list (which you can turn into a hash) like the
135 (pkg => ['a','b'], # may be a scalar (most common)
138 submitter => 'boo@baz.com',
139 severity => 'serious',
140 tags => ['a','b','c'], # may be an empty arrayref
143 The function should return 1 if the bug should be included; 0 if the
149 my %param = validate_with(params => \@_,
150 spec => {package => {type => SCALAR|ARRAYREF,
153 src => {type => SCALAR|ARRAYREF,
156 maint => {type => SCALAR|ARRAYREF,
159 submitter => {type => SCALAR|ARRAYREF,
162 severity => {type => SCALAR|ARRAYREF,
165 status => {type => SCALAR|ARRAYREF,
168 tag => {type => SCALAR|ARRAYREF,
171 owner => {type => SCALAR|ARRAYREF,
174 dist => {type => SCALAR|ARRAYREF,
177 function => {type => CODEREF,
180 bugs => {type => SCALAR|ARRAYREF,
183 archive => {type => BOOLEAN,
186 usertags => {type => HASHREF,
193 my %options = %param;
195 # A configuration option will set an array that we'll use here instead.
196 for my $routine (qw(Debbugs::Bugs::get_bugs_by_idx Debbugs::Bugs::get_bugs_flatfile)) {
197 my ($package) = $routine =~ m/^(.+)\:\:/;
198 eval "use $package;";
200 # We output errors here because using an invalid function
201 # in the configuration file isn't something that should
203 warn "use $package failed with $@";
206 @bugs = eval "${routine}(\%options)";
209 # We don't output errors here, because failure here
210 # via die may be a perfectly normal thing.
211 print STDERR "$@" if $DEBUG;
216 # If no one succeeded, die
225 count_bugs(function => sub {...})
227 Uses a subroutine to classify bugs into categories and return the
228 number of bugs which fall into those categories
233 my %param = validate_with(params => \@_,
234 spec => {function => {type => CODEREF,
236 archive => {type => BOOLEAN,
242 if ($param{archive}) {
243 $flatfile = IO::File->new("$config{spool_dir}/index.archive", 'r')
244 or die "Unable to open $config{spool_dir}/index.archive for reading: $!";
247 $flatfile = IO::File->new("$config{spool_dir}/index.db", 'r')
248 or die "Unable to open $config{spool_dir}/index.db for reading: $!";
252 if (m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/) {
253 my @x = $param{function}->(pkg => $1,
261 $count{$_}++ foreach @x;
269 =head2 get_bugs_by_idx
271 This routine uses the by-$index.idx indicies to try to speed up
278 my %param = validate_with(params => \@_,
279 spec => {package => {type => SCALAR|ARRAYREF,
282 submitter => {type => SCALAR|ARRAYREF,
285 severity => {type => SCALAR|ARRAYREF,
288 tag => {type => SCALAR|ARRAYREF,
291 archive => {type => BOOLEAN,
294 owner => {type => SCALAR|ARRAYREF,
297 src => {type => SCALAR|ARRAYREF,
300 maint => {type => SCALAR|ARRAYREF,
307 # We handle src packages, maint and maintenc by mapping to the
308 # appropriate binary packages, then removing all packages which
309 # don't match all queries
310 my @packages = __handle_pkg_src_and_maint(map {exists $param{$_}?($_,$param{$_}):()}
311 qw(package src maint)
313 if (exists $param{package} or
314 exists $param{src} or
315 exists $param{maint}) {
316 delete @param{qw(maint src)};
317 $param{package} = [@packages];
319 my $keys = keys(%param) - 1;
320 die "Need at least 1 key to search by" unless $keys;
321 my $arc = $param{archive} ? '-arc':'';
323 for my $key (grep {$_ ne 'archive'} keys %param) {
325 $index = 'submitter-email' if $key eq 'submitter';
326 $index = "$config{spool_dir}/by-${index}${arc}.idx";
327 tie(%idx, MLDBM => $index, O_RDONLY)
328 or die "Unable to open $index: $!";
329 for my $search (__make_list($param{$key})) {
330 next unless defined $idx{$search};
331 for my $bug (keys %{$idx{$search}}) {
332 # increment the number of searches that this bug matched
336 untie %idx or die 'Unable to untie %idx';
338 # Throw out results that do not match all of the search specifications
339 return map {$keys <= $bugs{$_}?($_):()} keys %bugs;
343 =head2 get_bugs_flatfile
345 This is the fallback search routine. It should be able to complete all
346 searches. [Or at least, that's the idea.]
350 sub get_bugs_flatfile{
351 my %param = validate_with(params => \@_,
352 spec => {package => {type => SCALAR|ARRAYREF,
355 src => {type => SCALAR|ARRAYREF,
358 maint => {type => SCALAR|ARRAYREF,
361 submitter => {type => SCALAR|ARRAYREF,
364 severity => {type => SCALAR|ARRAYREF,
367 status => {type => SCALAR|ARRAYREF,
370 tag => {type => SCALAR|ARRAYREF,
374 # owner => {type => SCALAR|ARRAYREF,
377 # dist => {type => SCALAR|ARRAYREF,
380 archive => {type => BOOLEAN,
383 usertags => {type => HASHREF,
386 function => {type => CODEREF,
392 if ($param{archive}) {
393 $flatfile = IO::File->new("$config{spool_dir}/index.archive", 'r')
394 or die "Unable to open $config{spool_dir}/index.archive for reading: $!";
397 $flatfile = IO::File->new("$config{spool_dir}/index.db", 'r')
398 or die "Unable to open $config{spool_dir}/index.db for reading: $!";
401 if (exists $param{tag} and exists $param{usertags}) {
403 # This complex slice makes a hash with the bugs which have the
404 # usertags passed in $param{tag} set.
405 @usertag_bugs{map {@{$_}}
406 @{$param{usertags}}{__make_list($param{tag})}
407 } = (1) x @{$param{usertags}}{__make_list($param{tag})}
409 # We handle src packages, maint and maintenc by mapping to the
410 # appropriate binary packages, then removing all packages which
411 # don't match all queries
412 my @packages = __handle_pkg_src_and_maint(map {exists $param{$_}?($_,$param{$_}):()}
413 qw(package src maint)
415 if (exists $param{package} or
416 exists $param{src} or
417 exists $param{maint}) {
418 delete @param{qw(maint src)};
419 $param{package} = [@packages];
422 while (<$flatfile>) {
423 next unless m/^(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+\[\s*([^]]*)\s*\]\s+(\w+)\s+(.*)$/;
424 my ($pkg,$bug,$time,$status,$submitter,$severity,$tags) = ($1,$2,$3,$4,$5,$6,$7);
425 next if exists $param{bugs} and not grep {$bug == $_} __make_list($param{bugs});
426 if (exists $param{package}) {
427 my @packages = splitpackages($pkg);
428 next unless grep { my $pkg_list = $_;
429 grep {$pkg_list eq $_} __make_list($param{package})
432 if (exists $param{src}) {
433 my @src_packages = map { getsrcpkgs($_)} __make_list($param{src});
434 my @packages = splitpackages($pkg);
435 next unless grep { my $pkg_list = $_;
436 grep {$pkg_list eq $_} @packages
439 if (exists $param{submitter}) {
440 my @p_addrs = map {lc($_->address)}
441 map {getparsedaddrs($_)}
442 __make_list($param{submitter});
443 my @f_addrs = map {$_->address}
444 getparsedaddrs($submitter||'');
445 next unless grep { my $f_addr = $_;
446 grep {$f_addr eq $_} @p_addrs
449 next if exists $param{severity} and not grep {$severity eq $_} __make_list($param{severity});
450 next if exists $param{status} and not grep {$status eq $_} __make_list($param{status});
451 if (exists $param{tag}) {
453 # either a normal tag, or a usertag must be set
454 $bug_ok = 1 if exists $param{usertags} and $usertag_bugs{$bug};
455 my @bug_tags = split ' ', $tags;
456 $bug_ok = 1 if grep {my $bug_tag = $_;
457 grep {$bug_tag eq $_} __make_list($param{tag});
461 # We do this last, because a function may be slow...
462 if (exists $param{function}) {
463 my @bug_tags = split ' ', $tags;
464 my @packages = splitpackages($pkg);
465 my $package = (@packages > 1)?\@packages:$packages[0];
467 $param{function}->(pkg => $package,
470 submitter => $submitter,
471 severity => $severity,
480 sub __handle_pkg_src_and_maint{
481 my %param = validate_with(params => \@_,
482 spec => {package => {type => SCALAR|ARRAYREF,
485 src => {type => SCALAR|ARRAYREF,
488 maint => {type => SCALAR|ARRAYREF,
495 my @packages = __make_list($param{package});
496 my $package_keys = @packages?1:0;
498 @packages{@packages} = (1) x @packages;
499 if (exists $param{src}) {
500 # We only want to increment the number of keys if there is
503 for my $package ((map { getsrcpkgs($_)} __make_list($param{src})),__make_list($param{src})) {
504 $packages{$package}++;
507 $package_keys += $key_inc;
509 if (exists $param{maint}) {
511 my $maint_rev = getmaintainers_reverse();
512 for my $package (map { exists $maint_rev->{$_}?@{$maint_rev->{$_}}:()}
513 __make_list($param{maint})) {
514 $packages{$package}++;
517 $package_keys += $key_inc;
519 return grep {$packages{$_} >= $package_keys} keys %packages;
523 # This private subroutine takes a scalar and turns it into a list;
524 # transforming arrayrefs into their contents along the way. It also
525 # turns undef into the empty list.
527 return map {defined $_?(ref($_) eq 'ARRAY'?@{$_}:$_):()} @_;