Tool/ProcessUtils.pm

filter: Statementstmt 22.2% 24 / 108 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 10 totaltotal 18.7% 31 / 165 CC 16 SCAR 24.3
CC
16
Cov
17%
get_bytes
30.0
safesystem
20.0
bipipe
12.0
CRAP 11.4
« Block/Read/BaseAlignedReader.pm ↑ Summary Tool/Tagger/Featurama.pm »
1 package Treex::Tool::ProcessUtils;
2 1 $Treex::Tool::ProcessUtils::VERSION = '2.20151102';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Open a bipipe and set binmode to utf8.
12 0 ▶sub bipipe {
Subroutine bipipe: not called CC=3 SCAR=24.8
Pod undocumented
13 0 my $cmd = shift;
14 0 my $binmode = shift;
15 0 ▶ if ( !defined $binmode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $binmode) 0 0 0
16 0 $binmode = ":utf8";
17 }
18  
19 0 my $reader;
20 my $writer;
21 0 my $pid = open2( $reader, $writer, $cmd );
22 0 ▶ log_fatal("Failed to open bipipe to: $cmd") if !$pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
23 0 $writer->autoflush(1);
24 0 $reader->autoflush(1);
25  
26 0 binmode( $reader, $binmode );
27 0 binmode( $writer, $binmode );
28  
29 0 return ( $reader, $writer, $pid );
30 }
31  
32 # Open a bipipe and set binmode. Command line arguments are passed in @_
33 # This is useful on Windows because the returned $pid would be "windows cmd (shell) pid" otherwise,
34 # not pid of the executed program, which we need to kill at the end (on Windows).
35 0 ▶sub bipipe_noshell {
Subroutine bipipe_noshell: not called CC=2 SCAR=17.9
Pod undocumented
36  
37 0 my $binmode = shift;
38 #$binmode param is mandatory in this case
39 #$binmode = ":utf8" if !defined $binmode;
40  
41 0 my $cmd = "@_";
42  
43 0 my $reader;
44 my $writer;
45 0 my $pid = open2( $reader, $writer, @_ );
46 0 ▶ log_fatal "Failed to open bipipe to: $cmd" if !$pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
47 0 $writer->autoflush(1);
48 0 $reader->autoflush(1);
49  
50 0 binmode( $reader, $binmode );
51 0 binmode( $writer, $binmode );
52  
53 0 return ( $reader, $writer, $pid );
54 }
55  
56 # Bipipe that reads both the child's STDOUT and the child's STDERR
57 # (fix for Windows as with bipipe_noshell).
58 0 ▶sub verbose_bipipe_noshell {
Subroutine verbose_bipipe_noshell: not called CC=2 SCAR=17.9
Pod undocumented
59  
60 0 my ($binmode, @cmd) = @_;
61  
62 0 my $reader;
63 my $writer;
64  
65 # open3: if err stream is false, connects it to output.
66 0 my $pid = open3( $writer, $reader, 0 , @cmd );
67 0 ▶ log_fatal "Failed to open verbose_bipipe to: @cmd" if !$pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
68  
69 0 $writer->autoflush(1);
70 0 $reader->autoflush(1);
71 0 binmode( $reader, $binmode );
72 0 binmode( $writer, $binmode );
73  
74 0 return ( $reader, $writer, $pid );
75 }
76  
77  
78 # Open a tripipe and set binmode to utf8.
79 0 ▶sub verbose_bipipe {
Subroutine verbose_bipipe: not called CC=3 SCAR=24.8
Pod undocumented
80 0 my $cmd = shift;
81 0 my $binmode = shift;
82 0 ▶ if ( !defined $binmode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $binmode) 0 0 0
83 0 $binmode = ":utf8";
84 }
85  
86 0 my $reader;
87 my $writer;
88 0 *ERR = *STDOUT;
89 0 my $pid = open3( $reader, '>&ERR', '>&ERR', $cmd );
90 0 ▶ log_fatal("Failed to open verbose_bipipe to: $cmd") if !$pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
91  
92 #$writer->autoflush(1);
93 #$reader->autoflush(1);
94  
95 0 binmode( $reader, $binmode );
96  
97 #binmode( $writer, $binmode );
98  
99 0 return ( $reader, $writer, $pid );
100 }
101  
102 0 ▶sub logging_bipipe {
Subroutine logging_bipipe: not called CC=3 SCAR=21.4
Pod undocumented
103 0 my $cmd = shift;
104 0 my $binmode = shift;
105 0 ▶ if ( !defined $binmode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $binmode) 0 0 0
106 0 $binmode = ":utf8";
107 }
108  
109 0 my $reader;
110 my $writer;
111 {
112  
113 # TODO use lexical filehandles instad
114 0 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.2
115 0 open( LOG, '>>process_utils.log' );
116 }
117 0 my $pid = open3( $reader, $writer, '>&LOG', $cmd );
118 0 ▶ log_fatal("Failed to open logging_pipe to: $cmd") if !$pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
119 0 $writer->autoflush(1);
120 0 $reader->autoflush(1);
121  
122 0 binmode( $reader, $binmode );
123  
124 #binmode( $writer, $binmode );
125  
126 0 return ( $reader, $writer, $pid );
127 }
128  
129 # Run a command very safely.
130 # Synopsis: safesystem(qw(echo hello)) or die;
131 0 ▶sub safesystem {
Subroutine safesystem: not called CC=4 SCAR=30.0
Pod undocumented
132  
133 #print STDERR "Executing: @_\n";
134 0 system(@_);
135 0 ▶ if ( $? == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 0 0 0
elsif ($? & 127) { } 0 0 0
136 0 print STDERR "Failed to execute: @_\n $!\n";
137 0 exit(1);
138 }
139 elsif ( $? & 127 ) {
140 0 ▶ printf STDERR "Execution of: @_\n died with signal %d, %s coredump\n",
Branch: 1 branch
BranchTrueFalseTotal
$? & 128 ? : 0 0 0
141 ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
142 0 exit(1);
143 }
144 else {
145 0 my $exitcode = $? >> 8;
146  
147 # print STDERR "Exit code: $exitcode\n" if $exitcode;
148 0 return !$exitcode;
149 }
150 }
151  
152 1 my %unit_to_multiplier = qw(
153 B 1
154 kB 1024
155 MB 1048576
156 GB 1073741824
157 );
158  
159 0 ▶sub get_bytes {
Subroutine get_bytes: not called CC=5 SCAR=34.0
Pod undocumented
160 0 my $fn = shift;
161 0 my $key = shift;
162 0 my $bytes;
163 0 ▶ open my $INF, $fn or die "Can't read $fn";
Branch: 1 branch
BranchTrueFalseTotal
open my $INF, $fn or die "Can't read $fn" 0 0 0
164 0 ▶ while (<$INF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $INF)) 0 0 0
165 0 chomp;
166 0 ▶ if (/^$key\s*([0-9]+)\s*([a-zA-Z]+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$key\s*([0-9]+)\s*([a-zA-Z]+)\s*$/) 0 0 0
167 0 my $amount = $1;
168 0 my $unit = $2;
169 0 ▶ die "Bad unit in $_" if !defined $unit_to_multiplier{$unit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unit_to_multiplier{$unit} 0 0 0
170 0 $bytes = $amount * $unit_to_multiplier{$unit};
171 0 last;
172 }
173 }
174 0 close $INF;
175 0 return $bytes;
176 }
177  
178 # Returns the percentage of physical memory I am occupying.
179 0 ▶sub memusage {
Subroutine memusage: not called CC=1 SCAR=6.9
Pod undocumented
180 0 my $memtotal = get_bytes( "/proc/meminfo", "MemTotal:" );
181 0 my $memusage = get_bytes( "/proc/self/status", "VmPeak:" );
182 0 return $memusage / $memtotal * 100;
183 }
184  
185 # Returns the absolute size of occupied memory.
186 0 ▶sub memusage_absolute {
Subroutine memusage_absolute: not called CC=1 SCAR=6.9
Pod undocumented
187 0 return get_bytes( '/proc/self/status', 'VmPeak:' );
188 }
189  
190 # Calls waitpid(ARG1, 0) and *preserves the exit status!!* of the whole perl process
191 0 ▶sub safewaitpid {
Subroutine safewaitpid: not called CC=1 SCAR=6.9
Pod undocumented
192 0 my $pid = shift;
193  
194 # we need to preserve $?, the exit status of the whole process
195 0 my $saved_exit_status = $?;
196 0 waitpid( $pid, 0 );
197 0 $? = $saved_exit_status; # restore the original exit status
198 }
199  
200 1 1;
201  
202 # Copyright 2008 Ondrej Bojar
« Block/Read/BaseAlignedReader.pm ↑ Summary Tool/Tagger/Featurama.pm »