| 1 |
package AllProv; |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
use integer; |
| 5 |
use CheckVer; |
| 6 |
|
| 7 |
# Terminology: |
| 8 |
# |
| 9 |
# * vname - The name of a provided package. In many cases this is the |
| 10 |
# same as the name of an actual binary; but in the model discussed |
| 11 |
# here, the vname is not attached to any specific actual binary. In |
| 12 |
# this model, all vnames are virtual. |
| 13 |
# |
| 14 |
# * pname - The name of one of the actual binaries which provides the |
| 15 |
# virtual package $vname. |
| 16 |
# |
| 17 |
# Each $vname has one or more @pname which provide it (where in Perl |
| 18 |
# idiom @pname means $pnames, more or less; the `@' is Perl's plural |
| 19 |
# marker). |
| 20 |
# |
| 21 |
# The topic of version numbers in this model can be slightly confusing. |
| 22 |
# In this model, the version numbers apply to vnames not to pnames! The |
| 23 |
# virtual package is the one which has the version. To complicate the |
| 24 |
# matter further, a vname can have more than one version number---and '' |
| 25 |
# is a valid number meaning [no version]. A simple example to |
| 26 |
# illustrate the point: |
| 27 |
# |
| 28 |
# Package: foo5 |
| 29 |
# Version: 5.1.2 |
| 30 |
# Provides: foo |
| 31 |
# |
| 32 |
# Package: bar |
| 33 |
# Version: 0.8 |
| 34 |
# Provides: foo5, foo |
| 35 |
# |
| 36 |
# Three vnames are seen: foo5, foo and bar. Note that bar is indeed a |
| 37 |
# vname, because every binary implicitly provides itself. This leads to |
| 38 |
# the AllProv entries |
| 39 |
# |
| 40 |
# foo5 => { |
| 41 |
# [no version] => { bar }, |
| 42 |
# 5.1.2 => { foo5 }, |
| 43 |
# }, |
| 44 |
# |
| 45 |
# foo => { |
| 46 |
# [no version] => { foo5, bar }, |
| 47 |
# }, |
| 48 |
# |
| 49 |
# bar => { |
| 50 |
# 0.8 => { bar }, |
| 51 |
# }, |
| 52 |
# |
| 53 |
# (Actually where it says "{ foo5, bar }", it technically |
| 54 |
# means "{ foo5=>1, bar=>1 }", but this detail is omitted to unclutter |
| 55 |
# the diagram. Only the keys matter here; the values are dummy trues.) |
| 56 |
# In the example, if one were looking for "foo5", one would find it |
| 57 |
# provided by either of the actual binaries bar and foo5. However, if |
| 58 |
# one were looking for "foo5 (>= 5)", then only the actual binary foo5 |
| 59 |
# could provide this. And "foo (>= 5)" is not available at all. |
| 60 |
# |
| 61 |
# The data pattern is |
| 62 |
# |
| 63 |
# vname => { |
| 64 |
# version_a => { pname1, pname2, pname3 }, |
| 65 |
# version_b => { pname4, pname4 }, |
| 66 |
# }, |
| 67 |
# |
| 68 |
# Debian plans to have versioned Provides at some time in the future. |
| 69 |
# The model used here correctly handles versioned Provides. The key to |
| 70 |
# understanding it all is that the version number applies not to the |
| 71 |
# pname (actual binary) but rather to the vname (virtual package), and |
| 72 |
# that each vname can have more than one version. |
| 73 |
|
| 74 |
# Instantiate and initialize a new AllProv object. Also, let the new |
| 75 |
# AllProv object and the existing AllBin object refer to one another by |
| 76 |
# the null '' hash key. (In a neat implementation, such a reference |
| 77 |
# would not be kept in the same spaces as the pnames and vnames. |
| 78 |
# However, a neat implementation would probably not be written in Perl. |
| 79 |
# This implementation seems to fit the present need. If the need grew |
| 80 |
# much, the module will probably want to be rewritten in C++, anyway.) |
| 81 |
# |
| 82 |
# As the foregoing implies, this constructor slightly modifies the |
| 83 |
# AllBin object passed to it. It adds thereto a reference to the new |
| 84 |
# AllProv object. |
| 85 |
# |
| 86 |
sub new { |
| 87 |
|
| 88 |
# Instantiate. |
| 89 |
my $class = shift; |
| 90 |
my $prov = {}; |
| 91 |
bless $prov, $class; |
| 92 |
|
| 93 |
# Build the provision hash. |
| 94 |
my $bin = shift; |
| 95 |
for my $pname ( grep {length} keys %$bin ) { |
| 96 |
my $bin1 = $bin->{$pname}; |
| 97 |
my $prov2 = $bin1->{prov2}; |
| 98 |
for my $vname ( keys %$prov2 ) { |
| 99 |
my $ver = $prov2->{$vname}; |
| 100 |
$prov->{$vname}{$_}{$pname} = 1 for keys %$ver; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
# Let the AllProv and AllBin hashes refer to one another. |
| 105 |
exists( $prov->{''} ) || exists( $bin->{''} ) |
| 106 |
and die "$0: impossible: null vname or pname\n"; |
| 107 |
$prov->{''} = $bin ; |
| 108 |
$bin ->{''} = $prov; |
| 109 |
|
| 110 |
return $prov; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
# Does the distribution provide the specified vname, relationship and |
| 115 |
# version, at the specified selection level? |
| 116 |
sub does_provide { |
| 117 |
my( $prov, $vname, $rel, $ver, $sel ) = @_; |
| 118 |
defined $sel or $sel = $DebSrcBin::select_up_default; |
| 119 |
my $bin = $prov->{''} |
| 120 |
or die "$0: AllProv fails to refer to AllBin\n"; |
| 121 |
my $vhash = $prov->{$vname}; |
| 122 |
for my $ver0 ( keys %$vhash ) { |
| 123 |
if ( CheckVer::check_ver $ver0, $rel, $ver ) { |
| 124 |
return '1' unless $bin; |
| 125 |
my $pnames = $vhash->{$ver0}; |
| 126 |
$bin->{$_}{select} >= $sel and return '1' for keys %$pnames; |
| 127 |
} |
| 128 |
} |
| 129 |
return ''; |
| 130 |
} |
| 131 |
|
| 132 |
# Check a single dependency alternation. |
| 133 |
sub check_altern { |
| 134 |
my( $prov, $altern, $sel ) = @_; |
| 135 |
for my $vent ( @$altern ) { |
| 136 |
return '1' if $prov->does_provide( |
| 137 |
$vent->{vname}, $vent->{rel}, $vent->{ver}, $sel |
| 138 |
); |
| 139 |
} |
| 140 |
return ''; |
| 141 |
} |
| 142 |
|
| 143 |
1; |
| 144 |
|
| 145 |
# Return the pnames which could satisfy a given vname, relation and |
| 146 |
# version. Optionally accept a reference $$xpname to a pname string. |
| 147 |
# If so, |
| 148 |
# |
| 149 |
# * ban the pname from the return list; and |
| 150 |
# |
| 151 |
# * if $$xpname given happens to be the only pname provided, clear the |
| 152 |
# string $$xpname to undef. |
| 153 |
# |
| 154 |
# (Some callers of this method may find the $$xpname uninteresting. |
| 155 |
# This is fine; they can ignore it; in fact, they need not even provide |
| 156 |
# it. Other callers, however---particularly some indirect callers |
| 157 |
# through &pnames_provided_str()---may need to be signalled when the |
| 158 |
# only pname provided happens to be the same as the name of the |
| 159 |
# depending package---or more to the point, when it happens to be the |
| 160 |
# same as the name of the *conflicting* package. Debian binaries which |
| 161 |
# provide a virtual binary also typically replace and conflict with the |
| 162 |
# same virtual binary. When no other package selected provides the |
| 163 |
# virtual binary, the conflict is probably uninteresting to the user of |
| 164 |
# these DebParse modules. The reference $$xpname provides a convenient |
| 165 |
# way to flag interested indirect callers to the fact.) |
| 166 |
# |
| 167 |
# Further optionally accept a reference $$sel to a selection level. If |
| 168 |
# so, |
| 169 |
# |
| 170 |
# * ban unselected binaries from the return list; and |
| 171 |
# |
| 172 |
# * if this leaves no binaries, clear $$sel to undef. |
| 173 |
# |
| 174 |
sub pnames_provided { |
| 175 |
my( $prov, $vname, $rel, $ver, $xpname, $xsel ) = @_; |
| 176 |
$prov->{$vname} or return (); |
| 177 |
my $bin = $prov->{''} |
| 178 |
or die "$0: AllProv fails to refer to AllBin\n"; |
| 179 |
my $vhash = $prov->{$vname}; |
| 180 |
my %pname; |
| 181 |
for my $ver0 ( keys %$vhash ) { |
| 182 |
CheckVer::check_ver $ver0, $rel, $ver or next; |
| 183 |
$pname{$_} = 1 for keys %{ $vhash->{$ver0} }; |
| 184 |
} |
| 185 |
# Optionally ban pnames as needed from the output. |
| 186 |
{ |
| 187 |
my @pname = keys %pname; |
| 188 |
if ( $xpname ) { |
| 189 |
delete $pname{$$xpname}; |
| 190 |
$$xpname = undef if @pname == 1 && $pname[0] eq $$xpname; |
| 191 |
} |
| 192 |
if ( $xsel ) { |
| 193 |
delete $pname{$_} |
| 194 |
for grep { $bin->{$_}{select} < $$xsel } @pname; |
| 195 |
$$xsel = undef unless keys %pname; |
| 196 |
} |
| 197 |
} |
| 198 |
return sort keys %pname; |
| 199 |
} |
| 200 |
|
| 201 |
# Return a string of pnames provided. Optionally suppress the |
| 202 |
# string if the only pname is identical to the vname. (Do not |
| 203 |
# suppress anything, however, based on $$xpname or $$xsel. Let the |
| 204 |
# caller and &pnames_provided() handle these. Just pass $$xpname |
| 205 |
# and $$xsel through.) |
| 206 |
sub pnames_provided_str { |
| 207 |
my( $prov, $vname, $rel, $ver, $suppress, $xpname, $xsel ) = @_; |
| 208 |
my @pp = $prov->pnames_provided( $vname, $rel, $ver, $xpname, $xsel ); |
| 209 |
return ( $suppress && @pp == 1 && $pp[0] eq $vname ) |
| 210 |
? '' : ' [' . join( ' ', @pp ) . ']'; |
| 211 |
} |
| 212 |
|
| 213 |
1; |
| 214 |
|