| 1 | package OPM::Installer::Utils::File; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: File related utility functions | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 10 | ▶ | use v5.10; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 10 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 10 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 10 | our $VERSION = '1.0.1'; # VERSION | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 10 | ▶ | use File::HomeDir; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 10 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 10 | ▶ | use File::Temp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 10 | ▶ | use HTTP::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 10 | ▶ | use HTTP::Tiny::FileProtocol; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 10 | ▶ | use IO::All; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 10 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 10 | ▶ | use OPM::Installer::Logger; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 10 | ▶ | use OPM::Installer::Utils::Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 10 | ▶ | use OPM::Repository; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 10 | ▶ | use Regexp::Common qw(URI); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 10 | ▶ | use Types::Standard qw(ArrayRef Str Bool); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 10 | our $ALLOWED_SCHEME = [ 'HTTP', 'file' ]; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 10 | has repositories => ( is => 'ro', isa => ArrayRef[Str], default => \&_repository_list ); | |||||||||||||||||||
| 28 | 10 | has package => ( is => 'ro', isa => Str, required => 1 ); | |||||||||||||||||||
| 29 | 10 | has framework_version => ( is => 'ro', isa => Str, required => 1 ); | |||||||||||||||||||
| 30 | 10 | has version => ( is => 'ro', isa => Str ); | |||||||||||||||||||
| 31 | 10 | has verbose => ( is => 'ro', isa => Bool ); | |||||||||||||||||||
| 32 | 12 | ▶ | has logger => ( is => 'ro', default => sub{ OPM::Installer::Logger->new } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 10 | has rc_config => ( is => 'ro', lazy => 1, default => \&_rc_config ); | |||||||||||||||||||
| 34 | 10 | has conf => ( is => 'ro' ); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | ▶ | sub list_available { | ||||||||||||||||||
|
Subroutine
list_available: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 37 | 0 | my $self = shift; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 0 | ▶ | my @repositories = @{ $self->repositories || [] }; | ||||||||||||||||||
|
Condition0%0/2
$self->repositories || []
MC/DC0%0/1
$self->repositories || []
$self->repositories
Truth table0%0/2
A
A= $self->repositories
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | for my $repo_url ( @repositories ) { | |||||||||||||||||||
| 42 | 0 | ▶ | $repo_url .= '/otrs.xml' if '.xml' ne substr $repo_url, -4; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | my $repo = OPM::Repository->new( | |||||||||||||||||||
| 46 | sources => \@repositories, | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | my $framework_version = $self->framework_version; | |||||||||||||||||||
| 50 | 0 | $framework_version =~ s{\.\d+$}{}; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | return $repo->list( | |||||||||||||||||||
| 53 | framework => $framework_version, | ||||||||||||||||||||
| 54 | details => 1, | ||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 12 | ▶ | sub resolve_path { | ||||||||||||||||||
|
Subroutine
resolve_path: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||
| 59 | 12 | my ($self) = @_; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 12 | my $path; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 12 | my $package = $self->package; | |||||||||||||||||||
| 64 | 12 | ▶ | if ( $self->_is_url( $package ) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 65 | # download file | ||||||||||||||||||||
| 66 | 2 | $path = $self->_download( $package ); | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | elsif ( -f $package ) { | ||||||||||||||||||||
| 69 | # do nothing, file already exists | ||||||||||||||||||||
| 70 | 1 | $path = $package; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||
| 73 | 9 | ▶ | my @repositories = @{ $self->repositories || [] }; | ||||||||||||||||||
|
Condition50%1/2
$self->repositories || []
MC/DC0%0/1
$self->repositories || []
$self->repositories
Truth table50%1/2
A
A= $self->repositories
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 9 | for my $repo ( @repositories ) { | |||||||||||||||||||
| 76 | 9 | ▶ | $repo .= '/otrs.xml' if '.xml' ne substr $repo, -4; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 9 | ▶ | say "Searching these repositories: @repositories" if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 9 | my $repo = OPM::Repository->new( | |||||||||||||||||||
| 82 | sources => \@repositories, | ||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 9 | my ($framework) = $self->framework_version =~ m{\A(\d+\.\d+)}; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 9 | my ($url) = $repo->find( | |||||||||||||||||||
| 88 | name => $package, | ||||||||||||||||||||
| 89 | framework => $framework, | ||||||||||||||||||||
| 90 | version => $self->version, | ||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 9 | ▶ | say "Found ", $url // '<nothing>' if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$url // "<nothing>"
MC/DC0%0/1
$url // "<nothing>"
$url
Truth table0%0/2
A
A= $url
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 9 | ▶ | return if !$url; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 6 | $path = $self->_download( $url ); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 9 | return $path; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | ▶ | sub _repository_list { | ||||||||||||||||||
|
Subroutine
_repository_list: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 104 | 0 | my ($self) = @_; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | my $config = $self->rc_config; | |||||||||||||||||||
| 107 | 0 | my $repositories = $config->{repository}; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | ▶ | return [] if !$repositories; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 0 | return $repositories; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 19 | ▶ | sub _is_url { | ||||||||||||||||||
|
Subroutine
_is_url: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 114 | 19 | my ($self, $package) = @_; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 19 | ▶ | my @allowed_schemes = ref $ALLOWED_SCHEME ? @{ $ALLOWED_SCHEME } : $ALLOWED_SCHEME; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 19 | my $matches; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | SCHEME: | ||||||||||||||||||||
| 121 | 19 | for my $scheme ( @allowed_schemes ) { | |||||||||||||||||||
| 122 | my $regex = ( lc $scheme eq 'http' ) ? | ||||||||||||||||||||
| 123 | $RE{URI}{HTTP}{-scheme => qr/https?/} : | ||||||||||||||||||||
| 124 | 34 | ▶ | $RE{URI}{$scheme}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 34 | ▶ | if ( $package =~ m{\A$regex\z} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 7 | $matches++; | |||||||||||||||||||
| 128 | 7 | last SCHEME; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 19 | ▶ | return if !$matches; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 7 | return 1; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 8 | ▶ | sub _download { | ||||||||||||||||||
|
Subroutine
_download: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 137 | 8 | my ($self, $url) = @_; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 8 | my $file = File::Temp->new->filename; | |||||||||||||||||||
| 140 | 8 | my $response = HTTP::Tiny->new->mirror( $url, $file ); | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 8 | $self->logger->notice( area => 'download', file => $file, success => $response->{success} ); | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 8 | ▶ | return if !$response->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 8 | return $file; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | ▶ | sub _rc_config { | ||||||||||||||||||
|
Subroutine
_rc_config: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 149 | 0 | my ($self) = @_; | |||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | my $utils = OPM::Installer::Utils::Config->new( conf => $self->conf ); | |||||||||||||||||||
| 152 | 0 | my $config = $utils->rc_config; | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | return $config; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 10 | 1; | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||