lib/Rex/Helper/Path.pm

filter: Statementstmt 76.9% 117 / 152 Branchbran 54.0% 27 / 50 Conditioncond 40.0% 6 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 67.0% 167 / 249 CC 33 SCAR 30.6
CC
33
Cov
69%
get_file_path
49.1
resolv_path
7.2
__ANON__
6.0
CRAP 21.4
« lib/Rex/Virtualization/Lxc/stop.pm ↑ Summary lib/Rex/Commands/LVM.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Helper::Path;
6  
7 96 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 96 our $VERSION = '1.16.1'; # VERSION
11  
12 96 ▶use Rex::Helper::File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 96 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 96 require Exporter;
15  
16 96 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 96 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 96 ▶use Cwd 'realpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 96 require Rex;
21 96 ▶use Rex::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 96 require Rex::Config;
23  
24 96 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 96 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 96 @EXPORT = qw(get_file_path get_tmp_file resolv_path parse_path resolve_symlink);
28  
29 96 set "path_map", {};
30  
31 #
32 # CALL: get_file_path("foo.txt", caller());
33 # RETURNS: module file
34 #
35 12 ▶sub get_file_path {
Subroutine get_file_path: called CC=19 SCAR=38.9
Pod undocumented
36 12 my ( $file_name, $caller_package, $caller_file ) = @_;
37  
38 12 $file_name = resolv_path($file_name);
39  
40 12 my $ends_with_slash = 0;
41 12 ▶ if ( $file_name =~ m/\/$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name =~ m[/$]u) 0 12 12
42 0 $ends_with_slash = 1;
43 }
44  
45 12 my $has_wildcard = 0;
46 12 my $base_name = basename($file_name);
47  
48 12 ▶ if ( $base_name =~ qr{\*} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_name =~ qr/\*/u) 0 12 12
49 0 $has_wildcard = 1;
50 0 $file_name = dirname($file_name);
51 }
52  
53 12 ▶ my $fix_path = sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
54 12 my ($path) = @_;
55 12 $path =~ s:^\./::;
56  
57 12 ▶ if ($has_wildcard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_wildcard) 0 12 12
58 0 $path = Rex::Helper::File::Spec->catfile( $path, $base_name );
59 }
60  
61 12 ▶ if ($ends_with_slash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ends_with_slash) 0 12 12
62 0 ▶ if ( $path !~ m/\/$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[/$]u) 0 0 0
63 0 return "$path/";
64 }
65 }
66  
67 12 return $path;
68 12 };
69  
70 12 ▶ if ( !$caller_package ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($caller_package) 0 12 12
71 0 ( $caller_package, $caller_file ) = caller();
72 }
73  
74 # check if a file in $BASE overwrites the module file
75 # first get the absolute path to the rexfile
76  
77 12 ▶ $::rexfile ||= $0;
Condition66%2/3
$main::rexfile ||= $0
l!l&&r!l&&!r
840
MC/DC0%0/2
$main::rexfile ||= $0
$main::rexfile $0
Truth table66%2/3
A ||= B
A= $main::rexfileB= $0
ABresult
1X1
011
000
78  
79 12 ▶ if ( $caller_file =~ m|^/loader/[^/]+/__Rexfile__.pm$| ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller_file =~ m[^/loader/[^/]+/__Rexfile__.pm$]u) 0 12 12
80 0 $caller_file = $::rexfile;
81 }
82  
83 12 my @path_parts;
84 12 ▶ if ( $^O =~ m/^MSWin/ && !Rex::is_ssh() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/u and not Rex::is_ssh()) { } 0 12 12
Condition33%1/3
$^O =~ /^MSWin/u and not Rex::is_ssh()
!ll&&!rl&&r
1200
MC/DC0%0/2
$^O =~ /^MSWin/u and not Rex::is_ssh()
$^O =~ /^MSWin/u not Rex::is_ssh()
Truth table33%1/3
A and not(B)
A= $^O =~ /^MSWin/uB= not Rex::is_ssh()
ABresult
0X0
100
111
85 0 @path_parts = split( /\//, $::rexfile );
86 }
87 else {
88 12 @path_parts = split( /\//, realpath($::rexfile) );
89 }
90 12 pop @path_parts;
91  
92 12 my $real_path = join( '/', @path_parts );
93  
94 12 my $map_setting = get("path_map");
95  
96 my %path_map = (
97 0 ▶ map { ( ( substr( $_, -1 ) eq '/' ) ? $_ : "$_/" ) => $map_setting->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
substr($_, -1) eq '/' ? : 0 0 0
98 keys %$map_setting
99 );
100  
101 12 foreach my $prefix (
102 0 sort { length($b) <=> length($a) }
103 0 grep { $file_name =~ m/^$_/ } keys %path_map
104 )
105 {
106 0 foreach my $pattern ( @{ $path_map{$prefix} } ) {
107 0 my $expansion =
108 Rex::Helper::File::Spec->catfile( parse_path($pattern),
109 substr( $file_name, length($prefix) ) );
110  
111 0 ▶ if ( -e $expansion ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $expansion) 0 0 0
112 0 return $fix_path->($expansion);
113 }
114  
115 0 $expansion = Rex::Helper::File::Spec->catfile( $real_path, $expansion );
116 0 ▶ if ( -e $expansion ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $expansion) 0 0 0
117 0 return $fix_path->($expansion);
118 }
119 }
120 }
121  
122 12 ▶ if ( -e $file_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file_name) 8 4 12
123 8 return $fix_path->($file_name);
124 }
125  
126 4 my $cat_file_name =
127 Rex::Helper::File::Spec->catfile( $real_path, $file_name );
128 4 ▶ if ( -e $cat_file_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cat_file_name) 0 4 4
129 0 return $fix_path->($cat_file_name);
130 }
131  
132 # walk down the wire to find the file...
133 4 my ($old_caller_file) = $caller_file;
134 4 my $i = 0;
135 4 ▶ while ( $caller_package && $i <= 50 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller_package and $i <= 50) 8 0 8
Condition33%1/3
$caller_package and $i <= 50
!ll&&!rl&&r
008
MC/DC0%0/2
$caller_package and $i <= 50
$caller_package $i <= 50
Truth table33%1/3
A and B
A= $caller_packageB= $i <= 50
ABresult
0X0
100
111
136 8 ( $caller_package, $caller_file ) = caller($i);
137 8 ▶ if ( !$caller_package ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($caller_package) 4 4 8
138 4 last;
139 }
140  
141 4 my $module_path = Rex::get_module_path($caller_package);
142 4 $cat_file_name =
143 Rex::Helper::File::Spec->catfile( $module_path, $file_name );
144 4 ▶ if ( -e $cat_file_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cat_file_name) 0 4 4
145 0 return $fix_path->($cat_file_name);
146 }
147  
148 4 $i++;
149 }
150  
151 $file_name =
152 4 Rex::Helper::File::Spec->catfile( dirname($old_caller_file), $file_name );
153  
154 4 return $fix_path->($file_name);
155 }
156  
157 977 ▶sub get_tmp_file {
Subroutine get_tmp_file: called CC=1 SCAR=0.0
Pod undocumented
158 977 return Rex::Helper::File::Spec->join( Rex::Config->get_tmp_dir(),
159 Rex::Commands::get_random( 12, 'a' .. 'z' ) . '.tmp' );
160 }
161  
162 1429 ▶sub resolv_path {
Subroutine resolv_path: called CC=6 SCAR=19.8
Pod undocumented
163 1429 my ( $path, $local ) = @_;
164  
165 1429 ▶ if ( $path !~ m/^~/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ /^~/u) 1427 2 1429
166  
167 # path starts not with ~ so we don't need to expand $HOME.
168 # just return it.
169 1427 return $path;
170 }
171  
172 2 my $home_path;
173 2 require Rex::User;
174 2 my $user_o = Rex::User->get;
175  
176 2 ▶ if ($local) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local) { } 1 1 2
177 1 ▶ if ( $^O =~ m/^MSWin/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/u) { } 0 1 1
178  
179 # windows path:
180 0 $home_path = $ENV{'USERPROFILE'};
181 }
182 else {
183 1 ▶ if ( $path =~ m/^~([a-zA-Z0-9_][^\/]+)\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^~([a-zA-Z0-9_][^/]+)/]u) { } 0 1 1
184 0 my $user_name = $1;
185 0 my %user_info = $user_o->get_user($user_name);
186 0 $home_path = $user_info{home};
187 0 $path =~ s/^~$user_name/$home_path/;
188 }
189 else {
190 1 $home_path = $ENV{'HOME'};
191 1 $path =~ s/^~/$home_path/;
192 }
193 }
194 }
195 else {
196 1 ▶ if ( $path =~ m/^~([a-zA-Z0-9_][^\/]+)\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^~([a-zA-Z0-9_][^/]+)/]u) { } 0 1 1
197 0 my $user_name = $1;
198 0 my %user_info = $user_o->get_user($user_name);
199 0 $home_path = $user_info{home};
200 0 $path =~ s/^~$user_name/$home_path/;
201 }
202 else {
203 1 my $exec = Rex::Interface::Exec->create;
204 1 my $remote_home = $exec->exec("echo \$HOME");
205 1 $remote_home =~ s/[\r\n]//gms;
206 1 $home_path = $remote_home;
207 1 $path =~ s/^~/$home_path/;
208 }
209 }
210  
211 2 return $path;
212 }
213  
214 156 ▶sub parse_path {
Subroutine parse_path: called CC=4 SCAR=15.4
Pod undocumented
215 156 my ( $path_with_macro, $replacement_for ) = @_;
216  
217 156 ▶ my $replace_macros = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
218 156 my ( $path, $substitution_for ) = @_;
219  
220 156 my $macro = join q(|), keys %{$substitution_for};
221  
222 156 ( my $substitution = $path ) =~ s/{($macro)}/$substitution_for->{$1}/gmsx;
223  
224 156 return $substitution;
225 156 };
226  
227 156 ▶ $replacement_for->{server} //= Rex::Commands::connection()->server;
Condition33%1/3
$replacement_for->{"server"} //= Rex::Commands::connection()->server
l!l&&r!l&&!r
01560
MC/DC0%0/2
$replacement_for->{"server"} //= Rex::Commands::connection()->server
$replacement_for->{"server"} Rex::Commands::connection()->server
Truth table33%1/3
A //= B
A= $replacement_for->{"server"}B= Rex::Commands::connection()->server
ABresult
1X1
011
000
228 156 ▶ $replacement_for->{environment} //= Rex::Commands::environment();
Condition33%1/3
$replacement_for->{"environment"} //= Rex::Commands::environment()
l!l&&r!l&&!r
01560
MC/DC0%0/2
$replacement_for->{"environment"} //= Rex::Commands::environment()
$replacement_for->{"environment"} Rex::Commands::environment()
Truth table33%1/3
A //= B
A= $replacement_for->{"environment"}B= Rex::Commands::environment()
ABresult
1X1
011
000
229  
230 156 my $replacement_path =
231 $replace_macros->( $path_with_macro, $replacement_for );
232  
233 156 ▶ if ( $replacement_path =~ m/\{([^\}]+)\}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replacement_path =~ /\{([^\}]+)\}/u) 0 156 156
234  
235 # if there are still some macros to replace, we need some
236 # information of the system
237  
238 0 require Rex::Commands::Gather;
239 0 my %hw = Rex::Commands::Gather::get_system_information();
240  
241 0 $replacement_path = $replace_macros->( $replacement_path, \%hw );
242 }
243  
244 156 return $replacement_path;
245 }
246  
247 13 ▶sub resolve_symlink {
Subroutine resolve_symlink: called CC=4 SCAR=13.9
Pod undocumented
248 13 my $path = shift;
249 13 my $fs = Rex::Interface::Fs::create();
250 13 my $resolution;
251  
252 13 ▶ if ( $fs->is_symlink($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_symlink($path)) 12 1 13
253 12 ▶ while ( my $link = $fs->readlink($path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link = $fs->readlink($path)) 13 12 25
254 13 ▶ if ( $link !~ m/^\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $link =~ m[^/]u) { } 0 13 13
255 0 $path = dirname($path) . "/" . $link;
256 }
257 else {
258 13 $path = $link;
259 }
260 13 $link = $fs->readlink($link);
261 }
262 12 $resolution = $path;
263 }
264  
265 13 return $resolution;
266 }
267  
268 96 1;
« lib/Rex/Virtualization/Lxc/stop.pm ↑ Summary lib/Rex/Commands/LVM.pm »