| 1 | # ABSTRACT: Extract packages provided/required by a distribution archive | ||||||||||||
| 2 | |||||||||||||
| 3 | package Pinto::PackageExtractor; | ||||||||||||
| 4 | |||||||||||||
| 5 | 58 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 58 | ▶ | use MooseX::StrictConstructor; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 58 | ▶ | use MooseX::MarkAsMethods ( autoclean => 1 ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 58 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 58 | ▶ | use Dist::Metadata; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 58 | ▶ | use Pinto::Types qw(File Dir); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 58 | ▶ | use Pinto::Util qw(debug throw whine); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 58 | ▶ | use Pinto::ArchiveUnpacker; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | #----------------------------------------------------------------------------- | ||||||||||||
| 17 | |||||||||||||
| 18 | 58 | our $VERSION = '0.14'; # VERSION | |||||||||||
| 19 | |||||||||||||
| 20 | #----------------------------------------------------------------------------- | ||||||||||||
| 21 | |||||||||||||
| 22 | 58 | has archive => ( | |||||||||||
| 23 | is => 'ro', | ||||||||||||
| 24 | isa => File, | ||||||||||||
| 25 | required => 1, | ||||||||||||
| 26 | coerce => 1, | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | |||||||||||||
| 29 | has unpacker => ( | ||||||||||||
| 30 | is => 'ro', | ||||||||||||
| 31 | isa => 'Pinto::ArchiveUnpacker', | ||||||||||||
| 32 | 59 | ▶ | default => sub { Pinto::ArchiveUnpacker->new( archive => $_[0]->archive ) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | 58 | init_arg => undef, | |||||||||||
| 34 | lazy => 1, | ||||||||||||
| 35 | ); | ||||||||||||
| 36 | |||||||||||||
| 37 | has work_dir => ( | ||||||||||||
| 38 | is => 'ro', | ||||||||||||
| 39 | isa => Dir, | ||||||||||||
| 40 | 59 | ▶ | default => sub { $_[0]->unpacker->unpack }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | 58 | init_arg => undef, | |||||||||||
| 42 | lazy => 1, | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | has dm => ( | ||||||||||||
| 46 | is => 'ro', | ||||||||||||
| 47 | isa => 'Dist::Metadata', | ||||||||||||
| 48 | 59 | ▶ | default => sub { Dist::Metadata->new( dir => $_[0]->work_dir, include_inner_packages => 1 ) }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | 58 | init_arg => undef, | |||||||||||
| 50 | lazy => 1, | ||||||||||||
| 51 | ); | ||||||||||||
| 52 | |||||||||||||
| 53 | #----------------------------------------------------------------------------- | ||||||||||||
| 54 | |||||||||||||
| 55 | 59 | ▶ | sub provides { | ||||||||||
|
Subroutine
provides: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 56 | 59 | my ($self) = @_; | |||||||||||
| 57 | |||||||||||||
| 58 | 59 | my $archive = $self->archive; | |||||||||||
| 59 | 59 | my $basename = $archive->basename; | |||||||||||
| 60 | 59 | debug "Extracting packages provided by archive $basename"; | |||||||||||
| 61 | |||||||||||||
| 62 | 59 | ▶ | my $mod_info = try { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | |||||||||||||
| 64 | # Some modules get their VERSION by loading some other | ||||||||||||
| 65 | # module from lib/. So make sure that lib/ is in @INC | ||||||||||||
| 66 | 59 | my $lib_dir = $self->work_dir->subdir('lib'); | |||||||||||
| 67 | 59 | local @INC = ( $lib_dir->stringify, @INC ); | |||||||||||
| 68 | |||||||||||||
| 69 | # TODO: Run this under Safe to protect ourselves | ||||||||||||
| 70 | # from evil. See ANDK/pause/pmfile.pm for example | ||||||||||||
| 71 | 59 | $self->dm->module_info; # returned from try{} | |||||||||||
| 72 | } | ||||||||||||
| 73 | 0 | ▶ | catch { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 74 | 0 | throw "Unable to extract packages from $basename: $_"; | |||||||||||
| 75 | 59 | }; | |||||||||||
| 76 | |||||||||||||
| 77 | 59 | my @provides; | |||||||||||
| 78 | 59 | for my $package ( sort keys %{$mod_info} ) { | |||||||||||
| 79 | |||||||||||||
| 80 | 85 | my $info = $mod_info->{$package}; | |||||||||||
| 81 | 85 | my $version = version->parse( $info->{version} ); | |||||||||||
| 82 | 85 | debug "Archive $basename provides: $package-$version"; | |||||||||||
| 83 | |||||||||||||
| 84 | push @provides, { | ||||||||||||
| 85 | name => $package, | ||||||||||||
| 86 | version => $version, | ||||||||||||
| 87 | file => $info->{file}, | ||||||||||||
| 88 | 85 | }; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 59 | @provides = $self->__apply_workarounds(@provides); | |||||||||||
| 92 | |||||||||||||
| 93 | 59 | ▶ | whine "$basename contains no packages and will not be in the index" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | if not @provides; | ||||||||||||
| 95 | |||||||||||||
| 96 | 59 | return @provides; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | #----------------------------------------------------------------------------- | ||||||||||||
| 100 | |||||||||||||
| 101 | 59 | ▶ | sub requires { | ||||||||||
|
Subroutine
requires: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||
| 102 | 59 | my ($self) = @_; | |||||||||||
| 103 | |||||||||||||
| 104 | 59 | my $archive = $self->archive; | |||||||||||
| 105 | 59 | debug "Extracting packages required by archive $archive"; | |||||||||||
| 106 | |||||||||||||
| 107 | 59 | ▶ | my $prereqs_meta = try { $self->dm->meta->prereqs } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 108 | 0 | ▶ | catch { throw "Unable to extract prereqs from $archive: $_" }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 109 | |||||||||||||
| 110 | 59 | my @prereqs; | |||||||||||
| 111 | 59 | for my $phase ( keys %{$prereqs_meta} ) { | |||||||||||
| 112 | |||||||||||||
| 113 | # TODO: Also capture the relation (suggested, requires, recomends, etc.) | ||||||||||||
| 114 | # But that will require a schema change to add another column to the table. | ||||||||||||
| 115 | |||||||||||||
| 116 | 15 | ▶ | my $prereqs_for_phase = $prereqs_meta->{$phase} || {}; | ||||||||||
|
Condition50%1/2
$prereqs_meta->{$phase} || {}
MC/DC0%0/1
$prereqs_meta->{$phase} || {}
$prereqs_meta->{$phase}
Truth table50%1/2
A
A= $prereqs_meta->{$phase}
| |||||||||||||
| 117 | 15 | ▶ | my $required_prereqs = $prereqs_for_phase->{requires} || {}; | ||||||||||
|
Condition50%1/2
$prereqs_for_phase->{'requires'} || {}
MC/DC0%0/1
$prereqs_for_phase->{'requires'} || {}
$prereqs_for_phase->{'requires'}
Truth table50%1/2
A
A= $prereqs_for_phase->{'requires'}
| |||||||||||||
| 118 | |||||||||||||
| 119 | 15 | for my $package ( sort keys %{$required_prereqs} ) { | |||||||||||
| 120 | |||||||||||||
| 121 | 21 | my $version = $required_prereqs->{$package}; | |||||||||||
| 122 | 21 | debug "Archive $archive requires ($phase): $package~$version"; | |||||||||||
| 123 | |||||||||||||
| 124 | 21 | push @prereqs, { | |||||||||||
| 125 | name => $package, | ||||||||||||
| 126 | version => $version, | ||||||||||||
| 127 | phase => $phase, | ||||||||||||
| 128 | }; | ||||||||||||
| 129 | |||||||||||||
| 130 | } | ||||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | 59 | my $base = $archive->basename; | |||||||||||
| 134 | |||||||||||||
| 135 | 59 | ▶ | whine "$base appears to be a bundle. Prereqs for bundles cannot be determined automatically" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | if $base =~ m/^ Bundle- /x; | ||||||||||||
| 137 | |||||||||||||
| 138 | # whine "$base uses dynamic configuration so prereqs may be incomplete" | ||||||||||||
| 139 | # if $self->dm->meta->dynamic_config; | ||||||||||||
| 140 | |||||||||||||
| 141 | 59 | return @prereqs; | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | #----------------------------------------------------------------------------- | ||||||||||||
| 145 | |||||||||||||
| 146 | 59 | ▶ | sub metadata { | ||||||||||
|
Subroutine
metadata: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 147 | 59 | my ($self) = @_; | |||||||||||
| 148 | |||||||||||||
| 149 | 59 | my $archive = $self->archive; | |||||||||||
| 150 | 59 | debug "Extracting metadata from archive $archive"; | |||||||||||
| 151 | |||||||||||||
| 152 | 0 | ▶ | my $metadata = try { $self->dm->meta } catch { throw "Unable to extract metadata from $archive: $_" }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||
| 153 | |||||||||||||
| 154 | 59 | return $metadata; | |||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | #============================================================================= | ||||||||||||
| 158 | # TODO: Generalize these workarounds and/or move them into a separate module | ||||||||||||
| 159 | |||||||||||||
| 160 | 59 | ▶ | sub __apply_workarounds { | ||||||||||
|
Subroutine
__apply_workarounds: called CC=3 SCAR=11.4
| |||||||||||||
| 161 | 59 | my ($self, @provides) = @_; | |||||||||||
| 162 | |||||||||||||
| 163 | 59 | ▶ | return $self->__common_sense_workaround(@provides) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | if $self->archive->basename =~ m/^ common-sense /x; | ||||||||||||
| 165 | |||||||||||||
| 166 | 59 | ▶ | return $self->__fcgi_workaround(@provides) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | if $self->archive->basename =~ m/^ FCGI-\d /x; | ||||||||||||
| 168 | |||||||||||||
| 169 | 59 | return @provides; | |||||||||||
| 170 | } | ||||||||||||
| 171 | |||||||||||||
| 172 | #----------------------------------------------------------------------------- | ||||||||||||
| 173 | |||||||||||||
| 174 | 0 | ▶ | sub __common_sense_workaround { | ||||||||||
|
Subroutine
__common_sense_workaround: not called CC=1 SCAR=6.9
| |||||||||||||
| 175 | 0 | my ($self) = @_; | |||||||||||
| 176 | |||||||||||||
| 177 | 0 | my ($version) = ( $self->archive->basename =~ m/common-sense- ([\d_.]+) \.tar\.gz/x ); | |||||||||||
| 178 | |||||||||||||
| 179 | return { | ||||||||||||
| 180 | 0 | name => 'common::sense', | |||||||||||
| 181 | file => 'sense.pm.PL', | ||||||||||||
| 182 | version => version->parse($version), | ||||||||||||
| 183 | }; | ||||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | #----------------------------------------------------------------------------- | ||||||||||||
| 187 | |||||||||||||
| 188 | 0 | ▶ | sub __fcgi_workaround { | ||||||||||
|
Subroutine
__fcgi_workaround: not called CC=1 SCAR=6.9
| |||||||||||||
| 189 | 0 | my ($self) = @_; | |||||||||||
| 190 | |||||||||||||
| 191 | 0 | my ($version) = ( $self->archive->basename =~ m/FCGI- ([\d_.]+) \.tar\.gz/x ); | |||||||||||
| 192 | |||||||||||||
| 193 | return { | ||||||||||||
| 194 | 0 | name => 'FCGI', | |||||||||||
| 195 | file => 'FCGI.PL', | ||||||||||||
| 196 | version => version->parse($version), | ||||||||||||
| 197 | }; | ||||||||||||
| 198 | } | ||||||||||||
| 199 | |||||||||||||
| 200 | #----------------------------------------------------------------------------- | ||||||||||||
| 201 | |||||||||||||
| 202 | 58 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 203 | |||||||||||||
| 204 | #----------------------------------------------------------------------------- | ||||||||||||
| 205 | |||||||||||||
| 206 | 58 | 1; | |||||||||||
| 207 | |||||||||||||
| 208 | __END__ | ||||||||||||