| 1 | package IPC::PrettyPipe::Execute::IPC::Run; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # ABSTRACT: execution backend using IPC::Run | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 4 | ▶ | use 5.10.0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 4 | ▶ | use Types::Standard qw[ InstanceOf ]; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | |||||||||||||||
| 10 | 4 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use IPC::Run (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 4 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 4 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 4 | our $VERSION = '0.13'; | |||||||||||||
| 16 | |||||||||||||||
| 17 | 4 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | # This attribute encapsulates an IPC::Run harness, tieing its creation | ||||||||||||||
| 20 | # to an IO::ReStoreFH object to ensure that filehandles are stored & | ||||||||||||||
| 21 | # restored properly. The IPC::Run harness is created on-demand just | ||||||||||||||
| 22 | # before it is used. | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 4 | has _harness => ( | |||||||||||||
| 25 | is => 'rwp', | ||||||||||||||
| 26 | clearer => 1, | ||||||||||||||
| 27 | predicate => 1, | ||||||||||||||
| 28 | init_arg => undef, | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | # store the IO::Restore object; created on demand by _harness.default | ||||||||||||||
| 32 | # don't create it otherwise! | ||||||||||||||
| 33 | 4 | has _storefh => ( | |||||||||||||
| 34 | is => 'rwp', | ||||||||||||||
| 35 | predicate => 1, | ||||||||||||||
| 36 | init_arg => undef, | ||||||||||||||
| 37 | clearer => 1 | ||||||||||||||
| 38 | ); | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 6 | ▶ | sub _create_harness { | ||||||||||||
|
Subroutine
_create_harness: called CC=7 SCAR=20.6
| |||||||||||||||
| 41 | 6 | my ( $self, $pipe ) = @_; | |||||||||||||
| 42 | |||||||||||||||
| 43 | # While the harness is instantiated, we store the current fh's | ||||||||||||||
| 44 | 6 | $self->_set__storefh( $pipe->_storefh); | |||||||||||||
| 45 | |||||||||||||||
| 46 | 6 | my @harness; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 6 | my @cmds = @{ $pipe->cmds->elements }; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 6 | ▶ | while ( @cmds ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | 8 | my $cmd = shift @cmds; | |||||||||||||
| 53 | |||||||||||||||
| 54 | 8 | ▶ | if ( $cmd->isa( 'IPC::PrettyPipe::Cmd' ) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 8 | ▶ | push @harness, '|' if @harness; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | |||||||||||||||
| 58 | push @harness, | ||||||||||||||
| 59 | [ | ||||||||||||||
| 60 | $cmd->cmd, | ||||||||||||||
| 61 | 8 | map { $_->render( flatten => 1 ) } @{ $cmd->args->elements }, | |||||||||||||
| 62 | ]; | ||||||||||||||
| 63 | |||||||||||||||
| 64 | push @harness, | ||||||||||||||
| 65 | 13 | ▶ | map { $_->spec, $_->has_file ? $_->file : () } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 8 | @{ $cmd->streams->elements }; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | elsif ( $cmd->isa( 'IPC::PrettyPipe' ) ) { | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | ▶ | croak( "cannot chain sub-pipes which have streams" ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | unless $cmd->streams->empty; | ||||||||||||||
| 72 | 0 | unshift @cmds, @{ $cmd->cmds->elements }; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 6 | $self->_set__harness( IPC::Run::harness(@harness) ); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | |||||||||||||||
| 80 | |||||||||||||||
| 81 | |||||||||||||||
| 82 | |||||||||||||||
| 83 | |||||||||||||||
| 84 | |||||||||||||||
| 85 | |||||||||||||||
| 86 | |||||||||||||||
| 87 | |||||||||||||||
| 88 | |||||||||||||||
| 89 | 6 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 90 | 6 | my ( $self, $pipe ) = @_; | |||||||||||||
| 91 | 6 | $self->_create_harness( $pipe); | |||||||||||||
| 92 | 6 | $self->_harness->run; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | |||||||||||||||
| 96 | |||||||||||||||
| 97 | |||||||||||||||
| 98 | |||||||||||||||
| 99 | |||||||||||||||
| 100 | |||||||||||||||
| 101 | |||||||||||||||
| 102 | |||||||||||||||
| 103 | |||||||||||||||
| 104 | 0 | ▶ | sub start { | ||||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 105 | 0 | my ( $self, $pipe ) = @_; | |||||||||||||
| 106 | 0 | $self->_create_harness( $pipe ); | |||||||||||||
| 107 | 0 | $self->_harness->start; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | |||||||||||||||
| 111 | |||||||||||||||
| 112 | |||||||||||||||
| 113 | |||||||||||||||
| 114 | |||||||||||||||
| 115 | |||||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | ▶ | sub pump { | ||||||||||||
|
Subroutine
pump: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||
| 119 | 0 | ▶ | Carp::croak( "must call run method to create harness\n" ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | unless $self->_has_harness; | ||||||||||||||
| 121 | 0 | $self->_harness->pump; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | |||||||||||||||
| 125 | |||||||||||||||
| 126 | |||||||||||||||
| 127 | |||||||||||||||
| 128 | |||||||||||||||
| 129 | |||||||||||||||
| 130 | sub finish { | ||||||||||||||
| 131 | my $self = shift; | ||||||||||||||
| 132 | Carp::croak( "must call run method to create harness\n" ) | ||||||||||||||
| 133 | unless $self->_has_harness; | ||||||||||||||
| 134 | $self->_harness->finish; | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | # the IO::ReStoreFH object lives only as long as the | ||||||||||||||
| 138 | # IPC::Run harness object, and that lives only | ||||||||||||||
| 139 | # as long as necessary. | ||||||||||||||
| 140 | 6 | ▶ | after 'run', 'finish' => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||
| 141 | |||||||||||||||
| 142 | 6 | my $self = shift; | |||||||||||||
| 143 | |||||||||||||||
| 144 | 6 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 145 | # get rid of harness first to avoid possible closing of file | ||||||||||||||
| 146 | # handles while the child is running. of course the child | ||||||||||||||
| 147 | # shouldn't be running at this point, but what the heck | ||||||||||||||
| 148 | 6 | $self->_clear_harness; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 152 | 0 | Carp::croak $_; | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | 6 | ▶ | finally { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 156 | 6 | $self->_clear_storefh; | |||||||||||||
| 157 | 6 | }; | |||||||||||||
| 158 | |||||||||||||||
| 159 | 4 | }; | |||||||||||||
| 160 | |||||||||||||||
| 161 | # this needs to go here 'cause this just defines the interface | ||||||||||||||
| 162 | 4 | with 'IPC::PrettyPipe::Executor'; | |||||||||||||
| 163 | |||||||||||||||
| 164 | 4 | 1; | |||||||||||||
| 165 | |||||||||||||||
| 166 | # | ||||||||||||||
| 167 | # This file is part of IPC-PrettyPipe | ||||||||||||||
| 168 | # | ||||||||||||||
| 169 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||
| 170 | # | ||||||||||||||
| 171 | # This is free software, licensed under: | ||||||||||||||
| 172 | # | ||||||||||||||
| 173 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||
| 174 | # | ||||||||||||||
| 175 | |||||||||||||||
| 176 | __END__ | ||||||||||||||