blib/lib/Gnuplot/Builder/Process.pm

filter: Statementstmt 38.9% 53 / 136 Branchbran 10.4% 5 / 48 Conditioncond 22.2% 2 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 46.4% 13 / 28 Podpod 33.3% 1 / 3 totaltotal 32.6% 75 / 230 SCAR 58.4
CC
31
Cov
31%
_new
30.0
with_new_process
20.0
_waitpid
20.0
CRAP 345.5
↑ Summary blib/lib/Gnuplot/Builder/Script.pm »
1 package Gnuplot::Builder::Process;
2 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 38 ▶use Gnuplot::Builder::PartiallyKeyedList;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 38 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub _get_env {
13 228 ▶ my ($basename, @default) = @_;
Subroutine _get_env: called CC=4 SCAR=13.9
14 228 my $name = "PERL_GNUPLOT_BUILDER_PROCESS_$basename";
15 228 ▶ if(defined($ENV{$name}) && $ENV{$name} ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{$name} and $ENV{$name} ne '') { } 5 223 228
Condition66%2/3
defined $ENV{$name} and $ENV{$name} ne ""
!ll&&!rl&&r
22305
MC/DC50%1/2
defined $ENV{$name} and $ENV{$name} ne ""
defined $ENV{$name} $ENV{$name} ne ""
Truth table66%2/3
A and B
A= defined $ENV{$name}B= $ENV{$name} ne ""
ABresult
0X0
100
111
16 5 return $ENV{$name};
17 }else {
18 223 ▶ return wantarray ? @default : $default[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 37 186 223
19 }
20 }
21  
22 38 our $ASYNC = _get_env("ASYNC", 0);
23 38 our $NO_STDERR = _get_env("NO_STDERR", 0);
24 38 our @COMMAND = _get_env("COMMAND", qw(gnuplot --persist));
25 38 our $MAX_PROCESSES = _get_env("MAX_PROCESSES", 2);
26 38 our $PAUSE_FINISH = _get_env("PAUSE_FINISH", 0);
27 38 our $TAP = undef;
28 38 our $ENCODING = _get_env("ENCODING", undef);
29  
30 38 my $END_SCRIPT_MARK = '@@@@@@_END_OF_GNUPLOT_BUILDER_@@@@@@';
31 38 my $processes = Gnuplot::Builder::PartiallyKeyedList->new;
32  
33 sub _clear_zombies {
34 0 ▶ $_->_waitpid(0) foreach $processes->get_all_values(); ## cannot use each() method because _waitpid() manipulates $processes...
Subroutine _clear_zombies: not called CC=2 SCAR=17.9
35 }
36  
37 {
38 38 my $null_handle;
39 sub _null_handle {
40 0 ▶ return $null_handle if defined $null_handle;
Subroutine _null_handle: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if defined $null_handle 0 0 0
41 0 my $devnull = File::Spec->devnull();
42 0 ▶ open $null_handle, ">", $devnull or confess("Cannot open $devnull: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $null_handle, ">", $devnull or confess("Cannot open ${devnull}: $!") 0 0 0
43 0 return $null_handle;
44 }
45 }
46  
47 ## PUBLIC ONLY IN TESTS: number of processes it keeps now
48 0 ▶sub FOR_TEST_process_num { $processes->size }
Subroutine FOR_TEST_process_num: not called CC=1 SCAR=1.2
Pod undocumented
49  
50 ## PUBLIC ONLY IN TESTS
51 38 *FOR_TEST_clear_zombies = *_clear_zombies;
52  
53 ## Documented public method.
54 sub wait_all {
55 1 ▶ while($processes->size > 0) {
Subroutine wait_all: called CC=2 SCAR=10.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($processes->size > 0) 0 1 1
56 0 my $proc = $processes->get_at(0);
57 0 $proc->_waitpid(1);
58 }
59 }
60  
61 ## create a new gnuplot process, create a writer to it and run the
62 ## given code. If the code throws an exception, the process is
63 ## terminated. It returns the output of the gnuplot process.
64 ##
65 ## Fields in %args are:
66 ##
67 ## do (CODE-REF mandatory): the code to execute. $do->($writer).
68 ##
69 ## async (BOOL optional, default = false): If set to true, it won't
70 ## wait for the gnuplot process to finish. In this case, the return
71 ## value is an empty string.
72 ##
73 ## no_stderr (BOOL optional, default = $NO_STDERR): If set to true,
74 ## the return value won't include gnuplot's STDERR. It just includes
75 ## STDOUT.
76 sub with_new_process {
77 0 ▶ my ($class, %args) = @_;
Subroutine with_new_process: not called CC=4 SCAR=30.0
Pod undocumented
78 0 my $code = $args{do};
79 0 ▶ croak "do parameter is mandatory" if !defined($code);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
80 0 ▶ my $async = defined($args{async}) ? $args{async} : $ASYNC;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'async'} ? : 0 0 0
81 0 ▶ my $no_stderr = defined($args{no_stderr}) ? $args{no_stderr} : $NO_STDERR;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'no_stderr'} ? : 0 0 0
82 0 my $process = $class->_new(capture => !$async, no_stderr => $no_stderr);
83 0 my $result = "";
84 try {
85 0 ▶ $code->($process->_writer);
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 $process->_close_input();
87 0 ▶ if(!$async) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $async) 0 0 0
88 0 $result = $process->_wait_to_finish();
89 }
90 }catch {
91 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 0 $process->_terminate();
93 0 die $e;
94 0 };
95 0 return $result;
96 }
97  
98 ## create a new gnuplot process. it blocks if the number of processes
99 ## has reached $MAX_PROCESSES.
100 ##
101 ## Fields in %args are:
102 ##
103 ## capture (BOOL optional, default: false): If true, it keeps the
104 ## STDOUT and STDERR of the process so that it can read them
105 ## afterward. Otherwise, it just discards the output.
106 ##
107 ## no_stderr (BOOL optional, default: false): If true, STDERR is
108 ## discarded instead of being redirected to STDOUT.
109 sub _new {
110 0 ▶ my ($class, %args) = @_;
Subroutine _new: not called CC=5 SCAR=34.0
111 0 _clear_zombies();
112 0 ▶ while($MAX_PROCESSES > 0 && $processes->size() >= $MAX_PROCESSES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Gnuplot::Builder::Process::MAX_PROCESSES > 0 and $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES) 0 0 0
Condition0%0/3
$Gnuplot::Builder::Process::MAX_PROCESSES > 0 and $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
!ll&&!rl&&r
000
MC/DC0%0/2
$Gnuplot::Builder::Process::MAX_PROCESSES > 0 and $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
$Gnuplot::Builder::Process::MAX_PROCESSES > 0 $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
Truth table0%0/3
A and B
A= $Gnuplot::Builder::Process::MAX_PROCESSES > 0B= $processes->size >= $Gnuplot::Builder::Process::MAX_PROCESSES
ABresult
0X0
100
111
113 ## wait for the first process to finish. it's not the smartest
114 ## way, but is it possible to wait for specific set of
115 ## processes?
116 0 my $proc = $processes->get_at(0);
117 0 $proc->_waitpid(1);
118 }
119 0 my $capture = $args{capture};
120 0 my $no_stderr = $args{no_stderr};
121 0 my ($write_handle, $read_handle, $pid);
122  
123 ## open3() does not seem to work well with lexical filehandles, so we use fileno()
124 0 ▶ $pid = open3($write_handle,
Branch: 2 branches
BranchTrueFalseTotal
$capture ? : 0 0 0
$no_stderr ? : 0 0 0
125 $capture ? $read_handle : '>&'.fileno(_null_handle()),
126 $no_stderr ? '>&'.fileno(_null_handle()) : undef,
127 @COMMAND);
128 0 my $self = bless {
129 pid => $pid,
130 write_handle => $write_handle,
131 read_handle => $read_handle,
132 }, $class;
133 0 $processes->set($pid, $self);
134 0 return $self;
135 }
136  
137  
138 ########
139 ######## OBJECT METHODS
140 ########
141  
142 ## Return the writer code-ref for this process.
143 sub _writer {
144 0 ▶ my ($self) = @_;
Subroutine _writer: not called CC=2 SCAR=17.9
145 0 ▶ croak "Input end is already closed" if not defined $self->{write_handle};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"write_handle"} 0 0 0
146 0 my $write_handle = $self->{write_handle};
147 0 my $pid = $self->{pid};
148 return sub {
149 0 ▶ my $msg = join "", @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
150 0 ▶ $msg = Encode::encode($ENCODING, $msg) if defined $ENCODING;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Gnuplot::Builder::Process::ENCODING 0 0 0
151 0 ▶ $TAP->($pid, "write", $msg) if defined $TAP;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Gnuplot::Builder::Process::TAP 0 0 0
152 0 print $write_handle ($msg);
153 0 };
154 ## If we are serious about avoiding dead-lock, we must use
155 ## select() to check writability first and to read from the
156 ## read_handle. But I guess the dead-lock happens only if the
157 ## user inputs too much data and the gnuplot outputs too much
158 ## data to STDOUT/STDERR. That's rare.
159 }
160  
161 ## lexical sub because MockTool uses it, too.
162 my $_finishing_commands = sub {
163 0 ▶ if($PAUSE_FINISH) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($Gnuplot::Builder::Process::PAUSE_FINISH) { } 0 0 0
164 0 return ('pause mouse close', 'exit');
165 }else {
166 0 return ('exit');
167 }
168 38 };
169  
170 ## Close the input channel. You can call this method multiple times.
171 sub _close_input {
172 0 ▶ my ($self) = @_;
Subroutine _close_input: not called CC=3 SCAR=24.8
173 0 ▶ return if not defined $self->{write_handle};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"write_handle"} 0 0 0
174 0 my $writer = $self->_writer;
175 0 $writer->("\n");
176 0 foreach my $statement (qq{set print "-"}, qq{print '$END_SCRIPT_MARK'}, $_finishing_commands->()) {
177 0 $writer->($statement . "\n");
178 }
179 0 undef $writer;
180 0 close $self->{write_handle};
181 0 $self->{write_handle} = undef;
182 }
183  
184 sub _waitpid {
185 0 ▶ my ($self, $blocking) = @_;
Subroutine _waitpid: not called CC=4 SCAR=30.0
186 0 ▶ my $result = waitpid($self->{pid}, $blocking ? 0 : WNOHANG);
Branch: 1 branch
BranchTrueFalseTotal
$blocking ? : 0 0 0
187 0 ▶ if($result == $self->{pid} || $result == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result == $self->{"pid"} or $result == -1) 0 0 0
Condition0%0/3
$result == $self->{"pid"} or $result == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
$result == $self->{"pid"} or $result == -1
$result == $self->{"pid"} $result == -1
Truth table0%0/3
A or B
A= $result == $self->{"pid"}B= $result == -1
ABresult
1X1
011
000
188 0 $processes->delete($self->{pid});
189 }
190 }
191  
192 ## Blocks until the process finishes. It automatically close the input
193 ## channel if necessary.
194 ##
195 ## If "capture" attribute is true, it returns the output of the
196 ## gnuplot process. Otherwise it returns an empty string.
197 sub _wait_to_finish {
198 0 ▶ my ($self) = @_;
Subroutine _wait_to_finish: not called CC=4 SCAR=30.0
199 0 $self->_close_input();
200  
201 0 my $result = "";
202 0 my $read_handle = $self->{read_handle};
203 0 ▶ if(defined $read_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_handle) 0 0 0
204 0 ▶ while(defined(my $line = <$read_handle>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $read_handle)) 0 0 0
205 0 $result .= $line;
206  
207 ## Wait for $END_SCRIPT_MARK that we told the gnuplot to
208 ## print. It is not enough to wait for EOF from $read_handle,
209 ## because in some cases, $read_handle won't be closed even
210 ## after the gnuplot process exits. For example, in Linux
211 ## 'wxt' terminal, 'gnuplot --persist' process spawns its own
212 ## child process to handle the wxt window. That child process
213 ## inherits the file descriptors from the gnuplot process, and
214 ## it won't close the output fd. So $read_handle won't be
215 ## closed until we close the wxt window. This is not good
216 ## especially we are in REPL mode.
217 0 my $end_position = index($result, $END_SCRIPT_MARK);
218 0 ▶ if($end_position != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_position != -1) 0 0 0
219 0 $result = substr($result, 0, $end_position);
220 0 last;
221 }
222 }
223 0 close $read_handle;
224 }
225 ## Do not actually wait for the process to finish, because it can
226 ## be a long-lasting process with plot windows.
227 0 return $result;
228 }
229  
230 sub _terminate {
231 0 ▶ my ($self) = @_;
Subroutine _terminate: not called CC=1 SCAR=6.9
232 0 kill 'TERM', $self->{pid};
233 }
234  
235 #### #### #### #### #### #### #### #### #### #### #### #### ####
236  
237 package Gnuplot::Builder::Process::MockTool;
238 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 ## tools for a process who mocks gnuplot, i.e., the process who
242 ## communicates with Gnuplot::Builder::Process.
243  
244  
245 ## Receive data from Gnuplot::Builder::Process and execute the $code
246 ## with the received data.
247 sub receive_from_builder {
248 0 ▶ my ($input_handle, $output_handle, $code) = @_;
Subroutine receive_from_builder: not called CC=4 SCAR=30.0
249 0 ▶ while(defined(my $line = <$input_handle>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $input_handle)) 0 0 0
250 0 $code->($line);
251  
252 ## Windows does not signal EOF on $input_handle so we must
253 ## detect the end of script by ourselves.
254 0 ▶ if(index($line, $END_SCRIPT_MARK) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($line, $END_SCRIPT_MARK) != -1) 0 0 0
255 0 print $output_handle "$END_SCRIPT_MARK\n";
256 0 $code->("$_\n") foreach $_finishing_commands->();
257 0 last;
258 }
259 }
260 }
261  
262  
263 38 1;
264  
265 __END__
↑ Summary blib/lib/Gnuplot/Builder/Script.pm »