| 1 | package CPAN::InGit::MirrorTree; | ||||||||||||||||||||||||||||
| 2 | 11 | our $VERSION = '0.005'; # VERSION | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Subclass of ArchiveTree which automatically mirrors files from upstream | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Scalar::Util 'refaddr', 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use POSIX 'strftime'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use IO::Uncompress::Gunzip qw( gunzip $GunzipError ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use Log::Any '$log'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 11 | extends 'CPAN::InGit::ArchiveTree'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 11 | has upstream_url => ( is => 'rw', coerce => \&_add_trailing_slash ); | |||||||||||||||||||||||||||
| 20 | 11 | has upstream_backup_url => ( is => 'rw', lazy => 1, builder => 1, coerce => \&_add_trailing_slash ); | |||||||||||||||||||||||||||
| 21 | 11 | has autofetch => ( is => 'rw', default => 1 ); | |||||||||||||||||||||||||||
| 22 | 11 | has package_details_max_age => ( is => 'rw', default => 86400 ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub _build_upstream_backup_url($self) { | ||||||||||||||||||||||||||
|
Subroutine
_build_upstream_backup_url: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | ($self->upstream_url||'') =~ m{^(https?)://www\.cpan\.org} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->upstream_url || ''
MC/DC0%0/1
$self->upstream_url || ''
$self->upstream_url
Truth table0%0/2
A
A= $self->upstream_url
| |||||||||||||||||||||||||||||
| 26 | ? "$1://backpan.perl.org/" | ||||||||||||||||||||||||||||
| 27 | : undef; | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub _add_trailing_slash { | ||||||||||||||||||||||||||
|
Subroutine
_add_trailing_slash: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 31 | 0 | my $x= shift; | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | defined $x? $x =~ s{/?\z}{/}r : $x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub _pack_config($self, $config) { | ||||||||||||||||||||||||||
|
Subroutine
_pack_config: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | 0 | $config->{upstream_url}= $self->upstream_url; | |||||||||||||||||||||||||||
| 37 | 0 | $config->{upstream_backup_url}= $self->upstream_backup_url; | |||||||||||||||||||||||||||
| 38 | 0 | $config->{autofetch}= $self->autofetch; | |||||||||||||||||||||||||||
| 39 | 0 | $config->{package_details_max_age}= $self->package_details_max_age; | |||||||||||||||||||||||||||
| 40 | 0 | $self->next::method($config); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub _unpack_config($self, $config) { | ||||||||||||||||||||||||||
|
Subroutine
_unpack_config: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 43 | 0 | $self->next::method($config); | |||||||||||||||||||||||||||
| 44 | 0 | $self->upstream_url($config->{upstream_url}); | |||||||||||||||||||||||||||
| 45 | $self->upstream_backup_url($config->{upstream_backup_url}) | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if exists $config->{upstream_backup_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | $self->autofetch($config->{autofetch}); | |||||||||||||||||||||||||||
| 48 | 0 | $self->package_details_max_age($config->{package_details_max_age}); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub get_path($self, $path) { | ||||||||||||||||||||||||||
|
Subroutine
get_path: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my $ent= $self->next::method($path); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($self->autofetch) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | # Special case for 02packages.details.txt, load it if missing or if cache is stale | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($path eq 'modules/02packages.details.txt') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($ent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $blob_last_update= $self->{_blob_last_update}{$ent->[0]->id} // do { | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_blob_last_update"}{$ent->[0]->id} // do {
my $head = substr($ent->[0]->content, 0, 10000);
$head =~ /^Last-Updated:\s*(.*)$/mu or die "Can't parse 02packages.details.txt";
(my $date = $1) =~ s/\s+\z//u;
$CPAN::InGit::MirrorTree::log->debug("Date in modules/02packages.details.txt is '${date}'");
"Time::Piece"->strptime($date, "%a, %d %b %Y %H:%M:%S GMT")->epoch
}
MC/DC0%0/2
$self->{"_blob_last_update"}{$ent->[0]->id} // do {
my $head = substr($ent->[0]->content, 0, 10000);
$head =~ /^Last-Updated:\s*(.*)$/mu or die "Can't parse 02packages.details.txt";
(my $date = $1) =~ s/\s+\z//u;
$CPAN::InGit::MirrorTree::log->debug("Date in modules/02packages.details.txt is '${date}'");
"Time::Piece"->strptime($date, "%a, %d %b %Y %H:%M:%S GMT")->epoch
}
$self->{"_blob_last_update"}{$ent->[0]->id}
do {
my $head = substr($ent->[0]->content, 0, 10000);
$head =~ /^Last-Updated:\s*(.*)$/mu or die "Can't parse 02packages.details.txt";
(my $date = $1) =~ s/\s+\z//u;
$CPAN::InGit::MirrorTree::log->debug("Date in modules/02packages.details.txt is '${date}'");
"Time::Piece"->strptime($date, "%a, %d %b %Y %H:%M:%S GMT")->epoch
}
Truth table0%0/3
A // B
A= $self->{"_blob_last_update"}{$ent->[0]->id}B= do {
my $head = substr($ent->[0]->content, 0, 10000);
$head =~ /^Last-Updated:\s*(.*)$/mu or die "Can't parse 02packages.details.txt";
(my $date = $1) =~ s/\s+\z//u;
$CPAN::InGit::MirrorTree::log->debug("Date in modules/02packages.details.txt is '${date}'");
"Time::Piece"->strptime($date, "%a, %d %b %Y %H:%M:%S GMT")->epoch
}
| |||||||||||||||||||||||||||||
| 58 | # parse it out of the file | ||||||||||||||||||||||||||||
| 59 | 0 | my $head= substr($ent->[0]->content, 0, 10000); | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | $head =~ /^Last-Updated:\s*(.*)$/m or die "Can't parse 02packages.details.txt"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | (my $date= $1) =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 62 | 0 | $log->debug("Date in modules/02packages.details.txt is '$date'"); | |||||||||||||||||||||||||||
| 63 | 0 | Time::Piece->strptime($date, "%a, %d %b %Y %H:%M:%S GMT")->epoch | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($blob_last_update >= time - $self->package_details_max_age) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $log->trace(' 02package.details.txt cache is current'); | |||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||
| 68 | 0 | $log->trace(' 02package.details.txt cache expired'); | |||||||||||||||||||||||||||
| 69 | 0 | $ent= undef; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | unless ($ent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $log->debug(" mirror autofetch $path"); | |||||||||||||||||||||||||||
| 74 | 0 | my $blob= $self->add_upstream_package_details; | |||||||||||||||||||||||||||
| 75 | 0 | $self->clear_package_details; # will lazily rebuild | |||||||||||||||||||||||||||
| 76 | 0 | $ent= [ $blob, 0100644 ]; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | ▶ | elsif ($path =~ m{^authors/id/(.*)} and !$ent) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$path =~ m[^authors/id/(.*)]u and not $ent
MC/DC0%0/2
$path =~ m[^authors/id/(.*)]u and not $ent
$path =~ m[^authors/id/(.*)]u
not $ent
Truth table0%0/3
A and not(B)
A= $path =~ m[^authors/id/(.*)]uB= not $ent
| |||||||||||||||||||||||||||||
| 80 | 0 | $log->debug(" mirror autofetch $path"); | |||||||||||||||||||||||||||
| 81 | 0 | my $author_path= $1; | |||||||||||||||||||||||||||
| 82 | 0 | my $blob= $self->add_upstream_author_file($author_path, undef_if_404 => 1); | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | $ent= [ $blob, 0100644 ] if $blob; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 0 | return $ent; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub fetch_upstream_file($self, $path, %options) { | ||||||||||||||||||||||||||
|
Subroutine
fetch_upstream_file: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | croak "No upstream URL for this tree" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | unless defined $self->upstream_url; | ||||||||||||||||||||||||||||
| 93 | 0 | my $url= $self->upstream_url . $path; | |||||||||||||||||||||||||||
| 94 | 0 | my $tx= $self->parent->useragent->get($url); | |||||||||||||||||||||||||||
| 95 | 0 | $log->debugf(" GET %s -> %s %s", $url, $tx->result->code, $tx->result->message); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | unless ($tx->result->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ($self->upstream_backup_url && $path =~ m{^authors/id/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->upstream_backup_url and $path =~ m[^authors/id/]u
MC/DC0%0/2
$self->upstream_backup_url and $path =~ m[^authors/id/]u
$self->upstream_backup_url
$path =~ m[^authors/id/]u
Truth table0%0/3
A and B
A= $self->upstream_backup_urlB= $path =~ m[^authors/id/]u
| |||||||||||||||||||||||||||||
| 98 | 0 | my $url2= $self->upstream_backup_url . $path; | |||||||||||||||||||||||||||
| 99 | 0 | my $tx2= $self->parent->useragent->get($url2); | |||||||||||||||||||||||||||
| 100 | 0 | $log->debugf(" GET %s -> %s %s", $url2, $tx2->result->code, $tx2->result->message); | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | return \$tx2->result->body | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | if $tx2->result->is_success; | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | return undef if $options{undef_if_404} && $tx->result->code == 404; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$options{"undef_if_404"} and $tx->result->code == 404
MC/DC0%0/2
$options{"undef_if_404"} and $tx->result->code == 404
$options{"undef_if_404"}
$tx->result->code == 404
Truth table0%0/3
A and B
A= $options{"undef_if_404"}B= $tx->result->code == 404
| |||||||||||||||||||||||||||||
| 105 | 0 | croak "Failed to find file upstream: ".$tx->result->message; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 0 | return \$tx->result->body; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub add_upstream_package_details($self, %options) { | ||||||||||||||||||||||||||
|
Subroutine
add_upstream_package_details: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $content_ref= $self->fetch_upstream_file('modules/02packages.details.txt.gz', %options) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | or return undef; | ||||||||||||||||||||||||||||
| 114 | # Unzip the file and store uncompressed, so that 'git diff' works nicely on it. | ||||||||||||||||||||||||||||
| 115 | 0 | my $txt; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | gunzip $content_ref => \$txt | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | or croak "gunzip failed: $GunzipError"; | ||||||||||||||||||||||||||||
| 118 | 0 | my $blob= Git::Raw::Blob->create($self->git_repo, $txt); | |||||||||||||||||||||||||||
| 119 | 0 | $self->set_path('modules/02packages.details.txt', $blob); | |||||||||||||||||||||||||||
| 120 | 0 | $self->{_blob_last_update}{$blob->id}= time; | |||||||||||||||||||||||||||
| 121 | 0 | return $blob; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub add_upstream_author_file($self, $author_path, %options) { | ||||||||||||||||||||||||||
|
Subroutine
add_upstream_author_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 0 | my $path= "authors/id/$author_path"; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | my $content_ref= $self->fetch_upstream_file($path, %options) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | or return undef; | ||||||||||||||||||||||||||||
| 129 | 0 | my $blob= Git::Raw::Blob->create($self->git_repo, $$content_ref); | |||||||||||||||||||||||||||
| 130 | 0 | $self->set_path($path, $blob); | |||||||||||||||||||||||||||
| 131 | 0 | return $blob; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||