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);
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(addfoundversion addfixedversion),
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,
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}) {
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 $newdata{$field} ne '') {
283 # Output field names in proper case, e.g. 'Merged-With'.
284 my $properfield = $fields{$field};
285 $properfield =~ s/(?:^|(?<=-))([a-z])/\u$1/g;
286 $contents .= "$properfield: $newdata{$field}\n";
296 writebug($bug_num,$status,$location,$minversion,$disablebughook)
298 Writes the bug status and summary files out.
300 Skips writting out a status file if minversion is 2
302 Does not call bughook if disablebughook is true.
307 my ($ref, $data, $location, $minversion, $disablebughook) = @_;
310 my %outputs = (1 => 'status', 2 => 'summary');
311 for my $version (keys %outputs) {
312 next if defined $minversion and $version < $minversion;
313 my $status = getbugcomponent($ref, $outputs{$version}, $location);
314 &quit("can't find location for $ref") unless defined $status;
315 open(S,"> $status.new") || &quit("opening $status.new: $!");
316 print(S makestatus($data, $version)) ||
317 &quit("writing $status.new: $!");
318 close(S) || &quit("closing $status.new: $!");
324 rename("$status.new",$status) || &quit("installing new $status: $!");
327 # $disablebughook is a bit of a hack to let format migration scripts use
328 # this function rather than having to duplicate it themselves.
329 &bughook($change,$ref,$data) unless $disablebughook;
332 =head2 unlockwritebug
334 unlockwritebug($bug_num,$status,$location,$minversion,$disablebughook);
336 Writes a bug, then calls unfilelock; see writebug for what these
348 The following functions are exported with the :versions tag
350 =head2 addfoundversions
352 addfoundversions($status,$package,$version,$isbinary);
359 sub addfoundversions {
363 my $isbinary = shift;
364 return unless defined $version;
365 undef $package if $package =~ m[(?:\s|/)];
366 my $source = $package;
368 if (defined $package and $isbinary) {
369 my @srcinfo = binarytosource($package, $version, undef);
371 # We know the source package(s). Use a fully-qualified version.
372 addfoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
375 # Otherwise, an unqualified version will have to do.
379 # Strip off various kinds of brain-damage.
381 $version =~ s/ *\(.*\)//;
382 $version =~ s/ +[A-Za-z].*//;
384 foreach my $ver (split /[,\s]+/, $version) {
385 my $sver = defined($source) ? "$source/$ver" : '';
386 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{found_versions}}) {
387 push @{$data->{found_versions}}, defined($source) ? $sver : $ver;
389 @{$data->{fixed_versions}} =
390 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
394 =head2 removefoundversions
396 removefoundversions($data,$package,$versiontoremove)
398 Removes found versions from $data
400 If a version is fully qualified (contains /) only versions matching
401 exactly are removed. Otherwise, all versions matching the version
404 Currently $package and $isbinary are entirely ignored, but accepted
405 for backwards compatibilty.
409 sub removefoundversions {
413 my $isbinary = shift;
414 return unless defined $version;
416 foreach my $ver (split /[,\s]+/, $version) {
418 # fully qualified version
419 @{$data->{found_versions}} =
421 @{$data->{found_versions}};
424 # non qualified version; delete all matchers
425 @{$data->{found_versions}} =
426 grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
427 @{$data->{found_versions}};
433 sub addfixedversions {
437 my $isbinary = shift;
438 return unless defined $version;
439 undef $package if $package =~ m[(?:\s|/)];
440 my $source = $package;
442 if (defined $package and $isbinary) {
443 my @srcinfo = binarytosource($package, $version, undef);
445 # We know the source package(s). Use a fully-qualified version.
446 addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
449 # Otherwise, an unqualified version will have to do.
453 # Strip off various kinds of brain-damage.
455 $version =~ s/ *\(.*\)//;
456 $version =~ s/ +[A-Za-z].*//;
458 foreach my $ver (split /[,\s]+/, $version) {
459 my $sver = defined($source) ? "$source/$ver" : '';
460 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{fixed_versions}}) {
461 push @{$data->{fixed_versions}}, defined($source) ? $sver : $ver;
463 @{$data->{found_versions}} =
464 grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
468 sub removefixedversions {
472 my $isbinary = shift;
473 return unless defined $version;
474 undef $package if $package =~ m[(?:\s|/)];
475 my $source = $package;
477 if (defined $package and $isbinary) {
478 my @srcinfo = binarytosource($package, $version, undef);
480 # We know the source package(s). Use a fully-qualified version.
481 removefixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
484 # Otherwise, an unqualified version will have to do.
488 foreach my $ver (split /[,\s]+/, $version) {
489 my $sver = defined($source) ? "$source/$ver" : '';
490 @{$data->{fixed_versions}} =
491 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
501 Split a package string from the status file into a list of package names.
507 return unless defined $pkgs;
508 return map lc, split /[ \t?,()]+/, $pkgs;
512 =head2 bug_archiveable
514 bug_archiveable(bug => $bug_num);
520 =item bug -- bug number (required)
522 =item status -- Status hashref returned by read_bug or get_bug_status (optional)
524 =item version -- Debbugs::Version information (optional)
526 =item days_until -- return days until the bug can be archived
530 Returns 1 if the bug can be archived
531 Returns 0 if the bug cannot be archived
533 If days_until is true, returns the number of days until the bug can be
534 archived, -1 if it cannot be archived. 0 means that the bug can be
535 archived the next time the archiver runs.
537 Returns undef on failure.
541 # This will eventually need to be fixed before we start using mod_perl
542 my $version_cache = {};
544 my %param = validate_with(params => \@_,
545 spec => {bug => {type => SCALAR,
548 status => {type => HASHREF,
551 days_until => {type => BOOLEAN,
556 # This is what we return if the bug cannot be archived.
557 my $cannot_archive = $param{days_until}?-1:0;
558 # read the status information
559 my $status = $param{status};
560 if (not exists $param{status} or not defined $status) {
561 $status = read_bug(bug=>$param{bug});
562 return undef if not defined $status;
564 # Bugs can be archived if they are
566 return $cannot_archive if not defined $status->{done} or not length $status->{done};
567 # If we just are checking if the bug can be archived, we'll not even bother
568 # checking the versioning information if the bug has been -done for less than 28 days.
569 if (not $param{days_until} and $config{remove_age} >
570 -M getbugcomponent($param{ref},'log')
572 return $cannot_archive;
574 # At this point, we have to get the versioning information for this bug.
575 # We examine the set of distribution tags. If a bug has no distribution
576 # tags set, we assume a default set, otherwise we use the tags the bug
579 # There must be fixed_versions for us to look at the versioning
581 if (@{$status->{fixed_versions}}) {
583 @dist_tags{@{$config{removal_distribution_tags}}} =
584 (1) x @{$config{removal_distribution_tags}};
586 @dists{@{$config{removal_default_distribution_tags}}} =
587 (1) x @{$config{removal_default_distribution_tags}};
588 for my $tag (split ' ', $status->{tags}) {
589 next unless $dist_tags{$tag};
593 for my $dist (keys %dists){
595 @versions = getversions($status->{package},
598 # TODO: This should probably be handled further out for efficiency and
599 # for more ease of distinguishing between pkg= and src= queries.
600 my @sourceversions = makesourceversions($status->{package},
603 @source_versions{@sourceversions} = (1) x @sourceversions;
605 if ('found' eq max_buggy(bug => $param{bug},
606 sourceversions => [keys %source_versions],
607 found => $status->{found_versions},
608 fixed => $status->{fixed_versions},
609 version_cache => $version_cache,
610 package => $status->{package},
612 return $cannot_archive;
615 # 6. at least 28 days have passed since the last action has occured or the bug was closed
616 # XXX We still need some more work here before we actually can archive;
617 # we really need to track when a bug was closed in a version.
618 my $age = ceil($config{remove_age} - -M getbugcomponent($param{bug},'log'));
620 return $param{days_until}?$age:0;
623 return $param{days_until}?0:1;
628 =head2 get_bug_status
630 my $status = get_bug_status(bug => $nnn);
632 my $status = get_bug_status($bug_num)
638 =item bug -- scalar bug number
640 =item status -- optional hashref of bug status as returned by readbug
641 (can be passed to avoid rereading the bug information)
643 =item bug_index -- optional tied index of bug status infomration;
644 currently not correctly implemented.
646 =item version -- optional version to check package status at
648 =item dist -- optional distribution to check package status at
650 =item arch -- optional architecture to check package status at
652 =item usertags -- optional hashref of usertags
654 =item sourceversion -- optional arrayref of source/version; overrides
655 dist, arch, and version. [The entries in this array must be in the
656 "source/version" format.] Eventually this can be used to for caching.
660 Note: Currently the version information is cached; this needs to be
661 changed before using this function in long lived programs.
669 my %param = validate_with(params => \@_,
670 spec => {bug => {type => SCALAR,
673 status => {type => HASHREF,
676 bug_index => {type => OBJECT,
679 version => {type => SCALAR,
682 dist => {type => SCALAR,
685 arch => {type => SCALAR,
688 usertags => {type => HASHREF,
691 sourceversions => {type => ARRAYREF,
698 if (defined $param{bug_index} and
699 exists $param{bug_index}{$param{bug}}) {
700 %status = %{ $param{bug_index}{$param{bug}} };
701 $status{pending} = $status{ status };
702 $status{id} = $param{bug};
705 if (defined $param{status}) {
706 %status = %{$param{status}};
709 my $location = getbuglocation($param{bug}, 'summary');
710 return {} if not length $location;
711 %status = %{ readbug( $param{bug}, $location ) };
713 $status{id} = $param{bug};
715 if (defined $param{usertags}{$param{bug}}) {
716 $status{keywords} = "" unless defined $status{keywords};
717 $status{keywords} .= " " unless $status{keywords} eq "";
718 $status{keywords} .= join(" ", @{$param{usertags}{$param{bug}}});
720 $status{tags} = $status{keywords};
721 my %tags = map { $_ => 1 } split ' ', $status{tags};
723 $status{"package"} =~ s/\s*$//;
724 $status{"package"} = 'unknown' if ($status{"package"} eq '');
725 $status{"severity"} = 'normal' if ($status{"severity"} eq '');
727 $status{"pending"} = 'pending';
728 $status{"pending"} = 'forwarded' if (length($status{"forwarded"}));
729 $status{"pending"} = 'pending-fixed' if ($tags{pending});
730 $status{"pending"} = 'fixed' if ($tags{fixed});
733 if (not exists $param{sourceversions}) {
735 if (defined $param{version}) {
736 @versions = ($param{version});
737 } elsif (defined $param{dist}) {
738 @versions = getversions($status{package}, $param{dist}, $param{arch});
741 # TODO: This should probably be handled further out for efficiency and
742 # for more ease of distinguishing between pkg= and src= queries.
743 @sourceversions = makesourceversions($status{package},
748 @sourceversions = @{$param{sourceversions}};
750 if (@sourceversions) {
751 my $maxbuggy = max_buggy(bug => $param{bug},
752 sourceversions => \@sourceversions,
753 found => $status{found_versions},
754 fixed => $status{fixed_versions},
755 package => $status{package},
756 version_cache => $version_cache,
758 if ($maxbuggy eq 'absent') {
759 $status{pending} = 'absent';
761 elsif ($maxbuggy eq 'fixed' ) {
762 $status{pending} = 'done';
765 if (length($status{done}) and
766 (not @sourceversions or not @{$status{fixed_versions}})) {
767 $status{"pending"} = 'done';
782 =item bug -- scalar bug number
784 =item sourceversion -- optional arrayref of source/version; overrides
785 dist, arch, and version. [The entries in this array must be in the
786 "source/version" format.] Eventually this can be used to for caching.
790 Note: Currently the version information is cached; this needs to be
791 changed before using this function in long lived programs.
796 my %param = validate_with(params => \@_,
797 spec => {bug => {type => SCALAR,
800 sourceversions => {type => ARRAYREF,
803 found => {type => ARRAYREF,
806 fixed => {type => ARRAYREF,
809 package => {type => SCALAR,
811 version_cache => {type => HASHREF,
816 # Resolve bugginess states (we might be looking at multiple
817 # architectures, say). Found wins, then fixed, then absent.
818 my $maxbuggy = 'absent';
819 for my $version (@{$param{sourceversions}}) {
820 my $buggy = buggy(bug => $param{bug},
822 found => $param{found},
823 fixed => $param{fixed},
824 version_cache => $param{version_cache},
825 package => $param{package},
827 if ($buggy eq 'found') {
829 } elsif ($buggy eq 'fixed') {
846 Returns the output of Debbugs::Versions::buggy for a particular
847 package, version and found/fixed set. Automatically turns found, fixed
848 and version into source/version strings.
850 Caching can be had by using the version_cache, but no attempt to check
851 to see if the on disk information is more recent than the cache is
852 made. [This will need to be fixed for long-lived processes.]
857 my %param = validate_with(params => \@_,
858 spec => {bug => {type => SCALAR,
861 found => {type => ARRAYREF,
864 fixed => {type => ARRAYREF,
867 version_cache => {type => HASHREF,
870 package => {type => SCALAR,
872 version => {type => SCALAR,
876 my @found = @{$param{found}};
877 my @fixed = @{$param{fixed}};
878 if (grep {$_ !~ m{/}} (@{$param{found}}, @{$param{fixed}})) {
879 # We have non-source version versions
880 @found = makesourceversions($param{package},undef,
883 @fixed = makesourceversions($param{package},undef,
887 if ($param{version} !~ m{/}) {
888 $param{version} = makesourceversions($param{package},undef,
892 # Figure out which source packages we need
894 @sources{map {m{(.+)/}; $1} @found} = (1) x @found;
895 @sources{map {m{(.+)/}; $1} @fixed} = (1) x @fixed;
896 @sources{map {m{(.+)/}; $1} $param{version}} = 1;
898 if (not defined $param{version_cache} or
899 not exists $param{version_cache}{join(',',sort keys %sources)}) {
900 $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
901 foreach my $source (keys %sources) {
902 my $srchash = substr $source, 0, 1;
903 my $version_fh = new IO::File "$config{version_packages_dir}/$srchash/$source", 'r' or
904 warn "Unable to open $config{version_packages_dir}/$srchash/$source: $!" and next;
905 $version->load($version_fh);
907 if (defined $param{version_cache}) {
908 $param{version_cache}{join(',',sort keys %sources)} = $version;
912 $version = $param{version_cache}{join(',',sort keys %sources)};
914 return $version->buggy($param{version},\@found,\@fixed);
917 sub isstrongseverity {
918 my $severity = shift;
919 $severity = $config{default_severity} if $severity eq '';
920 return grep { $_ eq $severity } @{$config{strong_severities}};
924 =head1 PRIVATE FUNCTIONS
928 sub update_realtime {
929 my ($file, $bug, $new) = @_;
931 # update realtime index.db
933 open(IDXDB, "<$file") or die "Couldn't open $file";
934 open(IDXNEW, ">$file.new");
938 while($line = <IDXDB>) {
939 @line = split /\s/, $line;
940 last if ($line[1] >= $bug);
945 if ($new eq "NOCHANGE") {
946 print IDXNEW $line if ($line ne "" && $line[1] == $bug);
947 } elsif ($new eq "REMOVE") {
952 if ($line ne "" && $line[1] > $bug) {
957 print IDXNEW while(<IDXDB>);
962 rename("$file.new", $file);
967 sub bughook_archive {
969 &filelock("debbugs.trace.lock");
970 &appendfile("debbugs.trace","archive $ref\n");
971 my $line = update_realtime(
972 "$config{spool_dir}/index.db.realtime",
975 update_realtime("$config{spool_dir}/index.archive.realtime",
981 my ( $type, $ref, $data ) = @_;
982 &filelock("debbugs.trace.lock");
984 &appendfile("debbugs.trace","$type $ref\n",makestatus($data, 1));
986 my $whendone = "open";
987 my $severity = $config{default_severity};
988 (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
991 $whendone = "forwarded" if length $data->{forwarded};
992 $whendone = "done" if length $data->{done};
993 $severity = $data->{severity} if length $data->{severity};
995 my $k = sprintf "%s %d %d %s [%s] %s %s\n",
996 $pkglist, $ref, $data->{date}, $whendone,
997 $data->{originator}, $severity, $data->{keywords};
999 update_realtime("$config{spool_dir}/index.db.realtime", $ref, $k);