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.
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 @dist_tags{@{$config{removal_distribution_tags}}} =
581 (1) x @{$config{removal_distribution_tags}};
583 @dists{@{$config{removal_default_distribution_tags}}} =
584 (1) x @{$config{removal_default_distribution_tags}};
585 for my $tag (split ' ', $status->{tags}) {
586 next unless $dist_tags{$tag};
590 for my $dist (keys %dists){
592 @versions = getversions($status->{package},
595 # TODO: This should probably be handled further out for efficiency and
596 # for more ease of distinguishing between pkg= and src= queries.
597 my @sourceversions = makesourceversions($status->{package},
600 @source_versions{@sourceversions} = (1) x @sourceversions;
602 if ('found' eq max_buggy(bug => $param{bug},
603 sourceversions => [keys %source_versions],
604 found => $status->{found_versions},
605 fixed => $status->{fixed_versions},
606 version_cache => $version_cache,
607 package => $status->{package},
609 return $cannot_archive;
611 # 6. at least 28 days have passed since the last action has occured or the bug was closed
612 # XXX We still need some more work here before we actually can archive;
613 # we really need to track when a bug was closed in a version.
614 my $age = ceil($config{remove_age} - -M getbugcomponent($param{bug},'log'));
616 return $param{days_until}?$age:0;
619 return $param{days_until}?0:1;
624 =head2 get_bug_status
626 my $status = get_bug_status(bug => $nnn);
628 my $status = get_bug_status($bug_num)
634 =item bug -- scalar bug number
636 =item status -- optional hashref of bug status as returned by readbug
637 (can be passed to avoid rereading the bug information)
639 =item bug_index -- optional tied index of bug status infomration;
640 currently not correctly implemented.
642 =item version -- optional version to check package status at
644 =item dist -- optional distribution to check package status at
646 =item arch -- optional architecture to check package status at
648 =item usertags -- optional hashref of usertags
650 =item sourceversion -- optional arrayref of source/version; overrides
651 dist, arch, and version. [The entries in this array must be in the
652 "source/version" format.] Eventually this can be used to for caching.
656 Note: Currently the version information is cached; this needs to be
657 changed before using this function in long lived programs.
665 my %param = validate_with(params => \@_,
666 spec => {bug => {type => SCALAR,
669 status => {type => HASHREF,
672 bug_index => {type => OBJECT,
675 version => {type => SCALAR,
678 dist => {type => SCALAR,
681 arch => {type => SCALAR,
684 usertags => {type => HASHREF,
687 sourceversions => {type => ARRAYREF,
694 if (defined $param{bug_index} and
695 exists $param{bug_index}{$param{bug}}) {
696 %status = %{ $param{bug_index}{$param{bug}} };
697 $status{pending} = $status{ status };
698 $status{id} = $param{bug};
701 if (defined $param{status}) {
702 %status = %{$param{status}};
705 my $location = getbuglocation($param{bug}, 'summary');
706 return {} if not length $location;
707 %status = %{ readbug( $param{bug}, $location ) };
709 $status{id} = $param{bug};
711 if (defined $param{usertags}{$param{bug}}) {
712 $status{keywords} = "" unless defined $status{keywords};
713 $status{keywords} .= " " unless $status{keywords} eq "";
714 $status{keywords} .= join(" ", @{$param{usertags}{$param{bug}}});
716 $status{tags} = $status{keywords};
717 my %tags = map { $_ => 1 } split ' ', $status{tags};
719 $status{"package"} =~ s/\s*$//;
720 $status{"package"} = 'unknown' if ($status{"package"} eq '');
721 $status{"severity"} = 'normal' if ($status{"severity"} eq '');
723 $status{"pending"} = 'pending';
724 $status{"pending"} = 'forwarded' if (length($status{"forwarded"}));
725 $status{"pending"} = 'pending-fixed' if ($tags{pending});
726 $status{"pending"} = 'fixed' if ($tags{fixed});
729 if (not exists $param{sourceversions}) {
731 if (defined $param{version}) {
732 @versions = ($param{version});
733 } elsif (defined $param{dist}) {
734 @versions = getversions($status{package}, $param{dist}, $param{arch});
737 # TODO: This should probably be handled further out for efficiency and
738 # for more ease of distinguishing between pkg= and src= queries.
739 @sourceversions = makesourceversions($status{package},
744 @sourceversions = @{$param{sourceversions}};
746 if (@sourceversions) {
747 my $maxbuggy = max_buggy(bug => $param{bug},
748 sourceversions => \@sourceversions,
749 found => $status{found_versions},
750 fixed => $status{fixed_versions},
751 package => $status{package},
752 version_cache => $version_cache,
754 if ($maxbuggy eq 'absent') {
755 $status{pending} = 'absent';
757 elsif ($maxbuggy eq 'fixed' ) {
758 $status{pending} = 'done';
761 if (length($status{done}) and
762 (not @sourceversions or not @{$status{fixed_versions}})) {
763 $status{"pending"} = 'done';
778 =item bug -- scalar bug number
780 =item sourceversion -- optional arrayref of source/version; overrides
781 dist, arch, and version. [The entries in this array must be in the
782 "source/version" format.] Eventually this can be used to for caching.
786 Note: Currently the version information is cached; this needs to be
787 changed before using this function in long lived programs.
792 my %param = validate_with(params => \@_,
793 spec => {bug => {type => SCALAR,
796 sourceversions => {type => ARRAYREF,
799 found => {type => ARRAYREF,
802 fixed => {type => ARRAYREF,
805 package => {type => SCALAR,
807 version_cache => {type => HASHREF,
812 # Resolve bugginess states (we might be looking at multiple
813 # architectures, say). Found wins, then fixed, then absent.
814 my $maxbuggy = 'absent';
815 for my $version (@{$param{sourceversions}}) {
816 my $buggy = buggy(bug => $param{bug},
818 found => $param{found},
819 fixed => $param{fixed},
820 version_cache => $param{version_cache},
821 package => $param{package},
823 if ($buggy eq 'found') {
825 } elsif ($buggy eq 'fixed') {
842 Returns the output of Debbugs::Versions::buggy for a particular
843 package, version and found/fixed set. Automatically turns found, fixed
844 and version into source/version strings.
846 Caching can be had by using the version_cache, but no attempt to check
847 to see if the on disk information is more recent than the cache is
848 made. [This will need to be fixed for long-lived processes.]
853 my %param = validate_with(params => \@_,
854 spec => {bug => {type => SCALAR,
857 found => {type => ARRAYREF,
860 fixed => {type => ARRAYREF,
863 version_cache => {type => HASHREF,
866 package => {type => SCALAR,
868 version => {type => SCALAR,
872 my @found = @{$param{found}};
873 my @fixed = @{$param{fixed}};
874 if (grep {$_ !~ m{/}} (@{$param{found}}, @{$param{fixed}})) {
875 # We have non-source version versions
876 @found = makesourceversions($param{package},undef,
879 @fixed = makesourceversions($param{package},undef,
883 if ($param{version} !~ m{/}) {
884 $param{version} = makesourceversions($param{package},undef,
888 # Figure out which source packages we need
890 @sources{map {m{(.+)/}; $1} @found} = (1) x @found;
891 @sources{map {m{(.+)/}; $1} @fixed} = (1) x @fixed;
892 @sources{map {m{(.+)/}; $1} $param{version}} = 1;
894 if (not defined $param{version_cache} or
895 not exists $param{version_cache}{join(',',sort keys %sources)}) {
896 $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
897 foreach my $source (keys %sources) {
898 my $srchash = substr $source, 0, 1;
899 my $version_fh = new IO::File "$config{version_packages_dir}/$srchash/$source", 'r';
900 $version->load($version_fh);
902 if (defined $param{version_cache}) {
903 $param{version_cache}{join(',',sort keys %sources)} = $version;
907 $version = $param{version_cache}{join(',',sort keys %sources)};
909 return $version->buggy($param{version},\@found,\@fixed);
912 sub isstrongseverity {
913 my $severity = shift;
914 $severity = $config{default_severity} if $severity eq '';
915 return grep { $_ eq $severity } @{$config{strong_severities}};
919 =head1 PRIVATE FUNCTIONS
923 sub update_realtime {
924 my ($file, $bug, $new) = @_;
926 # update realtime index.db
928 open(IDXDB, "<$file") or die "Couldn't open $file";
929 open(IDXNEW, ">$file.new");
933 while($line = <IDXDB>) {
934 @line = split /\s/, $line;
935 last if ($line[1] >= $bug);
940 if ($new eq "NOCHANGE") {
941 print IDXNEW $line if ($line ne "" && $line[1] == $bug);
942 } elsif ($new eq "REMOVE") {
947 if ($line ne "" && $line[1] > $bug) {
952 print IDXNEW while(<IDXDB>);
957 rename("$file.new", $file);
962 sub bughook_archive {
964 &filelock("debbugs.trace.lock");
965 &appendfile("debbugs.trace","archive $ref\n");
966 my $line = update_realtime(
967 "$config{spool_dir}/index.db.realtime",
970 update_realtime("$config{spool_dir}/index.archive.realtime",
976 my ( $type, $ref, $data ) = @_;
977 &filelock("debbugs.trace.lock");
979 &appendfile("debbugs.trace","$type $ref\n",makestatus($data, 1));
981 my $whendone = "open";
982 my $severity = $config{default_severity};
983 (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
986 $whendone = "forwarded" if length $data->{forwarded};
987 $whendone = "done" if length $data->{done};
988 $severity = $data->{severity} if length $data->{severity};
990 my $k = sprintf "%s %d %d %s [%s] %s %s\n",
991 $pkglist, $ref, $data->{date}, $whendone,
992 $data->{originator}, $severity, $data->{keywords};
994 update_realtime("$config{spool_dir}/index.db.realtime", $ref, $k);