| 1 |
package DebSrcBin; |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
use integer; |
| 5 |
use CheckVer; |
| 6 |
|
| 7 |
# List the subparsable fields. |
| 8 |
our %dkey_subparsable = map {$_=>1} qw( |
| 9 |
Depends |
| 10 |
Pre-Depends |
| 11 |
Recommends |
| 12 |
Suggests |
| 13 |
Conflicts |
| 14 |
Provides |
| 15 |
Replaces |
| 16 |
Enhances |
| 17 |
Build-Depends |
| 18 |
Build-Depends-Indep |
| 19 |
Build-Conflicts |
| 20 |
Build-Conflicts-Indep |
| 21 |
Binary |
| 22 |
); |
| 23 |
|
| 24 |
# List the required fields. (Do not list Source and xProvides, however. |
| 25 |
# These two fields are safely created in the instantiation even when the |
| 26 |
# control stanza does not provide them.) |
| 27 |
our @dkey_reqd = qw( |
| 28 |
Package |
| 29 |
Version |
| 30 |
Architecture |
| 31 |
); |
| 32 |
|
| 33 |
# List the irregularly keyed fields. |
| 34 |
our %dkey_irreg = map {$_=>1} qw( |
| 35 |
task |
| 36 |
url |
| 37 |
); |
| 38 |
|
| 39 |
# List the version-relational operators. |
| 40 |
our %rel = map {$_=>1} qw( << <= = >= >> ); |
| 41 |
|
| 42 |
# Define numerical priority levels. |
| 43 |
our %pri = ( |
| 44 |
required => 30, |
| 45 |
important => 20, |
| 46 |
standard => 10, |
| 47 |
optional => 0, |
| 48 |
extra => -10, |
| 49 |
); |
| 50 |
our $pri_default = -10; |
| 51 |
|
| 52 |
# Set the default selection level and threshold. |
| 53 |
our $select_default = 0; |
| 54 |
our $select_up_default = 1; |
| 55 |
|
| 56 |
# Specify all the architectures of interest (typically |
| 57 |
# qw( all any i386 )). Or, set $arch_sense to 0 and specify all the |
| 58 |
# architectures *not* of interest. |
| 59 |
our $arch_sense = 1; |
| 60 |
our %arch = map {$_=>1} qw( all any i386 ); |
| 61 |
|
| 62 |
# Construct the pattern a subparsable field fits. |
| 63 |
my $subp_pat; |
| 64 |
{ |
| 65 |
# This used to be one long pattern string, but then I couldn't read my own |
| 66 |
# pattern. It was too long. Splitting it into pieces here helps. (If you |
| 67 |
# don't know Perl very well, even splitting it may not help much, eh? |
| 68 |
# Actually, it's pretty straightforward Perl, so if you want to read Perl |
| 69 |
# then it would be worth your while to learn to read this.) -THB- |
| 70 |
my $pname = qr/([^\s()\[\]]+)/; |
| 71 |
my $archs = qr/\[\s*([^\[\]]*?)\s*\]/; |
| 72 |
my $rel = qr/([<=>]*)/; |
| 73 |
my $ver = qr/([^\s()]+?)/; |
| 74 |
my $rel_ver = qr/\(\s*${rel}\s*${ver}\s*\)/; |
| 75 |
$subp_pat = qr/^${pname}\s*${rel_ver}?\s*${archs}?$/; |
| 76 |
} |
| 77 |
|
| 78 |
# Private subroutine: evaluate an architecture string. |
| 79 |
sub eval_arch_str ($;$) { |
| 80 |
my( $arch_str, $arch_array ) = @_; |
| 81 |
my @arch; |
| 82 |
$$arch_array = \@arch if $arch_array; |
| 83 |
defined($arch_str) && length($arch_str) or return 1; |
| 84 |
@arch = map { |
| 85 |
my( $not, $arch ); |
| 86 |
( $not, $arch ) = /^(!)?(\S+)$/ |
| 87 |
or die "$0: bad architecture string\n$_\n$arch_str\n"; |
| 88 |
+{ |
| 89 |
not => $not, |
| 90 |
arch => $arch, |
| 91 |
} |
| 92 |
} split ' ', $arch_str or return 1; |
| 93 |
my $so = !$arch[0]{not}; |
| 94 |
!$arch[$_]{not} eq $so |
| 95 |
or die "$0: mismatched `!' symbols\n$arch_str\n" |
| 96 |
for ( 1 .. $#arch ); |
| 97 |
$arch_sense or $so = !$so; |
| 98 |
$arch{$_} and return $so for map { $_->{arch} } @arch; |
| 99 |
return !$so; |
| 100 |
} |
| 101 |
|
| 102 |
# Instantiate and initialize a new DebSrcBin object. |
| 103 |
sub new { |
| 104 |
|
| 105 |
# Instantiate the object. |
| 106 |
my $class = shift; |
| 107 |
my $self = {}; |
| 108 |
bless $self, $class; |
| 109 |
|
| 110 |
# Retrieve the next stanza from the Sources or Packages file. |
| 111 |
my $fh = shift; |
| 112 |
{ |
| 113 |
local $/ = ''; |
| 114 |
$self->{stanza} = <$fh>; |
| 115 |
# warn "vvvvvvvv\n$self->{stanza}^^^^^^^^\n"; # diagnostic |
| 116 |
} |
| 117 |
defined $self->{stanza} or $self = undef, return $self; |
| 118 |
$self->{ldesc} = ''; |
| 119 |
|
| 120 |
# Parse the stanza by line. |
| 121 |
for ( split /^/m, $self->{stanza} ) { |
| 122 |
if ( /^\s+\S/ ) { |
| 123 |
$self->{ldesc} .= $_; |
| 124 |
} |
| 125 |
elsif ( /^(\S*?):\s*(.*?)\s*$/ ) { |
| 126 |
my( $dkey, $dval ) = ( $1, $2 ); |
| 127 |
$dkey =~ /^[[:upper:]]/ || $dkey_irreg{$dkey} |
| 128 |
or die "$0: bad field key $dkey\n$self->{stanza}"; |
| 129 |
exists $self->{$dkey} |
| 130 |
and die "$0: repeated key $dkey\n$self->{stanza}"; |
| 131 |
$self->{$dkey} = $dval; |
| 132 |
} |
| 133 |
elsif ( /\S/ ) { |
| 134 |
die "$0: bad line\n$self->{stanza}"; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
# Ensure required fields. Skip if the architecture is irrelevant. |
| 139 |
# Number the Priority. |
| 140 |
defined( $self->{$_} ) && length( $self->{$_} ) |
| 141 |
or die "$0: missing $_ key\n$self->{stanza}" |
| 142 |
for @dkey_reqd; |
| 143 |
eval_arch_str $self->{Architecture} or return new( $class, $fh ); |
| 144 |
if ( defined $self->{Priority} ) { |
| 145 |
defined $pri{ $self->{Priority} } |
| 146 |
or die "$0: unknown priority $self->{Priority}\n$self->{stanza}"; |
| 147 |
$self->{pri} = $pri{ $self->{Priority} }; |
| 148 |
} |
| 149 |
else { $self->{pri} = $pri_default } |
| 150 |
|
| 151 |
# Subparse the subparsable lines. |
| 152 |
for my $dkey ( keys %$self ) { |
| 153 |
$dkey_subparsable{$dkey} or next; |
| 154 |
my $xkey = "x$dkey"; |
| 155 |
|
| 156 |
$self->{"$xkey"} = [ |
| 157 |
map { |
| 158 |
|
| 159 |
# The following is an *altern* (or *dependency alternative*): a |
| 160 |
# series of alternatives separated by " | " in the control |
| 161 |
# stanza. |
| 162 |
[ |
| 163 |
map { |
| 164 |
|
| 165 |
# Here is a single entry like "debram (>> 0.5.1)". |
| 166 |
my( $vname, $rel, $ver, $archs ); |
| 167 |
( $vname, $rel, $ver, $archs ) = /$subp_pat/o |
| 168 |
or die "$0: cannot subparse\n$_\n$self->{stanza}"; |
| 169 |
if ( defined($ver) ) { |
| 170 |
if ( $rel eq '<' ) { $rel = '<=' } |
| 171 |
elsif ( $rel eq '>' ) { $rel = '>=' } |
| 172 |
elsif ( !length($rel) ) { $rel = '=' } |
| 173 |
$rel{$rel} |
| 174 |
or die "$0: unknown version-relational operator $rel\n" |
| 175 |
. "$self->{stanza}"; |
| 176 |
} |
| 177 |
else { |
| 178 |
$rel = '='; |
| 179 |
$ver = ''; |
| 180 |
} |
| 181 |
my $arch_array; |
| 182 |
eval_arch_str( $archs, \$arch_array ) ? { |
| 183 |
vname => $vname, |
| 184 |
rel => $rel, |
| 185 |
ver => $ver, |
| 186 |
arch => $arch_array, |
| 187 |
} : (); |
| 188 |
|
| 189 |
} grep /\S/, split /\s*\|\s*/ |
| 190 |
] |
| 191 |
|
| 192 |
} grep /\S/, split /\s*,\s*/, $self->{$dkey} |
| 193 |
]; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
# Ensure the Source field. Create a select field. Let the package |
| 198 |
# provide itself. (These are unneeded but harmless for DebSrc |
| 199 |
# objects.) |
| 200 |
defined $self->{Source} or $self->{Source} = $self->{Package}; |
| 201 |
$self->{select} = $select_default; |
| 202 |
unshift @{ $self->{xProvides} }, [ |
| 203 |
{ |
| 204 |
vname => $self->{Package}, |
| 205 |
rel => '=', |
| 206 |
ver => $self->{Version}, |
| 207 |
} |
| 208 |
]; |
| 209 |
|
| 210 |
# Reform xProvides to show provided names and versions (if there are |
| 211 |
# any versions), producing data records of the form |
| 212 |
# |
| 213 |
# vname => { version_a, version_b }, |
| 214 |
# |
| 215 |
# (technically "{ version_a=>1, version_b=>1 }"), where the null string |
| 216 |
# indicates [no version]. |
| 217 |
# |
| 218 |
{ |
| 219 |
my %prov2; |
| 220 |
for my $vhash2 ( map {@$_} @{ $self->{xProvides} } ) { |
| 221 |
$vhash2->{rel} eq '=' |
| 222 |
or die "$0: unclear which version is provided " |
| 223 |
. "with $vhash2->{rel}\n$self->{stanza}\n"; |
| 224 |
my $vname = $vhash2->{vname}; |
| 225 |
my $ver = $vhash2->{ver}; |
| 226 |
$prov2{$vname}{ defined($ver) ? $ver : '' } = 1; |
| 227 |
} |
| 228 |
$self->{prov2} = \%prov2; |
| 229 |
} |
| 230 |
|
| 231 |
return $self; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
sub cmp_ver { |
| 236 |
my( $self, $ver ) = @_; |
| 237 |
CheckVer::cmp_ver_string $self->{Version}, $ver; |
| 238 |
} |
| 239 |
sub check_ver { |
| 240 |
my( $self, $rel, $ver ) = @_; |
| 241 |
CheckVer::check_ver $self->{Version}, $rel, $ver; |
| 242 |
} |
| 243 |
|
| 244 |
1; |
| 245 |
|