| 1 | # $Id$ | ||
| 2 | package Youri::Package::RPM::Test; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Youri::Package::RPM::Test - Fake rpm package implementation | ||
| 7 | |||
| 8 | =head1 DESCRIPTION | ||
| 9 | |||
| 10 | This is a fake L<Youri::Package> implementation for rpm, intended for testing | ||
| 11 | purposes. | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | use base 'Youri::Package::RPM'; | ||
| 18 | use feature qw(switch); | ||
| 19 | use overload | ||
| 20 | '""' => 'as_string', | ||
| 21 | '0+' => '_to_number', | ||
| 22 | fallback => 1; | ||
| 23 | |||
| 24 | use Carp; | ||
| 25 | use File::Basename; | ||
| 26 | use URPM; | ||
| 27 | |||
| 28 | our $AUTOLOAD; | ||
| 29 | |||
| 30 | my @tags = qw/ | ||
| 31 | name | ||
| 32 | version | ||
| 33 | release | ||
| 34 | filename | ||
| 35 | arch | ||
| 36 | url | ||
| 37 | summary | ||
| 38 | description | ||
| 39 | packager | ||
| 40 | buildtime | ||
| 41 | sourcerpm | ||
| 42 | gpg_key | ||
| 43 | /; | ||
| 44 | |||
| 45 | my %tags = map { $_ => 1 } @tags; | ||
| 46 | |||
| 47 | sub check_ranges_compatibility { | ||
| 48 | my ($class, $range1, $range2) = @_; | ||
| 49 | |||
| 50 | return URPM::ranges_overlap($range1, $range2); | ||
| 51 | } | ||
| 52 | |||
| 53 | =head1 CLASS METHODS | ||
| 54 | |||
| 55 | =head2 new(%args) | ||
| 56 | |||
| 57 | Creates and returns a new Youri::Package::RPM::Test object. | ||
| 58 | |||
| 59 | Specific parameters: | ||
| 60 | |||
| 61 | =over | ||
| 62 | |||
| 63 | =item file $file | ||
| 64 | |||
| 65 | Path of file to use for creating this package. | ||
| 66 | |||
| 67 | =item tags $tags | ||
| 68 | |||
| 69 | Hahref of tag values | ||
| 70 | |||
| 71 | =item requires $requires | ||
| 72 | |||
| 73 | Arraryef of package relationships. | ||
| 74 | |||
| 75 | =item provides $provides | ||
| 76 | |||
| 77 | Arraryef of package relationships. | ||
| 78 | |||
| 79 | =item obsoletes $obsoletes | ||
| 80 | |||
| 81 | Arraryef of package relationships. | ||
| 82 | |||
| 83 | =item conflicts $conflicts | ||
| 84 | |||
| 85 | Arraryef of package relationships. | ||
| 86 | |||
| 87 | =item files $files | ||
| 88 | |||
| 89 | Arraryef of package files. | ||
| 90 | |||
| 91 | =item changes $changes | ||
| 92 | |||
| 93 | Arraryef of package changes. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub _init { | ||
| 100 | my ($self, %options) = @_; | ||
| 101 | |||
| 102 | if (exists $options{tags}) { | ||
| 103 | $self->{_tags}->{$_} = $options{tags}->{$_} | ||
| 104 | foreach keys %{$options{tags}}; | ||
| 105 | } | ||
| 106 | |||
| 107 | if (exists $options{file}) { | ||
| 108 | croak "undefined file" | ||
| 109 | unless $options{file}; | ||
| 110 | croak "non-existing file $options{file}" | ||
| 111 | unless -f $options{file}; | ||
| 112 | croak "non-readable file $options{file}" | ||
| 113 | unless -r $options{file}; | ||
| 114 | my $filename = basename($options{file}); | ||
| 115 | given ($filename) { | ||
| 116 | when (/^([\w-]+)-([^-]+)-([^-]+)\.(\w+)\.rpm$/) { | ||
| 117 | # rpm4 style package, with combined dist suffix and release | ||
| 118 | $self->{_tags}->{name} = $1; | ||
| 119 | $self->{_tags}->{version} = $2; | ||
| 120 | $self->{_tags}->{release} = $3; | ||
| 121 | $self->{_tags}->{arch} = $4; | ||
| 122 | $self->{_file} = $options{file}; | ||
| 123 | } | ||
| 124 | when (/^([\w-]+)-([^-]+)-([^-]+)-([^-]+)\.(\w+)\.rpm$/) { | ||
| 125 | # rpm5 style package, with distinct dist suffix and release | ||
| 126 | $self->{_tags}->{name} = $1; | ||
| 127 | $self->{_tags}->{version} = $2; | ||
| 128 | $self->{_tags}->{release} = $3; | ||
| 129 | $self->{_tags}->{arch} = $5; | ||
| 130 | $self->{_file} = $options{file}; | ||
| 131 | } | ||
| 132 | default { | ||
| 133 | croak "non-compliant filename $filename"; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | $self->{_requires} = $options{requires}; | ||
| 139 | $self->{_provides} = $options{provides}; | ||
| 140 | $self->{_obsoletes} = $options{obsoletes}; | ||
| 141 | $self->{_conflicts} = $options{conflicts}; | ||
| 142 | $self->{_files} = $options{files}; | ||
| 143 | $self->{_changes} = $options{changes}; | ||
| 144 | |||
| 145 | # default values | ||
| 146 | $self->{_tags}->{name} ||= 'test'; | ||
| 147 | $self->{_tags}->{arch} ||= 'noarch'; | ||
| 148 | $self->{_tags}->{version} ||= 1; | ||
| 149 | $self->{_tags}->{release} ||= 1; | ||
| 150 | $self->{_tags}->{filename} = sprintf( | ||
| 151 | '%s-%s-%s.%s.rpm', | ||
| 152 | $self->{_tags}->{name}, | ||
| 153 | $self->{_tags}->{version}, | ||
| 154 | $self->{_tags}->{release}, | ||
| 155 | $self->{_tags}->{arch} | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub get_revision { | ||
| 160 | my ($self) = @_; | ||
| 161 | croak "Not a class method" unless ref $self; | ||
| 162 | |||
| 163 | my $revision = | ||
| 164 | ($self->{_tags}->{version} || '') . | ||
| 165 | '-' . | ||
| 166 | ($self->{_tags}->{release} || ''); | ||
| 167 | |||
| 168 | return $self->{_tags}->{epoch} ? | ||
| 169 | ($self->{_tags}->{epoch} || '') . ':' . $revision : $revision; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub get_tag { | ||
| 173 | my ($self, $tag) = @_; | ||
| 174 | croak "Not a class method" unless ref $self; | ||
| 175 | croak "invalid tag $tag" unless $tags{$tag}; | ||
| 176 | return $self->{_tags}->{$tag}; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub is_source { | ||
| 180 | my ($self) = @_; | ||
| 181 | croak "Not a class method" unless ref $self; | ||
| 182 | |||
| 183 | return $self->{_tags}->{arch} eq 'src'; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub is_binary { | ||
| 187 | my ($self) = @_; | ||
| 188 | croak "Not a class method" unless ref $self; | ||
| 189 | |||
| 190 | return $self->{_tags}->{arch} ne 'src'; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub get_type { | ||
| 194 | my ($self) = @_; | ||
| 195 | croak "Not a class method" unless ref $self; | ||
| 196 | |||
| 197 | return | ||
| 198 | $self->{_tags}->{arch} eq 'src' ? | ||
| 199 | "source" : | ||
| 200 | "binary"; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub get_canonical_name { | ||
| 204 | my ($self) = @_; | ||
| 205 | croak "Not a class method" unless ref $self; | ||
| 206 | |||
| 207 | # return name if arch is not defined | ||
| 208 | return $self->{_tags}->{name} | ||
| 209 | if ! $self->{_tags}->{arch}; | ||
| 210 | |||
| 211 | # otherwise return name if arch is source | ||
| 212 | return $self->{_tags}->{name} | ||
| 213 | if $self->{_tags}->{arch} eq 'src'; | ||
| 214 | |||
| 215 | # otherwise return name if sourcerpm is not defined | ||
| 216 | return $self->{_tags}->{name} | ||
| 217 | if ! $self->{_tags}->{sourcerpm}; | ||
| 218 | |||
| 219 | # otherwise source package name | ||
| 220 | $self->{_tags}->{sourcerpm} =~ /^(\S+)-[^-]+-[^-]+\.src\.rpm$/; | ||
| 221 | return $1; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub get_file_name { | ||
| 225 | my ($self) = @_; | ||
| 226 | croak "Not a class method" unless ref $self; | ||
| 227 | |||
| 228 | return | ||
| 229 | $self->as_string() . '.rpm'; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub as_string { | ||
| 233 | my ($self) = @_; | ||
| 234 | croak "Not a class method" unless ref $self; | ||
| 235 | |||
| 236 | return | ||
| 237 | ($self->{_tags}->{name} || '') . | ||
| 238 | '-' . | ||
| 239 | ($self->{_tags}->{version} || '') . | ||
| 240 | '-' . | ||
| 241 | ($self->{_tags}->{release} || '') . | ||
| 242 | '.' . | ||
| 243 | ($self->{_tags}->{arch} || ''); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub as_formated_string { | ||
| 247 | my ($self, $format) = @_; | ||
| 248 | croak "Not a class method" unless ref $self; | ||
| 249 | |||
| 250 | $format =~ s/%\{([^}]+)\}/$self->{_tags}->{$1}/eg; | ||
| 251 | return $format; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub _to_number { | ||
| 255 | return refaddr($_[0]); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub get_requires { | ||
| 259 | my ($self, $format) = @_; | ||
| 260 | croak "Not a class method" unless ref $self; | ||
| 261 | |||
| 262 | return $self->{_requires} ? @{$self->{_requires}} : (); | ||
| 263 | } | ||
| 264 | |||
| 265 | sub get_provides { | ||
| 266 | my ($self, $format) = @_; | ||
| 267 | croak "Not a class method" unless ref $self; | ||
| 268 | |||
| 269 | return $self->{_provides} ? @{$self->{_provides}} : (); | ||
| 270 | } | ||
| 271 | |||
| 272 | sub get_obsoletes { | ||
| 273 | my ($self, $format) = @_; | ||
| 274 | croak "Not a class method" unless ref $self; | ||
| 275 | |||
| 276 | return $self->{_obsoletes} ? @{$self->{_obsoletes}} : (); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub get_conflicts { | ||
| 280 | my ($self) = @_; | ||
| 281 | croak "Not a class method" unless ref $self; | ||
| 282 | |||
| 283 | return $self->{_conflicts} ? @{$self->{_conflicts}} : (); | ||
| 284 | } | ||
| 285 | |||
| 286 | sub get_files { | ||
| 287 | my ($self) = @_; | ||
| 288 | croak "Not a class method" unless ref $self; | ||
| 289 | |||
| 290 | return $self->{_files} ? @{$self->{_files}} : (); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub get_changes { | ||
| 294 | my ($self) = @_; | ||
| 295 | croak "Not a class method" unless ref $self; | ||
| 296 | |||
| 297 | return $self->{_changes} ? | ||
| 298 | map { | ||
| 299 | Youri::Package::Change->new($_->[0], $_->[1], $_->[2]) | ||
| 300 | } @{$self->{_changes}} : | ||
| 301 | (); | ||
| 302 | } | ||
| 303 | |||
| 304 | sub get_last_change { | ||
| 305 | my ($self) = @_; | ||
| 306 | croak "Not a class method" unless ref $self; | ||
| 307 | |||
| 308 | return $self->{_changes} ? | ||
| 309 | Youri::Package::Change->new( | ||
| 310 | $self->{_changes}->[0]->[0], | ||
| 311 | $self->{_changes}->[0]->[1], | ||
| 312 | $self->{_changes}->[0]->[2] | ||
| 313 | ) : | ||
| 314 | undef; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub compare { | ||
| 318 | my ($self, $package) = @_; | ||
| 319 | croak "Not a class method" unless ref $self; | ||
| 320 | |||
| 321 | return URPM::rpmvercmp($self->get_revision(), $package->get_revision()); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub satisfy_range { | ||
| 325 | my ($self, $range) = @_; | ||
| 326 | croak "Not a class method" unless ref $self; | ||
| 327 | |||
| 328 | return $self->check_ranges_compatibility( | ||
| 329 | '== ' . $self->get_revision(), | ||
| 330 | $range | ||
| 331 | ); | ||
| 332 | } | ||
| 333 | |||
| 334 | sub AUTOLOAD { | ||
| 335 | my ($self) = @_; | ||
| 336 | croak "Not a class method" unless ref $self; | ||
| 337 | |||
| 338 | my $method = $AUTOLOAD; | ||
| 339 | $method =~ s/.*:://; | ||
| 340 | return if $method eq 'DESTROY'; | ||
| 341 | croak "invalid method" unless $method =~ /^get_(\w+)$/; | ||
| 342 | |||
| 343 | my $tag = $1; | ||
| 344 | return $self->get_tag($1); | ||
| 345 | } | ||
| 346 | |||
| 347 | 1; |