lib/Treex/Core/Resource.pm

filter: Statementstmt 52.3% 33 / 63 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 43.5% 44 / 101 CC 12 SCAR 44.5
CC
12
Cov
39%
require_file_from_share
156.0
BEGIN
1.0
BEGIN
1.0
CRAP 85.5
« lib/Treex/Core/TredView/TreeLayout.pm ↑ Summary lib/Treex/Core/Run.pm »
1 package Treex::Core::Resource;
2 {
3 85 $Treex::Core::Resource::VERSION = '0.08633_1';
4 }
5 85 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 85 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 85 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #use Moose;
11 #use Treex::Core::Common;
12 85 ▶use LWP::Simple; #TODO rewrite using LWP:UserAgent to show progress
Subroutine BEGIN: called CC=1 SCAR=0.0
13 85 ▶use File::Path 2.08 qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 85 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 85 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 85 ▶use Treex::Core::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 85 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 85 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 85 @EXPORT_OK = qw(require_file_from_share);
21  
22 sub require_file_from_share {
23 0 ▶ my ( $rel_path_to_file, $who_wants_it, $make_executable ) = @_;
Subroutine require_file_from_share: not called CC=12 SCAR=50.5
Pod documented
24 0 my $writable; #will store first writable directory found
25 SEARCH:
26 0 foreach my $resource_dir ( Treex::Core::Config->resource_path() ) {
27 0 my $file = File::Spec->catfile( $resource_dir, $rel_path_to_file );
28 0 log_debug("Trying $file\n");
29 0 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
30 0 log_debug("Found $file\n");
31 0 return $file;
32 }
33 0 ▶ if ( !defined $writable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $writable) 0 0 0
34 0 ▶ if ( !-e $resource_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $resource_dir) 0 0 0
35 0 make_path($resource_dir);
36 }
37 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
38 0 $writable = $resource_dir;
39 0 log_debug("Found writable directory: $writable");
40 }
41 }
42 }
43 0 ▶ $who_wants_it = defined $who_wants_it ? " by $who_wants_it" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $who_wants_it ? : 0 0 0
44 0 log_info("Shared file '$rel_path_to_file' is missing$who_wants_it.");
45 0 ▶ log_fatal("Cannot find writable directory for downloading from share") if !defined $writable;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $writable 0 0 0
46  
47 0 my $url = Treex::Core::Config->share_url() . "/$rel_path_to_file";
48 0 log_info("Trying to download $url");
49  
50 0 my $file = "$writable/$rel_path_to_file";
51  
52 # first ensure that the directory exists
53 0 my $directory = $file;
54 0 $directory =~ s/[^\/]*$//;
55 0 File::Path::mkpath($directory);
56  
57 # downloading the file using LWP::Simple
58 0 my $response_code = getstore( $url, $file );
59  
60 0 ▶ if ( $response_code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_code == 200) { } 0 0 0
61 0 log_info("Successfully downloaded to $file");
62 }
63 else {
64 0 log_fatal(
65 "Error when trying to download "
66 . "$url and to store it as $file ($response_code)\n"
67 );
68 }
69  
70 # TODO: better solution
71 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
72 0 chmod 0755, $file;
73 }
74 0 return $file;
75 }
76  
77 85 1;
78  
79 __END__
80  
81  
82 =encoding utf-8
83  
84 =head1 NAME
85  
86 Treex::Core::Resource - Access to shared resources
87  
88 =head1 VERSION
89  
90 version 0.08633_1
91  
92 =head1 SYNOPSIS
93  
94 use Treex::Core::Resource qw(require_file_from_share);
95 my $path = require_file_from_share('relative/path/to/file');
96 open my $MODEL, '<', $path or log_fatal($!);
97  
98 =head1 DESCRIPTION
99  
100 This module provides access to shared resources (e.g. models). First it tries to locate it on local computer.
101 If not found, download from server (L<http://ufallab.ms.mff.cuni.cz/>)
102  
103 =head1 SUBROUTINES
104  
105 =over
106  
107 =item require_file_from_share($rel_path_to_file, $who_wants_it, $make_executable)
108  
109 Try to locate file in local resource paths, if not found, try to download it and stores it to first writable path.
110 Obtains paths from L<Treex::Core::Config->resource_path()|Treex::Core::Config/resource_path>
111 Returns path to file.
112  
113 =back
114  
115 =head1 AUTHOR
116  
117 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
118  
119 Tomáš Kraut <kraut@ufal.mff.cuni.cz>
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
124  
125 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Core/TredView/TreeLayout.pm ↑ Summary lib/Treex/Core/Run.pm »