blib/lib/HPC/Runner/Command/Logger/JSON/Archive.pm

filter: Statementstmt 35.2% 50 / 142 Branchbran 0.0% 0 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 15 / 24 Podpod 0.0% 0 / 4 totaltotal 30.6% 65 / 212 CC 23 SCAR 37.1
CC
23
Cov
27%
__ANON__
132.0
__ANON__
42.0
add_data
20.0
CRAP 40.8
« blib/lib/HPC/Runner/Command/execute_job/Base.pm ↑ Summary blib/lib/HPC/Runner/Command/execute_job/Utils/MemProfile.pm »
1 package HPC::Runner::Command::Logger::JSON::Archive;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Capture::Tiny ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp qw/ tempfile tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IPC::Run qw(run);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 extends 'Archive::Tar::Wrapper';
20  
21 sub contains_file {
22 0 ▶ my $self = shift;
Subroutine contains_file: not called CC=3 SCAR=24.8
Pod undocumented
23 0 my $file = shift;
24  
25 0 my $found = 0;
26 0 $self->list_reset();
27  
28 0 ▶ while ( my $entry = $self->list_next() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->list_next) 0 0 0
29 0 my ( $tar_path, $phys_path ) = @$entry;
30 0 ▶ if ( $tar_path eq $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tar_path eq $file) 0 0 0
31 0 $found = 1;
32 0 last;
33 }
34 }
35  
36 0 $self->list_reset();
37 0 return $found;
38 }
39  
40 sub add_data {
41 0 ▶ my $self = shift;
Subroutine add_data: not called CC=4 SCAR=30.0
Pod undocumented
42 0 my $file = shift;
43 0 my $data = shift;
44 0 my $append = shift;
45  
46 0 ▶ $append = 0 if !$append;
Branch: 1 branch
BranchTrueFalseTotal
unless $append 0 0 0
47  
48 0 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
49 0 ▶ $data = '' unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
50  
51 0 my $cwd = getcwd();
52 0 my $tmpdir = tempdir( CLEANUP => 0 );
53 0 chdir $tmpdir;
54  
55 0 my $rel_path = File::Spec->abs2rel($file);
56 0 path($rel_path)->touchpath;
57 0 path($rel_path)->touch;
58  
59 try {
60 0 ▶ write_file( $rel_path, { append => $append }, $data );
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 }
62 catch {
63 0 ▶ warn "We were not able to write data to file $file $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 };
65  
66 0 $self->add( $rel_path, $rel_path );
67  
68 0 chdir $cwd;
69 0 remove_tree($tmpdir);
70 }
71  
72 sub replace_content {
73 0 ▶ my $self = shift;
Subroutine replace_content: not called CC=1 SCAR=6.9
Pod undocumented
74 0 my $file = shift;
75 0 my $data = shift;
76  
77 0 $self->add_data( $file, $data, 0 );
78 }
79  
80 sub get_content {
81 0 ▶ my $self = shift;
Subroutine get_content: not called CC=3 SCAR=24.8
Pod undocumented
82 0 my $file = shift;
83  
84 0 $self->list_reset();
85 0 my $data = '{}';
86  
87 0 ▶ while ( my $entry = $self->list_next() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->list_next) 0 0 0
88 0 my ( $tar_path, $phys_path ) = @$entry;
89 0 ▶ if ( $tar_path eq $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tar_path eq $file) 0 0 0
90 try {
91 0 ▶ $data = read_file( $entry->[1] );
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 0 };
93 0 last;
94 }
95 }
96 0 $self->list_reset();
97 0 return $data;
98 }
99  
100 ############################################################
101 # Read and write are very nearly the same as the original. The only major
102 # difference was to get rid of the DEBUG statement about Running
103 ############################################################
104  
105 around 'read' => sub {
106 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
107 0 my $self = shift;
108 0 my $tarfile = shift;
109 0 my @files = @_;
110  
111 0 my $cwd = getcwd();
112  
113 0 $tarfile = File::Spec->rel2abs($tarfile);
114 chdir $self->{tardir}
115 0 ▶ or LOGDIE "Cannot chdir to $self->{tardir}";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"tardir"} or LOGDIE(qq[Cannot chdir to $self->{"tardir"}]) 0 0 0
116  
117 0 my $compr_opt = "";
118 0 $compr_opt = $self->is_compressed($tarfile);
119  
120 my $cmd = [
121 $self->{tar},
122 "${compr_opt}x$self->{tar_read_options}",
123 0 @{ $self->{tar_gnu_read_options} },
124 "-f", $tarfile, @files
125 ];
126  
127 0 my $rc = run( $cmd, \my ( $in, $out, $err ) );
128  
129 0 ▶ if ( !$rc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc) 0 0 0
130 0 ERROR "@$cmd failed: $err";
131 0 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 0 0 0
132 0 return undef;
133 }
134  
135 0 ▶ WARN $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
136  
137 0 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 0 0 0
138  
139 0 return 1;
140 1 };
141  
142 around 'write' => sub {
143 0 ▶ my ( $orig, $self, $tarfile, $compress ) = @_;
Subroutine __ANON__: not called CC=11 SCAR=48.8
144  
145 0 my $cwd = getcwd();
146 0 ▶ chdir $self->{tardir} or LOGDIE "Can't chdir to $self->{tardir} ($!)";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"tardir"} or LOGDIE(qq[Can't chdir to $self->{"tardir"} ($!)]) 0 0 0
147  
148 0 $tarfile = File::Spec->rel2abs($tarfile);
149  
150 0 my $compr_opt = "";
151 0 ▶ $compr_opt = "z" if $compress;
Branch: 1 branch
BranchTrueFalseTotal
if $compress 0 0 0
152  
153 0 ▶ opendir DIR, "." or LOGDIE "Cannot open $self->{tardir}";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, "." or LOGDIE(qq[Cannot open $self->{"tardir"}]) 0 0 0
154 0 my @top_entries = grep { $_ !~ /^\.\.?$/ } readdir DIR;
155 0 closedir DIR;
156  
157 my $cmd = [
158 $self->{tar}, "${compr_opt}cf$self->{tar_write_options}",
159 0 $tarfile, @{ $self->{tar_gnu_write_options} }
160 ];
161  
162 0 ▶ if ( @top_entries > $self->{max_cmd_line_args} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@top_entries > $self->{'max_cmd_line_args'}) { } 0 0 0
163 0 my $filelist_file = File::Spec->catdir( $self->{tmpdir}, "file-list" );
164 0 ▶ write_file( $filelist_file, { append => 0 }, '' )
Branch: 1 branch
BranchTrueFalseTotal
write_file($filelist_file, {"append", 0}, "") or LOGDIE("Cannot open $filelist_file ($!)") 0 0 0
165 or LOGDIE "Cannot open $filelist_file ($!)";
166  
167 0 for (@top_entries) {
168 0 write_file( $filelist_file, { append => 1 }, $_ );
169 }
170 0 push @$cmd, "-T", $filelist_file;
171 }
172 else {
173 0 push @$cmd, @top_entries;
174 }
175  
176 0 my $rc = run( $cmd, \my ( $in, $out, $err ) );
177  
178 0 ▶ if ( !$rc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc) 0 0 0
179 0 ERROR "@$cmd failed: $err";
180 0 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 0 0 0
181 0 return undef;
182 }
183  
184 0 ▶ WARN $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
185  
186 0 ▶ chdir $cwd or LOGDIE "Cannot chdir to $cwd";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or LOGDIE("Cannot chdir to $cwd") 0 0 0
187  
188 0 return 1;
189 1 };
190  
191 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 1 __PACKAGE__->meta->make_immutable;
193  
194 1 1;
« blib/lib/HPC/Runner/Command/execute_job/Base.pm ↑ Summary blib/lib/HPC/Runner/Command/execute_job/Utils/MemProfile.pm »