| 1 | package OrePAN2::Indexer; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Archive::Tar; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use CPAN::Meta 2.131560 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use File::Basename (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use File::Find qw( find ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use File::Temp qw( tempdir ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use File::pushd qw( pushd ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use IO::Zlib (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use OrePAN2::Index (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Parse::LocalDistribution (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use Role::Tiny::With; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 5 | with 'OrePAN2::Role::HasLogger'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use parent qw(Class::Accessor::Fast); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | our $VERSION = '2.0.0'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | __PACKAGE__->mk_accessors(qw(directory simple)); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ######################################################################## | ||||||||||||||||||||||||||||
| 30 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | ######################################################################## | ||||||||||||||||||||||||||||
| 32 | 5 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 33 | 5 | ▶ | my $options = ref $args[0] ? $args[0] : {@args}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | $options->{simple} //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"simple"} //= 0
MC/DC100%1/1
$options->{"simple"} //= 0
$options->{"simple"}
Truth table100%2/2
A
A= $options->{"simple"}
| |||||||||||||||||||||||||||||
| 35 | 5 | return $class->SUPER::new($options); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ######################################################################## | ||||||||||||||||||||||||||||
| 39 | 5 | ▶ | sub make_index { | ||||||||||||||||||||||||||
|
Subroutine
make_index: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | ######################################################################## | ||||||||||||||||||||||||||||
| 41 | 5 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 5 | my $no_compress = $args{no_compress}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 5 | my @files = $self->list_archive_files(); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | my $index = OrePAN2::Index->new(); | |||||||||||||||||||||||||||
| 48 | 5 | for my $archive_file (@files) { | |||||||||||||||||||||||||||
| 49 | 5 | $self->add_index( $index, $archive_file ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 5 | $self->write_index( $index, $no_compress ); | |||||||||||||||||||||||||||
| 52 | 5 | return $index; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ######################################################################## | ||||||||||||||||||||||||||||
| 56 | 5 | ▶ | sub add_index { | ||||||||||||||||||||||||||
|
Subroutine
add_index: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | ######################################################################## | ||||||||||||||||||||||||||||
| 58 | 5 | my ( $self, $index, $archive_file ) = @_; | |||||||||||||||||||||||||||
| 59 | 5 | my $tmpdir = tempdir( 'orepan2.XXXXXX', TMPDIR => 1, CLEANUP => 1 ); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 5 | my $tar = Archive::Tar->new; | |||||||||||||||||||||||||||
| 62 | 5 | $tar->read($archive_file); | |||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||
| 64 | 5 | my $guard = pushd($tmpdir); # setcwd alternative: extract relative to tmpdir | |||||||||||||||||||||||||||
| 65 | 5 | $tar->extract; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | my $provides = $self->scan_provides( $tmpdir, $archive_file ); | |||||||||||||||||||||||||||
| 69 | 5 | my $path = $self->_orepan_archive_path($archive_file); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 5 | foreach my $package ( sort keys %{$provides} ) { | |||||||||||||||||||||||||||
| 72 | 5 | $index->add_index( $package, $provides->{$package}->{version}, $path, ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | ######################################################################## | ||||||||||||||||||||||||||||
| 79 | 5 | ▶ | sub _orepan_archive_path { | ||||||||||||||||||||||||||
|
Subroutine
_orepan_archive_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | ######################################################################## | ||||||||||||||||||||||||||||
| 81 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 5 | my $archive_file = shift; | |||||||||||||||||||||||||||
| 83 | 5 | my $path = File::Spec->abs2rel( $archive_file, File::Spec->catfile( $self->directory, 'authors', 'id' ) ); | |||||||||||||||||||||||||||
| 84 | 5 | $path =~ s!\\!/!g; | |||||||||||||||||||||||||||
| 85 | 5 | return $path; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | ######################################################################## | ||||||||||||||||||||||||||||
| 89 | 5 | ▶ | sub scan_provides { | ||||||||||||||||||||||||||
|
Subroutine
scan_provides: called CC=7 SCAR=30.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | ######################################################################## | ||||||||||||||||||||||||||||
| 91 | 5 | my ( $self, $dir, $archive_file ) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | my $guard = pushd( glob("$dir/*") ); | |||||||||||||||||||||||||||
| 94 | 5 | for my $mfile ( 'META.json', 'META.yml', 'META.yaml' ) { | |||||||||||||||||||||||||||
| 95 | 5 | ▶ | next if !-f $mfile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 5 | my $meta = eval { CPAN::Meta->load_file($mfile) }; | |||||||||||||||||||||||||||
| 97 | 5 | ▶ | return $meta->{provides} if $meta && $meta->{provides}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$meta and $meta->{"provides"}
MC/DC0%0/2
$meta and $meta->{"provides"}
$meta
$meta->{"provides"}
Truth table33%1/3
A and B
A= $metaB= $meta->{"provides"}
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ($EVAL_ERROR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $self->log->warn( sprintf q{Error using '%s' from '%s'}, $mfile, $archive_file ); | |||||||||||||||||||||||||||
| 101 | 0 | $self->log->warn("$EVAL_ERROR"); | |||||||||||||||||||||||||||
| 102 | 0 | $self->log->warn('Attempting to continue...'); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | $self->log->info( sprintf q{Found META file in '%s' but it does not contain 'provides'}, $archive_file ); | |||||||||||||||||||||||||||
| 107 | 0 | $self->log->info('Scanning for provided modules...'); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $provides = eval { $self->_scan_provides('.') }; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | return $provides | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | if $provides; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | $self->log->warn("Error scanning: $EVAL_ERROR"); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Return empty provides. | ||||||||||||||||||||||||||||
| 116 | 0 | return {}; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ######################################################################## | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub _scan_provides { | ||||||||||||||||||||||||||
|
Subroutine
_scan_provides: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 121 | ######################################################################## | ||||||||||||||||||||||||||||
| 122 | 0 | my ( $self, $dir, $meta ) = @_; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $provides = Parse::LocalDistribution->new( { ALLOW_DEV_VERSION => 1 } )->parse($dir); | |||||||||||||||||||||||||||
| 125 | 0 | return $provides; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | ######################################################################## | ||||||||||||||||||||||||||||
| 129 | 5 | ▶ | sub write_index { | ||||||||||||||||||||||||||
|
Subroutine
write_index: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | ######################################################################## | ||||||||||||||||||||||||||||
| 131 | 5 | my ( $self, $index, $no_compress ) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | ▶ | my $pkgfname | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | = File::Spec->catfile( $self->directory, 'modules', $no_compress ? '02packages.details.txt' : '02packages.details.txt.gz' ); | ||||||||||||||||||||||||||||
| 135 | 5 | mkdir File::Basename::dirname($pkgfname); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 5 | my $fh = do { | |||||||||||||||||||||||||||
| 138 | 5 | ▶ | if ($no_compress) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | open my $fh, '>:raw', $pkgfname; | |||||||||||||||||||||||||||
| 140 | 1 | $fh; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||
| 143 | 4 | ▶ | IO::Zlib->new( $pkgfname, 'w' ) | ||||||||||||||||||||||||||
|
Condition50%1/2
'IO::Zlib'->new($pkgfname, 'w') || die("Cannot open $pkgfname for writing: $!\n")
MC/DC0%0/1
'IO::Zlib'->new($pkgfname, 'w') || die("Cannot open $pkgfname for writing: $!\n")
'IO::Zlib'->new($pkgfname, 'w')
Truth table50%1/2
A
A= 'IO::Zlib'->new($pkgfname, 'w')
| |||||||||||||||||||||||||||||
| 144 | or die "Cannot open $pkgfname for writing: $!\n"; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 5 | print {$fh} $index->as_string( { simple => $self->simple } ); | |||||||||||||||||||||||||||
| 149 | 5 | close $fh; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 5 | return; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | ######################################################################## | ||||||||||||||||||||||||||||
| 155 | 5 | ▶ | sub list_archive_files { | ||||||||||||||||||||||||||
|
Subroutine
list_archive_files: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | ######################################################################## | ||||||||||||||||||||||||||||
| 157 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 5 | my $authors_dir = File::Spec->catfile( $self->directory, 'authors' ); | |||||||||||||||||||||||||||
| 160 | 5 | ▶ | return () if !-d $authors_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | my @files; | |||||||||||||||||||||||||||
| 163 | find( | ||||||||||||||||||||||||||||
| 164 | 33 | ▶ | { wanted => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 165 | 33 | ▶ | return if $_ !~ m{ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | (?: | ||||||||||||||||||||||||||||
| 167 | [.]tar[.]gz | ||||||||||||||||||||||||||||
| 168 | | [.]tgz | ||||||||||||||||||||||||||||
| 169 | | [.]zip | ||||||||||||||||||||||||||||
| 170 | ) | ||||||||||||||||||||||||||||
| 171 | \z}xsm; | ||||||||||||||||||||||||||||
| 172 | 5 | push @files, $_; | |||||||||||||||||||||||||||
| 173 | }, | ||||||||||||||||||||||||||||
| 174 | 5 | no_chdir => 1, | |||||||||||||||||||||||||||
| 175 | }, | ||||||||||||||||||||||||||||
| 176 | $authors_dir | ||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Sort files by modication time so that we can index distributions from | ||||||||||||||||||||||||||||
| 180 | # earliest to latest version. | ||||||||||||||||||||||||||||
| 181 | 5 | my @sorted_files = reverse sort { -M $a <=> -M $b } @files; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 5 | return @sorted_files; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 5 | 1; | |||||||||||||||||||||||||||