| 1 | package MyCPAN::Indexer::CPANMiniInject; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use parent qw(MyCPAN::Indexer::Component); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use vars qw($VERSION $logger); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | $VERSION = '1.282'; | ||
| 8 | |||
| 9 | 1 | ▶ | use File::Basename; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use File::Spec::Functions qw(catfile); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Log::Log4perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use MyCPAN::Indexer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use YAML; | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | MyCPAN::Indexer::CPANMiniInject - Do the indexing, and put the dists in a MiniCPAN | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | Use this in backpan_indexer.pl by specifying it as the queue class: | ||
| 24 | |||
| 25 | # in backpan_indexer.config | ||
| 26 | worker_class MyCPAN::Indexer::CPANMiniInject | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This class takes a distribution and analyses it. Once it knows the modules | ||
| 31 | inside the distribution, it adds the distribution to a CPAN::Mini::Inject | ||
| 32 | staging repository. This portion specifically does not inject the modules | ||
| 33 | into the MiniCPAN. The injection has to happen after all of the workers | ||
| 34 | have finished. | ||
| 35 | |||
| 36 | =head2 Configuration | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item minicpan_inject_config | ||
| 41 | |||
| 42 | The location of the configuration file for CPAN::Mini::Config | ||
| 43 | |||
| 44 | =back | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | =head2 Methods | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item get_task( $Notes ) | ||
| 53 | |||
| 54 | C<get_task> sets the C<child_task> key in the C<$Notes> hash reference. The | ||
| 55 | value is a code reference that takes a distribution path as its only | ||
| 56 | argument and indexes that distribution. | ||
| 57 | |||
| 58 | See L<MyCPAN::Indexer::Tutorial> for details about what C<get_task> expects | ||
| 59 | and should do. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | BEGIN { | ||
| 64 | $logger = Log::Log4perl->get_logger( 'Worker' ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub component_type { $_[0]->worker_type } | ||
| 68 | |||
| 69 | sub get_task | ||
| 70 | { | ||
| 71 | my( $self ) = @_; | ||
| 72 | |||
| 73 | my $child_task = sub { | ||
| 74 | my $dist = shift; | ||
| 75 | |||
| 76 | my $basename = $self->_check_for_previous_result( $dist ); | ||
| 77 | return unless $basename; | ||
| 78 | |||
| 79 | my $Config = $self->get_config; | ||
| 80 | |||
| 81 | $logger->info( "Child [$$] processing $dist\n" ); | ||
| 82 | |||
| 83 | my $indexer = $self->get_coordinator->get_component( 'indexer' ); | ||
| 84 | |||
| 85 | unless( chdir $Config->temp_dir ) | ||
| 86 | { | ||
| 87 | $logger->error( "Could not change to " . $Config->temp_dir . " : $!\n" ); | ||
| 88 | exit 255; | ||
| 89 | } | ||
| 90 | |||
| 91 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 92 | alarm( $Config->alarm || 15 ); | ||
| 93 | my $info = eval { $indexer->run( $dist ) }; | ||
| 94 | |||
| 95 | unless( defined $info ) | ||
| 96 | { | ||
| 97 | $logger->error( "run failed: $@" ); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | elsif( ! eval { $info->run_info( 'completed' ) } ) | ||
| 101 | { | ||
| 102 | $logger->error( "$basename did not complete\n" ); | ||
| 103 | $self->_copy_bad_dist( $info ) if $Config->copy_bad_dists; | ||
| 104 | } | ||
| 105 | |||
| 106 | alarm 0; | ||
| 107 | |||
| 108 | $self->_add_run_info( $info ); | ||
| 109 | |||
| 110 | $self->get_note( 'reporter' )->( $info ); | ||
| 111 | |||
| 112 | $logger->debug( "Child [$$] process done" ); | ||
| 113 | |||
| 114 | 1; | ||
| 115 | }; | ||
| 116 | |||
| 117 | $self->set_note( 'child_task', $child_task ); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _copy_bad_dist | ||
| 121 | { | ||
| 122 | my( $self, $info ) = @_; | ||
| 123 | |||
| 124 | my $config = $self->get_config; | ||
| 125 | |||
| 126 | if( my $bad_dist_dir = $config->copy_bad_dists ) | ||
| 127 | { | ||
| 128 | my $dist_file = $info->dist_info( 'dist_file' ); | ||
| 129 | my $basename = $info->dist_info( 'dist_basename' ); | ||
| 130 | my $new_name = catfile( $bad_dist_dir, $basename ); | ||
| 131 | |||
| 132 | unless( -e $new_name ) | ||
| 133 | { | ||
| 134 | $logger->debug( "Copying bad dist" ); | ||
| 135 | |||
| 136 | my( $in, $out ); | ||
| 137 | |||
| 138 | unless( open $in, "<", $dist_file ) | ||
| 139 | { | ||
| 140 | $logger->fatal( "Could not open bad dist to $dist_file: $!" ); | ||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | unless( open $out, ">", $new_name ) | ||
| 145 | { | ||
| 146 | $logger->fatal( "Could not copy bad dist to $new_name: $!" ); | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | while( <$in> ) { print { $out } $_ } | ||
| 151 | close $in; | ||
| 152 | close $out; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub _check_for_previous_result | ||
| 158 | { | ||
| 159 | my( $self ) = @_; | ||
| 160 | |||
| 161 | my $Config = $self->get_config; | ||
| 162 | |||
| 163 | my $dist = $self->get_dist_info( 'filename' ); | ||
| 164 | |||
| 165 | ( my $basename = basename( $dist ) ) =~ s/\.(tgz|tar\.gz|zip)$//; | ||
| 166 | |||
| 167 | my $yml_dir = catfile( $Config->report_dir, "meta" ); | ||
| 168 | my $yml_error_dir = catfile( $Config->report_dir, "meta-errors" ); | ||
| 169 | |||
| 170 | my $yml_path = catfile( $yml_dir, "$basename.yml" ); | ||
| 171 | my $yml_error_path = catfile( $yml_error_dir, "$basename.yml" ); | ||
| 172 | |||
| 173 | if( my @path = grep { -e } ( $yml_path, $yml_error_path ) ) | ||
| 174 | { | ||
| 175 | $logger->debug( "Found run output for $basename in $path[0]. Skipping...\n" ); | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | |||
| 179 | return $basename; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub _add_run_info | ||
| 183 | { | ||
| 184 | my( $self, $info ) = @_; | ||
| 185 | |||
| 186 | my $Config = $self->get_config; | ||
| 187 | |||
| 188 | return unless eval { $info->can( 'set_run_info' ) }; | ||
| 189 | |||
| 190 | $info->set_run_info( $_, $Config->get( $_ ) ) | ||
| 191 | foreach ( $Config->directives ); | ||
| 192 | |||
| 193 | $info->set_run_info( 'uuid', $Config->UUID ); | ||
| 194 | |||
| 195 | $info->set_run_info( 'child_pid', $$ ); | ||
| 196 | $info->set_run_info( 'parent_pid', getppid ); | ||
| 197 | |||
| 198 | $info->set_run_info( 'ENV', \%ENV ); | ||
| 199 | |||
| 200 | return 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | =back | ||
| 204 | |||
| 205 | |||
| 206 | =head1 SEE ALSO | ||
| 207 | |||
| 208 | MyCPAN::Indexer, MyCPAN::Indexer::Tutorial | ||
| 209 | |||
| 210 | =head1 SOURCE AVAILABILITY | ||
| 211 | |||
| 212 | This code is in Github: | ||
| 213 | |||
| 214 | git://github.com/briandfoy/mycpan-indexer.git | ||
| 215 | |||
| 216 | =head1 AUTHOR | ||
| 217 | |||
| 218 | brian d foy, C<< <bdfoy@cpan.org> >> | ||
| 219 | |||
| 220 | =head1 COPYRIGHT AND LICENSE | ||
| 221 | |||
| 222 | Copyright © 2008-2018, brian d foy <bdfoy@cpan.org>. All rights reserved. | ||
| 223 | |||
| 224 | You may redistribute this under the terms of the Artistic License 2.0. | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | 1; | ||