bin/remperl

filter: Statementstmt 91.0% 132 / 145 Branchbran 80.6% 71 / 88 Conditioncond 59.4% 22 / 37 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 83.3% 10 / 12 Podpod n/a totaltotal 79.8% 241 / 302 CC 27 SCAR 26.7
CC
27
Cov
83%
parse_perl_opts
21.0
__ANON__
20.0
use_spec_to_line
4.0
CRAP 14.4
« blib/lib/Remote/Perl/Client.pm ↑ Summary lib/Remote/Perl.pm »
1 #!/usr/bin/env perl
2 49 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 49 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 49 ▶use lib "$Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 49 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 49 ▶use Remote::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Only load Pod::Usage if required because its dependencies load slowly.
10 3 ▶sub pod2usage { require Pod::Usage; Pod::Usage::pod2usage(@_) }
Subroutine pod2usage: called CC=1 SCAR=0.0
11  
12 # -- Option variables ----------------------------------------------------------
13  
14 49 my $rsh = 'ssh';
15 49 my $pipe_cmd = 0;
16 49 my $window = 65_536;
17 49 my $stdin_file = undef;
18 49 my $stdin_str = undef;
19 49 my $serve_modules = 0;
20 49 my @inc_local;
21 49 my $tmpfile = 0;
22 49 my $tmpfile_mode = undef;
23 49 my $no_data_warn = 0;
24 49 my $no_system_inc = 0;
25 49 my $serve_restrict = 0;
26 49 my @serve_allow;
27 49 my $help = 0;
28 49 my $version = 0;
29  
30 # -- GetOptions spec (shared between parse_perl_opts and GetOptions) -----------
31  
32 49 my @getopt_spec = (
33 'rsh=s' => \$rsh,
34 'pipe-cmd' => \$pipe_cmd,
35 'window-size=i' => \$window,
36 'stdin-file=s' => \$stdin_file,
37 'stdin-str=s' => \$stdin_str,
38 'serve-modules!' => \$serve_modules,
39 'inc-local=s@' => \@inc_local,
40 'no-system-inc' => \$no_system_inc,
41 'serve-restrict-paths' => \$serve_restrict,
42 'serve-allow=s@' => \@serve_allow,
43 'tmpfile!' => \$tmpfile,
44 'tmpfile-mode=s' => \$tmpfile_mode,
45 'no-data-warn' => \$no_data_warn,
46 'help' => \$help,
47 'version' => \$version,
48 );
49  
50 # -- Perl-compatible short option parsing --------------------------------------
51  
52 # Mirrors Perl's own option parsing rules for -e, -m, -M, -w, -h, -V.
53 # Processes @ARGV in-place before GetOptions runs.
54  
55 9 ▶sub use_spec_to_line($flag, $spec) {
Subroutine use_spec_to_line: called CC=4 SCAR=13.9
56 # '=' form: -m and -M are identical; use split like Perl does
57 9 ▶ if ($spec =~ /^([^=]+)=(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /^([^=]+)=(.+)$/u) 4 5 9
58 4 return "use $1 split(/,/,q{$2});\n";
59 }
60 5 ▶ if ($flag eq 'm') { return "use $spec ();\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "m") 1 4 5
61 4 ▶ if ($spec =~ /^-(.+)/) { return "no $1;\n" }
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /^-(.+)/u) 2 2 4
62 2 return "use $spec;\n";
63 }
64  
65 49 ▶sub parse_perl_opts() {
Subroutine parse_perl_opts: called CC=21 SCAR=30.4
66 49 my @eval_code;
67 my @use_specs;
68 49 my $warnings = 0;
69 49 my @new_argv;
70 my @trailing; # args after stop point (not seen by GetOptions)
71 49 my $positionals = 0;
72  
73 # Long options that consume the next argv element as a value.
74 # Derived from @getopt_spec: any key containing = or : takes an argument.
75 49 my %long_val;
76 49 ▶ for (my $i = 0; $i < @getopt_spec; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @getopt_spec) 735 49 784
77 735 ▶ if ($getopt_spec[$i] =~ /[=:]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt_spec[$i] =~ /[=:]/u) 343 392 735
78 343 (my $name = $getopt_spec[$i]) =~ s/[=:!+].*//;
79 343 $long_val{"--$name"} = 1;
80 }
81 }
82  
83 49 ▶ while (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 160 17 177
84 160 my $arg = shift @ARGV;
85  
86 # -- stops all processing
87 160 ▶ if ($arg eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq "--") 2 158 160
88 2 push @trailing, @ARGV;
89 2 last;
90 }
91  
92 # Long options: pass through to GetOptions (with value if applicable)
93 158 ▶ if ($arg =~ /^--/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^--/u) 50 108 158
94 50 push @new_argv, $arg;
95 # If --foo=bar, value is already embedded; otherwise consume next
96 50 ▶ if ($arg !~ /=/ && $long_val{$arg} && @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $arg =~ /=/u and $long_val{$arg} and @ARGV) 4 46 50
Condition66%4/6
not $arg =~ /=/u and $long_val{$arg} and @ARGV
!ll&&!rl&&r
4604
not $arg =~ /=/u and $long_val{$arg}
!ll&&!rl&&r
0464
MC/DC33%1/3
not $arg =~ /=/u and $long_val{$arg} and @ARGV
not $arg =~ /=/u $long_val{$arg} @ARGV
Truth table50%2/4
not(A) and B and C
A= not $arg =~ /=/uB= $long_val{$arg}C= @ARGV
ABCresult
0XX0
10X0
1100
1111
97 4 push @new_argv, shift @ARGV;
98 }
99 50 next;
100 }
101  
102 # Not an option: positional argument
103 108 ▶ if ($arg !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /^-/u) 71 37 108
104 71 push @new_argv, $arg;
105 71 $positionals++;
106 71 ▶ if ($positionals >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($positionals >= 2) 27 44 71
107 27 push @trailing, @ARGV;
108 27 last;
109 }
110 44 next;
111 }
112  
113 # Short option bundle: strip leading '-' and process char by char
114 37 my $bundle = substr($arg, 1);
115 37 ▶ while (length $bundle) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bundle) 39 34 73
116 39 my $c = substr($bundle, 0, 1, '');
117  
118 39 ▶ if ($c eq 'w') {
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq 'w') { } 6 33 39
elsif ($c eq 'h') { } 1 32 33
elsif ($c eq 'V') { } 1 31 32
elsif ($c eq 'e' or $c eq 'm' or $c eq 'M') { } 30 1 31
119 6 $warnings = 1;
120 }
121 elsif ($c eq 'h') {
122 1 pod2usage(-verbose => 1, -exitstatus => 0);
123 }
124 elsif ($c eq 'V') {
125 1 $version = 1;
126 }
127 ▶ elsif ($c eq 'e' || $c eq 'm' || $c eq 'M') {
Condition100%6/6
$c eq "e" or $c eq "m" or $c eq "M"
l!l&&r!l&&!r
2281
$c eq "e" or $c eq "m"
l!l&&r!l&&!r
2119
MC/DC100%3/3
$c eq "e" or $c eq "m" or $c eq "M"
$c eq "e" $c eq "m" $c eq "M"
Truth table100%4/4
A or B or C
A= $c eq "e"B= $c eq "m"C= $c eq "M"
ABCresult
1XX1
01X1
0011
0000
128 # Value-consuming: rest of bundle, or next argv element
129 30 my $val;
130 30 ▶ if (length $bundle) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bundle) { } 9 21 30
131 9 $val = $bundle;
132 9 $bundle = '';
133 }
134 else {
135 21 $val = shift @ARGV;
136 21 ▶ die "remperl: missing argument for -$c\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 20 21
137 unless defined $val;
138 }
139 29 ▶ if ($c eq 'e') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq 'e') { } 20 9 29
140 20 push @eval_code, $val;
141 }
142 else {
143 9 push @use_specs, [$c, $val];
144 }
145 }
146 else {
147 1 die "remperl: unrecognized switch: -$c\n";
148 }
149 }
150 }
151  
152 46 @ARGV = @new_argv;
153 46 return (\@eval_code, \@use_specs, $warnings, \@trailing);
154 }
155  
156 49 my ($eval_codes, $use_specs, $warnings, $trailing) = parse_perl_opts();
157  
158 # -- Long option parsing -------------------------------------------------------
159  
160 46 ▶GetOptions(@getopt_spec) or pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(@getopt_spec) or pod2usage(2) 45 1 46
161  
162 # Re-append args that appeared after the stop point (2nd positional or --).
163 # These are script arguments and must not be processed by GetOptions.
164 45 push @ARGV, @$trailing;
165  
166 45 ▶pod2usage(-verbose => 1, -exitstatus => 0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 44 45
167  
168 44 ▶if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 1 43 44
169 1 print "remperl $Remote::Perl::VERSION\n";
170 1 exit 0;
171 }
172  
173 # No arguments at all: show usage instead of a cryptic "missing host" error.
174 43 ▶pod2usage(-verbose => 99, -sections => 'SYNOPSIS', -exitstatus => 0) unless @ARGV || @$eval_codes;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV or @$eval_codes 0 43 43
Condition33%1/3
@ARGV or @$eval_codes
l!l&&r!l&&!r
4300
MC/DC0%0/2
@ARGV or @$eval_codes
@ARGV @$eval_codes
Truth table33%1/3
A or B
A= @ARGVB= @$eval_codes
ABresult
1X1
011
000
175  
176 # -- Build the remote command --------------------------------------------------
177  
178 43 my @cmd;
179 43 ▶if ($pipe_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pipe_cmd) { } 43 0 43
180 43 ▶ my $spec = shift @ARGV
Condition50%1/2
shift @ARGV // die("remperl: --pipe-cmd requires a command argument\n")
l!l
430
MC/DC0%0/1
shift @ARGV // die("remperl: --pipe-cmd requires a command argument\n")
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
Aresult
11
00
181 // die "remperl: --pipe-cmd requires a command argument\n";
182 43 @cmd = ('sh', '-c', $spec);
183 }
184 else {
185 0 ▶ my $host = shift @ARGV
Condition0%0/2
shift @ARGV // die("remperl: missing host argument\n")
l!l
00
MC/DC0%0/1
shift @ARGV // die("remperl: missing host argument\n")
shift @ARGV
Truth table0%0/2
A
A= shift @ARGV
Aresult
11
00
186 // die "remperl: missing host argument\n";
187 0 @cmd = ($rsh, $host, 'perl');
188 }
189  
190 # -- Source: file or -e --------------------------------------------------------
191  
192 43 ▶my $eval_code = @$eval_codes ? join("\n", @$eval_codes) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$eval_codes ? : 18 25 43
193  
194 # Build preamble from -m/-M specs
195 43 my $preamble = '';
196 43 for my $spec (@$use_specs) {
197 9 $preamble .= use_spec_to_line($spec->[0], $spec->[1]);
198 }
199  
200 43 my $script;
201 43 ▶if (!defined $eval_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $eval_code) { } 25 18 43
202 25 ▶ die "remperl: -m/-M requires -e\n" if length $preamble;
Branch: 1 branch
BranchTrueFalseTotal
if length $preamble 1 24 25
203 24 ▶ $script = shift @ARGV
Condition50%1/2
shift @ARGV // die("remperl: missing script argument (or use -e CODE)\n")
l!l
240
MC/DC0%0/1
shift @ARGV // die("remperl: missing script argument (or use -e CODE)\n")
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
Aresult
11
00
204 // die "remperl: missing script argument (or use -e CODE)\n";
205 }
206 else {
207 18 ▶ $eval_code = $preamble . $eval_code if length $preamble;
Branch: 1 branch
BranchTrueFalseTotal
if length $preamble 8 10 18
208 }
209  
210 # -- stdin ---------------------------------------------------------------------
211  
212 42 ▶die "remperl: --stdin-file and --stdin-str are mutually exclusive\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $stdin_file and defined $stdin_str 0 42 42
Condition66%2/3
defined $stdin_file and defined $stdin_str
!ll&&!rl&&r
4020
MC/DC0%0/2
defined $stdin_file and defined $stdin_str
defined $stdin_file defined $stdin_str
Truth table66%2/3
A and B
A= defined $stdin_fileB= defined $stdin_str
ABresult
0X0
100
111
213 if defined $stdin_file && defined $stdin_str;
214  
215 42 my $stdin;
216 42 ▶if (defined $stdin_str) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $stdin_str) { } 2 40 42
elsif (defined $stdin_file) { } 2 38 40
217 2 $stdin = $stdin_str;
218 }
219 elsif (defined $stdin_file) {
220 2 ▶ open(my $fh, '<', $stdin_file)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $stdin_file or die "remperl: cannot open '${stdin_file}': $!\n" 2 0 2
221 or die "remperl: cannot open '$stdin_file': $!\n";
222 2 binmode($fh);
223 2 $stdin = $fh;
224 }
225 else {
226 38 $stdin = \*STDIN;
227 }
228  
229 # -- Connect and run -----------------------------------------------------------
230  
231 42 STDOUT->autoflush(1);
232  
233 42 ▶my @inc = $no_system_inc ? @inc_local : (@inc_local, @INC);
Branch: 1 branch
BranchTrueFalseTotal
$no_system_inc ? : 0 42 42
234  
235 # -- Module serving path restriction -------------------------------------------
236  
237 # --serve-allow implies --serve-restrict-paths
238 42 ▶$serve_restrict = 1 if @serve_allow;
Branch: 1 branch
BranchTrueFalseTotal
if @serve_allow 0 42 42
239  
240 42 my $serve_filter;
241 42 ▶if ($serve_restrict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serve_restrict) 0 42 42
242 0 require Cwd;
243 # Allowed dirs: explicit --serve-allow + implicit --inc-local
244 0 my @allowed = grep { defined } map { Cwd::realpath($_) } (@serve_allow, @inc_local);
245 0 ▶ $serve_filter = sub($path) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
246 0 ▶ my $real = Cwd::realpath($path) // return 0;
Condition0%0/2
Cwd::realpath($path) // (return 0)
l!l
00
MC/DC0%0/1
Cwd::realpath($path) // (return 0)
Cwd::realpath($path)
Truth table0%0/2
A
A= Cwd::realpath($path)
Aresult
11
00
247 0 for my $dir (@allowed) {
248 0 ▶ return 1 if index($real, "$dir/") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($real, "$dir/") == 0 0 0 0
249 }
250 0 return 0;
251 0 };
252 }
253  
254 42 ▶die "remperl: unknown --tmpfile-mode value '$tmpfile_mode'\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmpfile_mode and not $tmpfile_mode =~ /^(auto|linux|perl|named|off)$/u 0 42 42
Condition33%1/3
defined $tmpfile_mode and not $tmpfile_mode =~ /^(auto|linux|perl|named|off)$/u
!ll&&!rl&&r
4200
MC/DC0%0/2
defined $tmpfile_mode and not $tmpfile_mode =~ /^(auto|linux|perl|named|off)$/u
defined $tmpfile_mode not $tmpfile_mode =~ /^(auto|linux|perl|named|off)$/u
Truth table33%1/3
A and not(B)
A= defined $tmpfile_modeB= not $tmpfile_mode =~ /^(auto|linux|perl|named|off)$/u
ABresult
0X0
100
111
255 if defined $tmpfile_mode && $tmpfile_mode !~ /^(auto|linux|perl|named|off)$/;
256  
257 # --tmpfile-mode implies --tmpfile; --tmpfile alone means auto.
258 42 ▶$tmpfile = 1 if defined $tmpfile_mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmpfile_mode 0 42 42
259 42 ▶my $tmpfile_val = !$tmpfile ? 0
Branch: 3 branches
BranchTrueFalseTotal
!$tmpfile ? : 42 0 42
!defined($tmpfile_mode) ? : 0 0 0
$tmpfile_mode eq 'off' ? : 0 0 0
260 : !defined($tmpfile_mode) ? 'auto'
261 : $tmpfile_mode eq 'off' ? 0
262 : $tmpfile_mode;
263  
264 42 ▶my $r = Remote::Perl->new(
Branch: 1 branch
BranchTrueFalseTotal
$no_data_warn ? : 0 42 42
265 cmd => \@cmd,
266 window => $window,
267 serve => $serve_modules,
268 inc => \@inc,
269 serve_filter => $serve_filter,
270 tmpfile => $tmpfile_val,
271 data_warn => ($no_data_warn ? 0 : 1),
272 );
273  
274 # Forward signals through the protocol so the remote executor receives them
275 # regardless of transport (SSH, docker, etc.).
276 21 for my $sig (qw(INT TERM QUIT HUP)) {
277 0 ▶ $SIG{$sig} = sub { eval { $r->send_signal($sig) } };
Subroutine __ANON__: not called CC=1 SCAR=2.6
278 }
279  
280 21 my @script_args = @ARGV;
281  
282 my %run_opts = (
283 64 ▶ on_stdout => sub { print STDOUT $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
284 3 ▶ on_stderr => sub { print STDERR $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
285 21 stdin => $stdin,
286 args => \@script_args,
287 warnings => $warnings,
288 );
289  
290 21 ▶my ($rc, $msg) = defined($eval_code)
Branch: 1 branch
BranchTrueFalseTotal
defined $eval_code ? : 9 12 21
291 ? $r->run_code($eval_code, %run_opts)
292 : $r->run_file($script, %run_opts);
293  
294 21 ▶print STDERR "remperl: $msg\n" if $rc && defined $msg && length $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $rc and defined $msg and length $msg 2 19 21
Condition83%5/6
$rc and defined $msg and length $msg
!ll&&!rl&&r
1812
$rc and defined $msg
!ll&&!rl&&r
1803
MC/DC66%2/3
$rc and defined $msg and length $msg
$rc defined $msg length $msg
Truth table75%3/4
A and B and C
A= $rcB= defined $msgC= length $msg
ABCresult
0XX0
10X0
1100
1111
295  
296 21 $r->disconnect;
297 21 ▶exit($rc // 1);
Condition50%1/2
$rc // 1
l!l
210
MC/DC0%0/1
$rc // 1
$rc
Truth table50%1/2
A
A= $rc
Aresult
11
00
298  
299 __END__
« blib/lib/Remote/Perl/Client.pm ↑ Summary lib/Remote/Perl.pm »