| 1 | package OrePAN2::S3; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Amazon::Credentials; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Amazon::S3::Lite; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Carp; | ||
| 9 | use CLI::Simple::Utils qw(choose); | ||
| 10 | use CLI::Simple::Constants qw(:booleans); | ||
| 11 | use Cwd qw(abs_path); | ||
| 12 | use Data::Dumper; | ||
| 13 | use English qw(-no_match_vars); | ||
| 14 | use Encode qw(encode_utf8); | ||
| 15 | use File::Basename qw(basename dirname); | ||
| 16 | use File::ShareDir qw(dist_dir); | ||
| 17 | use File::Copy; | ||
| 18 | use File::Temp qw(tempfile tempdir); | ||
| 19 | use JSON; | ||
| 20 | use List::Util qw(pairs); | ||
| 21 | use Scalar::Util qw(openhandle reftype); | ||
| 22 | |||
| 23 | use Readonly; | ||
| 24 | |||
| 25 | Readonly::Scalar our $PACKAGES_DETAILS_INDEX => '02packages.details.txt.gz'; | ||
| 26 | Readonly::Scalar our $DEFAULT_CONFIG => sprintf '%s/%s', $ENV{HOME} // q{}, '/.orepan2-s3.json'; | ||
| 27 | Readonly::Scalar our $METACPAN_URL => 'https://metacpan.org/pod'; | ||
| 28 | Readonly::Scalar our $AUTHOR_PATH => 'D/DU/DUMMY'; | ||
| 29 | |||
| 30 | use Role::Tiny::With; | ||
| 31 | |||
| 32 | with 'OrePAN2::S3::Role::Inject'; | ||
| 33 | with 'OrePAN2::S3::Role::Delete'; | ||
| 34 | with 'OrePAN2::S3::Role::Upload'; | ||
| 35 | with 'OrePAN2::S3::Role::UploadArtifacts'; | ||
| 36 | |||
| 37 | use parent qw(CLI::Simple); | ||
| 38 | |||
| 39 | our $VERSION = '2.1.2'; | ||
| 40 | our $GIT_SHA = 'b44f2719bda3edd84aee992ea81b9a86c52cc492'; | ||
| 41 | our $GIT_DIRTY = 'b44f2719bda3edd84aee992ea81b9a86c52cc492'; | ||
| 42 | |||
| 43 | our %DOC_INDEX; | ||
| 44 | |||
| 45 | __PACKAGE__->use_log4perl( level => 'info' ); | ||
| 46 | |||
| 47 | caller or __PACKAGE__->main(); | ||
| 48 | |||
| 49 | ######################################################################## | ||
| 50 | sub slurp_file { | ||
| 51 | ######################################################################## | ||
| 52 | my ( $file, $json ) = @_; | ||
| 53 | |||
| 54 | local $RS = undef; | ||
| 55 | |||
| 56 | my $content; | ||
| 57 | |||
| 58 | if ( openhandle $file ) { | ||
| 59 | $content = <$file>; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | open my $fh, '<', $file | ||
| 63 | or die "could not open $file for reading: $OS_ERROR"; | ||
| 64 | |||
| 65 | $content = <$fh>; | ||
| 66 | |||
| 67 | close $fh; | ||
| 68 | } | ||
| 69 | |||
| 70 | return JSON->new->decode($content) | ||
| 71 | if $json; | ||
| 72 | |||
| 73 | return wantarray ? split /\n/xsm, $content : $content; | ||
| 74 | } | ||
| 75 | |||
| 76 | ######################################################################## | ||
| 77 | sub write_config { | ||
| 78 | ######################################################################## | ||
| 79 | my ( $self, $config ) = @_; | ||
| 80 | |||
| 81 | my $file = $self->get_config_file; | ||
| 82 | |||
| 83 | croak "ERROR: no config file set or config not found\n" | ||
| 84 | if !$file || !-e $file; | ||
| 85 | |||
| 86 | croak "ERROR: $file is not writable\n" | ||
| 87 | if !-w $file; | ||
| 88 | |||
| 89 | my $full_config = slurp_file( $file, $TRUE ); # slurp as JSON | ||
| 90 | my $profile_name //= $self->get_profile_name // 'default'; | ||
| 91 | |||
| 92 | $full_config->{$profile_name} = $config; | ||
| 93 | |||
| 94 | open my $fh, '>', $file | ||
| 95 | or croak "ERROR: Could not open $file for writing: $OS_ERROR\n"; | ||
| 96 | |||
| 97 | print {$fh} JSON->new->pretty->encode($full_config); | ||
| 98 | |||
| 99 | close $fh; | ||
| 100 | |||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | ######################################################################## | ||
| 105 | sub fetch_config { | ||
| 106 | ######################################################################## | ||
| 107 | my ( $self, $profile_name ) = @_; | ||
| 108 | |||
| 109 | return $self->get_config | ||
| 110 | if $self->get_config; | ||
| 111 | |||
| 112 | my $file = $self->get_config_file; | ||
| 113 | |||
| 114 | croak "ERROR: no config file specified\n" | ||
| 115 | if !$file; | ||
| 116 | |||
| 117 | $file = abs_path($file); | ||
| 118 | |||
| 119 | die "$file not found\n" | ||
| 120 | if !-e $file; | ||
| 121 | |||
| 122 | $profile_name //= $self->get_profile_name // 'default'; | ||
| 123 | $self->get_logger->debug( sprintf 'using profile: "%s" from: "%s"', $profile_name, $file ); | ||
| 124 | |||
| 125 | $self->set_config_file($file); | ||
| 126 | |||
| 127 | my $config = eval { return JSON->new->decode( scalar slurp_file($file) ); }; | ||
| 128 | |||
| 129 | croak "ERROR: could not read config file ($file)\n$EVAL_ERROR" | ||
| 130 | if !$config || $EVAL_ERROR; | ||
| 131 | |||
| 132 | if ( $config->{$profile_name} && ref $config->{$profile_name} ) { | ||
| 133 | $config = $config->{$profile_name}; | ||
| 134 | } | ||
| 135 | elsif ( $config->{$profile_name} ) { | ||
| 136 | $config = $config->{ $config->{$profile_name} }; | ||
| 137 | } | ||
| 138 | |||
| 139 | croak sprintf "ERROR: %s not a valid profile name\n", $profile_name | ||
| 140 | if !ref $config; | ||
| 141 | |||
| 142 | $self->set_config($config); | ||
| 143 | |||
| 144 | return $config; | ||
| 145 | } | ||
| 146 | |||
| 147 | ######################################################################## | ||
| 148 | sub has_packages_version_index { | ||
| 149 | ######################################################################## | ||
| 150 | my ($self) = @_; | ||
| 151 | |||
| 152 | my $config = $self->get_config; | ||
| 153 | |||
| 154 | return $config->{packages_version_index}; | ||
| 155 | } | ||
| 156 | |||
| 157 | ######################################################################## | ||
| 158 | sub init_s3 { | ||
| 159 | ######################################################################## | ||
| 160 | my ($self) = @_; | ||
| 161 | |||
| 162 | my $config = $self->get_config; | ||
| 163 | my $credentials = $self->get_credentials; | ||
| 164 | |||
| 165 | my $region = $config->{AWS}{region} // $credentials->get_region // $ENV{AWS_REGION} // $ENV{AWS_DEFAULT_REGION} | ||
| 166 | // 'us-east-1'; | ||
| 167 | |||
| 168 | my $s3 = Amazon::S3::Lite->new( | ||
| 169 | { region => $region, | ||
| 170 | credentials => $credentials, | ||
| 171 | } | ||
| 172 | ); | ||
| 173 | |||
| 174 | $self->set_s3($s3); | ||
| 175 | |||
| 176 | return; | ||
| 177 | } | ||
| 178 | |||
| 179 | ######################################################################## | ||
| 180 | sub cmd_download_version_index { | ||
| 181 | ######################################################################## | ||
| 182 | my ($self) = @_; | ||
| 183 | |||
| 184 | my $packages_index = $self->has_packages_version_index; | ||
| 185 | |||
| 186 | die "ERROR: no 'packages_version_index' defined in configuration\n" | ||
| 187 | if !$packages_index; | ||
| 188 | |||
| 189 | require DarkPAN::Indexer; | ||
| 190 | |||
| 191 | my $indexer = DarkPAN::Indexer->new( config => $self->get_config ); | ||
| 192 | |||
| 193 | my $database = $indexer->get_storage->retrieve_index($packages_index); | ||
| 194 | |||
| 195 | die sprintf "ERROR: could not download %s\n%s", $packages_index, $EVAL_ERROR | ||
| 196 | if $EVAL_ERROR; | ||
| 197 | |||
| 198 | my $packages_db = basename( $packages_index, '.gz' ); | ||
| 199 | move $database, $packages_db; | ||
| 200 | |||
| 201 | if ( -e $packages_db ) { | ||
| 202 | print {*STDOUT} "$packages_db\n"; | ||
| 203 | return $SUCCESS; | ||
| 204 | } | ||
| 205 | |||
| 206 | die sprintf "ERROR: could not move %s => %s\n", $database, $packages_db; | ||
| 207 | } | ||
| 208 | |||
| 209 | ######################################################################## | ||
| 210 | sub fetch_orepan_index { | ||
| 211 | ######################################################################## | ||
| 212 | my ($self) = @_; | ||
| 213 | |||
| 214 | my ( $fh, $filename ) = tempfile( | ||
| 215 | 'XXXXXX', | ||
| 216 | SUFFIX => '.gz', | ||
| 217 | UNLINK => $FALSE, | ||
| 218 | DIR => '/tmp', | ||
| 219 | ); | ||
| 220 | |||
| 221 | my $config = $self->get_config; | ||
| 222 | |||
| 223 | my $key = sprintf '%s/modules/%s', $config->{AWS}{prefix}, $PACKAGES_DETAILS_INDEX; | ||
| 224 | $self->get_s3->get_object( $self->get_bucket_name, $key, filename => $filename ); | ||
| 225 | |||
| 226 | return $filename; | ||
| 227 | } | ||
| 228 | |||
| 229 | ######################################################################## | ||
| 230 | sub cmd_invalidate_index { | ||
| 231 | ######################################################################## | ||
| 232 | my ($self) = @_; | ||
| 233 | |||
| 234 | my (@paths) = $self->get_args; | ||
| 235 | |||
| 236 | my $rsp = eval { $self->_invalidate_index(@paths); }; | ||
| 237 | |||
| 238 | if ( !$rsp || $EVAL_ERROR ) { | ||
| 239 | print {*STDERR} $EVAL_ERROR; | ||
| 240 | return $FAILURE; | ||
| 241 | } | ||
| 242 | |||
| 243 | print {*STDOUT} JSON->new->pretty->encode($rsp); | ||
| 244 | |||
| 245 | return $SUCCESS; | ||
| 246 | } | ||
| 247 | |||
| 248 | ######################################################################## | ||
| 249 | sub _invalidate_index { | ||
| 250 | ######################################################################## | ||
| 251 | my ( $self, @invalidation_paths ) = @_; | ||
| 252 | |||
| 253 | my $config = $self->get_config; | ||
| 254 | |||
| 255 | my $distribution_id = $config->{CloudFront}{DistributionId}; | ||
| 256 | |||
| 257 | croak "ERROR: no CloudFront configuration found\n" | ||
| 258 | if !$distribution_id; | ||
| 259 | |||
| 260 | eval { require Amazon::API::CloudFront; }; | ||
| 261 | |||
| 262 | croak "ERROR: install Amazon::API::CloudFront\n" | ||
| 263 | if $EVAL_ERROR; | ||
| 264 | |||
| 265 | push @invalidation_paths, @{ $config->{CloudFront}{InvalidationPaths} // [] }; | ||
| 266 | push @invalidation_paths, '/index.html', '/docs/*', '/orepan2/modules/02packages.details.txt.gz'; | ||
| 267 | |||
| 268 | my $packages_index = $self->has_packages_version_index; | ||
| 269 | |||
| 270 | if ($packages_index) { | ||
| 271 | push @invalidation_paths, "/$packages_index"; | ||
| 272 | } | ||
| 273 | |||
| 274 | my $invalidation_batch = $self->create_invalidation_batch( $distribution_id, \@invalidation_paths ); | ||
| 275 | |||
| 276 | local $ENV{AWS_PROFILE} = $self->get_profile // $config->{AWS}{profile}; | ||
| 277 | |||
| 278 | my $cf = Amazon::API::CloudFront->new; | ||
| 279 | |||
| 280 | return $cf->CreateInvalidation($invalidation_batch); | ||
| 281 | } | ||
| 282 | |||
| 283 | ######################################################################## | ||
| 284 | sub create_invalidation_batch { | ||
| 285 | ######################################################################## | ||
| 286 | my ( $self, $distribution_id, $items, $reference ) = @_; | ||
| 287 | |||
| 288 | if ( !$reference ) { | ||
| 289 | require Data::UUID; | ||
| 290 | $reference = Data::UUID->new->create_str; | ||
| 291 | } | ||
| 292 | |||
| 293 | croak "ERROR: items missing\n" | ||
| 294 | if !$items; | ||
| 295 | |||
| 296 | croak "ERROR: items must be an array\n" | ||
| 297 | if !ref $items || reftype($items) ne 'ARRAY'; | ||
| 298 | |||
| 299 | croak "ERROR: no items to invalidate\n" | ||
| 300 | if !@{$items}; | ||
| 301 | |||
| 302 | my $invalidation_batch = { | ||
| 303 | DistributionId => $distribution_id, | ||
| 304 | InvalidationBatch => { | ||
| 305 | CallerReference => $reference, | ||
| 306 | Paths => { | ||
| 307 | Items => $items, | ||
| 308 | Quantity => scalar @{$items}, | ||
| 309 | } | ||
| 310 | } | ||
| 311 | }; | ||
| 312 | |||
| 313 | return $invalidation_batch; | ||
| 314 | } | ||
| 315 | |||
| 316 | ######################################################################## | ||
| 317 | sub _upload_html { | ||
| 318 | ######################################################################## | ||
| 319 | my ( $self, $file, $key ) = @_; | ||
| 320 | |||
| 321 | croak sprintf '%s not found', $file | ||
| 322 | if !ref $file && !-e $file; | ||
| 323 | |||
| 324 | my $content = ref $file ? ${$file} : slurp_file($file); | ||
| 325 | my $encoded = encode_utf8($content); | ||
| 326 | |||
| 327 | $self->get_s3->put_object( $self->get_bucket_name, $key, $encoded, content_type => 'text/html', ); | ||
| 328 | |||
| 329 | $self->get_logger->info( sprintf 'adding %s to document index', $key ); | ||
| 330 | |||
| 331 | $DOC_INDEX{$key} = q{/} . $key; | ||
| 332 | |||
| 333 | return; | ||
| 334 | } | ||
| 335 | |||
| 336 | # should be a method of OrePAN2::Index (someday) | ||
| 337 | ######################################################################## | ||
| 338 | sub _packages_for_archive { | ||
| 339 | ######################################################################## | ||
| 340 | my ( $self, $index, $archive_path ) = @_; | ||
| 341 | |||
| 342 | return grep { | ||
| 343 | my ( undef, $path ) = $index->lookup($_); | ||
| 344 | $path eq $archive_path; | ||
| 345 | } $index->packages; | ||
| 346 | } | ||
| 347 | |||
| 348 | ######################################################################## | ||
| 349 | # COMMANDS | ||
| 350 | ######################################################################## | ||
| 351 | |||
| 352 | ######################################################################## | ||
| 353 | sub cmd_list_packages { | ||
| 354 | ######################################################################## | ||
| 355 | my ($self) = @_; | ||
| 356 | |||
| 357 | my $s3 = $self->get_s3; | ||
| 358 | |||
| 359 | my $config = $self->get_config; | ||
| 360 | |||
| 361 | my $prefix = $config->{AWS}{prefix}; | ||
| 362 | my $path = sprintf '%s/authors/id/%s', $prefix, $self->get_author_path; | ||
| 363 | |||
| 364 | my @objects = $s3->list_all_objects_v2( $self->get_bucket_name, prefix => $path ); | ||
| 365 | |||
| 366 | my %packages; | ||
| 367 | |||
| 368 | foreach my $o (@objects) { | ||
| 369 | my $key = $o->{key}; | ||
| 370 | |||
| 371 | if ( my ($package_prefix) = $key =~ /^(.*?)[-]([\d.]+)[.]tar[.]gz$/xsm ) { | ||
| 372 | $packages{$1} //= []; | ||
| 373 | push @{ $packages{$1} }, $2; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | return $SUCCESS | ||
| 378 | if !keys %packages; | ||
| 379 | |||
| 380 | my $has_ascii_table = eval { | ||
| 381 | require Text::ASCIITable; ## scandeps: suggests | ||
| 382 | 1; | ||
| 383 | }; | ||
| 384 | |||
| 385 | if ( !$has_ascii_table || $self->get_format eq 'json' ) { | ||
| 386 | print {*STDOUT} JSON->new->pretty->encode( \%packages ); | ||
| 387 | return $SUCCESS; | ||
| 388 | } | ||
| 389 | |||
| 390 | my $has_pager = $self->_has_pager; | ||
| 391 | |||
| 392 | my $t = Text::ASCIITable->new( { headingText => sprintf 'DarkPAN: https://%s/%s', $self->get_bucket_name, $prefix } ); | ||
| 393 | $t->setCols( 'Packages', 'Versions' ); | ||
| 394 | |||
| 395 | foreach my $p ( sort keys %packages ) { | ||
| 396 | $t->addRow( basename($p), join "\n", sort @{ $packages{$p} } ); | ||
| 397 | } | ||
| 398 | |||
| 399 | print {*STDOUT} $t; | ||
| 400 | |||
| 401 | return $SUCCESS; | ||
| 402 | } | ||
| 403 | |||
| 404 | ######################################################################## | ||
| 405 | sub _has_pager { | ||
| 406 | ######################################################################## | ||
| 407 | my ($self) = @_; | ||
| 408 | |||
| 409 | return eval { | ||
| 410 | return | ||
| 411 | if !$self->get_cli_pager; | ||
| 412 | |||
| 413 | require IO::Interactive; | ||
| 414 | |||
| 415 | return | ||
| 416 | if !IO::Interactive::is_interactive | ||
| 417 | |||
| 418 | require IO::Pager; | ||
| 419 | |||
| 420 | IO::Pager->new(*STDOUT); | ||
| 421 | |||
| 422 | 1; | ||
| 423 | }; | ||
| 424 | } | ||
| 425 | |||
| 426 | ######################################################################## | ||
| 427 | sub cmd_upload_site_index { | ||
| 428 | ######################################################################## | ||
| 429 | my ( $self, $index ) = @_; | ||
| 430 | |||
| 431 | if ( !$index ) { | ||
| 432 | ($index) = $self->get_args(); | ||
| 433 | } | ||
| 434 | |||
| 435 | $index //= 'index.html'; | ||
| 436 | |||
| 437 | return $self->_upload_html( $index, 'index.html' ); | ||
| 438 | } | ||
| 439 | |||
| 440 | ######################################################################## | ||
| 441 | sub cmd_show_orepan_index { | ||
| 442 | ######################################################################## | ||
| 443 | my ($self) = @_; | ||
| 444 | |||
| 445 | my $file = $self->fetch_orepan_index; | ||
| 446 | |||
| 447 | require OrePAN2::Index; | ||
| 448 | |||
| 449 | my $index = OrePAN2::Index->new(); | ||
| 450 | |||
| 451 | $index->load($file); | ||
| 452 | |||
| 453 | my $listing = $index->as_string; | ||
| 454 | |||
| 455 | unlink $file; | ||
| 456 | |||
| 457 | return $self->send_output($listing); | ||
| 458 | } | ||
| 459 | |||
| 460 | ######################################################################## | ||
| 461 | sub cmd_create_docs { | ||
| 462 | ######################################################################## | ||
| 463 | my ($self) = @_; | ||
| 464 | |||
| 465 | require DarkPAN::Utils; | ||
| 466 | |||
| 467 | my ($distribution) = $self->get_args(); | ||
| 468 | $distribution //= $self->get_distribution(); | ||
| 469 | |||
| 470 | die "use -d or pass distribution name as an argument\n" | ||
| 471 | if !$distribution; | ||
| 472 | |||
| 473 | my ( $key_prefix, $version ) = DarkPAN::Utils::parse_distribution_path($distribution); | ||
| 474 | |||
| 475 | my $dpu = choose { | ||
| 476 | if ( -e $distribution && $distribution !~ /^(?:http|D\/DU)/xsm ) { | ||
| 477 | require Archive::Tar; | ||
| 478 | |||
| 479 | my $tar = Archive::Tar->new; | ||
| 480 | $tar->read($distribution); | ||
| 481 | return DarkPAN::Utils->new( package => $tar ); | ||
| 482 | } | ||
| 483 | |||
| 484 | $distribution = basename($distribution); | ||
| 485 | |||
| 486 | if ( $distribution !~ /^http/xsm ) { | ||
| 487 | $distribution = sprintf '%s/%s', $self->get_author_path, $distribution; | ||
| 488 | } | ||
| 489 | |||
| 490 | my $orepan_url = $self->get_url // $self->get_config->{url}; | ||
| 491 | |||
| 492 | die "use --url or set url in config\n" | ||
| 493 | if !$orepan_url; | ||
| 494 | |||
| 495 | my $dpu = DarkPAN::Utils->new( base_url => $orepan_url ); | ||
| 496 | |||
| 497 | $dpu->fetch_package($distribution); | ||
| 498 | |||
| 499 | return $dpu; | ||
| 500 | }; | ||
| 501 | |||
| 502 | my $module_name = $key_prefix; | ||
| 503 | $module_name =~ s/\-/::/gxsm; | ||
| 504 | |||
| 505 | my $file = $dpu->extract_module( $distribution, $module_name ); | ||
| 506 | my $readme = $dpu->extract_file( sprintf '%s-%s/README.md', $key_prefix, $version ); | ||
| 507 | |||
| 508 | my $changelog = $dpu->extract_file( sprintf '%s-%s/Changes', $key_prefix, $version ); | ||
| 509 | $changelog //= $dpu->extract_file( sprintf '%s-%s/CHANGES', $key_prefix, $version ); | ||
| 510 | $changelog //= $dpu->extract_file( sprintf '%s-%s/ChangeLog', $key_prefix, $version ); | ||
| 511 | |||
| 512 | if ( $self->get_upload ) { | ||
| 513 | if ($file) { | ||
| 514 | $self->upload_html( | ||
| 515 | name => "$key_prefix.html", | ||
| 516 | content => $file, | ||
| 517 | prefix => $key_prefix, | ||
| 518 | wrap => $TRUE, | ||
| 519 | distribution => basename($distribution), | ||
| 520 | ); | ||
| 521 | } | ||
| 522 | |||
| 523 | if ($readme) { | ||
| 524 | $self->upload_html( | ||
| 525 | name => 'README.html', | ||
| 526 | markdown => $readme, | ||
| 527 | prefix => $key_prefix, | ||
| 528 | wrap => $TRUE, | ||
| 529 | ); | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | my $tar = Archive::Tar->new; | ||
| 534 | |||
| 535 | $tar->add_data( 'README.md', $readme // q{} ); | ||
| 536 | $tar->add_data( "$key_prefix.pm", $file // q{} ); | ||
| 537 | $tar->add_data( 'ChangeLog', $changelog // q{} ); | ||
| 538 | |||
| 539 | $tar->write( 'docs.tar.gz', $TRUE ); | ||
| 540 | |||
| 541 | return $SUCCESS; | ||
| 542 | } | ||
| 543 | |||
| 544 | ######################################################################## | ||
| 545 | sub upload_html { | ||
| 546 | ######################################################################## | ||
| 547 | my ( $self, %args ) = @_; | ||
| 548 | |||
| 549 | my ( $content, $prefix, $name, $markdown, $distribution ) = @args{qw(content prefix name markdown distribution)}; | ||
| 550 | |||
| 551 | my $perldoc_url_distros = $self->get_config->{perldoc_url_distros} // []; | ||
| 552 | my $perldoc_url_prefix; | ||
| 553 | |||
| 554 | if ($distribution) { | ||
| 555 | foreach my $p ( pairs @{$perldoc_url_distros} ) { | ||
| 556 | |||
| 557 | my ( $pattern, $flags ) = @{$p}; | ||
| 558 | my $qr = qr/(?$flags:$pattern)/; | ||
| 559 | next if $distribution !~ $qr; | ||
| 560 | |||
| 561 | $perldoc_url_prefix = $self->get_config->{perldoc_url_prefix}; | ||
| 562 | last; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | |||
| 566 | $perldoc_url_prefix //= $METACPAN_URL; | ||
| 567 | |||
| 568 | my $html = choose { | ||
| 569 | if ($markdown) { | ||
| 570 | require Text::Markdown::Discount; | ||
| 571 | |||
| 572 | Text::Markdown::Discount::markdown($markdown); | ||
| 573 | } | ||
| 574 | |||
| 575 | require DarkPAN::Utils::Docs; | ||
| 576 | |||
| 577 | my $docs = DarkPAN::Utils::Docs->new( | ||
| 578 | text => $content, | ||
| 579 | url_prefix => $perldoc_url_prefix, | ||
| 580 | ); | ||
| 581 | |||
| 582 | return $docs->get_html; | ||
| 583 | }; | ||
| 584 | |||
| 585 | if ( $args{wrap} ) { | ||
| 586 | $html = <<"END_OF_HTML"; | ||
| 587 | <!DOCTYPE HTML> | ||
| 588 | <html> | ||
| 589 | <head> | ||
| 590 | <title>README</title> | ||
| 591 | <link rel="stylesheet" href="/css/pod.css"> | ||
| 592 | </head> | ||
| 593 | <body> | ||
| 594 | $html | ||
| 595 | </body> | ||
| 596 | </html> | ||
| 597 | END_OF_HTML | ||
| 598 | } | ||
| 599 | |||
| 600 | my $key = sprintf 'docs/%s/%s', $prefix, $name; | ||
| 601 | |||
| 602 | $self->_upload_html( \$html, $key ); | ||
| 603 | |||
| 604 | return; | ||
| 605 | } | ||
| 606 | |||
| 607 | ######################################################################## | ||
| 608 | sub look_for_object { | ||
| 609 | ######################################################################## | ||
| 610 | my ( $self, $prefix, $name ) = @_; | ||
| 611 | |||
| 612 | $DOC_INDEX{ sprintf 'docs/%s/%s', $prefix, $name }; | ||
| 613 | |||
| 614 | } | ||
| 615 | |||
| 616 | ######################################################################## | ||
| 617 | sub cmd_create_site_index { | ||
| 618 | ######################################################################## | ||
| 619 | my ($self) = @_; | ||
| 620 | |||
| 621 | require DarkPAN::Utils; | ||
| 622 | |||
| 623 | my $config = $self->get_config; | ||
| 624 | |||
| 625 | my $file = $self->fetch_orepan_index; | ||
| 626 | |||
| 627 | my $repo = $self->parse_index($file); | ||
| 628 | |||
| 629 | unlink $file; | ||
| 630 | |||
| 631 | $self->get_logger->debug( Dumper( [ repo => $repo ] ) ); | ||
| 632 | |||
| 633 | no strict 'refs'; ## no critic | ||
| 634 | |||
| 635 | *{'utils::module_name'} = sub { | ||
| 636 | my ( $self, $distribution ) = @_; | ||
| 637 | |||
| 638 | my $module_name = basename("/$distribution"); | ||
| 639 | $module_name =~ s/-\d+[.].*$//xsm; | ||
| 640 | |||
| 641 | return $module_name; | ||
| 642 | }; | ||
| 643 | |||
| 644 | my $utils = bless {}, 'utils'; | ||
| 645 | |||
| 646 | my $sections = $config->{custom_sections} // {}; | ||
| 647 | |||
| 648 | foreach my $var ( keys %{$sections} ) { | ||
| 649 | my ( $pattern, $flags ) = @{ $sections->{$var} }; | ||
| 650 | |||
| 651 | my $qr = qr/(?$flags:$pattern)/; | ||
| 652 | $sections->{$var} = [ $pattern, $flags, $qr ]; | ||
| 653 | } | ||
| 654 | |||
| 655 | my %readme_links; | ||
| 656 | my %pod_links; | ||
| 657 | |||
| 658 | foreach my $distribution ( keys %{$repo} ) { | ||
| 659 | $self->get_logger->debug( 'distribution: ' . $distribution ); | ||
| 660 | |||
| 661 | my ($distribution_name) = DarkPAN::Utils::parse_distribution_path($distribution); | ||
| 662 | |||
| 663 | if ( !$distribution_name ) { | ||
| 664 | $self->get_logger->warn("WARN: could not get distribution name from $distribution"); | ||
| 665 | next; | ||
| 666 | } | ||
| 667 | |||
| 668 | my $readme = $self->look_for_object( $distribution_name, 'README.html' ); | ||
| 669 | |||
| 670 | my $pod = $self->look_for_object( $distribution_name, "${distribution_name}.html" ); | ||
| 671 | |||
| 672 | my $old_name = $distribution; | ||
| 673 | $distribution =~ s/^.*\/([^\/]+)/$1/xsm; | ||
| 674 | $repo->{$distribution} = delete $repo->{$old_name}; | ||
| 675 | |||
| 676 | if ($readme) { | ||
| 677 | $readme_links{$distribution} = $readme; | ||
| 678 | } | ||
| 679 | |||
| 680 | if ($pod) { | ||
| 681 | $pod_links{$distribution} = $pod; | ||
| 682 | } | ||
| 683 | |||
| 684 | if ($sections) { | ||
| 685 | foreach my $var ( keys %{$sections} ) { | ||
| 686 | if ( $distribution =~ $sections->{$var}->[2] ) { | ||
| 687 | $sections->{$var}->[3] //= {}; | ||
| 688 | $sections->{$var}->[3]->{$distribution} = delete $repo->{$distribution}; | ||
| 689 | } | ||
| 690 | } | ||
| 691 | } | ||
| 692 | } | ||
| 693 | |||
| 694 | my $params = { | ||
| 695 | utils => $utils, | ||
| 696 | repo => $repo, | ||
| 697 | readme_links => \%readme_links, | ||
| 698 | pod_links => \%pod_links, | ||
| 699 | localtime => scalar localtime, | ||
| 700 | map { ( $_ => $sections->{$_}->[3] ) } keys %{$sections}, | ||
| 701 | |||
| 702 | }; | ||
| 703 | |||
| 704 | $self->get_logger->trace( Dumper( [ params => $params ] ) ); | ||
| 705 | |||
| 706 | my $text = $self->get_template; | ||
| 707 | |||
| 708 | require Template; | ||
| 709 | |||
| 710 | my $template = Template->new(); | ||
| 711 | |||
| 712 | my $output = q{}; | ||
| 713 | |||
| 714 | $template->process( \$text, $params, \$output ) | ||
| 715 | or die $template->error(); | ||
| 716 | |||
| 717 | return $self->send_output($output) | ||
| 718 | if !$self->get_upload; | ||
| 719 | |||
| 720 | $self->_upload_html( \$output, 'index.html' ); | ||
| 721 | |||
| 722 | $self->get_logger->debug($output); | ||
| 723 | |||
| 724 | if ( $self->get_config->{CloudFront}{DistributionId} && $self->get_invalidate_index ) { | ||
| 725 | $self->_invalidate_index; | ||
| 726 | } | ||
| 727 | |||
| 728 | return $SUCCESS; | ||
| 729 | } | ||
| 730 | |||
| 731 | ######################################################################## | ||
| 732 | sub send_output { | ||
| 733 | ######################################################################## | ||
| 734 | my ( $self, $content ) = @_; | ||
| 735 | |||
| 736 | my $outfile = $self->get_output; | ||
| 737 | |||
| 738 | my $fh = eval { | ||
| 739 | |||
| 740 | return *STDOUT | ||
| 741 | if !$outfile; | ||
| 742 | |||
| 743 | open my $fh, '>', $outfile; | ||
| 744 | |||
| 745 | return $fh; | ||
| 746 | }; | ||
| 747 | |||
| 748 | die "could not open file for output\n$EVAL_ERROR" | ||
| 749 | if !$fh; | ||
| 750 | |||
| 751 | print {$fh} $content; | ||
| 752 | |||
| 753 | $outfile && close $fh; | ||
| 754 | |||
| 755 | return $SUCCESS; | ||
| 756 | } | ||
| 757 | |||
| 758 | ######################################################################## | ||
| 759 | sub parse_index { | ||
| 760 | ######################################################################## | ||
| 761 | my ( $self, $file ) = @_; | ||
| 762 | |||
| 763 | require OrePAN2::Index; | ||
| 764 | |||
| 765 | my $index = OrePAN2::Index->new(); | ||
| 766 | |||
| 767 | $index->load($file); | ||
| 768 | |||
| 769 | my $listing = $index->as_string; | ||
| 770 | $listing =~ s/^(.*)\n\n//xsm; | ||
| 771 | |||
| 772 | my %repo; | ||
| 773 | |||
| 774 | foreach ( split /\n/xsm, $listing ) { | ||
| 775 | my ( $module, $version, $package ) = split /\s+/xsm; | ||
| 776 | $repo{$package} //= []; | ||
| 777 | push @{ $repo{$package} }, [ $module, $version ]; | ||
| 778 | } | ||
| 779 | |||
| 780 | return \%repo; | ||
| 781 | } | ||
| 782 | |||
| 783 | ######################################################################## | ||
| 784 | sub cmd_download_orepan_index { | ||
| 785 | ######################################################################## | ||
| 786 | my ($self) = @_; | ||
| 787 | |||
| 788 | my $filename = eval { return $self->fetch_orepan_index(); }; | ||
| 789 | |||
| 790 | die "ERROR: Could not download $PACKAGES_DETAILS_INDEX\n$EVAL_ERROR" | ||
| 791 | if !$filename || !-s "$filename"; | ||
| 792 | |||
| 793 | print {*STDERR} "copying $filename => $PACKAGES_DETAILS_INDEX\n"; | ||
| 794 | |||
| 795 | copy $filename, $PACKAGES_DETAILS_INDEX | ||
| 796 | or die "ERROR: could not copy $filename -> $PACKAGES_DETAILS_INDEX\n"; | ||
| 797 | |||
| 798 | unlink $filename; | ||
| 799 | |||
| 800 | print {*STDOUT} $PACKAGES_DETAILS_INDEX . "\n"; | ||
| 801 | |||
| 802 | return; | ||
| 803 | } | ||
| 804 | |||
| 805 | ######################################################################## | ||
| 806 | sub init { | ||
| 807 | ######################################################################## | ||
| 808 | my ($self) = @_; | ||
| 809 | |||
| 810 | my $config = $self->fetch_config; | ||
| 811 | |||
| 812 | my $profile = $self->get_profile // $config->{AWS}{profile}; | ||
| 813 | |||
| 814 | my $dist = __PACKAGE__; | ||
| 815 | $dist =~ s/::/-/xsmg; | ||
| 816 | my $dist_dir = $self->set_dist_dir( dist_dir($dist) ); | ||
| 817 | |||
| 818 | my $credentials = Amazon::Credentials->new( profile => $profile ); | ||
| 819 | $self->set_credentials($credentials); | ||
| 820 | |||
| 821 | my $bucket_name = $self->get_bucket_name // $config->{AWS}->{bucket}; | ||
| 822 | |||
| 823 | $self->set_bucket_name($bucket_name); | ||
| 824 | |||
| 825 | $self->init_s3; | ||
| 826 | |||
| 827 | $self->_init_doc_index; | ||
| 828 | |||
| 829 | $self->set_author_path( $config->{author_path} // $AUTHOR_PATH ); | ||
| 830 | |||
| 831 | $self->fetch_template; | ||
| 832 | |||
| 833 | return; | ||
| 834 | } | ||
| 835 | |||
| 836 | ######################################################################## | ||
| 837 | sub _init_doc_index { | ||
| 838 | ######################################################################## | ||
| 839 | my ($self) = @_; | ||
| 840 | |||
| 841 | my $s3 = $self->get_s3; | ||
| 842 | |||
| 843 | my $bucket_name = $self->get_bucket_name; | ||
| 844 | |||
| 845 | my (@object_list) = $s3->list_all_objects_v2( $bucket_name, prefix => 'docs/' ); | ||
| 846 | |||
| 847 | foreach (@object_list) { | ||
| 848 | $DOC_INDEX{ $_->{key} } = q{/} . $_->{key}; | ||
| 849 | } | ||
| 850 | |||
| 851 | return; | ||
| 852 | } | ||
| 853 | |||
| 854 | ######################################################################## | ||
| 855 | sub fetch_template { | ||
| 856 | ######################################################################## | ||
| 857 | my ($self) = @_; | ||
| 858 | |||
| 859 | my $template = $self->get_template; | ||
| 860 | |||
| 861 | my $index = $self->get_config->{index} // {}; | ||
| 862 | |||
| 863 | my $config_dir = dirname( $self->get_config_file ); | ||
| 864 | |||
| 865 | # see if the index is set in the config file... | ||
| 866 | if ( !$template && $index->{template} ) { | ||
| 867 | $template = $index->{template}; | ||
| 868 | } | ||
| 869 | else { | ||
| 870 | $template = 'default'; | ||
| 871 | } | ||
| 872 | |||
| 873 | $self->get_logger->debug( sprintf 'using index template: "%s"', $template ne 'default' ? $template : '__DATA__' ); | ||
| 874 | my $bucket_name = $self->get_bucket_name; | ||
| 875 | |||
| 876 | if ( $template =~ m{\As3://(.*)$}xsm ) { | ||
| 877 | my $key = $1; | ||
| 878 | my $object = eval { $self->get_s3->get_object( $bucket_name, $key ); }; | ||
| 879 | |||
| 880 | croak sprintf "ERROR: could not retrieve %s from %s\n%s", $key, $bucket_name, $OS_ERROR | ||
| 881 | if !$object || $EVAL_ERROR; | ||
| 882 | |||
| 883 | $self->set_template( $object->{content} ); | ||
| 884 | |||
| 885 | $self->get_logger->debug( sprintf 'successfully loaded index template: "%s" from "%s"', $key, $bucket_name ); | ||
| 886 | } | ||
| 887 | else { | ||
| 888 | $template = $template =~ /^\//xsm ? $template : sprintf '%s/%s', $config_dir, $template; | ||
| 889 | my $index_template = $template eq 'default' ? slurp_file(*DATA) : slurp_file($template); | ||
| 890 | $index_template =~ s/\n\n=pod.*$/\n/xsm; | ||
| 891 | |||
| 892 | $self->set_template($index_template); | ||
| 893 | } | ||
| 894 | |||
| 895 | return; | ||
| 896 | } | ||
| 897 | |||
| 898 | ######################################################################## | ||
| 899 | sub extract_from_tarball { | ||
| 900 | ######################################################################## | ||
| 901 | my ( $tarball, $file ) = @_; | ||
| 902 | |||
| 903 | require Archive::Tar; | ||
| 904 | |||
| 905 | my $t = Archive::Tar->new; | ||
| 906 | |||
| 907 | $t->read( $tarball, 1 ) | ||
| 908 | or croak "failed to read tarball: $tarball"; | ||
| 909 | |||
| 910 | my $prefix = basename($tarball); | ||
| 911 | $prefix =~ s/[.]tar.*$//xsm; | ||
| 912 | |||
| 913 | croak "file not found ($prefix/$file)" | ||
| 914 | if !$t->contains_file("$prefix/$file"); | ||
| 915 | |||
| 916 | return $t->get_content("$prefix/$file"); | ||
| 917 | } | ||
| 918 | |||
| 919 | ######################################################################## | ||
| 920 | sub main { | ||
| 921 | ######################################################################## | ||
| 922 | my $cli = OrePAN2::S3->new( | ||
| 923 | option_specs => [ | ||
| 924 | qw( | ||
| 925 | bucket-name|b=s | ||
| 926 | cli-pager! | ||
| 927 | config-file|c=s | ||
| 928 | delete-all | ||
| 929 | dirty-check! | ||
| 930 | distribution|d=s | ||
| 931 | dryrun | ||
| 932 | force|f | ||
| 933 | format=s | ||
| 934 | help|h | ||
| 935 | invalidate-index! | ||
| 936 | output|o=s | ||
| 937 | profile-name|n=s | ||
| 938 | profile|p=s | ||
| 939 | save-index | ||
| 940 | template|t=s | ||
| 941 | url|U=s | ||
| 942 | update-site-index! | ||
| 943 | upload | ||
| 944 | ) | ||
| 945 | ], | ||
| 946 | default_options => { | ||
| 947 | config_file => $DEFAULT_CONFIG, | ||
| 948 | cli_pager => $TRUE, | ||
| 949 | profile_name => 'default', | ||
| 950 | profile => $ENV{AWS_PROFILE}, | ||
| 951 | dirty_check => $TRUE, | ||
| 952 | invalidate_index => $TRUE, | ||
| 953 | update_site_index => $TRUE, | ||
| 954 | format => 'json', | ||
| 955 | }, | ||
| 956 | extra_options => [qw(config credentials template author_path s3 dist_dir)], | ||
| 957 | commands => { | ||
| 958 | 'create-docs' => \&cmd_create_docs, | ||
| 959 | 'create-site-index' => \&cmd_create_site_index, | ||
| 960 | 'delete' => \&cmd_delete, | ||
| 961 | 'download-index' => \&cmd_download_orepan_index, | ||
| 962 | 'download-version-index' => \&cmd_download_version_index, | ||
| 963 | 'dump-template' => sub { | ||
| 964 | print {*STDOUT} shift->get_template; | ||
| 965 | return 0; | ||
| 966 | }, | ||
| 967 | 'inject' => \&cmd_inject, | ||
| 968 | 'invalidate-index' => \&cmd_invalidate_index, | ||
| 969 | 'list-packages' => \&cmd_list_packages, | ||
| 970 | 'show' => \&cmd_show_orepan_index, | ||
| 971 | 'upload' => \&cmd_upload, | ||
| 972 | 'upload-index' => \&cmd_upload_site_index, | ||
| 973 | 'upload-artifacts' => \&cmd_upload_artifacts, | ||
| 974 | }, | ||
| 975 | alias => { commands => { add => 'upload' } }, | ||
| 976 | abbreviations => $TRUE, | ||
| 977 | ); | ||
| 978 | |||
| 979 | return $cli->run(); | ||
| 980 | } | ||
| 981 | |||
| 982 | 1; | ||
| 983 | |||
| 984 | __DATA__ | ||