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 binarytosource);
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 # create the found/fixed hashes which indicate when a
193 # particular version was marked found or marked fixed.
194 @{$data{$field}}{@{$data{"${field}_versions"}}} =
195 (('') x (@{$data{"${field}_date"}} - @{$data{"${field}_versions"}}),
196 @{$data{"${field}_date"}});
200 for my $field (@rfc1522_fields) {
201 $data{$field} = decode_rfc1522($data{$field});
210 lockreadbug($bug_num,$location)
212 Performs a filelock, then reads the bug; the bug is unlocked if the
213 return is undefined, otherwise, you need to call unfilelock or
216 See readbug above for information on what this returns
221 my ($lref, $location) = @_;
222 &filelock("lock/$lref");
223 my $data = read_bug(bug => $lref, location => $location);
224 &unfilelock unless defined $data;
228 my @v1fieldorder = qw(originator date subject msgid package
229 keywords done forwarded mergedwith severity);
233 my $content = makestatus($status,$version)
234 my $content = makestatus($status);
236 Creates the content for a status file based on the $status hashref
239 Really only useful for writebug
241 Currently defaults to version 2 (non-encoded rfc1522 names) but will
242 eventually default to version 3. If you care, you should specify a
248 my ($data,$version) = @_;
249 $version = 2 unless defined $version;
253 my %newdata = %$data;
254 for my $field (qw(found fixed)) {
255 if (exists $newdata{$field}) {
256 $newdata{"${field}_date"} =
257 [map {$newdata{$field}{$_}||''} keys %{$newdata{$field}}];
261 for my $field (qw(found_versions fixed_versions found_date fixed_date)) {
262 $newdata{$field} = join ' ', @{$newdata{$field}||[]};
266 for my $field (@rfc1522_fields) {
267 $newdata{$field} = encode_rfc1522($newdata{$field});
272 for my $field (@v1fieldorder) {
273 if (exists $newdata{$field} and defined $newdata{$field}) {
274 $contents .= "$newdata{$field}\n";
279 } elsif ($version == 2 or $version == 3) {
280 # Version 2 or 3. Add a file format version number for the sake of
281 # further extensibility in the future.
282 $contents .= "Format-Version: $version\n";
283 for my $field (keys %fields) {
284 if (exists $newdata{$field} and defined $newdata{$field}
285 and $newdata{$field} ne '') {
286 # Output field names in proper case, e.g. 'Merged-With'.
287 my $properfield = $fields{$field};
288 $properfield =~ s/(?:^|(?<=-))([a-z])/\u$1/g;
289 $contents .= "$properfield: $newdata{$field}\n";
299 writebug($bug_num,$status,$location,$minversion,$disablebughook)
301 Writes the bug status and summary files out.
303 Skips writting out a status file if minversion is 2
305 Does not call bughook if disablebughook is true.
310 my ($ref, $data, $location, $minversion, $disablebughook) = @_;
313 my %outputs = (1 => 'status', 2 => 'summary');
314 for my $version (keys %outputs) {
315 next if defined $minversion and $version < $minversion;
316 my $status = getbugcomponent($ref, $outputs{$version}, $location);
317 &quit("can't find location for $ref") unless defined $status;
318 open(S,"> $status.new") || &quit("opening $status.new: $!");
319 print(S makestatus($data, $version)) ||
320 &quit("writing $status.new: $!");
321 close(S) || &quit("closing $status.new: $!");
327 rename("$status.new",$status) || &quit("installing new $status: $!");
330 # $disablebughook is a bit of a hack to let format migration scripts use
331 # this function rather than having to duplicate it themselves.
332 &bughook($change,$ref,$data) unless $disablebughook;
335 =head2 unlockwritebug
337 unlockwritebug($bug_num,$status,$location,$minversion,$disablebughook);
339 Writes a bug, then calls unfilelock; see writebug for what these
351 The following functions are exported with the :versions tag
353 =head2 addfoundversions
355 addfoundversions($status,$package,$version,$isbinary);
362 sub addfoundversions {
366 my $isbinary = shift;
367 return unless defined $version;
368 undef $package if $package =~ m[(?:\s|/)];
369 my $source = $package;
371 if (defined $package and $isbinary) {
372 my @srcinfo = binarytosource($package, $version, undef);
374 # We know the source package(s). Use a fully-qualified version.
375 addfoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
378 # Otherwise, an unqualified version will have to do.
382 # Strip off various kinds of brain-damage.
384 $version =~ s/ *\(.*\)//;
385 $version =~ s/ +[A-Za-z].*//;
387 foreach my $ver (split /[,\s]+/, $version) {
388 my $sver = defined($source) ? "$source/$ver" : '';
389 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{found_versions}}) {
390 push @{$data->{found_versions}}, defined($source) ? $sver : $ver;
392 @{$data->{fixed_versions}} =
393 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
397 =head2 removefoundversions
399 removefoundversions($data,$package,$versiontoremove)
401 Removes found versions from $data
403 If a version is fully qualified (contains /) only versions matching
404 exactly are removed. Otherwise, all versions matching the version
407 Currently $package and $isbinary are entirely ignored, but accepted
408 for backwards compatibilty.
412 sub removefoundversions {
416 my $isbinary = shift;
417 return unless defined $version;
419 foreach my $ver (split /[,\s]+/, $version) {
421 # fully qualified version
422 @{$data->{found_versions}} =
424 @{$data->{found_versions}};
427 # non qualified version; delete all matchers
428 @{$data->{found_versions}} =
429 grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
430 @{$data->{found_versions}};
436 sub addfixedversions {
440 my $isbinary = shift;
441 return unless defined $version;
442 undef $package if $package =~ m[(?:\s|/)];
443 my $source = $package;
445 if (defined $package and $isbinary) {
446 my @srcinfo = binarytosource($package, $version, undef);
448 # We know the source package(s). Use a fully-qualified version.
449 addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
452 # Otherwise, an unqualified version will have to do.
456 # Strip off various kinds of brain-damage.
458 $version =~ s/ *\(.*\)//;
459 $version =~ s/ +[A-Za-z].*//;
461 foreach my $ver (split /[,\s]+/, $version) {
462 my $sver = defined($source) ? "$source/$ver" : '';
463 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{fixed_versions}}) {
464 push @{$data->{fixed_versions}}, defined($source) ? $sver : $ver;
466 @{$data->{found_versions}} =
467 grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
471 sub removefixedversions {
475 my $isbinary = shift;
476 return unless defined $version;
477 undef $package if $package =~ m[(?:\s|/)];
478 my $source = $package;
480 if (defined $package and $isbinary) {
481 my @srcinfo = binarytosource($package, $version, undef);
483 # We know the source package(s). Use a fully-qualified version.
484 removefixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
487 # Otherwise, an unqualified version will have to do.
491 foreach my $ver (split /[,\s]+/, $version) {
492 my $sver = defined($source) ? "$source/$ver" : '';
493 @{$data->{fixed_versions}} =
494 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
504 Split a package string from the status file into a list of package names.
510 return unless defined $pkgs;
511 return map lc, split /[ \t?,()]+/, $pkgs;
515 =head2 bug_archiveable
517 bug_archiveable(bug => $bug_num);
523 =item bug -- bug number (required)
525 =item status -- Status hashref returned by read_bug or get_bug_status (optional)
527 =item version -- Debbugs::Version information (optional)
529 =item days_until -- return days until the bug can be archived
533 Returns 1 if the bug can be archived
534 Returns 0 if the bug cannot be archived
536 If days_until is true, returns the number of days until the bug can be
537 archived, -1 if it cannot be archived. 0 means that the bug can be
538 archived the next time the archiver runs.
540 Returns undef on failure.
544 # This will eventually need to be fixed before we start using mod_perl
545 my $version_cache = {};
547 my %param = validate_with(params => \@_,
548 spec => {bug => {type => SCALAR,
551 status => {type => HASHREF,
554 days_until => {type => BOOLEAN,
559 # This is what we return if the bug cannot be archived.
560 my $cannot_archive = $param{days_until}?-1:0;
561 # read the status information
562 my $status = $param{status};
563 if (not exists $param{status} or not defined $status) {
564 $status = read_bug(bug=>$param{bug});
565 return undef if not defined $status;
567 # Bugs can be archived if they are
569 return $cannot_archive if not defined $status->{done} or not length $status->{done};
570 # If we just are checking if the bug can be archived, we'll not even bother
571 # checking the versioning information if the bug has been -done for less than 28 days.
572 if (not $param{days_until} and $config{remove_age} >
573 -M getbugcomponent($param{ref},'log')
575 return $cannot_archive;
577 # At this point, we have to get the versioning information for this bug.
578 # We examine the set of distribution tags. If a bug has no distribution
579 # tags set, we assume a default set, otherwise we use the tags the bug
582 # There must be fixed_versions for us to look at the versioning
584 if (@{$status->{fixed_versions}}) {
586 @dist_tags{@{$config{removal_distribution_tags}}} =
587 (1) x @{$config{removal_distribution_tags}};
589 @dists{@{$config{removal_default_distribution_tags}}} =
590 (1) x @{$config{removal_default_distribution_tags}};
591 for my $tag (split ' ', $status->{tags}) {
592 next unless $dist_tags{$tag};
596 for my $dist (keys %dists){
598 @versions = getversions($status->{package},
601 # TODO: This should probably be handled further out for efficiency and
602 # for more ease of distinguishing between pkg= and src= queries.
603 my @sourceversions = makesourceversions($status->{package},
606 @source_versions{@sourceversions} = (1) x @sourceversions;
608 if ('found' eq max_buggy(bug => $param{bug},
609 sourceversions => [keys %source_versions],
610 found => $status->{found_versions},
611 fixed => $status->{fixed_versions},
612 version_cache => $version_cache,
613 package => $status->{package},
615 return $cannot_archive;
618 # 6. at least 28 days have passed since the last action has occured or the bug was closed
619 # XXX We still need some more work here before we actually can archive;
620 # we really need to track when a bug was closed in a version.
621 my $age = ceil($config{remove_age} - -M getbugcomponent($param{bug},'log'));
623 return $param{days_until}?$age:0;
626 return $param{days_until}?0:1;
631 =head2 get_bug_status
633 my $status = get_bug_status(bug => $nnn);
635 my $status = get_bug_status($bug_num)
641 =item bug -- scalar bug number
643 =item status -- optional hashref of bug status as returned by readbug
644 (can be passed to avoid rereading the bug information)
646 =item bug_index -- optional tied index of bug status infomration;
647 currently not correctly implemented.
649 =item version -- optional version to check package status at
651 =item dist -- optional distribution to check package status at
653 =item arch -- optional architecture to check package status at
655 =item usertags -- optional hashref of usertags
657 =item sourceversion -- optional arrayref of source/version; overrides
658 dist, arch, and version. [The entries in this array must be in the
659 "source/version" format.] Eventually this can be used to for caching.
663 Note: Currently the version information is cached; this needs to be
664 changed before using this function in long lived programs.
672 my %param = validate_with(params => \@_,
673 spec => {bug => {type => SCALAR,
676 status => {type => HASHREF,
679 bug_index => {type => OBJECT,
682 version => {type => SCALAR,
685 dist => {type => SCALAR,
688 arch => {type => SCALAR,
691 usertags => {type => HASHREF,
694 sourceversions => {type => ARRAYREF,
701 if (defined $param{bug_index} and
702 exists $param{bug_index}{$param{bug}}) {
703 %status = %{ $param{bug_index}{$param{bug}} };
704 $status{pending} = $status{ status };
705 $status{id} = $param{bug};
708 if (defined $param{status}) {
709 %status = %{$param{status}};
712 my $location = getbuglocation($param{bug}, 'summary');
713 return {} if not length $location;
714 %status = %{ readbug( $param{bug}, $location ) };
716 $status{id} = $param{bug};
718 if (defined $param{usertags}{$param{bug}}) {
719 $status{keywords} = "" unless defined $status{keywords};
720 $status{keywords} .= " " unless $status{keywords} eq "";
721 $status{keywords} .= join(" ", @{$param{usertags}{$param{bug}}});
723 $status{tags} = $status{keywords};
724 my %tags = map { $_ => 1 } split ' ', $status{tags};
726 $status{"package"} =~ s/\s*$//;
727 $status{"package"} = 'unknown' if ($status{"package"} eq '');
728 $status{"severity"} = 'normal' if ($status{"severity"} eq '');
730 $status{"pending"} = 'pending';
731 $status{"pending"} = 'forwarded' if (length($status{"forwarded"}));
732 $status{"pending"} = 'pending-fixed' if ($tags{pending});
733 $status{"pending"} = 'fixed' if ($tags{fixed});
736 if (not exists $param{sourceversions}) {
738 if (defined $param{version}) {
739 @versions = ($param{version});
740 } elsif (defined $param{dist}) {
741 @versions = getversions($status{package}, $param{dist}, $param{arch});
744 # TODO: This should probably be handled further out for efficiency and
745 # for more ease of distinguishing between pkg= and src= queries.
746 @sourceversions = makesourceversions($status{package},
751 @sourceversions = @{$param{sourceversions}};
753 if (@sourceversions) {
754 my $maxbuggy = max_buggy(bug => $param{bug},
755 sourceversions => \@sourceversions,
756 found => $status{found_versions},
757 fixed => $status{fixed_versions},
758 package => $status{package},
759 version_cache => $version_cache,
761 if ($maxbuggy eq 'absent') {
762 $status{pending} = 'absent';
764 elsif ($maxbuggy eq 'fixed' ) {
765 $status{pending} = 'done';
768 if (length($status{done}) and
769 (not @sourceversions or not @{$status{fixed_versions}})) {
770 $status{"pending"} = 'done';
785 =item bug -- scalar bug number
787 =item sourceversion -- optional arrayref of source/version; overrides
788 dist, arch, and version. [The entries in this array must be in the
789 "source/version" format.] Eventually this can be used to for caching.
793 Note: Currently the version information is cached; this needs to be
794 changed before using this function in long lived programs.
799 my %param = validate_with(params => \@_,
800 spec => {bug => {type => SCALAR,
803 sourceversions => {type => ARRAYREF,
806 found => {type => ARRAYREF,
809 fixed => {type => ARRAYREF,
812 package => {type => SCALAR,
814 version_cache => {type => HASHREF,
819 # Resolve bugginess states (we might be looking at multiple
820 # architectures, say). Found wins, then fixed, then absent.
821 my $maxbuggy = 'absent';
822 for my $version (@{$param{sourceversions}}) {
823 my $buggy = buggy(bug => $param{bug},
825 found => $param{found},
826 fixed => $param{fixed},
827 version_cache => $param{version_cache},
828 package => $param{package},
830 if ($buggy eq 'found') {
832 } elsif ($buggy eq 'fixed') {
849 Returns the output of Debbugs::Versions::buggy for a particular
850 package, version and found/fixed set. Automatically turns found, fixed
851 and version into source/version strings.
853 Caching can be had by using the version_cache, but no attempt to check
854 to see if the on disk information is more recent than the cache is
855 made. [This will need to be fixed for long-lived processes.]
860 my %param = validate_with(params => \@_,
861 spec => {bug => {type => SCALAR,
864 found => {type => ARRAYREF,
867 fixed => {type => ARRAYREF,
870 version_cache => {type => HASHREF,
873 package => {type => SCALAR,
875 version => {type => SCALAR,
879 my @found = @{$param{found}};
880 my @fixed = @{$param{fixed}};
881 if (grep {$_ !~ m{/}} (@{$param{found}}, @{$param{fixed}})) {
882 # We have non-source version versions
883 @found = makesourceversions($param{package},undef,
886 @fixed = makesourceversions($param{package},undef,
890 if ($param{version} !~ m{/}) {
891 $param{version} = makesourceversions($param{package},undef,
895 # Figure out which source packages we need
897 @sources{map {m{(.+)/}; $1} @found} = (1) x @found;
898 @sources{map {m{(.+)/}; $1} @fixed} = (1) x @fixed;
899 @sources{map {m{(.+)/}; $1} $param{version}} = 1;
901 if (not defined $param{version_cache} or
902 not exists $param{version_cache}{join(',',sort keys %sources)}) {
903 $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
904 foreach my $source (keys %sources) {
905 my $srchash = substr $source, 0, 1;
906 my $version_fh = new IO::File "$config{version_packages_dir}/$srchash/$source", 'r' or
907 warn "Unable to open $config{version_packages_dir}/$srchash/$source: $!" and next;
908 $version->load($version_fh);
910 if (defined $param{version_cache}) {
911 $param{version_cache}{join(',',sort keys %sources)} = $version;
915 $version = $param{version_cache}{join(',',sort keys %sources)};
917 return $version->buggy($param{version},\@found,\@fixed);
920 sub isstrongseverity {
921 my $severity = shift;
922 $severity = $config{default_severity} if $severity eq '';
923 return grep { $_ eq $severity } @{$config{strong_severities}};
927 =head1 PRIVATE FUNCTIONS
931 sub update_realtime {
932 my ($file, $bug, $new) = @_;
934 # update realtime index.db
936 open(IDXDB, "<$file") or die "Couldn't open $file";
937 open(IDXNEW, ">$file.new");
941 while($line = <IDXDB>) {
942 @line = split /\s/, $line;
943 last if ($line[1] >= $bug);
948 if ($new eq "NOCHANGE") {
949 print IDXNEW $line if ($line ne "" and $line[1] == $bug);
950 } elsif ($new eq "REMOVE") {
955 if (defined $line and $line ne "" and @line and $line[1] > $bug) {
960 print IDXNEW while(<IDXDB>);
965 rename("$file.new", $file);
970 sub bughook_archive {
972 &filelock("debbugs.trace.lock");
973 &appendfile("debbugs.trace","archive $ref\n");
974 my $line = update_realtime(
975 "$config{spool_dir}/index.db.realtime",
978 update_realtime("$config{spool_dir}/index.archive.realtime",
984 my ( $type, $ref, $data ) = @_;
985 &filelock("debbugs.trace.lock");
987 &appendfile("debbugs.trace","$type $ref\n",makestatus($data, 1));
989 my $whendone = "open";
990 my $severity = $config{default_severity};
991 (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
994 $whendone = "forwarded" if defined $data->{forwarded} and length $data->{forwarded};
995 $whendone = "done" if defined $data->{done} and length $data->{done};
996 $severity = $data->{severity} if length $data->{severity};
998 my $k = sprintf "%s %d %d %s [%s] %s %s\n",
999 $pkglist, $ref, $data->{date}, $whendone,
1000 $data->{originator}, $severity, $data->{keywords};
1002 update_realtime("$config{spool_dir}/index.db.realtime", $ref, $k);