- unless (tied %_versions) {
- tie %_versions, 'MLDBM', $gVersionIndex, O_RDONLY
- or die "can't open versions index: $!";
- }
- my $version = $_versions{$pkg};
- return () unless defined $version;
- my %version = %{$version};
-
- if (defined $arch and exists $version{$dist}{$arch}) {
- my $ver = $version{$pkg}{$dist}{$arch};
- return $ver if defined $ver;
- return ();
- } else {
- my %uniq;
- for my $ar (keys %{$version{$dist}}) {
- $uniq{$version{$dist}{$ar}} = 1 unless $ar eq 'source';
- }
- if (%uniq) {
- return keys %uniq;
- } elsif (exists $version{$dist}{source}) {
- # Maybe this is actually a source package with no corresponding
- # binaries?
- return $version{$dist}{source};
- } else {
- return ();
- }
- }
+
+
+=head2 get_versions
+
+ get_versions(package=>'foopkg',
+ dist => 'unstable',
+ arch => 'i386',
+ );
+
+Returns a list of the versions of package in the distributions and
+architectures listed. This routine only returns unique values.
+
+=over
+
+=item package -- package to return list of versions
+
+=item dist -- distribution (unstable, stable, testing); can be an
+arrayref
+
+=item arch -- architecture (i386, source, ...); can be an arrayref
+
+=item time -- returns a version=>time hash at which the newest package
+matching this version was uploaded
+
+=item source -- returns source/version instead of just versions
+
+=item no_source_arch -- discards the source architecture when arch is
+not passed. [Used for finding the versions of binary packages only.]
+Defaults to 0, which does not discard the source architecture. (This
+may change in the future, so if you care, please code accordingly.)
+
+=item return_archs -- returns a version=>[archs] hash indicating which
+architectures are at which versions.
+
+=item largest_source_version_only -- if there is more than one source
+version in a particular distribution, discards all versions but the
+largest in that distribution. Defaults to 1, as this used to be the
+way that the Debian archive worked.
+
+=back
+
+When called in scalar context, this function will return hashrefs or
+arrayrefs as appropriate, in list context, it will return paired lists
+or unpaired lists as appropriate.
+
+=cut
+
+our %_versions;
+our %_versions_time;
+
+sub get_versions{
+ my %param = validate_with(params => \@_,
+ spec => {package => {type => SCALAR|ARRAYREF,
+ },
+ dist => {type => SCALAR|ARRAYREF,
+ default => 'unstable',
+ },
+ arch => {type => SCALAR|ARRAYREF,
+ optional => 1,
+ },
+ time => {type => BOOLEAN,
+ default => 0,
+ },
+ source => {type => BOOLEAN,
+ default => 0,
+ },
+ no_source_arch => {type => BOOLEAN,
+ default => 0,
+ },
+ return_archs => {type => BOOLEAN,
+ default => 0,
+ },
+ largest_source_version_only => {type => BOOLEAN,
+ default => 1,
+ },
+ },
+ );
+ my $versions;
+ if ($param{time}) {
+ return () if not defined $gVersionTimeIndex;
+ unless (tied %_versions_time) {
+ tie %_versions_time, 'MLDBM', $gVersionTimeIndex, O_RDONLY
+ or die "can't open versions index $gVersionTimeIndex: $!";
+ }
+ $versions = \%_versions_time;
+ }
+ else {
+ return () if not defined $gVersionIndex;
+ unless (tied %_versions) {
+ tie %_versions, 'MLDBM', $gVersionIndex, O_RDONLY
+ or die "can't open versions index $gVersionIndex: $!";
+ }
+ $versions = \%_versions;
+ }
+ my %versions;
+ for my $package (make_list($param{package})) {
+ my $source_only = 0;
+ if ($package =~ s/^src://) {
+ $source_only = 1;
+ }
+ my $version = $versions->{$package};
+ next unless defined $version;
+ for my $dist (make_list($param{dist})) {
+ for my $arch (exists $param{arch}?
+ make_list($param{arch}):
+ (grep {not $param{no_source_arch} or
+ $_ ne 'source'
+ } $source_only?'source':keys %{$version->{$dist}})) {
+ next unless defined $version->{$dist}{$arch};
+ my @vers = ref $version->{$dist}{$arch} eq 'HASH' ?
+ keys %{$version->{$dist}{$arch}} :
+ make_list($version->{$dist}{$arch});
+ if ($param{largest_source_version_only} and
+ $arch eq 'source' and @vers > 1) {
+ # order the versions, then pick the biggest version number
+ @vers = sort_versions(@vers);
+ @vers = $vers[-1];
+ }
+ for my $ver (@vers) {
+ my $f_ver = $ver;
+ if ($param{source}) {
+ ($f_ver) = make_source_versions(package => $package,
+ arch => $arch,
+ versions => $ver);
+ next unless defined $f_ver;
+ }
+ if ($param{time}) {
+ $versions{$f_ver} = max($versions{$f_ver}||0,$version->{$dist}{$arch}{$ver});
+ }
+ else {
+ push @{$versions{$f_ver}},$arch;
+ }
+ }
+ }
+ }
+ }
+ if ($param{time} or $param{return_archs}) {
+ return wantarray?%versions :\%versions;
+ }
+ return wantarray?keys %versions :[keys %versions];