| 1 | package Zapp::Task::Script; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Run a Script | ||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||
| 4 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 5 | #pod | ||||||||||||||||||||||||||||||||||||||
| 6 | #pod This task executes arbitrary scripts. By default, they are run as | ||||||||||||||||||||||||||||||||||||||
| 7 | #pod scripts for the current shell. With a shebang line (e.g. | ||||||||||||||||||||||||||||||||||||||
| 8 | #pod C<#!/usr/bin/perl>), any arbitrary interpreter can be invoked. | ||||||||||||||||||||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||||||||||||||||||||
| 10 | #pod =head2 Output | ||||||||||||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||||||||||||
| 12 | #pod output - The output of the script | ||||||||||||||||||||||||||||||||||||||
| 13 | #pod pid - The process ID of the script | ||||||||||||||||||||||||||||||||||||||
| 14 | #pod exit - The exit code | ||||||||||||||||||||||||||||||||||||||
| 15 | #pod info - A text description of the exit code | ||||||||||||||||||||||||||||||||||||||
| 16 | #pod | ||||||||||||||||||||||||||||||||||||||
| 17 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||||||||||||
| 19 | #pod =cut | ||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Mojo::Base 'Zapp::Task', -signatures; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Mojo::File qw( tempdir tempfile ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use IPC::Open3 qw( open3 ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Cwd qw( cwd ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Symbol qw( gensym ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use Mojo::Util qw( decode ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | ▶ | sub schema( $class ) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
schema: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 29 | return { | ||||||||||||||||||||||||||||||||||||||
| 30 | 0 | input => { | |||||||||||||||||||||||||||||||||||||
| 31 | type => 'object', | ||||||||||||||||||||||||||||||||||||||
| 32 | required => [qw( script )], | ||||||||||||||||||||||||||||||||||||||
| 33 | properties => { | ||||||||||||||||||||||||||||||||||||||
| 34 | env => { | ||||||||||||||||||||||||||||||||||||||
| 35 | type => 'array', | ||||||||||||||||||||||||||||||||||||||
| 36 | items => { | ||||||||||||||||||||||||||||||||||||||
| 37 | type => 'object', | ||||||||||||||||||||||||||||||||||||||
| 38 | properties => { | ||||||||||||||||||||||||||||||||||||||
| 39 | name => { | ||||||||||||||||||||||||||||||||||||||
| 40 | type => 'string', | ||||||||||||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||||||||||||
| 42 | value => { | ||||||||||||||||||||||||||||||||||||||
| 43 | type => 'string', | ||||||||||||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||||||||||||
| 45 | }, | ||||||||||||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||||||||||||
| 48 | script => { | ||||||||||||||||||||||||||||||||||||||
| 49 | type => 'string', | ||||||||||||||||||||||||||||||||||||||
| 50 | format => 'textarea', | ||||||||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||||||||||||
| 54 | output => { | ||||||||||||||||||||||||||||||||||||||
| 55 | type => 'object', | ||||||||||||||||||||||||||||||||||||||
| 56 | required => [qw( output )], | ||||||||||||||||||||||||||||||||||||||
| 57 | properties => { | ||||||||||||||||||||||||||||||||||||||
| 58 | output => { | ||||||||||||||||||||||||||||||||||||||
| 59 | type => 'string', | ||||||||||||||||||||||||||||||||||||||
| 60 | format => 'textarea', | ||||||||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||||||||
| 62 | pid => { | ||||||||||||||||||||||||||||||||||||||
| 63 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||||||||||||
| 65 | exit => { | ||||||||||||||||||||||||||||||||||||||
| 66 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||||||||||||
| 68 | info => { | ||||||||||||||||||||||||||||||||||||||
| 69 | type => 'string', | ||||||||||||||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | # Match ANSI escape sequences that are not formatting/color (m) | ||||||||||||||||||||||||||||||||||||||
| 77 | 2 | my $ANSI = qr{ \e \[ [HJ] }x; | |||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | ▶ | sub run( $self, $input ) { | |||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 80 | # The script came from the browser's form with \r\n as line ending | ||||||||||||||||||||||||||||||||||||||
| 81 | # character, but we need to use the native OS's line ending | ||||||||||||||||||||||||||||||||||||||
| 82 | # character... | ||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $script = $input->{script} =~ s/\r\n/\n/gr; | |||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $exe = $ENV{SHELL} // "/bin/sh"; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{"SHELL"} // "/bin/sh"
MC/DC0%0/1
$ENV{"SHELL"} // "/bin/sh"
$ENV{"SHELL"}
Truth table0%0/2
A
A= $ENV{"SHELL"}
| |||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ( $script =~ /^\#!([^\n]+)/s ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $exe = $1; | |||||||||||||||||||||||||||||||||||||
| 88 | 0 | $script =~ s/^\#!\n+//; | |||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $cwd = cwd; | |||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $dir = tempdir; | |||||||||||||||||||||||||||||||||||||
| 93 | 0 | chdir $dir; | |||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $file = tempfile( DIR => $dir, UNLINK => 1 ); | |||||||||||||||||||||||||||||||||||||
| 95 | 0 | $file->spurt( $script ); | |||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my ( $stdout, $stderr, $pid ); | |||||||||||||||||||||||||||||||||||||
| 98 | 0 | $stderr = gensym; | |||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||||||||||||
| 101 | 0 | local %ENV = %ENV; | |||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | for my $var ( @{ $input->{vars} // [] } ) { | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$input->{"vars"} // []
MC/DC0%0/1
$input->{"vars"} // []
$input->{"vars"}
Truth table0%0/2
A
A= $input->{"vars"}
| |||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $ENV{ $var->{name} } = $var->{value}; | |||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||
| 105 | #; $self->app->log->debug( "Environment: \n" . join "\n", map { "$_=$ENV{$_}" } keys %ENV ); | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $pid = open3( my $stdin, $stdout, $stderr, $exe, $file ); | |||||||||||||||||||||||||||||||||||||
| 108 | # XXX: Put PID somewhere we can use it while process is running | ||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( !$pid || $pid <= 0 ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $pid or $pid <= 0
MC/DC0%0/2
not $pid or $pid <= 0
not $pid
$pid <= 0
Truth table0%0/3
not(A) or B
A= not $pidB= $pid <= 0
| |||||||||||||||||||||||||||||||||||||||
| 112 | 0 | chdir $cwd; | |||||||||||||||||||||||||||||||||||||
| 113 | 0 | return $self->fail({ | |||||||||||||||||||||||||||||||||||||
| 114 | info => 'Could not execute script: ' . $!, | ||||||||||||||||||||||||||||||||||||||
| 115 | output => '', | ||||||||||||||||||||||||||||||||||||||
| 116 | exit => -1, | ||||||||||||||||||||||||||||||||||||||
| 117 | pid => 0, | ||||||||||||||||||||||||||||||||||||||
| 118 | error_output => '', | ||||||||||||||||||||||||||||||||||||||
| 119 | }); | ||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | # XXX: Read from stdout/stderr at the same time so both can be seen | ||||||||||||||||||||||||||||||||||||||
| 123 | # while process runs | ||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $output = ''; | |||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | while ( my $line = <$stdout> ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $output .= $line =~ s/$ANSI//gr; | |||||||||||||||||||||||||||||||||||||
| 127 | # XXX: Put output somewhere it can be seen while process runs | ||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $error_output = ''; | |||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | while ( my $line = <$stderr> ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $error_output .= $line =~ s/$ANSI//gr; | |||||||||||||||||||||||||||||||||||||
| 132 | # XXX: Put output somewhere it can be seen while process runs | ||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | 0 | waitpid $pid, 0; | |||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $exit = $?; | |||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $info | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 138 | = $exit == -1 ? 'Could not execute script: ' . $! | ||||||||||||||||||||||||||||||||||||||
| 139 | : $exit & 127 ? 'Script ended with signal: ' . ( $? & 127 ) | ||||||||||||||||||||||||||||||||||||||
| 140 | : 'Script exited with value: ' . ( $? >> 8 ) | ||||||||||||||||||||||||||||||||||||||
| 141 | ; | ||||||||||||||||||||||||||||||||||||||
| 142 | 0 | $exit >>= 8; | |||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my $method = $exit == 0 ? 'finish' : 'fail'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 145 | 0 | chdir $cwd; | |||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | return $self->$method({ | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
decode("UTF-8", $output) // $output
decode("UTF-8", $error_output) // $error_output
MC/DC0%0/2
decode("UTF-8", $output) // $output
decode("UTF-8", $output)
$output
MC/DC0%0/2
decode("UTF-8", $error_output) // $error_output
decode("UTF-8", $error_output)
$error_output
Truth table0%0/3
A // B
A= decode("UTF-8", $output)B= $output
Truth table0%0/3
A // B
A= decode("UTF-8", $error_output)B= $error_output
| |||||||||||||||||||||||||||||||||||||||
| 147 | pid => $pid, | ||||||||||||||||||||||||||||||||||||||
| 148 | output => decode( 'UTF-8', $output ) // $output, | ||||||||||||||||||||||||||||||||||||||
| 149 | exit => $exit, | ||||||||||||||||||||||||||||||||||||||
| 150 | info => $info, | ||||||||||||||||||||||||||||||||||||||
| 151 | error_output => decode( 'UTF-8', $error_output ) // $error_output, | ||||||||||||||||||||||||||||||||||||||
| 152 | }); | ||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||
| 155 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||
| 157 | =pod | ||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||
| 159 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||
| 161 | Zapp::Task::Script - Run a Script | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | version 0.001 | ||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||
| 167 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||
| 169 | This task executes arbitrary scripts. By default, they are run as | ||||||||||||||||||||||||||||||||||||||
| 170 | scripts for the current shell. With a shebang line (e.g. | ||||||||||||||||||||||||||||||||||||||
| 171 | C<#!/usr/bin/perl>), any arbitrary interpreter can be invoked. | ||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | =head2 Output | ||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||
| 175 | output - The output of the script | ||||||||||||||||||||||||||||||||||||||
| 176 | pid - The process ID of the script | ||||||||||||||||||||||||||||||||||||||
| 177 | exit - The exit code | ||||||||||||||||||||||||||||||||||||||
| 178 | info - A text description of the exit code | ||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||
| 180 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||
| 182 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||
| 184 | Doug Bell <preaction@cpan.org> | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||
| 188 | This software is copyright (c) 2020 by Doug Bell. | ||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||
| 190 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 191 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||
| 195 | __DATA__ | ||||||||||||||||||||||||||||||||||||||