Enbld/RcFile.pm

filter: Statementstmt 52.9% 91 / 172 Branchbran 25.0% 19 / 76 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.0% 19 / 25 Podpod 0.0% 0 / 9 totaltotal 44.8% 131 / 292 SCAR 58.6
CC
42
Cov
44%
do_load
72.0
do_copy
72.0
DSL
42.0
CRAP 350.2
« Enbld.pm ↑ Summary Enbld/Definition/Vim.pm »
1 package Enbld::RcFile;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Path qw/make_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Digest::file qw/digest_file_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Digest::SHA qw/sha1_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 require Enbld::Message;
18 2 require Enbld::Error;
19 2 require Enbld::Exception;
20  
21 2 our @cmd_list = qw/load copy set/;
22  
23 sub new {
24 3 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
25  
26 3 my $self = {
27 command => undef,
28 filepath => undef,
29  
30 from => undef,
31  
32 source => undef,
33 url => undef,
34  
35 contents => undef,
36  
37 directory => undef,
38  
39 fullpath => undef,
40 filename => undef,
41  
42 digest => undef,
43  
44 @_,
45 };
46  
47 3 bless $self, $class;
48  
49 3 $self->_parse_filepath;
50 3 $self->_validate;
51  
52 3 return $self;
53 }
54  
55 sub do {
56 2 ▶ my $self = shift;
Subroutine do: called CC=5 SCAR=19.7
Pod undocumented
57  
58 2 ▶ if ( $self->{command} eq 'load' && $self->{from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command"} eq "load" and $self->{"from"}) 0 2 2
Condition33%1/3
$self->{"command"} eq "load" and $self->{"from"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"command"} eq "load" and $self->{"from"}
$self->{"command"} eq "load" $self->{"from"}
Truth table33%1/3
A and B
A= $self->{"command"} eq "load"B= $self->{"from"}
ABresult
0X0
100
111
59 0 $self->{url} = $self->{from};
60 }
61  
62 2 ▶ if ( $self->{command} eq 'copy' && $self->{from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command"} eq "copy" and $self->{"from"}) 0 2 2
Condition33%1/3
$self->{"command"} eq "copy" and $self->{"from"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"command"} eq "copy" and $self->{"from"}
$self->{"command"} eq "copy" $self->{"from"}
Truth table33%1/3
A and B
A= $self->{"command"} eq "copy"B= $self->{"from"}
ABresult
0X0
100
111
63 0 $self->{source} = $self->{from};
64 }
65  
66 2 my $cmd = 'do_' . $self->{command};
67 2 my $result = $self->$cmd;
68  
69 2 return $result;
70 }
71  
72 sub _validate {
73 3 ▶ my $self = shift;
Subroutine _validate: called CC=5 SCAR=18.4
74  
75 3 ▶ _err( "Configuration file's command is not specified." ) unless $self->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"command"} 0 3 3
76  
77 3 ▶ if ( ! grep { $_ eq $self->{command} } @cmd_list ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $self->{"command"};} @Enbld::RcFile::cmd_list) 0 3 3
78 0 _err( "'$self->{command}' is invalid command type." );
79 }
80  
81 3 ▶ _err( "Configuration file's path not set." ) unless $self->{filepath};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"filepath"} 0 3 3
82  
83 3 ▶ unless ( -d $self->{directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"directory"}) 0 3 3
84 0 make_path( $self->{directory} );
85 }
86 }
87  
88 sub _parse_filepath {
89 3 ▶ my $self = shift;
Subroutine _parse_filepath: called CC=5 SCAR=16.6
90  
91 3 ▶ $self->{directory} = $ENV{HOME} unless $self->{directory};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"directory"} 3 0 3
92  
93 $self->{fullpath} = File::Spec->file_name_is_absolute( $self->{filepath} ) ?
94 $self->{filepath} :
95 3 ▶ File::Spec->catfile( $self->{directory}, $self->{filepath} );
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($self->{'filepath'}) ? : 0 3 3
96  
97 3 my $dirs;
98 3 ( undef, $dirs, $self->{filename} ) = File::Spec->splitpath( $self->{fullpath} );
99  
100 3 ▶ if ( ! _check_permission( $dirs )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_check_permission($dirs)) 0 3 3
101 0 _err( "Please check write permission for $dirs." );
102 }
103  
104 3 ▶ if ( -f $self->{fullpath} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"fullpath"}) 1 2 3
105 1 $self->{digest} = digest_file_hex( $self->{fullpath}, 'SHA-1' );
106 }
107  
108 3 return $self->{fullpath};
109 }
110  
111 sub _check_permission {
112 3 ▶ my $dir = shift;
Subroutine _check_permission: called CC=3 SCAR=12.3
113  
114 3 my @list = File::Spec->splitdir( $dir );
115  
116 3 ▶ while( @list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 3 0 3
117 3 my $path = File::Spec->catdir( @list );
118  
119 3 ▶ return $path if ( -d -w $path );
Branch: 1 branch
BranchTrueFalseTotal
if -d -w $path 3 0 3
120  
121 0 pop @list;
122 }
123  
124 0 return;
125 }
126  
127 sub do_load {
128 0 ▶ my $self = shift;
Subroutine do_load: not called CC=8 SCAR=42.8
Pod undocumented
129  
130 0 ▶ _err( "Download URL is not set." ) unless $self->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"url"} 0 0 0
131  
132 0 _notify( "=====> Load configuration file '$self->{filename}' from '$self->{url}'." );
133  
134 0 require Enbld::HTTP;
135 0 my $temp = File::Temp->newdir;
136 0 my $path = File::Spec->catfile( $temp, $self->{filename} );
137 0 my $downloaded = Enbld::HTTP->download( $self->{url}, $path );
138
139 0 ▶ unless ( -f -T $downloaded ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f -T $downloaded) 0 0 0
140 0 _err( "Configuration file '$self->{filename}' isn't text file." );
141 }
142  
143 0 ▶ if ( $self->{contents} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"contents"}) 0 0 0
144 0 open my $temphandle, '>>', $downloaded;
145 0 print $temphandle $self->{contents};
146 0 close $temphandle;
147 }
148  
149 0 ▶ if ( $self->{digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digest"}) 0 0 0
150 0 my $digest = digest_file_hex( $path, 'SHA-1' );
151  
152 0 ▶ if ( $self->{digest} eq $digest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digest"} eq $digest) 0 0 0
153 0 _notify( "Configuration file not have the necessity for change." );
154 0 return;
155 }
156  
157 0 my ( undef, $dir, $filename ) = File::Spec->splitpath( $self->{fullpath} );
158  
159 0 ▶ move( $self->{fullpath}, File::Spec->catfile( $dir, $filename . time ) )
Branch: 1 branch
BranchTrueFalseTotal
move($self->{"fullpath"}, "File::Spec"->catfile($dir, $filename . time)) or _err($!) 0 0 0
160 or _err( $! );
161 }
162  
163 0 ▶ copy( $path, $self->{fullpath} ) or _err( $! );
Branch: 1 branch
BranchTrueFalseTotal
copy($path, $self->{"fullpath"}) or _err($!) 0 0 0
164  
165 0 _notify( "=====> Finish configuration file '$self->{filename}'" );
166  
167 0 return $self->{filename};
168 }
169  
170 sub do_set {
171 2 ▶ my $self = shift;
Subroutine do_set: called CC=5 SCAR=19.5
Pod undocumented
172  
173 2 ▶ _err( "Configuration file's contents isn't set." ) unless $self->{contents};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"contents"} 0 2 2
174  
175 2 _notify( "=====> Set configuration file '$self->{filename}'" );
176  
177 2 my ( undef, $dir, $filename ) = File::Spec->splitpath( $self->{fullpath} );
178  
179 2 ▶ if ( $self->{digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digest"}) 0 2 2
180 0 ▶ if ( $self->{digest} eq sha1_hex( $self->{contents}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digest"} eq &sha1_hex($self->{"contents"})) 0 0 0
181 0 _notify( "Configuration file not have the necessity for change." );
182 0 return;
183 };
184  
185 0 ▶ move( $self->{fullpath}, File::Spec->catfile( $dir, $filename . time ))
Branch: 1 branch
BranchTrueFalseTotal
move($self->{"fullpath"}, "File::Spec"->catfile($dir, $filename . time)) or _err($!) 0 0 0
186 or _err( $! );
187 }
188  
189 2 make_path( $dir );
190  
191 2 open my $fh, '>', $self->{fullpath};
192 2 print $fh $self->{contents};
193 2 close $fh;
194  
195 2 _notify( "=====> Finish configuration file '$self->{filename}'" );
196  
197 2 return $self->{filename};
198 }
199  
200 sub do_copy {
201 0 ▶ my $self = shift;
Subroutine do_copy: not called CC=8 SCAR=42.8
Pod undocumented
202  
203 0 ▶ _err( "Configuration file's source path is not set." ) unless $self->{source};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"source"} 0 0 0
204  
205 0 ▶ unless ( -f -T $self->{source} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f -T $self->{"source"}) 0 0 0
206 0 _err( "Configuration file '$self->{filename}' isn't text file." );
207 }
208  
209 0 _notify( "=====> Copy configuration file '$self->{filename}'" );
210  
211 0 my $temp = File::Temp->newdir;
212 0 my $path = File::Spec->catfile( $temp, $self->{filename} );
213 0 copy( $self->{source}, $path );
214  
215 0 ▶ if ( $self->{contents} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"contents"}) 0 0 0
216 0 open my $temphandle, '>>', $path;
217 0 print $temphandle $self->{contents};
218 0 close $temphandle;
219 }
220  
221 0 ▶ if ( $self->{digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digest"}) 0 0 0
222 0 my ( undef, $dir, $filename ) = File::Spec->splitpath( $self->{fullpath} );
223  
224 0 my $digest = digest_file_hex( $path, 'SHA-1' );
225  
226 0 ▶ if ( $self->{digest} eq $digest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digest"} eq $digest) 0 0 0
227 0 _notify( "Configuration file not have the necessity for change." );
228 0 return;
229 }
230 0 ▶ move( $self->{fullpath}, File::Spec->catfile( $dir, $filename . time ) )
Branch: 1 branch
BranchTrueFalseTotal
move($self->{"fullpath"}, "File::Spec"->catfile($dir, $filename . time)) or _err($!) 0 0 0
231 or _err( $! );
232 }
233  
234 0 ▶ copy( $path, $self->{fullpath} ) or _err( $! );
Branch: 1 branch
BranchTrueFalseTotal
copy($path, $self->{"fullpath"}) or _err($!) 0 0 0
235  
236 0 _notify( "=====> Finish configuration file '$self->{filename}'" );
237  
238 0 return $self->{filename};
239 }
240  
241 sub filename {
242 6 ▶ my $self = shift;
Subroutine filename: called CC=1 SCAR=0.0
Pod undocumented
243  
244 6 return $self->{filename};
245 }
246  
247 sub filepath {
248 0 ▶ my $self = shift;
Subroutine filepath: not called CC=1 SCAR=6.9
Pod undocumented
249  
250 0 return $self->{filepath};
251 }
252  
253 sub serialize {
254 1 ▶ my $self = shift;
Subroutine serialize: called CC=5 SCAR=17.1
Pod undocumented
255  
256 1 my $serialized;
257  
258 1 $serialized->{filepath} = $self->{filepath};
259 1 $serialized->{command} = $self->{command};
260  
261 1 ▶ $serialized->{contents} = $self->{contents} if $self->{contents};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"contents"} 1 0 1
262 1 ▶ $serialized->{url} = $self->{url} if $self->{url};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"url"} 0 1 1
263 1 ▶ $serialized->{source} = $self->{source} if $self->{source};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"source"} 0 1 1
264  
265 1 ▶ if ( $self->{directory} ne $ENV{HOME} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"directory"} ne $ENV{"HOME"}) 0 1 1
266 0 $serialized->{directory} = $self->{directory};
267 }
268  
269 1 return $serialized;
270 }
271  
272 sub DSL {
273 0 ▶ my $self = shift;
Subroutine DSL: not called CC=6 SCAR=37.4
Pod undocumented
274  
275 0 my @rcfile;
276  
277 0 my $str = "conf '" . $self->{filepath} . "' => " . $self->{command} . " {\n";
278  
279 0 push @rcfile, $str;
280  
281 0 ▶ if ( $self->{command} eq 'load' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command"} eq "load") 0 0 0
282 0 push @rcfile, ' ' . "from '" . $self->{url} . "';\n";
283 }
284  
285 0 ▶ if ( $self->{command} eq 'copy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command"} eq "copy") 0 0 0
286 0 push @rcfile, ' ' . "from '" . $self->{source} . "';\n";
287 }
288  
289 0 ▶ if ( $self->{directory} ne $ENV{HOME} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"directory"} ne $ENV{"HOME"}) 0 0 0
290 0 push @rcfile, ' ' . "to '" . $self->{directory} . "';\n";
291 }
292  
293 0 ▶ if ( $self->{contents} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"contents"}) 0 0 0
294 0 my @contents = split( "\n", $self->{contents} );
295  
296 0 foreach my $line ( @contents ) {
297 0 push @rcfile, ' ' . "content '" . $line . "';\n";
298 }
299 }
300  
301 0 push @rcfile, "};\n";
302  
303 0 return \@rcfile;
304 }
305  
306 sub _err {
307 0 ▶ my $err = shift;
Subroutine _err: not called CC=1 SCAR=6.9
308 0 my $param = shift;
309  
310 0 die( Enbld::Error->new( $err, $param ));
311 }
312  
313 sub _exception {
314 0 ▶ my $exception = shift;
Subroutine _exception: not called CC=1 SCAR=6.9
315 0 my $param = shift;
316  
317 0 croak( Enbld::Exception->new( $exception, $param ));
318 }
319  
320 sub _notify {
321 4 ▶ my $msg = shift;
Subroutine _notify: called CC=1 SCAR=0.0
322  
323 4 Enbld::Message->notify( $msg );
324 }
325  
326 2 1;
« Enbld.pm ↑ Summary Enbld/Definition/Vim.pm »