Treex/Core/Resource.pm

filter: Statementstmt 45.0% 32 / 71 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 36.7% 43 / 117 CC 16 SCAR 51.2
CC
16
Cov
32%
require_file_from_share
272.0
BEGIN
1.0
BEGIN
1.0
CRAP 167.8
« Treex/Core/TredView.pm ↑ Summary Treex/Block/Util/FixInvalidIDs.pm »
1 package Treex::Core::Resource;
2 72 $Treex::Core::Resource::VERSION = '2.20210102';
3 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 72 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #use Moose;
9 #use Treex::Core::Common;
10 72 ▶use LWP::Simple; #TODO rewrite using LWP:UserAgent to show progress
Subroutine BEGIN: called CC=1 SCAR=0.0
11 72 ▶use File::Path 2.08 qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 72 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 72 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 72 ▶use Treex::Core::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 72 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 72 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 72 @EXPORT_OK = qw(require_file_from_share);
19  
20 0 ▶sub require_file_from_share {
Subroutine require_file_from_share: not called CC=16 SCAR=56.1
Pod documented
21 0 my ( $path_to_file, $who_wants_it, $make_executable, $skip_download ) = @_;
22
23 # The following three cases are handled first
24 # ./relative_path
25 # ../relative_path
26 # /absolute_path
27 # These files are not searched within Treex Share.
28 0 ▶ if ($path_to_file =~ m|^[.]{0,2}/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_to_file =~ m[^[.]{0,2}/]) 0 0 0
29 0 log_debug("Looking for absolute or relative path $path_to_file\n");
30 0 ▶ return $path_to_file if -e $path_to_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path_to_file 0 0 0
31 0 my $file = File::Spec->rel2abs($path_to_file);
32 0 log_fatal "Cannot find '$path_to_file'.\nNote that it starts with '/' or '.', so it is not searched for within Treex Share.\nFile '$file' does not exist.\n";
33 }
34  
35 0 my $writable; #will store first writable directory found
36 SEARCH:
37 0 foreach my $resource_dir ( Treex::Core::Config->resource_path() ) {
38 0 ▶ next if (!$resource_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $resource_dir 0 0 0
39 0 my $file = File::Spec->catfile( $resource_dir, $path_to_file );
40 0 log_debug("Trying $file\n");
41 0 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
42 0 log_debug("Found $file\n");
43 0 return $file;
44 }
45 0 ▶ if ( !defined $writable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $writable) 0 0 0
46 0 ▶ if ( !-e $resource_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $resource_dir) 0 0 0
47 0 make_path($resource_dir);
48 }
49 0 ▶ if ( -d $resource_dir && -w $resource_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $resource_dir and -w $resource_dir) 0 0 0
Condition0%0/3
-d $resource_dir and -w $resource_dir
!ll&&!rl&&r
000
MC/DC0%0/2
-d $resource_dir and -w $resource_dir
-d $resource_dir -w $resource_dir
Truth table0%0/3
A and B
A= -d $resource_dirB= -w $resource_dir
ABresult
0X0
100
111
50 0 $writable = $resource_dir;
51 0 log_debug("Found writable directory: $writable");
52 }
53 }
54 }
55 0 ▶ $who_wants_it = defined $who_wants_it ? " by $who_wants_it" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $who_wants_it ? : 0 0 0
56 0 log_info("Shared file '$path_to_file' is missing$who_wants_it.");
57 0 ▶ log_fatal("Cannot find writable directory for downloading from share") if !defined $writable;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $writable 0 0 0
58  
59 0 my $file = "$writable/$path_to_file";
60  
61 # first ensure that the directory exists
62 0 my $directory = $file;
63 0 $directory =~ s/[^\/]*$//;
64 0 File::Path::mkpath($directory);
65
66 # skip download, just return the path, the caller block will handle the download itself
67 0 ▶ if ($skip_download){
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_download) 0 0 0
68 0 log_warn("File $file does not exist, auto-download skipped.");
69 0 return $file;
70 }
71  
72 0 my $url = Treex::Core::Config->share_url() . "/$path_to_file";
73 0 log_info("Trying to download $url");
74  
75 # downloading the file using LWP::Simple
76 0 my $response_code = getstore( $url, $file );
77  
78 0 ▶ if ( $response_code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_code == 200) { } 0 0 0
79 0 log_info("Successfully downloaded to $file");
80 }
81 else {
82 0 log_fatal(
83 "Error when trying to download "
84 . "$url and to store it as $file ($response_code)\n"
85 );
86 }
87  
88 # TODO: better solution
89 0 ▶ if ( $file =~ /installed_tools/ || $make_executable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /installed_tools/ or $make_executable) 0 0 0
Condition0%0/3
$file =~ /installed_tools/ or $make_executable
l!l&&r!l&&!r
000
MC/DC0%0/2
$file =~ /installed_tools/ or $make_executable
$file =~ /installed_tools/ $make_executable
Truth table0%0/3
A or B
A= $file =~ /installed_tools/B= $make_executable
ABresult
1X1
011
000
90 0 chmod 0755, $file;
91 }
92 0 return $file;
93 }
94  
95 72 1;
96  
97 __END__
« Treex/Core/TredView.pm ↑ Summary Treex/Block/Util/FixInvalidIDs.pm »