lib/SSH/Batch/ForNodes.pm

filter: Statementstmt 91.9% 149 / 162 Branchbran 80.6% 71 / 88 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 9 totaltotal 83.1% 237 / 285 CC 54 SCAR 24.0
CC
54
Cov
87%
parse_expr
20.7
init_rc
13.0
parse_atom
8.0
CRAP 11.0
« blib/lib/SSH/Batch/ForNodes.pm ↑ Summary bin/atnodes »
1 # vim:set ft=perl ts=4 sw=4 et
2  
3 package SSH::Batch::ForNodes;
4  
5 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 64 our $VERSION = '0.029';
9  
10 64 ▶use Set::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 64 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub clear_universe ();
14 sub init_rc ();
15 sub load_rc ($$);
16 sub parse_line ($$);
17 sub parse_expr ($);
18 sub parse_term ($);
19 sub parse_atom ($);
20 sub expand_seg ($@);
21 sub expand_wildcards ($);
22  
23 64 my $RangePat = qr/\w+(?:(?:-|\.\.)\w+)?/;
24 64 my %Vars;
25 64 our $HostUniverse = Set::Scalar->new;
26  
27 sub clear_universe () {
28 0 ▶ $HostUniverse->empty;
Subroutine clear_universe: not called CC=1 SCAR=6.9
Pod undocumented
29 }
30  
31 sub init_rc () {
32 52 ▶ my $rcfile = $ENV{SSH_BATCH_RC} || q();
Subroutine init_rc: called CC=9 SCAR=25.6
Pod undocumented
Condition50%1/2
$ENV{'SSH_BATCH_RC'} || ''
l!l
052
MC/DC0%0/1
$ENV{'SSH_BATCH_RC'} || ''
$ENV{'SSH_BATCH_RC'}
Truth table50%1/2
A
A= $ENV{'SSH_BATCH_RC'}
Aresult
11
00
33 52 ▶ if(! $rcfile){
Branch: 1 branch
BranchTrueFalseTotal
unless ($rcfile) 52 0 52
34 52 ▶ my $home = $ENV{SSH_BATCH_HOME} || File::HomeDir->my_home;
Condition33%1/3
$ENV{'SSH_BATCH_HOME'} || 'File::HomeDir'->my_home
l!l&&r!l&&!r
5200
MC/DC0%0/2
$ENV{'SSH_BATCH_HOME'} || 'File::HomeDir'->my_home
$ENV{'SSH_BATCH_HOME'} 'File::HomeDir'->my_home
Truth table33%1/3
A || B
A= $ENV{'SSH_BATCH_HOME'}B= 'File::HomeDir'->my_home
ABresult
1X1
011
000
35 52 ▶ if (!defined $home || !-d $home) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $home and -d $home) 3 49 52
Condition66%2/3
defined $home and -d $home
!ll&&!rl&&r
0349
MC/DC50%1/2
defined $home and -d $home
defined $home -d $home
Truth table66%2/3
A and B
A= defined $homeB= -d $home
ABresult
0X0
100
111
36 3 die "Can't find the home for the current user.\n";
37 }
38 49 $rcfile = "$home/.fornodesrc";
39 }
40  
41 # auto create $rcfile if $rcfile not exists
42 49 ▶ if (! -e $rcfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $rcfile) 0 49 49
43 0 ▶ open my $rc, '>', $rcfile or
Branch: 1 branch
BranchTrueFalseTotal
open my $rc, ">", $rcfile or die "Can't auto create ${rcfile}: $!\n" 0 0 0
44 die "Can't auto create $rcfile: $!\n";
45 0 close $rc;
46 }
47  
48 49 ▶ open my $rc, $rcfile or
Branch: 1 branch
BranchTrueFalseTotal
open my $rc, $rcfile or die "Can't open $rcfile for reading: $!\n" 0 49 49
49 die "Can't open $rcfile for reading: $!\n";
50 49 load_rc($rc, $rcfile);
51 49 close $rc;
52 #return ($rc, $rcfile);
53 }
54  
55 sub load_rc ($$) {
56 49 ▶ my ($rc, $rcfile) = @_;
Subroutine load_rc: called CC=5 SCAR=16.1
Pod undocumented
57 49 my $accum_ln;
58 49 ▶ while (<$rc>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $rc)) 92 49 141
59 92 s/\#.*//;
60 92 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 18 74 92
61 74 chomp;
62 74 ▶ if (s/\\\s*$//s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/\\\s*$//s) 5 69 74
63 5 $accum_ln .= " $_";
64 5 next;
65 }
66 69 ▶ if (defined $accum_ln) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $accum_ln) 3 66 69
67 3 parse_line("$accum_ln $_", $rcfile);
68 3 undef $accum_ln;
69 3 next;
70 }
71 66 parse_line($_, $rcfile);
72 }
73 }
74  
75 sub parse_line ($$) {
76 69 ▶ local *_ = \($_[0]);
Subroutine parse_line: called CC=5 SCAR=18.0
Pod undocumented
77 69 my $rcfile = $_[1];
78 69 ▶ if (/^\s*([^=\s]*)\s*=\s*(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*([^=\s]*)\s*=\s*(.*)/) { } 69 0 69
79 69 my ($var, $def) = ($1, $2);
80 69 ▶ if ($var !~ /^\w[-\.\w]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($var =~ /^\w[-\.\w]*$/) 0 69 69
81 0 die "Invalid variable name in $rcfile, line $.: ",
82 "$var\n";
83 }
84 69 my $set;
85 69 eval {
86 69 $set = parse_expr($def);
87 };
88 69 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 69 69
89 0 die "Failed to parse the variable $var\'s value in $rcfile, ",
90 "line $.: $@";
91 } else {
92 69 ▶ if (defined $Vars{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Vars{$var}) 0 69 69
93 0 die "Variable redefinition in $rcfile line $.: $_\n";
94 }
95 69 $Vars{$var} = $set;
96 }
97 } else {
98 0 die "Syntax error in $rcfile, line $.: $_\n";
99 }
100 }
101  
102 sub parse_expr ($) {
103 116 ▶ local *_ = \($_[0]);
Subroutine parse_expr: called CC=20 SCAR=30.3
Pod undocumented
104  
105 # trim
106 116 s/(?:^\s+|\s+$)//gs;
107  
108 116 my @toplevel;
109 116 while (1) {
110 374 ▶ if (/\G \s* (?<= [\}\)\s] ) ([-+*\/]) (?= [\{\(\s] ) \s*/gcx) {
Branch: 4 branches
BranchTrueFalseTotal
if (m[\G \s* (?<= [\}\)\s] ) ([-+*/]) (?= [\{\(\s] ) \s*]cgx) { } 33 341 374
elsif (/\G \{ .*? \} /cgx) { } 47 294 341
elsif (/\G \S+ /cgx) { } 140 154 294
elsif (/\G \s+ /cgx) { } 38 116 154
111 33 push @toplevel, $1;
112 } elsif (/\G \{ .*? \} /gcx) {
113 47 push @toplevel, $&;
114 } elsif (/\G \S+ /gcx) {
115 140 push @toplevel, $&;
116 } elsif (/\G \s+ /gcx) {
117 38 push @toplevel, '+';
118 } else {
119 116 last;
120 }
121 }
122 116 my $expect_term = 1;
123 116 for my $raw_op (@toplevel) { # op would be either operands or operators
124 258 my $op = $raw_op;
125  
126 258 ▶ if ($op =~ /^[-+*\/]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ m[^[-+*/]$]) 71 187 258
127 71 ▶ if ($expect_term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_term) 0 71 71
128 0 die "Expecting terms but found operator $op.\n";
129 }
130 71 $expect_term = 1;
131 71 next;
132 }
133 187 ▶ if (!$expect_term) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expect_term) 0 187 187
134 0 die "Expecting operators but found term $op\n";
135 }
136 187 $expect_term = 0;
137 187 eval {
138 187 $raw_op = parse_term($op);
139 };
140 187 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 183 187
141 4 die $@;
142 }
143 }
144 112 my @lower;
145 112 ▶ while (@toplevel > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@toplevel > 1) 71 112 183
146 71 my $a = shift @toplevel;
147 71 my $op = shift @toplevel;
148 71 ▶ if ($op eq '+') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '+') { } 56 15 71
elsif ($op eq '-') { } 8 7 15
elsif ($op eq '*') { } 6 1 7
elsif ($op eq '/') { } 1 0 1
149 56 push @lower, $a, $op;
150 #unshift @toplevel, $a + $b;
151 } elsif ($op eq '-') {
152 8 push @lower, $a, $op;
153 #unshift @toplevel, $a - $b;
154 } elsif ($op eq '*') {
155 6 my $b = shift @toplevel;
156 6 unshift @toplevel, $a * $b;
157 } elsif ($op eq '/') {
158 1 my $b = shift @toplevel;
159 1 unshift @toplevel, $a / $b;
160 } else {
161 0 die "Invalid operator : [$op]\n";
162 }
163 }
164 112 ▶ if (@toplevel) {
Branch: 1 branch
BranchTrueFalseTotal
if (@toplevel) 112 0 112
165 112 push @lower, @toplevel;
166 }
167 112 ▶ while (@lower > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lower > 1) 64 112 176
168 64 my $a = shift @lower;
169 64 my $op = shift @lower;
170 64 my $b = shift @lower;
171  
172 64 ▶ if ($op eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '+') { } 56 8 64
elsif ($op eq '-') { } 8 0 8
173 56 unshift @lower, $a + $b;
174 } elsif ($op eq '-') {
175 8 unshift @lower, $a - $b;
176 } else {
177 0 die "Unexpected operator: [$op]\n";
178 }
179 }
180 112 ▶ return @lower ? $lower[0] : Set::Scalar->new;
Branch: 1 branch
BranchTrueFalseTotal
@lower ? : 112 0 112
181 }
182  
183 sub parse_term ($) {
184 187 ▶ local *_ = \($_[0]);
Subroutine parse_term: called CC=5 SCAR=16.5
Pod undocumented
185 187 ▶ if (/^ \{ ( [^}\s]* ) \} $/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^ \{ ( [^}\s]* ) \} $/x) 46 141 187
186 46 my $var = $1;
187 46 ▶ if ($var !~ /^\w[-\.\w]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($var =~ /^\w[-\.\w]*$/) 0 46 46
188 0 die "Invalid variable name in term $_: $var\n";
189 }
190 46 my $set = $Vars{$var};
191 46 ▶ if (!defined $set) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set) 0 46 46
192 0 die "Variable $var not defined.\n";
193 }
194 46 return $set;
195 }
196 141 ▶ if (/[{}]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[{}]/) 1 140 141
197 1 die "Invalid variable reference syntax: $_\n";
198 }
199 140 return parse_atom($_);
200 }
201  
202 sub parse_atom ($) {
203 140 ▶ local *_ = \($_[0]);
Subroutine parse_atom: called CC=8 SCAR=20.8
Pod undocumented
204 140 my @segs;
205 140 while (1) {
206 352 ▶ if (/ \G \[ ( [^\]]* ) \] /xgc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/ \G \[ ( [^\]]* ) \] /cgx) { } 44 308 352
elsif (/\G[^\[]+/cg) { } 171 137 308
207 44 my $range = $1;
208 #warn "Range: $range\n";
209 44 ▶ if ($range !~ m/^$RangePat(?:\s*,\s*$RangePat)*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($range =~ /^$RangePat(?:\s*,\s*$RangePat)*$/) 3 41 44
210 3 die "Bad range: [$range]\n";
211 }
212 41 my @ranges = split /,/, $range;
213 41 my @num;
214 41 for my $range (@ranges) {
215 60 my ($a, $b) = split /(?:-|\.\.)/, $range;
216 #if (defined $b && ($a =~ /\D/ || $b =~ /\D/) && length $a ne length $b) {
217 #die "End points are not of equal lengths in the host range: $a-$b\n";
218 #}
219 60 ▶ push @num, defined $b ? $a..$b : $a;
Branch: 1 branch
BranchTrueFalseTotal
defined $b ? : 57 3 60
220 #print "@num";
221 }
222 41 push @segs, \@num;
223 } elsif (/\G[^\[]+/gc) {
224 171 push @segs, [$&];
225 171 next;
226 } else {
227 137 last;
228 }
229 }
230 137 my $hosts = expand_seg(\@segs);
231 137 my $set = Set::Scalar->new;
232 137 for my $host (@$hosts) {
233 #warn "Host: $host\n";
234 428 ▶ if ($host =~ /[\*\?]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /[\*\?]/) { } 17 411 428
235 17 $set->insert(expand_wildcards($host));
236 } else {
237 #warn "Inserting $host: $host\n";
238 411 $set->insert($host);
239 411 $HostUniverse->insert($host);
240 }
241 }
242 137 return $set;
243 }
244  
245 sub expand_seg ($@) {
246 349 ▶ my ($list, $prefixes) = @_;
Subroutine expand_seg: called CC=6 SCAR=17.9
Pod undocumented
247 349 my $cur = shift @$list;
248 349 ▶ return $prefixes unless defined $cur;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cur 137 212 349
249 212 my @new_prefixes;
250 212 ▶ if (!$prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $prefixes) { } 137 75 212
251 137 for my $alt (@$cur) {
252 152 push @new_prefixes, $alt;
253 }
254 } else {
255 75 for my $prefix (@$prefixes) {
256 368 for my $alt (@$cur) {
257 644 push @new_prefixes, $prefix . $alt;
258 }
259 }
260 }
261 212 return expand_seg($list, \@new_prefixes);
262 }
263  
264 sub expand_wildcards ($) {
265 17 ▶ my $pat = quotemeta $_[0];
Subroutine expand_wildcards: called CC=3 SCAR=11.0
Pod undocumented
266 17 $pat =~ s/\\\*/.*?/g;
267 17 $pat =~ s/\\\?/./g;
268 17 my @retvals;
269 17 ▶ while (defined(my $host = $HostUniverse->each)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $host = $SSH::Batch::ForNodes::HostUniverse->each)) 102 17 119
270 102 ▶ if ($host =~ /^$pat$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /^$pat$/) 16 86 102
271 16 push @retvals, $host;
272 }
273 }
274 17 return @retvals;
275 }
276  
277 64 1;
278 __END__
« blib/lib/SSH/Batch/ForNodes.pm ↑ Summary bin/atnodes »