]> git.donarmstrong.com Git - debbugs.git/commitdiff
* Rewrite binary_to_source to be more complete and understandable
authorDon Armstrong <don@donarmstrong.com>
Thu, 10 Sep 2009 01:54:21 +0000 (18:54 -0700)
committerDon Armstrong <don@donarmstrong.com>
Thu, 10 Sep 2009 01:54:21 +0000 (18:54 -0700)
 * Update Debbugs::SOAP to use binary_to_source; increment compatibility version
 * Change all usages of binarytosource to use binary_to_source

Debbugs/Packages.pm
Debbugs/Recipients.pm
Debbugs/SOAP.pm
Debbugs/Status.pm
scripts/process

index 9a632fc53e6663ae51056d4832f662914f212266..8ef34515b3776f808222b0cc5704c42e1a12c457 100644 (file)
@@ -25,7 +25,7 @@ BEGIN {
      @EXPORT = ();
      %EXPORT_TAGS = (versions => [qw(getversions get_versions make_source_versions)],
                     mapping  => [qw(getpkgsrc getpkgcomponent getsrcpkgs),
-                                 qw(binarytosource sourcetobinary makesourceversions)
+                                 qw(binary_to_source sourcetobinary makesourceversions)
                                 ],
                    );
      @EXPORT_OK = ();
@@ -121,83 +121,160 @@ sub getsrcpkgs {
     return @{$_srcpkg->{$src}};
 }
 
-=head2 binarytosource
+=head2 binary_to_source
 
-Returns a reference to the source package name and version pair
-corresponding to a given binary package name, version, and architecture.
+     binary_to_source(package => 'foo',
+                      version => '1.2.3',
+                      arch    => 'i386');
 
-If undef is passed as the architecture, returns a list of references
-to all possible pairs of source package names and versions for all
-architectures, with any duplicates removed.
 
-If the binary version is not passed either, returns a list of possible
-source package names for all architectures at all versions, with any
-duplicates removed.
+Turn a binary package (at optional version in optional architecture)
+into a single (or set) of source packages (optionally) with associated
+versions.
+
+By default, in LIST context, returns a LIST of array refs of source
+package, source version pairs corresponding to the binary package(s),
+arch(s), and verion(s) passed.
+
+In SCALAR context, only the corresponding source packages are
+returned, concatenated with ', ' if necessary.
+
+=over
+
+=item binary -- binary package name(s) as a SCALAR or ARRAYREF
+
+=item version -- binary package version(s) as a SCALAR or ARRAYREF;
+optional, defaults to all versions.
+
+=item arch -- binary package architecture(s) as a SCALAR or ARRAYREF;
+optional, defaults to all architectures.
+
+=item source_only -- return only the source name (forced on if in
+SCALAR context), defaults to false.
+
+=item scalar_only -- return a scalar only (forced true if in SCALAR
+context, also causes source_only to be true), defaults to false.
+
+=item cache -- optional HASHREF to be used to cache results of
+binary_to_source.
+
+=back
 
 =cut
 
 our %_binarytosource;
-sub binarytosource {
-    my ($binname, $binver, $binarch) = @_;
+sub binary_to_source{
+    my %param = validate_with(params => \@_,
+                             spec   => {binary => {type => SCALAR|ARRAYREF,
+                                                   },
+                                        version => {type => SCALAR|ARRAYREF,
+                                                    optional => 1,
+                                                   },
+                                        arch    => {type => SCALAR|ARRAYREF,
+                                                    optional => 1,
+                                                   },
+                                        source_only => {default => 0,
+                                                       },
+                                        scalar_only => {default => 0,
+                                                       },
+                                        cache => {type => HASHREF,
+                                                  default => {},
+                                                 },
+                                       },
+                            );
 
     # TODO: This gets hit a lot, especially from buggyversion() - probably
     # need an extra cache for speed here.
     return () unless defined $gBinarySourceMap;
 
-    if ($binname =~ m/^src:(.+)$/) {
-       return $1;
-    }
-    if (not tied %_binarytosource) {
-        tie %_binarytosource, MLDBM => $gBinarySourceMap, O_RDONLY or
-             die "Unable to open $gBinarySourceMap for reading";
+    if ($param{scalar_only} or not wantarray) {
+       $param{source_only} = 1;
+       $param{scalar_only} = 1;
     }
 
-    # avoid autovivification
-    my $binary = $_binarytosource{$binname};
-    return () unless defined $binary;
-    my %binary = %{$binary};
-    if (not defined $binver) {
-        my %uniq;
-        for my $ver (keys %binary) {
-             for my $ar (keys %{$binary{$ver}}) {
-                  my $src = $binary{$ver}{$ar};
-                  next unless defined $src;
-                  $uniq{$src->[0]} = 1;
-             }
-        }
-        return keys %uniq;
+    my @source;
+    my @packages = grep {defined $_} make_list(exists $param{package}?$param{package}:[]);
+    my @versions = grep {defined $_} make_list(exists $param{version}?$param{version}:[]);
+    my @archs = grep {defined $_} make_list(exists $param{arch}?$param{arch}:[]);
+    return () unless @packages;
+    my $cache_key = join("\1",
+                        join("\0",@packages),
+                        join("\0",@versions),
+                        join("\0",@archs),
+                        join("\0",@param{qw(source_only scalar_only)}));
+    if (exists $param{cache}{$cache_key}) {
+       return $param{scalar_only} ? $param{cache}{$cache_key}[0]:
+           @{$param{cache}{$cache_key}};
+    }
+    for my $package (make_list($param{package})) {
+       if ($package =~ m/^src:(.+)$/) {
+           push @source,[$1,''];
+           next;
+       }
+       if (not tied %_binarytosource) {
+           tie %_binarytosource, MLDBM => $config{binary_source_map}, O_RDONLY or
+               die "Unable to open $config{binary_source_map} for reading";
+       }
+       # avoid autovivification
+       my $binary = $_binarytosource{$package};
+       if (not @versions) {
+           next unless defined $binary;
+           for my $ver (keys %{$binary}) {
+               for my $ar (keys %{$binary->{$ver}}) {
+                   my $src = $binary->{$ver}{$ar};
+                   next unless defined $src;
+                   push @source,[$src->[0],$src->[1]];
+               }
+           }
+       }
+       else {
+           my $found_one_version = 0;
+           for my $version (@versions) {
+               next unless exists $binary->{$version};
+               if (exists $binary->{$version}{all}) {
+                   push @source,dclone($binary->{$version}{all});
+                   next;
+               }
+               my @t_archs;
+               if (@archs) {
+                   @t_archs = @archs;
+               }
+               else {
+                   @t_archs = keys %{$binary->{$version}};
+               }
+               for my $arch (@t_archs) {
+                   push @source,dclone($binary->{$version}{$arch}) if
+                       exists $binary->{$version}{$arch};
+               }
+           }
+       }
+    }
+    my @result;
+
+    if ($param{source_only}) {
+       my %uniq;
+       for my $s (@source) {
+           $uniq{$s->[0]} = 1;
+       }
+       @result = sort keys %uniq;
+       if ($param{scalar_only}) {
+           @result = join(', ',@result);
+       }
     }
-    elsif (exists $binary{$binver}) {
-        if (defined $binarch and length $binarch) {
-             my $src = $binary{$binver}{$binarch};
-             if (not defined $src and exists $binary{$binver}{all}) {
-                 $src = $binary{$binver}{all};
-             }
-             return () unless defined $src; # not on this arch
-             # Copy the data to avoid tiedness problems.
-             return dclone($src);
-        } else {
-             # Get (srcname, srcver) pairs for all architectures and
-             # remove any duplicates. This involves some slightly tricky
-             # multidimensional hashing; sorry. Fortunately there'll
-             # usually only be one pair returned.
-             my %uniq;
-             for my $ar (keys %{$binary{$binver}}) {
-                  my $src = $binary{$binver}{$ar};
-                  next unless defined $src;
-                  $uniq{$src->[0]}{$src->[1]} = 1;
-             }
-             my @uniq;
-             for my $sn (sort keys %uniq) {
-                  push @uniq, [$sn, $_] for sort keys %{$uniq{$sn}};
-             }
-             return @uniq;
-        }
+    else {
+       my %uniq;
+       for my $s (@source) {
+           $uniq{$s->[0]}{$s->[1]} = 1;
+       }
+       for my $sn (sort keys %uniq) {
+           push @result, [$sn, $_] for sort keys %{$uniq{$sn}};
+       }
     }
 
     # No $gBinarySourceMap, or it didn't have an entry for this name and
     # version.
-    return ();
+    $param{cache}{$cache_key} = \@result;
+    return $param{scalar_only} ? $result[0] : @result;
 }
 
 =head2 sourcetobinary
@@ -504,7 +581,9 @@ sub make_source_versions {
                        }
                        next;
                    }
-                   my @srcinfo = binarytosource($pkg, $version, $arch);
+                   my @srcinfo = binary_to_source(binary => $pkg,
+                                                  version => $version,
+                                                  arch    => $arch);
                    if (not @srcinfo) {
                        # We don't have explicit information about the
                        # binary-to-source mapping for this version
index 9421a6008e835c4ea235876f00a862faad472339..a06e69249f4f41877ea92613b4aabf40cf6cb192 100644 (file)
@@ -46,7 +46,7 @@ use Params::Validate qw(:types validate_with);
 use Debbugs::Common qw(:misc :util);
 use Debbugs::Status qw(splitpackages isstrongseverity);
 
-use Debbugs::Packages qw(binarytosource);
+use Debbugs::Packages qw(binary_to_source);
 
 use Debbugs::Mail qw(get_addresses);
 
@@ -114,7 +114,9 @@ sub add_recipients {
      for my $p (splitpackages($param{data}{package})) {
          $p = lc($p);
          if (defined $config{subscription_domain}) {
-              my @source_packages = binarytosource($p);
+              my @source_packages = binary_to_source(binary => $p,
+                                                     source_only => 1,
+                                                    );
               if (@source_packages) {
                    for my $source (@source_packages) {
                         _add_address(recipients => $param{recipients},
index 32e874e0b366626982412b5e6609add1a67c67e1..db066bfd870d4c67902b3f51ad5a2b349556b072 100644 (file)
@@ -47,7 +47,7 @@ use Storable qw(nstore retrieve);
 use Scalar::Util qw(looks_like_number);
 
 
-our $CURRENT_VERSION = 1;
+our $CURRENT_VERSION = 2;
 
 =head2 get_usertag
 
@@ -262,7 +262,12 @@ architecture. If undef is passed as the architecture, returns a list
 of references to all possible pairs of source package names and
 versions for all architectures, with any duplicates removed.
 
-(This function corresponds to L<Debbugs::Packages::binarytosource>)
+As of comaptibility version 2, this has changed to use the more
+powerful binary_to_source routine, which allows returning source only,
+concatenated scalars, and other useful features.
+
+See the documentation of L<Debbugs::Packages::binary_to_source> for
+details.
 
 =cut
 
@@ -270,7 +275,15 @@ sub binary_to_source{
      my $VERSION = __populate_version(pop);
      my ($self,@params) = @_;
 
-     return [Debbugs::Packages::binarytosource(@params)];
+     if ($VERSION <= 1) {
+        return [Debbugs::Packages::binary_to_source(binary => $params[0],
+                                                    (@params > 1)?(version => $params[1]):(),
+                                                    (@params > 2)?(arch    => $params[2]):(),
+                                                   )];
+     }
+     else {
+        return [Debbugs::Packages::binary_to_source(@params)];
+     }
 }
 
 =head2 source_to_binary
index 00516b404d30ddd0bfbf56f954ea6e5fdcc119d0..05275403fee04b8eec19d24d43a871e28a3a514b 100644 (file)
@@ -40,7 +40,7 @@ use Params::Validate qw(validate_with :types);
 use Debbugs::Common qw(:util :lock :quit :misc);
 use Debbugs::Config qw(:config);
 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
-use Debbugs::Packages qw(makesourceversions make_source_versions getversions get_versions binarytosource);
+use Debbugs::Packages qw(makesourceversions make_source_versions getversions get_versions binary_to_source);
 use Debbugs::Versions;
 use Debbugs::Versions::Dpkg;
 use POSIX qw(ceil);
@@ -623,7 +623,8 @@ sub addfoundversions {
     my $source = $package;
 
     if (defined $package and $isbinary) {
-        my @srcinfo = binarytosource($package, $version, undef);
+        my @srcinfo = binary_to_source(binary => $package,
+                                      version => $version);
         if (@srcinfo) {
             # We know the source package(s). Use a fully-qualified version.
             addfoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
@@ -697,7 +698,8 @@ sub addfixedversions {
     my $source = $package;
 
     if (defined $package and $isbinary) {
-        my @srcinfo = binarytosource($package, $version, undef);
+        my @srcinfo = binary_to_source(binary => $package,
+                                      version => $version);
         if (@srcinfo) {
             # We know the source package(s). Use a fully-qualified version.
             addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
@@ -1059,25 +1061,10 @@ sub get_bug_status {
 
      $status{package} = '' if not defined $status{package};
      $status{"package"} =~ s/\s*$//;
-     # if we aren't supposed to indicate the source, we'll return
-     # unknown here.
-     $status{source} = 'unknown';
-     if ($param{indicatesource}) {
-        my @packages = split /\s*,\s*/, $status{package};
-        my @source;
-        for my $package (@packages) {
-            next if $package eq '';
-            if ($package =~ /^src\:(.+)$/) {
-                push @source,$1;
-            }
-            else {
-                push @source, binarytosource($package);
-            }
-        }
-        if (@source) {
-            $status{source} = join(', ',@source);
-        }
-     }
+
+     $status{source} = binary_to_source(binary=>[split /\s*,\s*/, $status{package}],
+                                       source_only => 1,
+                                      );
 
      $status{"package"} = 'unknown' if ($status{"package"} eq '');
      $status{"severity"} = 'normal' if (not defined $status{severity} or $status{"severity"} eq '');
index 2ffbdf965424efae835c7d3980bacdd7e214a53c..a3ea9b4a08ff0a21d8ea07dfc8d58bca59ae20de 100755 (executable)
@@ -14,7 +14,7 @@ use IO::File;
 use MIME::Parser;
 use Debbugs::MIME qw(decode_rfc1522 create_mime_message getmailbody);
 use Debbugs::Mail qw(send_mail_message encode_headers get_addresses);
-use Debbugs::Packages qw(getpkgsrc binarytosource);
+use Debbugs::Packages qw(getpkgsrc binary_to_source);
 use Debbugs::User qw(read_usertags write_usertags);
 use Debbugs::Common qw(:lock get_hashname package_maintainer);
 use Debbugs::Status qw(writebug isstrongseverity lockreadbugmerge lockreadbug read_bug splitpackages :versions);
@@ -1078,7 +1078,9 @@ sub checkmaintainers {
        $p = $1;
        next unless defined $p;
        if (defined $gSubscriptionDomain) {
-           my @source = binarytosource($p);
+           my @source = binary_to_source(binary => $p,
+                                         source_only => 1,
+                                        );
            if (@source) {
                push @addsrcaddrs,
                    map {"$_\@$gSubscriptionDomain"} @source;