blib/lib/App/ppll.pm

filter: Statementstmt 96.0% 48 / 50 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 97.0% 65 / 67 CC 1 SCAR 0.2
CC
1
Cov
96%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/App/ppll/Worker.pm ↑ Summary bin/ppll »
1 package App::ppll v0.0.1; ## no critic [NamingConventions::Capitalization]
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 App::ppll - Command runner
8  
9 =head1 VERSION
10  
11 0.0.1
12  
13 =head1 DESCRIPTION
14  
15 C<ppll> is a tool to control the execution of commands. It can run commands in
16 parallel, construct commands from lists of parameters, and more.
17  
18 It handles the output of commands and can prefix lines with which command
19 produced it, print timestamps, etc.
20  
21 C<ppll> has functionality similar to C<xargs> and C<parallel>.
22  
23 This page documents C<ppll>’s Perl API. For user documentation of the C<ppll>
24 command see L<ppll|ppll>.
25  
26 =head1 SYNOPSIS
27  
28 my $ppll = App::ppll->new( %args );
29 $ppll->call();
30  
31 =cut
32  
33 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use utf8::all;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 8 ▶use Digest::MD5 qw( md5 );
Subroutine BEGIN: called CC=1 SCAR=0.0
42 8 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 8 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 8 ▶use List::Flatten qw( flat );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 8 ▶use List::Util qw( max shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
46 8 ▶use POSIX qw( isatty );
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 ▶use Pod::Usage qw( pod2usage );
Subroutine BEGIN: called CC=1 SCAR=0.0
48 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 8 ▶use String::ShellQuote qw( shell_quote );
Subroutine BEGIN: called CC=1 SCAR=0.0
50 0 ▶use Sys::CPU qw( cpu_count );
Subroutine BEGIN: called CC=1 SCAR=2.6
51 use Term::ANSIColor qw( colored );
52 use Term::ReadKey;
53 use Time::Duration qw( concise duration );
54 use Time::HiRes qw( time );
55  
56 use experimental 'signatures';
57  
58 require App::ppll::Worker;
59  
60 Readonly my @COLOURS => (
61  
62 'white on_black',
63 'black on_red',
64 'black on_green',
65 'black on_yellow',
66 'white on_blue',
67 'black on_magenta',
68 'black on_cyan',
69 'black on_white',
70 'white on_bright_black',
71 'black on_bright_red',
72 'black on_bright_green',
73 'black on_bright_yellow',
74 'black on_bright_blue',
75 'black on_bright_magenta',
76 'black on_bright_cyan',
77 'black on_bright_white',
78  
79 );
80  
81 Readonly my @MODES => ( qw(
82 auto
83 fields
84 lines
85 slpf
86 ) );
87  
88 Readonly my $WIDTH => 80;
89  
90 =head1 SUBROUTINES/METHODS
91  
92 =head2 C<call>
93  
94 Runs C<ppll>.
95  
96 Returns an integer suitable for C<exit> (0 if everything went fine, non-0
97 otherwise).
98  
99 =cut
100  
101 sub call ( $self ) { ## no critic [Subroutines::ProhibitExcessComplexity]
102 if ( $self->{help} ) {
103 pod2usage(
104 -exitval => 'NOEXIT',
105 -output => \*STDOUT,
106 -verbose => 1,
107 );
108 return 0;
109 }
110  
111 if ( $self->{version} ) {
112 say $App::ppll::VERSION;
113 return 0;
114 }
115  
116 do {
117 ## no critic [Variables::RequireLocalizedPunctuationVars]
118 $SIG{INT} = $SIG{TERM} = sub { $self->stop };
119 };
120  
121 my $prefix_width
122 = max( 1, map {length} grep {defined} flat( @{ $self->{parameters} } ) );
123  
124 $self->{pool} = [];
125  
126 my ( @failed, @succeeded );
127 while ( @{ $self->{pool} } or @{ $self->{parameters} } ) {
128 while ( @{ $self->{parameters} } and @{ $self->{pool} } < $self->{jobs} ) {
129 my $queue = @{ $self->{parameters} }[0];
130  
131 unless ( @$queue ) {
132 last
133 if ( @{ $self->{pool} } );
134  
135 shift @{ $self->{parameters} };
136  
137 say STDERR $self->_coloured( '┄' x $self->_width, 'faint' )
138 if @{ $self->{parameters} };
139  
140 next;
141 }
142  
143 my $parameter = shift @{$queue};
144  
145 my $colour = defined $parameter ? _string_colour( $parameter ) : undef;
146  
147 my $prefix
148 = defined $parameter
149 ? $self->_coloured( sprintf( "% -${prefix_width}s", $parameter ),
150 $colour )
151 : undef;
152  
153 my @argv = $self->_make_argv( $parameter );
154 my $worker = App::ppll::Worker->new(
155 argv => \@argv,
156 colour => $colour,
157 err => $self->_printer( $prefix, '≫', *STDERR ),
158 out => $self->_printer( $prefix, '>', *STDOUT ),
159 parameter => $parameter,
160 prefix => $prefix,
161 );
162  
163 $worker->{err}->( _ps_four() . shell_quote( @argv ) );
164  
165 $worker->start;
166  
167 push @{ $self->{pool} }, $worker;
168 } ## end while ( @{ $self->{parameters...}})
169  
170 for my $worker ( @{ $self->{pool} } ) {
171 my $result = $worker->result;
172  
173 next
174 unless defined $result;
175  
176 if ( $result == 0 ) {
177 push @succeeded, $worker;
178  
179 $worker->{err}->( $self->_coloured( '✓', 'green' ) );
180 } else {
181 push @failed, $worker;
182  
183 $worker->{err}
184 ->( $self->_coloured( sprintf( '❌ %d', $result ), 'red' ) );
185  
186 $self->stop
187 unless ( $self->{force} );
188 }
189 }
190  
191 @{ $self->{pool} } = grep { not $_->{finished} } @{ $self->{pool} };
192 } ## end while ( @{ $self->{pool} ...})
193  
194 my $ok
195 = !@failed && ( ( @succeeded + @failed ) == $self->{total_parameters} );
196  
197 if ( $self->{summary} ) {
198 say STDERR $self->_coloured( '━' x $self->_width, 'faint' );
199  
200 say STDERR $self->_coloured(
201 sprintf(
202 '%d/%d in %s, %d failed',
203 ( @succeeded + @failed ),
204 $self->{total_parameters},
205 concise( duration( time - $BASETIME ) ),
206 scalar @failed,
207 ),
208 $ok ? 'green' : 'red',
209 );
210 say STDERR $self->_coloured( '❌ ' . $_, 'red' ) for @failed;
211 }
212  
213 return $ok ? 0 : 1;
214 } ## end sub call ( $self )
215  
216 =head2 C<new( %args )>
217  
218 Constucts a new C<App::ppll> object that represents an invocation of C<ppll>.
219 Does I<not> run anything, to do that use L<call|call>.
220  
221 =head3 NAMED ARGUMENTS
222  
223 =over
224  
225 =item C<argv>
226  
227 An array ref to use instead of C<@ARGV>.
228  
229 =back
230  
231 =cut
232  
233 sub new ( $class, %args ) { ## no critic [Subroutines::ProhibitExcessComplexity]
234 my $self = bless {
235 argv => $args{argv} // [@ARGV],
236 mode => undef,
237 }, $class;
238  
239 my $p = Getopt::Long::Parser->new(
240 config => [ qw(
241 bundling
242 no_auto_abbrev
243 no_getopt_compat
244 no_ignore_case
245 no_permute
246 ) ] );
247  
248 $p->getoptionsfromarray(
249 $self->{argv},
250  
251 'colour|color!' => \$self->{colour},
252  
253 'command|c=s' => \$self->{command},
254  
255 'delimiter|d=s' => \$self->{delimiter},
256  
257 'empty!' => \$self->{empty},
258  
259 'fields|f' => sub { $self->{mode} = 'fields' },
260  
261 'force|k!' => \$self->{force},
262  
263 'help|h|?' => \$self->{help},
264  
265 'jobs|j=o' => \$self->{jobs},
266  
267 'lines|l' => sub { $self->{mode} = 'lines' },
268  
269 'markers!' => \$self->{markers},
270  
271 'mode=s' => sub ( $opt, $val ) {
272 croak sprintf( 'Unknown mode ‘%s’' )
273 unless scalar grep { $val eq $_ } @MODES;
274  
275 $self->{mode} = $val;
276 },
277  
278 'quiet|q' => sub { $self->{verbosity}-- },
279  
280 'random|R!' => \$self->{random},
281  
282 'replace-string|replstr|I=s' => \$self->{replstr},
283  
284 'reverse|r!' => \$self->{reverse},
285  
286 'sequence|s=s' => sub ( $opt, $val, @ ) {
287 $self->_push_parameters( $self->_parse_sequence( $val ) );
288 },
289  
290 'serial-lines-parallel-fields|slpf' => sub { $self->{mode} = 'slpf' },
291  
292 'summary|S!' => \$self->{summary},
293  
294 'timestamp-format=s' => \$self->{timestamp_format},
295  
296 'timestamps|t!' => sub ( $opt, $val, @ ) {
297 $self->{timestamp_format} //= '%T.%3N'
298 if $val;
299 },
300  
301 'verbose|v' => sub { $self->{verbosity}++ },
302  
303 'version|V' => \$self->{version},
304  
305 )
306 or pod2usage(
307 -exitval => 2,
308 -output => \*STDERR,
309 -verbose => 0,
310 );
311  
312 return $self
313 if $self->{help}
314 or $self->{version};
315  
316 $self->{colour} //= isatty( *STDOUT );
317  
318 $self->{mode} //= 'fields'
319 if $self->{delimiter};
320  
321 $self->{delimiter}
322 //= '(?:'
323 . join( '|', map {quotemeta} split( //, ( $ENV{IFS} // " \t\n" ) ) ) . ')+';
324  
325 $self->{jobs} //= cpu_count() // 1;
326  
327 if ( defined $self->{command} ) {
328 $self->{cmd}
329 = [ ( $ENV{SHELL} // '/bin/sh' ), '-c', $self->{command}, '-' ];
330 $self->{parameters} = [ $self->{argv} ];
331 } else {
332 $self->{cmd} = $self->{argv};
333 }
334  
335 unless ( $self->{parameters} or isatty( *STDIN ) ) {
336 my @lines;
337  
338 ## no critic [InputOutput::ProhibitExplicitStdin]
339 while ( my $line = <STDIN> ) {
340 chomp $line;
341 push @lines, $line;
342 }
343  
344 $self->{mode} = scalar @lines == 1 ? 'fields' : 'lines'
345 if ( $self->{mode} // 'auto' ) eq 'auto';
346  
347 if ( $self->{mode} eq 'fields' ) {
348 $self->_push_parameters( map { $self->_split_fields( $_ ) } @lines );
349 } elsif ( $self->{mode} eq 'lines' ) {
350 $self->_push_parameters( @lines );
351 } elsif ( $self->{mode} eq 'slpf' ) {
352 for my $line ( @lines ) {
353 $self->_push_parameters;
354 push @{ $self->{parameters} }, []
355 if @{ $self->{parameters}->[-1] } > 0;
356 $self->_push_parameters( $self->_split_fields( $line ) );
357 }
358 } else {
359 croak sprintf( 'Bad mode ‘%s’', $self->{mode} );
360 }
361 } ## end unless ( $self->{parameters...})
362  
363 $self->{parameters} //= [ [undef] ];
364  
365 $self->{parameters} = map { reverse @$_ } reverse @{ $self->parameters }
366 if $self->{reverse};
367  
368 $self->{parameters} = map { shuffle @$_ } shuffle @{ $self->parameters }
369 if $self->{random};
370  
371 $self->{total_parameters} = scalar flat( @{ $self->{parameters} } );
372 $self->{prefix} //= $self->{total_parameters} > 1;
373 $self->{summary} //= $self->{total_parameters} > 1;
374  
375 return $self;
376 } ## end sub new
377  
378 =head2 stop
379  
380 =cut
381  
382 sub stop( $self ) {
383 $self->{parameters} = [];
384  
385 for my $worker ( @{ $self->{pool} } ) {
386 $worker->stop;
387 }
388  
389 return;
390 }
391  
392 sub _coloured ( $self, $str, @args ) {
393 return $str
394 unless $self->{colour};
395  
396 return colored( $str, @args );
397 }
398  
399 sub _make_argv ( $self, $parameter ) {
400 my @argv = @{ $self->{cmd} };
401  
402 return @argv
403 unless defined $parameter;
404  
405 my $replstr = $self->{replstr} // '{}';
406  
407 return ( @argv, $parameter )
408 unless scalar grep {m/\Q$replstr\E/} @argv;
409  
410 return map {s/\Q$replstr\E/$parameter/gr} @argv;
411 }
412  
413 sub _parse_sequence ( $self, $str ) {
414 my @seq;
415  
416 for my $part ( split( /,/, $str ) ) {
417 $part =~ m/^(?:(.*)\.\.)?(.*)$/
418 or croak sprintf( 'Bad sequence specifier ‘%s’', $part );
419  
420 my $beg = $1 // '1';
421 my $end = $2;
422  
423 my $w = max( map {length} ( $beg, $end ) );
424 push @seq,
425 sprintf( '%0*s', $w, $beg ) lt sprintf( '%0*s', $w, $end )
426 ? $beg .. $end
427 : reverse $end .. $beg;
428 }
429  
430 return wantarray ? @seq : \@seq;
431 }
432  
433 sub _printer ( $self, $prefix, $marker, $dest ) {
434 binmode $dest, ':encoding(UTF-8)';
435 $dest->autoflush( 1 );
436  
437 my @subs;
438  
439 push @subs, sub {$prefix}
440 if defined $prefix
441 and $self->{prefix};
442  
443 push @subs, sub {
444 $self->_coloured(
445 DateTime->from_epoch(
446 epoch => time,
447 time_zone => 'local',
448 )->strftime( $self->{timestamp_format} ),
449 'reverse faint',
450 );
451 }
452 if $self->{timestamp_format};
453  
454 push @subs, sub { $self->_coloured( $marker, 'faint' ) }
455 if $self->{markers};
456  
457 push @subs, sub {' '}
458 if @subs;
459  
460 return sub {
461 for ( @_ ) {
462 chomp;
463 say {$dest} join( '', map { $_->() } @subs ) . $_;
464 }
465 return;
466 };
467 } ## end sub _printer
468  
469 sub _ps_four() {
470 state $ps_four;
471  
472 unless ( defined $ps_four ) {
473 $ps_four = $ENV{PS4};
474 utf8::decode( ## no critic [Subroutines::ProhibitCallsToUnexportedSubs]
475 $ps_four,
476 ) if $ps_four;
477 $ps_four //= '+ ';
478 }
479  
480 return $ps_four;
481 }
482  
483 sub _push_parameters ( $self, @parameters ) {
484 $self->{parameters} //= [ [] ];
485  
486 @parameters = grep {m/./} @parameters
487 unless $self->{empty};
488  
489 confess unless $self->{parameters};
490  
491 push @{ $self->{parameters}->[-1] }, @parameters;
492  
493 return;
494 }
495  
496 sub _split_fields ( $self, $str ) {
497 return split( $self->{delimiter}, $str );
498 }
499  
500 sub _string_colour( $str ) {
501 return $COLOURS[ unpack( 'L', substr( md5( $str ), 0, 2 * 2 ) ) %
502 scalar @COLOURS ];
503 }
504  
505 sub _width( $self ) {
506 return $ENV{COLUMNS}
507 if $ENV{COLUMNS};
508  
509 return ( GetTerminalSize() )[0] // $WIDTH;
510 }
511  
512 =head1 AUTHOR
513  
514 Theo -q Willows, C<< <theo@willows.se> >>
515  
516 =head1 BUGS AND LIMITATIONS
517  
518 Please report any bugs or feature requests through the web interface at
519 L<https://gitlab.com/munkei-software/ppll/issues>.
520  
521 =head1 SUPPORT
522  
523 You can find documentation for this module with the perldoc command.
524  
525 perldoc ppll
526  
527 Or:
528  
529 ppll --help
530  
531 You can also look for information at:
532  
533 =over
534  
535 =item * MetaCPAN
536  
537 L<https://metacpan.org/pod/App::ppll>
538  
539 =item * GitLab
540  
541 L<https://gitlab.com/munkei-software/ppll>
542  
543 =back
544  
545 =head1 LICENSE AND COPYRIGHT
546  
547 Copyright 2019 Theo Willows.
548  
549 This program is free software; you can redistribute it and/or modify it under
550 the terms of either: the GNU General Public License as published by the Free
551 Software Foundation; or the Artistic License.
552  
553 See L<http://dev.perl.org/licenses/> for more information.
554  
555 =cut
556  
557 1;
« blib/lib/App/ppll/Worker.pm ↑ Summary bin/ppll »