2 package Debbugs::Status;
6 Debbugs::Status -- Routines for dealing with summary and status files
15 This module is a replacement for the parts of errorlib.pl which write
16 and read status and summary files.
18 It also contains generic routines for returning information about the
19 status of a particular bug
27 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
28 use base qw(Exporter);
30 use Params::Validate qw(validate_with :types);
31 use Debbugs::Common qw(:util :lock :quit);
32 use Debbugs::Config qw(:config);
33 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
34 use Debbugs::Packages qw(makesourceversions getversions);
35 use Debbugs::Versions;
36 use Debbugs::Versions::Dpkg;
42 $DEBUG = 0 unless defined $DEBUG;
45 %EXPORT_TAGS = (status => [qw(splitpackages get_bug_status buggy bug_archiveable),
48 read => [qw(readbug read_bug lockreadbug)],
49 write => [qw(writebug makestatus unlockwritebug)],
50 versions => [qw(addfoundversions addfixedversions),
51 qw(removefoundversions)
55 Exporter::export_ok_tags(qw(status read write versions));
56 $EXPORT_TAGS{all} = [@EXPORT_OK];
62 readbug($bug_num,$location)
65 Reads a summary file from the archive given a bug number and a bug
66 location. Valid locations are those understood by L</getbugcomponent>
71 my %fields = (originator => 'submitter',
74 msgid => 'message-id',
75 'package' => 'package',
78 forwarded => 'forwarded-to',
79 mergedwith => 'merged-with',
80 severity => 'severity',
82 found_versions => 'found-in',
83 found_date => 'found-date',
84 fixed_versions => 'fixed-in',
85 fixed_date => 'fixed-date',
87 blockedby => 'blocked-by',
90 # Fields which need to be RFC1522-decoded in format versions earlier than 3.
91 my @rfc1522_fields = qw(originator subject done forwarded owner);
94 return read_bug(bug => $_[0],
95 (@_ > 1)?(location => $_[1]):()
101 read_bug(bug => $bug_num,
102 location => 'archive',
104 read_bug(summary => 'path/to/bugnum.summary');
107 A more complete function than readbug; it enables you to pass a full
108 path to the summary file instead of the bug number and/or location.
114 =item bug -- the bug number
116 =item location -- optional location which is passed to getbugcomponent
118 =item summary -- complete path to the .summary file which will be read
122 One of C<bug> or C<summary> must be passed. This function will return
123 undef on failure, and will die if improper arguments are passed.
131 my %param = validate_with(params => \@_,
132 spec => {bug => {type => SCALAR,
136 location => {type => SCALAR|UNDEF,
139 summary => {type => SCALAR,
144 die "One of bug or summary must be passed to read_bug"
145 if not exists $param{bug} and not exists $param{summary};
147 if (not defined $param{summary}) {
148 my ($lref, $location) = @param{qw(bug location)};
149 if (not defined $location) {
150 $location = getbuglocation($lref,'summary');
151 return undef if not defined $location;
153 $status = getbugcomponent($lref, 'summary', $location);
154 return undef unless defined $status;
157 $status = $param{summary};
159 my $status_fh = new IO::File $status, 'r' or
160 warn "Unable to open $status for reading: $!" and return undef;
167 while (<$status_fh>) {
170 $version = $1 if /^Format-Version: ([0-9]+)/i;
173 # Version 3 is the latest format version currently supported.
174 return undef if $version > 3;
176 my %namemap = reverse %fields;
177 for my $line (@lines) {
178 if ($line =~ /(\S+?): (.*)/) {
179 my ($name, $value) = (lc $1, $2);
180 $data{$namemap{$name}} = $value if exists $namemap{$name};
183 for my $field (keys %fields) {
184 $data{$field} = '' unless exists $data{$field};
187 $data{severity} = $config{default_severity} if $data{severity} eq '';
188 for my $field (qw(found_versions fixed_versions found_date fixed_date)) {
189 $data{$field} = [split ' ', $data{$field}];
191 for my $field (qw(found fixed)) {
192 @{$data{$field}}{@{$data{"${field}_versions"}}} =
193 (('') x (@{$data{"${field}_date"}} - @{$data{"${field}_versions"}}),
194 @{$data{"${field}_date"}});
198 for my $field (@rfc1522_fields) {
199 $data{$field} = decode_rfc1522($data{$field});
208 lockreadbug($bug_num,$location)
210 Performs a filelock, then reads the bug; the bug is unlocked if the
211 return is undefined, otherwise, you need to call unfilelock or
214 See readbug above for information on what this returns
219 my ($lref, $location) = @_;
220 &filelock("lock/$lref");
221 my $data = read_bug(bug => $lref, location => $location);
222 &unfilelock unless defined $data;
226 my @v1fieldorder = qw(originator date subject msgid package
227 keywords done forwarded mergedwith severity);
231 my $content = makestatus($status,$version)
232 my $content = makestatus($status);
234 Creates the content for a status file based on the $status hashref
237 Really only useful for writebug
239 Currently defaults to version 2 (non-encoded rfc1522 names) but will
240 eventually default to version 3. If you care, you should specify a
246 my ($data,$version) = @_;
247 $version = 2 unless defined $version;
251 my %newdata = %$data;
252 for my $field (qw(found fixed)) {
253 if (exists $newdata{$field}) {
254 $newdata{"${field}_date"} =
255 [map {$newdata{$field}{$_}||''} keys %{$newdata{$field}}];
259 for my $field (qw(found_versions fixed_versions found_date fixed_date)) {
260 $newdata{$field} = [split ' ', $newdata{$field}||''];
264 for my $field (@rfc1522_fields) {
265 $newdata{$field} = encode_rfc1522($newdata{$field});
270 for my $field (@v1fieldorder) {
271 if (exists $newdata{$field} and defined $newdata{$field}) {
272 $contents .= "$newdata{$field}\n";
277 } elsif ($version == 2 or $version == 3) {
278 # Version 2 or 3. Add a file format version number for the sake of
279 # further extensibility in the future.
280 $contents .= "Format-Version: $version\n";
281 for my $field (keys %fields) {
282 if (exists $newdata{$field} and defined $newdata{$field}
283 and $newdata{$field} ne '') {
284 # Output field names in proper case, e.g. 'Merged-With'.
285 my $properfield = $fields{$field};
286 $properfield =~ s/(?:^|(?<=-))([a-z])/\u$1/g;
287 $contents .= "$properfield: $newdata{$field}\n";
297 writebug($bug_num,$status,$location,$minversion,$disablebughook)
299 Writes the bug status and summary files out.
301 Skips writting out a status file if minversion is 2
303 Does not call bughook if disablebughook is true.
308 my ($ref, $data, $location, $minversion, $disablebughook) = @_;
311 my %outputs = (1 => 'status', 2 => 'summary');
312 for my $version (keys %outputs) {
313 next if defined $minversion and $version < $minversion;
314 my $status = getbugcomponent($ref, $outputs{$version}, $location);
315 &quit("can't find location for $ref") unless defined $status;
316 open(S,"> $status.new") || &quit("opening $status.new: $!");
317 print(S makestatus($data, $version)) ||
318 &quit("writing $status.new: $!");
319 close(S) || &quit("closing $status.new: $!");
325 rename("$status.new",$status) || &quit("installing new $status: $!");
328 # $disablebughook is a bit of a hack to let format migration scripts use
329 # this function rather than having to duplicate it themselves.
330 &bughook($change,$ref,$data) unless $disablebughook;
333 =head2 unlockwritebug
335 unlockwritebug($bug_num,$status,$location,$minversion,$disablebughook);
337 Writes a bug, then calls unfilelock; see writebug for what these
349 The following functions are exported with the :versions tag
351 =head2 addfoundversions
353 addfoundversions($status,$package,$version,$isbinary);
360 sub addfoundversions {
364 my $isbinary = shift;
365 return unless defined $version;
366 undef $package if $package =~ m[(?:\s|/)];
367 my $source = $package;
369 if (defined $package and $isbinary) {
370 my @srcinfo = binarytosource($package, $version, undef);
372 # We know the source package(s). Use a fully-qualified version.
373 addfoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
376 # Otherwise, an unqualified version will have to do.
380 # Strip off various kinds of brain-damage.
382 $version =~ s/ *\(.*\)//;
383 $version =~ s/ +[A-Za-z].*//;
385 foreach my $ver (split /[,\s]+/, $version) {
386 my $sver = defined($source) ? "$source/$ver" : '';
387 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{found_versions}}) {
388 push @{$data->{found_versions}}, defined($source) ? $sver : $ver;
390 @{$data->{fixed_versions}} =
391 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
395 =head2 removefoundversions
397 removefoundversions($data,$package,$versiontoremove)
399 Removes found versions from $data
401 If a version is fully qualified (contains /) only versions matching
402 exactly are removed. Otherwise, all versions matching the version
405 Currently $package and $isbinary are entirely ignored, but accepted
406 for backwards compatibilty.
410 sub removefoundversions {
414 my $isbinary = shift;
415 return unless defined $version;
417 foreach my $ver (split /[,\s]+/, $version) {
419 # fully qualified version
420 @{$data->{found_versions}} =
422 @{$data->{found_versions}};
425 # non qualified version; delete all matchers
426 @{$data->{found_versions}} =
427 grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
428 @{$data->{found_versions}};
434 sub addfixedversions {
438 my $isbinary = shift;
439 return unless defined $version;
440 undef $package if $package =~ m[(?:\s|/)];
441 my $source = $package;
443 if (defined $package and $isbinary) {
444 my @srcinfo = binarytosource($package, $version, undef);
446 # We know the source package(s). Use a fully-qualified version.
447 addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
450 # Otherwise, an unqualified version will have to do.
454 # Strip off various kinds of brain-damage.
456 $version =~ s/ *\(.*\)//;
457 $version =~ s/ +[A-Za-z].*//;
459 foreach my $ver (split /[,\s]+/, $version) {
460 my $sver = defined($source) ? "$source/$ver" : '';
461 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{fixed_versions}}) {
462 push @{$data->{fixed_versions}}, defined($source) ? $sver : $ver;
464 @{$data->{found_versions}} =
465 grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
469 sub removefixedversions {
473 my $isbinary = shift;
474 return unless defined $version;
475 undef $package if $package =~ m[(?:\s|/)];
476 my $source = $package;
478 if (defined $package and $isbinary) {
479 my @srcinfo = binarytosource($package, $version, undef);
481 # We know the source package(s). Use a fully-qualified version.
482 removefixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
485 # Otherwise, an unqualified version will have to do.
489 foreach my $ver (split /[,\s]+/, $version) {
490 my $sver = defined($source) ? "$source/$ver" : '';
491 @{$data->{fixed_versions}} =
492 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
502 Split a package string from the status file into a list of package names.
508 return unless defined $pkgs;
509 return map lc, split /[ \t?,()]+/, $pkgs;
513 =head2 bug_archiveable
515 bug_archiveable(bug => $bug_num);
521 =item bug -- bug number (required)
523 =item status -- Status hashref returned by read_bug or get_bug_status (optional)
525 =item version -- Debbugs::Version information (optional)
527 =item days_until -- return days until the bug can be archived
531 Returns 1 if the bug can be archived
532 Returns 0 if the bug cannot be archived
534 If days_until is true, returns the number of days until the bug can be
535 archived, -1 if it cannot be archived. 0 means that the bug can be
536 archived the next time the archiver runs.
538 Returns undef on failure.
542 # This will eventually need to be fixed before we start using mod_perl
543 my $version_cache = {};
545 my %param = validate_with(params => \@_,
546 spec => {bug => {type => SCALAR,
549 status => {type => HASHREF,
552 days_until => {type => BOOLEAN,
557 # This is what we return if the bug cannot be archived.
558 my $cannot_archive = $param{days_until}?-1:0;
559 # read the status information
560 my $status = $param{status};
561 if (not exists $param{status} or not defined $status) {
562 $status = read_bug(bug=>$param{bug});
563 return undef if not defined $status;
565 # Bugs can be archived if they are
567 return $cannot_archive if not defined $status->{done} or not length $status->{done};
568 # If we just are checking if the bug can be archived, we'll not even bother
569 # checking the versioning information if the bug has been -done for less than 28 days.
570 if (not $param{days_until} and $config{remove_age} >
571 -M getbugcomponent($param{ref},'log')
573 return $cannot_archive;
575 # At this point, we have to get the versioning information for this bug.
576 # We examine the set of distribution tags. If a bug has no distribution
577 # tags set, we assume a default set, otherwise we use the tags the bug
580 # There must be fixed_versions for us to look at the versioning
582 if (@{$status->{fixed_versions}}) {
584 @dist_tags{@{$config{removal_distribution_tags}}} =
585 (1) x @{$config{removal_distribution_tags}};
587 @dists{@{$config{removal_default_distribution_tags}}} =
588 (1) x @{$config{removal_default_distribution_tags}};
589 for my $tag (split ' ', $status->{tags}) {
590 next unless $dist_tags{$tag};
594 for my $dist (keys %dists){
596 @versions = getversions($status->{package},
599 # TODO: This should probably be handled further out for efficiency and
600 # for more ease of distinguishing between pkg= and src= queries.
601 my @sourceversions = makesourceversions($status->{package},
604 @source_versions{@sourceversions} = (1) x @sourceversions;
606 if ('found' eq max_buggy(bug => $param{bug},
607 sourceversions => [keys %source_versions],
608 found => $status->{found_versions},
609 fixed => $status->{fixed_versions},
610 version_cache => $version_cache,
611 package => $status->{package},
613 return $cannot_archive;
616 # 6. at least 28 days have passed since the last action has occured or the bug was closed
617 # XXX We still need some more work here before we actually can archive;
618 # we really need to track when a bug was closed in a version.
619 my $age = ceil($config{remove_age} - -M getbugcomponent($param{bug},'log'));
621 return $param{days_until}?$age:0;
624 return $param{days_until}?0:1;
629 =head2 get_bug_status
631 my $status = get_bug_status(bug => $nnn);
633 my $status = get_bug_status($bug_num)
639 =item bug -- scalar bug number
641 =item status -- optional hashref of bug status as returned by readbug
642 (can be passed to avoid rereading the bug information)
644 =item bug_index -- optional tied index of bug status infomration;
645 currently not correctly implemented.
647 =item version -- optional version to check package status at
649 =item dist -- optional distribution to check package status at
651 =item arch -- optional architecture to check package status at
653 =item usertags -- optional hashref of usertags
655 =item sourceversion -- optional arrayref of source/version; overrides
656 dist, arch, and version. [The entries in this array must be in the
657 "source/version" format.] Eventually this can be used to for caching.
661 Note: Currently the version information is cached; this needs to be
662 changed before using this function in long lived programs.
670 my %param = validate_with(params => \@_,
671 spec => {bug => {type => SCALAR,
674 status => {type => HASHREF,
677 bug_index => {type => OBJECT,
680 version => {type => SCALAR,
683 dist => {type => SCALAR,
686 arch => {type => SCALAR,
689 usertags => {type => HASHREF,
692 sourceversions => {type => ARRAYREF,
699 if (defined $param{bug_index} and
700 exists $param{bug_index}{$param{bug}}) {
701 %status = %{ $param{bug_index}{$param{bug}} };
702 $status{pending} = $status{ status };
703 $status{id} = $param{bug};
706 if (defined $param{status}) {
707 %status = %{$param{status}};
710 my $location = getbuglocation($param{bug}, 'summary');
711 return {} if not length $location;
712 %status = %{ readbug( $param{bug}, $location ) };
714 $status{id} = $param{bug};
716 if (defined $param{usertags}{$param{bug}}) {
717 $status{keywords} = "" unless defined $status{keywords};
718 $status{keywords} .= " " unless $status{keywords} eq "";
719 $status{keywords} .= join(" ", @{$param{usertags}{$param{bug}}});
721 $status{tags} = $status{keywords};
722 my %tags = map { $_ => 1 } split ' ', $status{tags};
724 $status{"package"} =~ s/\s*$//;
725 $status{"package"} = 'unknown' if ($status{"package"} eq '');
726 $status{"severity"} = 'normal' if ($status{"severity"} eq '');
728 $status{"pending"} = 'pending';
729 $status{"pending"} = 'forwarded' if (length($status{"forwarded"}));
730 $status{"pending"} = 'pending-fixed' if ($tags{pending});
731 $status{"pending"} = 'fixed' if ($tags{fixed});
734 if (not exists $param{sourceversions}) {
736 if (defined $param{version}) {
737 @versions = ($param{version});
738 } elsif (defined $param{dist}) {
739 @versions = getversions($status{package}, $param{dist}, $param{arch});
742 # TODO: This should probably be handled further out for efficiency and
743 # for more ease of distinguishing between pkg= and src= queries.
744 @sourceversions = makesourceversions($status{package},
749 @sourceversions = @{$param{sourceversions}};
751 if (@sourceversions) {
752 my $maxbuggy = max_buggy(bug => $param{bug},
753 sourceversions => \@sourceversions,
754 found => $status{found_versions},
755 fixed => $status{fixed_versions},
756 package => $status{package},
757 version_cache => $version_cache,
759 if ($maxbuggy eq 'absent') {
760 $status{pending} = 'absent';
762 elsif ($maxbuggy eq 'fixed' ) {
763 $status{pending} = 'done';
766 if (length($status{done}) and
767 (not @sourceversions or not @{$status{fixed_versions}})) {
768 $status{"pending"} = 'done';
783 =item bug -- scalar bug number
785 =item sourceversion -- optional arrayref of source/version; overrides
786 dist, arch, and version. [The entries in this array must be in the
787 "source/version" format.] Eventually this can be used to for caching.
791 Note: Currently the version information is cached; this needs to be
792 changed before using this function in long lived programs.
797 my %param = validate_with(params => \@_,
798 spec => {bug => {type => SCALAR,
801 sourceversions => {type => ARRAYREF,
804 found => {type => ARRAYREF,
807 fixed => {type => ARRAYREF,
810 package => {type => SCALAR,
812 version_cache => {type => HASHREF,
817 # Resolve bugginess states (we might be looking at multiple
818 # architectures, say). Found wins, then fixed, then absent.
819 my $maxbuggy = 'absent';
820 for my $version (@{$param{sourceversions}}) {
821 my $buggy = buggy(bug => $param{bug},
823 found => $param{found},
824 fixed => $param{fixed},
825 version_cache => $param{version_cache},
826 package => $param{package},
828 if ($buggy eq 'found') {
830 } elsif ($buggy eq 'fixed') {
847 Returns the output of Debbugs::Versions::buggy for a particular
848 package, version and found/fixed set. Automatically turns found, fixed
849 and version into source/version strings.
851 Caching can be had by using the version_cache, but no attempt to check
852 to see if the on disk information is more recent than the cache is
853 made. [This will need to be fixed for long-lived processes.]
858 my %param = validate_with(params => \@_,
859 spec => {bug => {type => SCALAR,
862 found => {type => ARRAYREF,
865 fixed => {type => ARRAYREF,
868 version_cache => {type => HASHREF,
871 package => {type => SCALAR,
873 version => {type => SCALAR,
877 my @found = @{$param{found}};
878 my @fixed = @{$param{fixed}};
879 if (grep {$_ !~ m{/}} (@{$param{found}}, @{$param{fixed}})) {
880 # We have non-source version versions
881 @found = makesourceversions($param{package},undef,
884 @fixed = makesourceversions($param{package},undef,
888 if ($param{version} !~ m{/}) {
889 $param{version} = makesourceversions($param{package},undef,
893 # Figure out which source packages we need
895 @sources{map {m{(.+)/}; $1} @found} = (1) x @found;
896 @sources{map {m{(.+)/}; $1} @fixed} = (1) x @fixed;
897 @sources{map {m{(.+)/}; $1} $param{version}} = 1;
899 if (not defined $param{version_cache} or
900 not exists $param{version_cache}{join(',',sort keys %sources)}) {
901 $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
902 foreach my $source (keys %sources) {
903 my $srchash = substr $source, 0, 1;
904 my $version_fh = new IO::File "$config{version_packages_dir}/$srchash/$source", 'r' or
905 warn "Unable to open $config{version_packages_dir}/$srchash/$source: $!" and next;
906 $version->load($version_fh);
908 if (defined $param{version_cache}) {
909 $param{version_cache}{join(',',sort keys %sources)} = $version;
913 $version = $param{version_cache}{join(',',sort keys %sources)};
915 return $version->buggy($param{version},\@found,\@fixed);
918 sub isstrongseverity {
919 my $severity = shift;
920 $severity = $config{default_severity} if $severity eq '';
921 return grep { $_ eq $severity } @{$config{strong_severities}};
925 =head1 PRIVATE FUNCTIONS
929 sub update_realtime {
930 my ($file, $bug, $new) = @_;
932 # update realtime index.db
934 open(IDXDB, "<$file") or die "Couldn't open $file";
935 open(IDXNEW, ">$file.new");
939 while($line = <IDXDB>) {
940 @line = split /\s/, $line;
941 last if ($line[1] >= $bug);
946 if ($new eq "NOCHANGE") {
947 print IDXNEW $line if ($line ne "" and $line[1] == $bug);
948 } elsif ($new eq "REMOVE") {
953 if (defined $line and $line ne "" and @line and $line[1] > $bug) {
958 print IDXNEW while(<IDXDB>);
963 rename("$file.new", $file);
968 sub bughook_archive {
970 &filelock("debbugs.trace.lock");
971 &appendfile("debbugs.trace","archive $ref\n");
972 my $line = update_realtime(
973 "$config{spool_dir}/index.db.realtime",
976 update_realtime("$config{spool_dir}/index.archive.realtime",
982 my ( $type, $ref, $data ) = @_;
983 &filelock("debbugs.trace.lock");
985 &appendfile("debbugs.trace","$type $ref\n",makestatus($data, 1));
987 my $whendone = "open";
988 my $severity = $config{default_severity};
989 (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
992 $whendone = "forwarded" if defined $data->{forwarded} and length $data->{forwarded};
993 $whendone = "done" if defined $data->{done} and length $data->{done};
994 $severity = $data->{severity} if length $data->{severity};
996 my $k = sprintf "%s %d %d %s [%s] %s %s\n",
997 $pkglist, $ref, $data->{date}, $whendone,
998 $data->{originator}, $severity, $data->{keywords};
1000 update_realtime("$config{spool_dir}/index.db.realtime", $ref, $k);