blib/lib/Hadoop/HDFS/Command.pm

filter: Statementstmt 20.1% 48 / 238 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 43.7% 14 / 32 Podpod 100% 1 / 1 totaltotal 16.1% 63 / 390 SCAR 73.1
CC
48
Cov
14%
_dfs_ls
506.0
_dfs_put
72.0
_parse_options
56.0
CRAP 1501.1
↑ Summary
1 package Hadoop::HDFS::Command;
2 2 $Hadoop::HDFS::Command::VERSION = '0.005';
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Capture::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IPC::Cmd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Ref::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Types::Standard qw(Bool Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶{ use Moo; }
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 has cmd_hdfs => (
20 is => 'rw',
21 isa => sub {
22 0 ▶ my $val = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
23 0 ▶ return if $val && -e $val && -x _;
Branch: 1 branch
BranchTrueFalseTotal
if $val and -e $val and -x _ 0 0 0
Condition0%0/6
$val and -e $val and -x _
!ll&&!rl&&r
000
$val and -e $val
!ll&&!rl&&r
000
MC/DC0%0/3
$val and -e $val and -x _
$val -e $val -x _
Truth table0%0/4
A and B and C
A= $valB= -e $valC= -x _
ABCresult
0XX0
10X0
1100
1111
24 0 Carp::confess sprintf "The command `%s` either does not exist or not an executable!",
25 $val,
26 ;
27 },
28 0 ▶ default => sub { '/usr/bin/hdfs' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 2 lazy => 1,
30 );
31  
32 has enable_log => (
33 is => 'rw',
34 isa => Bool,
35 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 2 lazy => 1,
37 );
38  
39 2 has runas => (
40 is => 'rw',
41 isa => Str,
42 default => scalar getpwuid $<,
43 lazy => 1,
44 );
45  
46 before ['_capture', '_capture_with_stdin'] => sub {
47 0 ▶ my ($self, $options, @cmd) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
48 0 ▶ unshift @cmd, 'sudo', '-u', $self->runas
Branch: 1 branch
BranchTrueFalseTotal
unless $self->runas eq getpwuid $< 0 0 0
49 unless $self->runas eq getpwuid $<;
50 0 @_ = ($self, $options, @cmd);
51 2 };
52  
53 sub dfs {
54 0 ▶ my $self = shift;
Subroutine dfs: not called CC=4 SCAR=30.0
Pod documented
55 0 ▶ my $options = Ref::Util::is_hashref $_[0] ? shift( @_ ) : {};
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_hashref($_[0]) ? : 0 0 0
56 0 ▶ (my $cmd = shift || die "No dfs command specified") =~ s{ \A [-]+ }{}xms;
Condition0%0/2
shift() || die('No dfs command specified')
l!l
00
MC/DC0%0/1
shift() || die('No dfs command specified')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
57 0 my $method = '_dfs_' . $cmd;
58 0 ▶ Carp::croak "'$cmd' is not implemented!" if ! $self->can( $method );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($method) 0 0 0
59 0 $self->$method( $options, @_ );
60 }
61  
62 sub _dfs_ls {
63 0 ▶ my $self = shift;
Subroutine _dfs_ls: not called CC=22 SCAR=62.3
64 0 state $strp;
65  
66 0 my $options = shift;
67 0 my @params = @_;
68 0 my @flags = qw( d h R );
69 0 my($arg, $paths) = $self->_parse_options(
70 \@params,
71 \@flags,
72 undef,
73 {
74 require_params => 1,
75 },
76 );
77  
78 0 my $want_epoch = $options->{want_epoch};
79 0 my $cb = delete $options->{callback};
80  
81 0 ▶ if ( $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
82 0 ▶ die "callback needs to be a CODE" if ! Ref::Util::is_coderef $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($cb) 0 0 0
83 0 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 0 0
84 0 Carp::croak "You need to call this function in void context when callback is specified";
85 }
86 }
87  
88 my @response = $self->_capture(
89 $options,
90 $self->cmd_hdfs,
91 qw( dfs -ls ),
92 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
93 0 @{ $paths },
94 );
95  
96 # directory is empty
97 #
98 0 ▶ return if ! @response;
Branch: 1 branch
BranchTrueFalseTotal
unless @response 0 0 0
99  
100 0 ▶ if ( $response[0] && $response[0] =~ m{ \A Found \s+ [0-9] }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response[0] and $response[0] =~ / \A Found \s+ [0-9] /msx) 0 0 0
Condition0%0/3
$response[0] and $response[0] =~ / \A Found \s+ [0-9] /msx
!ll&&!rl&&r
000
MC/DC0%0/2
$response[0] and $response[0] =~ / \A Found \s+ [0-9] /msx
$response[0] $response[0] =~ / \A Found \s+ [0-9] /msx
Truth table0%0/3
A and B
A= $response[0]B= $response[0] =~ / \A Found \s+ [0-9] /msx
ABresult
0X0
100
111
101 0 shift @response; # junk
102 }
103  
104 0 my $space = q{ };
105  
106 0 my @rv;
107 0 for my $line ( @response ) {
108 0 my($mode, $replication, $user, $group, @unknown) = split m{ \s+ }xms, $line, 5;
109 0 my @rest = map { split $space, $_ } @unknown;
110 0 my $size;
111 0 ▶ if ( $arg->{h}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'h'}) { } 0 0 0
112 0 ▶ if ( $rest[0] eq '0' || $rest[1] !~ m{ [a-zA-Z_] }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest[0] eq '0' or not $rest[1] =~ / [a-zA-Z_] /msx) { } 0 0 0
Condition0%0/3
$rest[0] eq 0 or not $rest[1] =~ / [a-zA-Z_] /msx
l!l&&r!l&&!r
000
MC/DC0%0/2
$rest[0] eq 0 or not $rest[1] =~ / [a-zA-Z_] /msx
$rest[0] eq 0 not $rest[1] =~ / [a-zA-Z_] /msx
Truth table0%0/3
A or not(B)
A= $rest[0] eq 0B= not $rest[1] =~ / [a-zA-Z_] /msx
ABresult
1X1
011
000
113 0 $size = shift @rest;
114 }
115 else {
116 0 $size = join $space, shift @rest, shift @rest;
117 }
118 }
119 else {
120 0 $size = shift @rest;
121 }
122 0 my $date = join ' ', shift @rest, shift @rest;
123 0 ▶ my $path = shift( @rest ) || die "Unable to parse $line to gather the path";
Condition0%0/2
shift @rest || die("Unable to parse $line to gather the path")
l!l
00
MC/DC0%0/1
shift @rest || die("Unable to parse $line to gather the path")
shift @rest
Truth table0%0/2
A
A= shift @rest
Aresult
11
00
124 0 ▶ my $is_dir = $mode =~ m{ \A [d] }xms ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ / \A [d] /msx ? : 0 0 0
125  
126 0 ▶ my %record = (
Branch: 1 branch
BranchTrueFalseTotal
$is_dir ? : 0 0 0
127 mode => $mode,
128 replication => $replication,
129 user => $user,
130 group => $group,
131 size => $size,
132 date => $date,
133 path => $path,
134 type => $is_dir ? 'dir' : 'file',
135 );
136  
137 0 ▶ if ( $want_epoch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_epoch) 0 0 0
138 0 ▶ $strp ||= DateTime::Format::Strptime->new(
Condition0%0/3
$strp ||= "DateTime::Format::Strptime"->new("pattern", "%Y-%m-%d %H:%M", "time_zone", "CET", "on_error", "croak")
l!l&&r!l&&!r
000
MC/DC0%0/2
$strp ||= "DateTime::Format::Strptime"->new("pattern", "%Y-%m-%d %H:%M", "time_zone", "CET", "on_error", "croak")
$strp "DateTime::Format::Strptime"->new("pattern", "%Y-%m-%d %H:%M", "time_zone", "CET", "on_error", "croak")
Truth table0%0/3
A ||= B
A= $strpB= "DateTime::Format::Strptime"->new("pattern", "%Y-%m-%d %H:%M", "time_zone", "CET", "on_error", "croak")
ABresult
1X1
011
000
139 pattern => '%Y-%m-%d %H:%M',
140 time_zone => 'CET',
141 on_error => 'croak',
142 );
143 eval {
144 0 $record{epoch} = $strp->parse_datetime( $date )->epoch;
145 0 1;
146 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $record{"epoch"} = $strp->parse_datetime($date)->epoch; 1 } } or do { my $eval_error = $@ || "Zombie error"; $self->_log("debug", "Failed to convert %s into an epoch: %s", $date, $eval_error) } 0 0 0
147 0 ▶ my $eval_error = $@ || 'Zombie error';
Condition0%0/2
$@ || 'Zombie error'
l!l
00
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
148 0 $self->_log( debug => 'Failed to convert %s into an epoch: %s',
149 $date,
150 $eval_error,
151 );
152 };
153 }
154  
155 0 ▶ if ( @rest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 0 0 0
156 # interpret as the rest of the path as spaces in paths are ok
157 # possibly this will need to be revisited in the future.
158 #
159 0 $record{path} = join $space, $record{path}, @rest;
160 }
161  
162 0 ▶ if ( $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
163 # control the flow from the callback
164 # So, the return value matters.
165 #
166 0 ▶ if ( ! $cb->( \%record ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$cb(\%record)) 0 0 0
167 0 $self->_log( info => 'Terminating the ls processing as the user callback did not return a true value.');
168 0 last;
169 }
170 0 next;
171 }
172  
173 0 push @rv, { %record };
174 }
175  
176 0 ▶ return if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
177 0 return @rv;
178 }
179  
180 sub _dfs_du {
181 0 ▶ my $self = shift;
Subroutine _dfs_du: not called CC=3 SCAR=24.8
182 0 my $options = shift;
183 0 my @params = @_;
184 0 my @flags = qw( h s );
185 0 my($arg, $paths) = $self->_parse_options(
186 \@params,
187 \@flags,
188 undef,
189 {
190 require_params => 1,
191 },
192 );
193  
194 my @rv = $self->_capture(
195 $options,
196 $self->cmd_hdfs,
197 qw( dfs -du ),
198 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
199 0 ▶ @{ $paths },
Branch: 1 branch
BranchTrueFalseTotal
my(@rv) = $self->_capture($options, $self->cmd_hdfs, ("dfs", "-du"), map({'-' . $_;} grep({$arg->{$_};} @flags)), @{$paths;}) or die "No output collected from -du command" 0 0 0
200 ) or die "No output collected from -du command";
201  
202 return map {
203 0 my @val = split m{ \s{2,} }xms, $_;
204 {
205 0 ▶ size => shift( @val ),
Branch: 1 branch
BranchTrueFalseTotal
@val ? : 0 0 0
206 name => pop( @val ),
207 ( @val ? (
208 disk_space_consumed => shift( @val ),
209 ) : () ),
210 }
211 } @rv;
212 }
213  
214 sub _dfs_mv {
215 0 ▶ my $self = shift;
Subroutine _dfs_mv: not called CC=3 SCAR=24.8
216 0 my $options = shift;
217 0 my @params = @_;
218 0 my($arg, $paths) = $self->_parse_options(
219 \@params,
220 [],
221 undef,
222 {
223 require_params => 1,
224 },
225 );
226 0 ▶ my $source = shift @{ $paths } || die "Source path not specified";
Condition0%0/2
shift @{$paths;} || die('Source path not specified')
l!l
00
MC/DC0%0/1
shift @{$paths;} || die('Source path not specified')
shift @{$paths;}
Truth table0%0/2
A
A= shift @{$paths;}
Aresult
11
00
227 0 ▶ my $target = shift @{ $paths } || die "Target path not specified";
Condition0%0/2
shift @{$paths;} || die('Target path not specified')
l!l
00
MC/DC0%0/1
shift @{$paths;} || die('Target path not specified')
shift @{$paths;}
Truth table0%0/2
A
A= shift @{$paths;}
Aresult
11
00
228  
229 # will die on error
230 0 $self->_capture(
231 $options,
232 $self->cmd_hdfs,
233 qw( dfs -mv ),
234 $source => $target,
235 );
236  
237 0 return;
238 }
239  
240 sub _dfs_rm {
241 0 ▶ my $self = shift;
Subroutine _dfs_rm: not called CC=1 SCAR=6.9
242 0 my $options = shift;
243 0 my @params = @_;
244 0 my @flags = qw( f r skipTrash );
245 0 my($arg, $paths) = $self->_parse_options(
246 \@params,
247 \@flags,
248 undef,
249 {
250 require_params => 1,
251 },
252 );
253  
254 my @response = $self->_capture(
255 $options,
256 $self->cmd_hdfs,
257 qw( dfs -rm ),
258 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
259 0 @{ $paths },
260 );
261  
262 # just a confirmation message
263 0 return @response;
264 }
265  
266 sub _dfs_put {
267 0 ▶ my $self = shift;
Subroutine _dfs_put: not called CC=8 SCAR=42.8
268 0 my $options = shift;
269 0 my @params = @_;
270 0 my @flags = qw( f p l - );
271 0 my($arg, $paths) = $self->_parse_options(
272 \@params,
273 \@flags,
274 undef,
275 {
276 require_params => 1,
277 },
278 );
279  
280 0 ▶ if ( $paths->[0] && $paths->[0] eq '\-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paths->[0] and $paths->[0] eq "\\-") 0 0 0
Condition0%0/3
$paths->[0] and $paths->[0] eq "\\-"
!ll&&!rl&&r
000
MC/DC0%0/2
$paths->[0] and $paths->[0] eq "\\-"
$paths->[0] $paths->[0] eq "\\-"
Truth table0%0/3
A and B
A= $paths->[0]B= $paths->[0] eq "\\-"
ABresult
0X0
100
111
281 0 shift @{ $paths };
282 0 ▶ $options->{stdin} = pop( @{ $paths } ) || die "stdin content not specified!";
Condition0%0/2
pop @{$paths;} || die('stdin content not specified!')
l!l
00
MC/DC0%0/1
pop @{$paths;} || die('stdin content not specified!')
pop @{$paths;}
Truth table0%0/2
A
A= pop @{$paths;}
Aresult
11
00
283 }
284  
285 0 ▶ if ( @{ $paths } < ( $options->{stdin} ? 1 : 2 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$paths;} < ($options->{"stdin"} ? 1 : 2)) 0 0 0
$options->{'stdin'} ? : 0 0 0
286 0 die "Missing arguments!";
287 }
288  
289 my @response = $self->_capture_with_stdin(
290 $options,
291 $self->cmd_hdfs,
292 qw( dfs -put ),
293 0 ▶ ( map { $_ eq '-' ? $_ : '-' . $_ } grep { $arg->{ $_ } } @flags ),
Branch: 2 branches
BranchTrueFalseTotal
$_ eq '-' ? : 0 0 0
$options->{'stdin'} ? : 0 0 0
294 ( $options->{stdin} ? '-' : () ),
295 0 @{ $paths },
296 );
297  
298 # just a confirmation message
299 0 return @response;
300 }
301  
302 sub _dfs_test {
303 0 ▶ my $self = shift;
Subroutine _dfs_test: not called CC=2 SCAR=17.9
304 0 my $options = shift;
305 0 my @params = @_;
306 0 my @flags = qw( d e f s z );
307 0 my($arg, $paths) = $self->_parse_options(
308 \@params,
309 \@flags,
310 undef,
311 {
312 require_params => 1,
313 },
314 );
315 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_capture($options, $self->cmd_hdfs, ("dfs", "-test"), map({'-' . $_;} grep({$arg->{$_};} @flags)), @{$paths;}); return 1 } } or return 0 0 0 0
316 $self->_capture(
317 $options,
318 $self->cmd_hdfs,
319 qw( dfs -test ),
320 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
321 0 @{ $paths },
322 );
323 0 return 1;
324 } or return 0;
325 }
326  
327 sub _dfs_mkdir {
328 0 ▶ my $self = shift;
Subroutine _dfs_mkdir: not called CC=1 SCAR=6.9
329 0 my $options = shift;
330 0 my @params = @_;
331 0 my @flags = qw( p );
332 0 my($arg, $paths) = $self->_parse_options(
333 \@params,
334 \@flags,
335 undef,
336 {
337 require_params => 1,
338 },
339 );
340 my @response = $self->_capture(
341 $options,
342 $self->cmd_hdfs,
343 qw( dfs -mkdir ),
344 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
345 0 @{ $paths },
346 );
347  
348 # just a confirmation message
349 return @response
350 0 }
351  
352 sub _dfs_chmod {
353 0 ▶ my $self = shift;
Subroutine _dfs_chmod: not called CC=1 SCAR=6.9
354 0 my $options = shift;
355 0 my @params = @_;
356 0 my @flags = qw( p );
357 0 my($arg, $paths) = $self->_parse_options(
358 \@params,
359 \@flags,
360 undef,
361 {
362 require_params => 1,
363 },
364 );
365 my @response = $self->_capture(
366 $options,
367 $self->cmd_hdfs,
368 qw( dfs -chmod ),
369 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
370 0 @{ $paths },
371 );
372  
373 # just a confirmation message
374 return @response
375 0 }
376  
377 sub _dfs_chown {
378 0 ▶ my $self = shift;
Subroutine _dfs_chown: not called CC=1 SCAR=6.9
379 0 my $options = shift;
380 0 my @params = @_;
381 0 my @flags = qw( p );
382 0 my($arg, $paths) = $self->_parse_options(
383 \@params,
384 \@flags,
385 undef,
386 {
387 require_params => 1,
388 },
389 );
390 my @response = $self->_capture(
391 $options,
392 $self->cmd_hdfs,
393 qw( dfs -chown ),
394 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
395 0 @{ $paths },
396 );
397  
398 # just a confirmation message
399 return @response
400 0 }
401  
402 sub _dfs_get {
403 0 ▶ my $self = shift;
Subroutine _dfs_get: not called CC=1 SCAR=6.9
404 0 my $options = shift;
405 0 my @params = @_;
406 0 my @flags = qw( p ignoreCrc crc );
407 0 my($arg, $paths) = $self->_parse_options(
408 \@params,
409 \@flags,
410 undef,
411 {
412 require_params => 1,
413 },
414 );
415 my @response = $self->_capture(
416 $options,
417 $self->cmd_hdfs,
418 qw( dfs -get ),
419 0 ( map { '-' . $_ } grep { $arg->{ $_ } } @flags ),
420 0 @{ $paths },
421 );
422  
423 # just a confirmation message
424 return @response
425 0 }
426  
427 sub _parse_options {
428 0 ▶ my $self = shift;
Subroutine _parse_options: not called CC=7 SCAR=40.3
429 # TODO: collect dfs generic options
430 #
431 # Generic options supported are
432 # -conf <configuration file> specify an application configuration file
433 # -D <property=value> use value for given property
434 # -fs <local|namenode:port> specify a namenode
435 # -jt <local|resourcemanager:port> specify a ResourceManager
436 # -files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
437 # -libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
438 # -archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.
439  
440 0 my($params, $flags, $opt, $conf) = @_;
441 0 ▶ $conf ||= {};
Condition0%0/2
$conf ||= {}
l!l
00
MC/DC0%0/1
$conf ||= {}
$conf
Truth table0%0/2
A
A= $conf
Aresult
11
00
442 0 ▶ my @params = map { $_ eq '-' ? '\-' : $_ } @{ $params };
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '-' ? : 0 0 0
443  
444 Getopt::Long::GetOptionsFromArray(
445 \@params,
446 \my %arg,
447 (
448 0 ▶ map { Ref::Util::is_arrayref $_ ? @{ $_ } : () }
Branch: 2 branches
BranchTrueFalseTotal
&Getopt::Long::GetOptionsFromArray(\@params, \my %arg, map({Ref::Util::XS::is_arrayref($_) ? @{$_;} : ();} $flags, $opt)) or die "Unable to parse parameters: '@{$params;}'" 0 0 0
Ref::Util::XS::is_arrayref($_) ? : 0 0 0
449 $flags,
450 $opt,
451 ),
452 0 ) || die qq{Unable to parse parameters: '@{$params}'};
453  
454 0 ▶ if ( $conf->{require_params} && ! @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"require_params"} and not @params) 0 0 0
Condition0%0/3
$conf->{"require_params"} and not @params
!ll&&!rl&&r
000
MC/DC0%0/2
$conf->{"require_params"} and not @params
$conf->{"require_params"} not @params
Truth table0%0/3
A and not(B)
A= $conf->{"require_params"}B= not @params
ABresult
0X0
100
111
455 0 die "No parameters were specified!";
456 }
457  
458 0 return \%arg, [ @params ];
459 }
460  
461 sub _capture {
462 my $self = shift;
463 my $options = shift;
464 my @cmd = @_;
465  
466 $self->_log( debug => 'Executing command: %s', join(' ', @cmd) );
467  
468 my $start = time;
469  
470 my($stdout, $stderr, $fail) = Capture::Tiny::capture {
471 system( @cmd );
472 };
473  
474 $self->_log( debug => 'Execution took %.3f seconds', time - $start );
475  
476 if ( $fail ) {
477 my $code = $fail >> 8;
478 $stderr ||= '[no error]';
479 my $msg = "External command (@cmd) failed with status=$code: $stderr";
480 if ( $options->{ignore_fail} ) {
481 if ( ! $options->{silent} ) {
482 warn "[Fatal error downgraded to a warning] $msg";
483 }
484 return $self->_split_on_newlines( $stdout || '' );
485 }
486 die $msg;
487 }
488  
489 if ( $stderr ) {
490 warn "Warning from external command: $stderr";
491 }
492  
493 return $self->_split_on_newlines( $stdout );
494 }
495  
496 sub _capture_with_stdin {
497 my $self = shift;
498 # TODO: use a single capture method.
499 my $options = shift;
500 my @cmd = @_;
501  
502 my $stdin = delete $options->{stdin};
503  
504 $self->_log( debug => 'Executing command(IPC): %s', join(' ', @cmd) );
505  
506 my $start = time;
507  
508 my $res = IPC::Cmd::run_forked(
509 \@cmd,
510 {
511 ( $stdin ? (
512 child_stdin => $stdin,
513 ) : () ),
514 #timeout => $timeout,
515 terminate_on_parent_sudden_death => 1,
516 }
517 );
518  
519 $self->_log( debug => 'Execution took %.3f seconds', time - $start );
520  
521 my($stdout, $stderr, $fail);
522  
523 my $success = defined $res->{exit_code}
524 && $res->{exit_code} == 0
525 && ! $res->{timeout};
526  
527 $fail = $success ? 0 : $res->{exit_code};
528 $stderr = $res->{stderr};
529 $stdout = $res->{stdout};
530  
531 if ( $fail ) {
532 my $code = $fail >> 8;
533 $stderr ||= $res->{err_msg} || '[no error]';
534 my $msg = "External command (@cmd) failed with status=$code: $stderr";
535 if ( $options->{ignore_fail} ) {
536 if ( ! $options->{silent} ) {
537 warn "[Fatal error downgraded to a warning] $msg";
538 }
539 return $self->_split_on_newlines( $stdout || '' );
540 }
541 die $msg;
542 }
543  
544 if ( $stderr ) {
545 warn "Warning from external command: $stderr";
546 }
547  
548 return $self->_split_on_newlines( $stdout );
549 }
550  
551 sub _split_on_newlines {
552 0 ▶ my $self = shift;
Subroutine _split_on_newlines: not called CC=1 SCAR=6.9
553 0 my $rv = shift;
554  
555 0 $rv =~ s{ \A \s+ }{}xms;
556 0 $rv =~ s{ \s+ \z }{}xms;
557  
558 0 return split m{ \n+ }xms, $rv;
559 }
560  
561 sub _log {
562 0 ▶ my $self = shift;
Subroutine _log: not called CC=2 SCAR=17.9
563 0 ▶ return if ! $self->enable_log;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enable_log 0 0 0
564 0 my($level, $tmpl, @param) = @_;
565 0 my $msg = sprintf "[%s] %s\n", uc $level, $tmpl;
566 0 printf STDERR $msg, @param;
567 }
568  
569 2 1;
570  
571 __END__
572  
573 =pod
574  
575 =encoding UTF-8
576  
577 =head1 NAME
578  
579 Hadoop::HDFS::Command
580  
581 =head1 VERSION
582  
583 version 0.005
584  
585 =head1 SYNOPSIS
586  
587 use Hadoop::HDFS::Command;
588 my $hdfs = Hadoop::HDFS::Command->new;
589 my @rv = $hdfs->$command( @command_args );
590  
591 =head1 DESCRIPTION
592  
593 This is a simple wrapper around the hdfs commandline to make them easier to
594 call from Perl and parse their output.
595  
596 The interface is partially done at the moment (see the implemented wrappers
597 down below).
598  
599 You can always use the WebHDFS to do similar operations instead of failling
600 back to the commandline. However there are several benefits of using the
601 cli; i) you'll end up with a single C<JVM> invocation, so the response
602 might be faster ii) Some functionality / endpoints might be buggy for WebHDFS
603 but might work with the cli (for example escaping certain values is broken
604 in some versions, but works with the cli).
605  
606 =head1 NAME
607  
608 Hadoop::HDFS::Command - Wrappers for various hadoop hdfs cli commands
609  
610 =head1 METHODS
611  
612 =head2 new
613  
614 The constructor. Available attributes are listed below.
615  
616 =head3 cmd_hdfs
617  
618 Default value is C</usr/bin/hdfs>. This option needs to be altered if you have
619 the C<`hdfs`> command in some other place.
620  
621 =head3 enable_log :Bool
622  
623 Can be used to enable the internal logging feature. Disabled by default.
624  
625 =head2 dfs
626  
627 One of the top level commands, including an interface to the sub-commands
628 listed below. The calling convention of the sub commands is as simple as:
629  
630 my @rv = $hdfs->dfs( \%options, $sub_command => @subcommand_args );
631 # options hash is optional
632 my @rv = $hdfs->dfs( $sub_command => @subcommand_args );
633  
634 Available options are listed below:
635  
636 =over 4
637  
638 =item ignore_fail :Bool
639  
640 Global.
641  
642 =item silent :Bool
643  
644 Global.
645  
646 =item want_epoch :Bool
647  
648 Only used for C<ls>. Converts timestamps to epoch.
649  
650 =item callback :CODE
651  
652 Only used for C<ls>. The callback always needs to return true to continue
653 processing, returning false from it will short-circuit the processor.
654  
655 =back
656  
657 =head3 du
658  
659 The C<@subcommand_args> can have these defined: C<-s>, C<-h>.
660  
661 my @rv = $hdfs->dfs( du => @subcommand_args => $hdfs_path );
662 my @rv = $hdfs->dfs( du => qw( -h -s ) => "/tmp" );
663 my @rv = $hdfs->dfs(
664 {
665 ignore_fail => 1,
666 silent => 1,
667 },
668 du => -s => @hdfs_paths,
669 );
670  
671 =head3 ls
672  
673 The C<@subcommand_args> can have these defined: C<-d>, C<-h>, C<R>.
674  
675 my @rv = $hdfs->dfs( ls => @subcommand_args => $hdfs_path );
676  
677 The callback can be used to prevent buffering and process the result set yourself.
678 The callback always needs to return true to continue processing. If you want to
679 skip some entries but continue processing then a true value needs to be returned.
680 A bare return (which is false) will short circuit the iterator and discard any
681 remaining records.
682  
683 my %options = (
684 callback => sub {
685 # This callback will receive a hash meta-data about the file.
686 my $file = shift;
687 if ( $file->{type} eq 'dir' ) {
688 # do something
689 }
690  
691 # skip this one, but continue processing
692 return 1 if $file->{type} ne 'file';
693  
694 # do something
695  
696 return if $something_really_bad_so_end_this_processor;
697  
698 # continue processing
699 return 1;
700 },
701 # The meta-data passed to the callback will have an "epoch"
702 # key set when this is true.
703 want_epoch => 1,
704 );
705 # execute the command recursively on the path
706 $hdfs->dfs( \%options, ls => -R => $hdfs_path );
707  
708 =head3 mv
709  
710 my @rv = $hdfs->dfs( mv => $hdfs_source_path, $hdfs_dest_path );
711  
712 =head3 put
713  
714 The C<@subcommand_args> can have these defined: C<-f>, C<-p>, C<-l>
715  
716 $hdfs->dfs( put => @subcommand_args, $local_path, $hdfs_path );
717 # notice the additional "-"
718 $hdfs->dfs( put => '-f', '-', $hdfs_path, $in_memory_data );
719  
720 =head3 rm
721  
722 The C<@subcommand_args> can have these defined: C<-f>, C<-r>, C<-skipTrash>
723  
724 $hdfs->dfs( rm => @subcommand_args, $hdfs_path );
725  
726 =head3 test
727  
728 The C<@subcommand_args> can have these defined: C<-d>, C<-e>, C<-f>, C<-s>, C<-z>
729  
730 $hdfs->dfs( test => @subcommand_args, $hdfs_path );
731  
732 =head3 mkdir
733  
734 The C<@subcommand_args> can have these defined: C<-p>
735  
736 $hdfs->dfs( mkdir => @subcommand_args, $path );
737  
738 =head3 chmod
739  
740 The C<@subcommand_args> can have these defined: C<-R>
741  
742 $hdfs->dfs( chmod => @subcommand_args, $mode, $path );
743  
744 =head3 chown
745  
746 The C<@subcommand_args> can have these defined: C<-R>
747  
748 $hdfs->dfs( chown => @subcommand_args, $OWNERCOLONGROUP, $path );
749  
750 =head3 get
751  
752 The C<@subcommand_args> can have these defined: C<-p>, C<-ignoreCrc>, C<-crc>
753  
754 $hdfs->dfs( get => @subcommand_args, $src, $localdst );
755  
756 =head1 SEE ALSO
757  
758 C<`hdfs dfs -help`>.
759  
760 =head1 AUTHOR
761  
762 Burak Gursoy <burak@cpan.org>
763  
764 =head1 COPYRIGHT AND LICENSE
765  
766 This software is copyright (c) 2016 by Burak Gursoy.
767  
768 This is free software; you can redistribute it and/or modify it under
769 the same terms as the Perl 5 programming language system itself.
770  
771 =cut
↑ Summary