| 1 | # $Id: URPM.pm 2370 2013-01-03 19:26:49Z guillomovitch $ | ||
| 2 | package Youri::Package::RPM::URPM; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Youri::Package::RPM::URPM - URPM-based rpm package implementation | ||
| 7 | |||
| 8 | =head1 DESCRIPTION | ||
| 9 | |||
| 10 | This is an URPM-based L<Youri::Package> implementation for rpm. | ||
| 11 | |||
| 12 | It is merely a wrapper over URPM::Package class, with a more structured | ||
| 13 | interface. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use base 'Youri::Package::RPM'; | ||
| 20 | use overload | ||
| 21 | '""' => 'as_string', | ||
| 22 | '0+' => '_to_number', | ||
| 23 | fallback => 1; | ||
| 24 | |||
| 25 | use Carp; | ||
| 26 | use English qw(-no_match_vars); | ||
| 27 | use Expect; | ||
| 28 | use File::Spec; | ||
| 29 | use Scalar::Util qw/refaddr blessed/; | ||
| 30 | use URPM; | ||
| 31 | |||
| 32 | use Youri::Package::Change; | ||
| 33 | use Youri::Package::File; | ||
| 34 | use Youri::Package::Relationship; | ||
| 35 | |||
| 36 | =head1 CLASS METHODS | ||
| 37 | |||
| 38 | =head2 new(%args) | ||
| 39 | |||
| 40 | Creates and returns a new Youri::Package::RPM::URPM object. | ||
| 41 | |||
| 42 | Specific parameters: | ||
| 43 | |||
| 44 | =over | ||
| 45 | |||
| 46 | =item file $file | ||
| 47 | |||
| 48 | Path of file to use for creating this package. | ||
| 49 | |||
| 50 | =item header $header | ||
| 51 | |||
| 52 | L<URPM::Package> object to use for creating this package. | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub _init { | ||
| 59 | my ($self, %options) = @_; | ||
| 60 | |||
| 61 | my $header; | ||
| 62 | HEADER: { | ||
| 63 | if (exists $options{header}) { | ||
| 64 | croak "undefined header" | ||
| 65 | unless $options{header}; | ||
| 66 | croak "invalid header" | ||
| 67 | unless $options{header}->isa('URPM::Package'); | ||
| 68 | $header = $options{header}; | ||
| 69 | last HEADER; | ||
| 70 | } | ||
| 71 | |||
| 72 | if (exists $options{file}) { | ||
| 73 | croak "undefined file" | ||
| 74 | unless $options{file}; | ||
| 75 | croak "non-existing file $options{file}" | ||
| 76 | unless -f $options{file}; | ||
| 77 | croak "non-readable file $options{file}" | ||
| 78 | unless -r $options{file}; | ||
| 79 | my $urpm = URPM->new(); | ||
| 80 | $urpm->parse_rpm($options{file}, keep_all_tags => 1); | ||
| 81 | $header = $urpm->{depslist}->[0]; | ||
| 82 | croak "non-rpm file $options{file}" unless $header; | ||
| 83 | last HEADER; | ||
| 84 | } | ||
| 85 | |||
| 86 | croak "no way to extract header from arguments"; | ||
| 87 | } | ||
| 88 | |||
| 89 | $self->{_header} = $header; | ||
| 90 | $self->{_file} = File::Spec->rel2abs($options{file}); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub compare_revisions { | ||
| 94 | my ($class, $revision1, $revision2) = @_; | ||
| 95 | |||
| 96 | return URPM::rpmvercmp($revision1, $revision2); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub check_ranges_compatibility { | ||
| 100 | my ($class, $range1, $range2) = @_; | ||
| 101 | |||
| 102 | return URPM::ranges_overlap($range1, $range2); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub get_name { | ||
| 106 | my ($self) = @_; | ||
| 107 | croak "Not a class method" unless ref $self; | ||
| 108 | |||
| 109 | return $self->{_header}->name(); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub get_version { | ||
| 113 | my ($self) = @_; | ||
| 114 | croak "Not a class method" unless ref $self; | ||
| 115 | |||
| 116 | return $self->{_header}->version(); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub get_release { | ||
| 120 | my ($self) = @_; | ||
| 121 | croak "Not a class method" unless ref $self; | ||
| 122 | |||
| 123 | return $self->{_header}->release(); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub get_revision { | ||
| 127 | my ($self) = @_; | ||
| 128 | croak "Not a class method" unless ref $self; | ||
| 129 | |||
| 130 | return $self->{_header}->queryformat('%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}'); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub get_file_name { | ||
| 134 | my ($self) = @_; | ||
| 135 | croak "Not a class method" unless ref $self; | ||
| 136 | |||
| 137 | return $self->{_header}->filename(); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub get_arch { | ||
| 141 | my ($self) = @_; | ||
| 142 | croak "Not a class method" unless ref $self; | ||
| 143 | |||
| 144 | return $self->{_header}->arch(); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub get_url { | ||
| 148 | my ($self) = @_; | ||
| 149 | croak "Not a class method" unless ref $self; | ||
| 150 | |||
| 151 | return $self->{_header}->url(); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub get_summary { | ||
| 155 | my ($self) = @_; | ||
| 156 | croak "Not a class method" unless ref $self; | ||
| 157 | |||
| 158 | return $self->{_header}->summary(); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub get_description { | ||
| 162 | my ($self) = @_; | ||
| 163 | croak "Not a class method" unless ref $self; | ||
| 164 | |||
| 165 | return $self->{_header}->description(); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub get_packager { | ||
| 169 | my ($self) = @_; | ||
| 170 | croak "Not a class method" unless ref $self; | ||
| 171 | |||
| 172 | return $self->{_header}->packager(); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub is_source { | ||
| 176 | my ($self) = @_; | ||
| 177 | croak "Not a class method" unless ref $self; | ||
| 178 | |||
| 179 | return $self->{_header}->arch() eq 'src'; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub is_binary { | ||
| 183 | my ($self) = @_; | ||
| 184 | croak "Not a class method" unless ref $self; | ||
| 185 | |||
| 186 | return $self->{_header}->arch() ne 'src'; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub get_type { | ||
| 190 | my ($self) = @_; | ||
| 191 | croak "Not a class method" unless ref $self; | ||
| 192 | |||
| 193 | return | ||
| 194 | $self->{_header}->arch() eq 'src' ? | ||
| 195 | "source" : | ||
| 196 | "binary"; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub get_age { | ||
| 200 | my ($self) = @_; | ||
| 201 | croak "Not a class method" unless ref $self; | ||
| 202 | |||
| 203 | return $self->{_header}->buildtime(); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub get_source_package { | ||
| 207 | my ($self) = @_; | ||
| 208 | croak "Not a class method" unless ref $self; | ||
| 209 | |||
| 210 | return $self->{_header}->sourcerpm(); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub get_canonical_name { | ||
| 214 | my ($self) = @_; | ||
| 215 | croak "Not a class method" unless ref $self; | ||
| 216 | |||
| 217 | if ($self->{_header}->arch() eq 'src') { | ||
| 218 | return $self->{_header}->name(); | ||
| 219 | } else { | ||
| 220 | $self->{_header}->sourcerpm() =~ /^(\S+)-[^-]+-[^-]+\.src\.rpm$/; | ||
| 221 | return $1; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | sub get_canonical_revision { | ||
| 226 | my ($self) = @_; | ||
| 227 | croak "Not a class method" unless ref $self; | ||
| 228 | |||
| 229 | if ($self->{_header}->arch() eq 'src') { | ||
| 230 | return $self->{_header}->get_revision(); | ||
| 231 | } else { | ||
| 232 | $self->{_header}->sourcerpm() =~ /^\S+-([^-]+-[^-]+)\.src\.rpm$/; | ||
| 233 | return $1; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | sub get_tag { | ||
| 238 | my ($self, $tag) = @_; | ||
| 239 | croak "Not a class method" unless ref $self; | ||
| 240 | croak "invalid tag $tag" unless $self->{_header}->can($tag); | ||
| 241 | return $self->{_header}->$tag(); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub get_requires { | ||
| 245 | my ($self) = @_; | ||
| 246 | croak "Not a class method" unless ref $self; | ||
| 247 | |||
| 248 | my $pattern = qr/^([^[]+)(?:\[\*\])?(?:\[(.+)\])?$/; | ||
| 249 | |||
| 250 | return map { | ||
| 251 | $_ =~ $pattern; | ||
| 252 | Youri::Package::Relationship->new($1, $2) | ||
| 253 | } $self->{_header}->requires(); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub get_provides { | ||
| 257 | my ($self) = @_; | ||
| 258 | croak "Not a class method" unless ref $self; | ||
| 259 | |||
| 260 | my $pattern = qr/^([^[]+)(?:\[(.+)\])?$/; | ||
| 261 | |||
| 262 | return map { | ||
| 263 | $_ =~ /$pattern/; | ||
| 264 | Youri::Package::Relationship->new($1, $2 && $2 ne '*' ? $2 : undef) | ||
| 265 | } $self->{_header}->provides(); | ||
| 266 | } | ||
| 267 | |||
| 268 | sub get_obsoletes { | ||
| 269 | my ($self) = @_; | ||
| 270 | croak "Not a class method" unless ref $self; | ||
| 271 | |||
| 272 | my $pattern = qr/^([^[]+)(?:\[(.+)\])?$/; | ||
| 273 | |||
| 274 | return map { | ||
| 275 | $_ =~ $pattern; | ||
| 276 | Youri::Package::Relationship->new($1, $2 && $2 ne '*' ? $2 : undef) | ||
| 277 | } $self->{_header}->obsoletes(); | ||
| 278 | } | ||
| 279 | |||
| 280 | sub get_conflicts { | ||
| 281 | my ($self) = @_; | ||
| 282 | croak "Not a class method" unless ref $self; | ||
| 283 | |||
| 284 | my $pattern = qr/^([^[]+)(?:\[(.+)\])?$/; | ||
| 285 | |||
| 286 | return map { | ||
| 287 | $_ =~ $pattern; | ||
| 288 | Youri::Package::Relationship->new($1, $2 && $2 ne '*' ? $2 : undef) | ||
| 289 | } return $self->{_header}->conflicts(); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub get_files { | ||
| 293 | my ($self) = @_; | ||
| 294 | croak "Not a class method" unless ref $self; | ||
| 295 | |||
| 296 | my @modes = $self->{_header}->files_mode(); | ||
| 297 | my @md5sums = $self->{_header}->files_md5sum(); | ||
| 298 | |||
| 299 | return map { | ||
| 300 | Youri::Package::File->new($_, shift @modes, shift @md5sums) | ||
| 301 | } $self->{_header}->files(); | ||
| 302 | } | ||
| 303 | |||
| 304 | sub get_gpg_key { | ||
| 305 | my ($self) = @_; | ||
| 306 | croak "Not a class method" unless ref $self; | ||
| 307 | |||
| 308 | my $signature = $self->{_header}->queryformat('%{SIGGPG:pgpsig}'); | ||
| 309 | return if $signature eq '(not a blob)'; | ||
| 310 | my $key_id = (split(/\s+/, $signature))[-1]; | ||
| 311 | return substr($key_id, 8); | ||
| 312 | } | ||
| 313 | |||
| 314 | sub get_changes { | ||
| 315 | my ($self) = @_; | ||
| 316 | croak "Not a class method" unless ref $self; | ||
| 317 | |||
| 318 | my @times = $self->{_header}->changelog_time(); | ||
| 319 | my @texts = $self->{_header}->changelog_text(); | ||
| 320 | |||
| 321 | return map { | ||
| 322 | Youri::Package::Change->new($_, shift @times, shift @texts) | ||
| 323 | } $self->{_header}->changelog_name(); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub get_last_change { | ||
| 327 | my ($self) = @_; | ||
| 328 | croak "Not a class method" unless ref $self; | ||
| 329 | |||
| 330 | my $text = ($self->{_header}->changelog_text())[0]; | ||
| 331 | my $name = ($self->{_header}->changelog_name())[0]; | ||
| 332 | my $time = ($self->{_header}->changelog_time())[0]; | ||
| 333 | |||
| 334 | return $text ? | ||
| 335 | Youri::Package::Change->new($name, $time, $text) : | ||
| 336 | undef; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub as_string { | ||
| 340 | my ($self) = @_; | ||
| 341 | croak "Not a class method" unless ref $self; | ||
| 342 | |||
| 343 | return $self->{_header}->fullname(); | ||
| 344 | } | ||
| 345 | |||
| 346 | sub as_formated_string { | ||
| 347 | my ($self, $format) = @_; | ||
| 348 | croak "Not a class method" unless ref $self; | ||
| 349 | |||
| 350 | return $self->{_header}->queryformat($format); | ||
| 351 | } | ||
| 352 | |||
| 353 | sub _to_number { | ||
| 354 | return refaddr($_[0]); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub compare { | ||
| 358 | my ($self, $package) = @_; | ||
| 359 | croak "Not a class method" unless ref $self; | ||
| 360 | croak "Not a __PACKAGE__ object" unless | ||
| 361 | blessed $package && $package->isa(__PACKAGE__); | ||
| 362 | |||
| 363 | return $self->{_header}->compare_pkg($package->{_header}); | ||
| 364 | } | ||
| 365 | |||
| 366 | sub satisfy_range { | ||
| 367 | my ($self, $range) = @_; | ||
| 368 | croak "Not a class method" unless ref $self; | ||
| 369 | |||
| 370 | return $self->check_ranges_compatibility( | ||
| 371 | '== ' . $self->get_revision(), | ||
| 372 | $range | ||
| 373 | ); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub sign { | ||
| 377 | my ($self, $name, $path, $passphrase) = @_; | ||
| 378 | croak "Not a class method" unless ref $self; | ||
| 379 | |||
| 380 | # check if parent directory is writable | ||
| 381 | my $parent = (File::Spec->splitpath($self->{_file}))[1]; | ||
| 382 | croak "Unsignable package, parent directory is read-only" | ||
| 383 | unless -w $parent; | ||
| 384 | |||
| 385 | my $command = | ||
| 386 | 'LC_ALL=C rpm --resign ' . $self->{_file} . | ||
| 387 | ' --define "_signature gpg"' . | ||
| 388 | ' --define "_gpg_name ' . $name . '"' . | ||
| 389 | ' --define "_gpg_path ' . $path . '"'; | ||
| 390 | my $expect = Expect->spawn($command) | ||
| 391 | or croak "Couldn't spawn command $command: $ERRNO\n"; | ||
| 392 | my @log; | ||
| 393 | $expect->log_stdout(0); | ||
| 394 | $expect->log_file(sub { push(@log, $_[0]); }); | ||
| 395 | $expect->expect(10, 'Enter pass phrase:') | ||
| 396 | or croak "Unexpected output: $log[-1]\n"; | ||
| 397 | $expect->send("$passphrase\n"); | ||
| 398 | |||
| 399 | $expect->soft_close(); | ||
| 400 | |||
| 401 | croak "Signature error: " . $log[-1] if $expect->exitstatus(); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub extract { | ||
| 405 | my ($self) = @_; | ||
| 406 | croak "Not a class method" unless ref $self; | ||
| 407 | |||
| 408 | system("rpm2cpio $self->{_file} | cpio -id >/dev/null 2>&1"); | ||
| 409 | } | ||
| 410 | |||
| 411 | =head1 COPYRIGHT AND LICENSE | ||
| 412 | |||
| 413 | Copyright (C) 2002-2006, YOURI project | ||
| 414 | |||
| 415 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 416 | |||
| 417 | =cut | ||
| 418 | |||
| 419 | 1; |