HostLanguage.pm

filter: Statementstmt 15.0% 20 / 133 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 31.2% 5 / 16 Podpod 0.0% 0 / 10 totaltotal 11.4% 25 / 219 CC 29 SCAR 35.8
CC
29
Cov
11%
read_configfile
72.0
parse_configfile
72.0
read_csshrc
42.0
CRAP 36.0
« ParSCP.pm ↑ Summary
1 package Net::HostLanguage;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Set::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our @EXPORT = qw{
10 parse_configfile
11 translate
12 $VERBOSE
13 };
14  
15 2 our $VERBOSE = 0;
16  
17 # Create methods for each defined machine or cluster
18 0 ▶sub create_machine_alias {
Subroutine create_machine_alias: not called CC=2 SCAR=13.0
Pod undocumented
19 0 my %cluster = @_;
20  
21 0 my %method; # keys: machine addresses. Values: the unique name of the associated method
22  
23 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 0 for my $m (keys(%cluster)) {
25 0 my $name = uniquename($m);
26 0 ▶ *{__PACKAGE__.'::'.$name} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 $cluster{$m}
28 0 };
29 0 $method{$m} = $name;
30 }
31  
32 0 return \%method;
33 }
34  
35 # sub read_csshrc
36 # Configuration dump produced by 'cssh -u'
37 # Example of .csshrc file:
38 # window_tiling=yes
39 # window_tiling_direction=right
40 # clusters = beno ben beo bno bco be bo eo et num beat local beow
41 # beow = beowulf europa orion tegasaste
42 # beno = beowulf europa nereida orion
43 # ben = beowulf europa nereida
44 # beo = beowulf europa orion
45 # bno = beowulf nereida orion
46 # bco = beowulf casnereida orion
47 # be = beowulf europa
48 # bo = beowulf orion
49 # eo = europa orion
50 # et = europa etsii
51 # # europa etsii
52 # num = 193.145.105.175 193.145.101.246
53 # # With @
54 # beat = casiano@beowulf casiano@europa
55 # local = local1 local2 local3
56 0 ▶sub read_csshrc {
Subroutine read_csshrc: not called CC=6 SCAR=37.4
Pod undocumented
57 0 my $configfile = shift;
58  
59 0 open(my $f, $configfile);
60  
61 # We are interested in lines matching 'option = values'
62 0 my @desc = grep { m{^\s*(\S+)\s*=\s*(.*)} } <$f>;
63 0 close($f);
64  
65 0 my %config = map { m{^\s*(\S+)\s*=\s*(.*)} } @desc;
66  
67 # From cssh man page:
68 # extra_cluster_file = <null>
69 # Define an extra cluster file in the format of /etc/clusters.
70 # Multiple files can be specified, seperated by commas. Both ~ and $HOME
71 # are acceptable as a to reference the users home directory, i.e.
72 # extra_cluster_file = ~/clusters, $HOME/clus
73 #
74 0 ▶ if (defined($config{extra_cluster_file})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config{"extra_cluster_file"}) 0 0 0
75 0 $config{extra_cluster_file} =~ s/(\~|\$HOME)/$ENV{HOME}/ge;
76 0 my @extra = split /\s*,\s*/, $config{extra_cluster_file};
77 0 for my $extra (@extra) {
78 0 ▶ if (-r $extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $extra) 0 0 0
79 0 open(my $e, $extra);
80 push @desc, grep {
81 0 my $def = $_ =~ m{^\s*(\S+)\s*=\s*(.*)};
82 0 my $cl = $1;
83 0 ▶ $config{clusters} .= " $cl" if ($cl && $config{clusters} !~ /\b$cl\b/);
Branch: 1 branch
BranchTrueFalseTotal
if $cl and not $config{"clusters"} =~ /\b$cl\b/ 0 0 0
Condition0%0/3
$cl and not $config{"clusters"} =~ /\b$cl\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$cl and not $config{"clusters"} =~ /\b$cl\b/
$cl not $config{"clusters"} =~ /\b$cl\b/
Truth table0%0/3
A and not(B)
A= $clB= not $config{"clusters"} =~ /\b$cl\b/
ABresult
0X0
100
111
84 0 $def;
85 } <$e>;
86 0 close($e);
87 }
88 }
89 }
90 0 chomp(@desc);
91  
92 # Get the clusters. It starts 'cluster = ... '
93 # clusters = beno ben beo bno bco be bo eo et num beat local beow
94 0 my $regexp = $config{clusters};
95  
96 # We create a regexp to search for the clusters definitions.
97 # The regexp is the "or" of the cluster names followed by '='
98 # (^beo\s*=)|(^be\s*=) | ...
99 0 $regexp =~ s/\s*(\S+)\s*/(^$1\\s*=)|/g;
100 # (beno\s*=) | (ben\s*=) | ... | (beow\s*=) |
101 # Chomp the final or '|'
102 0 $regexp =~ s/[|]\s*$//;
103  
104 # Select the lines that correspond to clusters
105 0 return grep { m{$regexp}x } @desc;
106 }
107  
108 0 ▶sub slurp {
Subroutine slurp: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $configfile = shift;
110  
111 0 open(my $f, $configfile);
112 0 my @desc = <$f>;
113 0 chomp(@desc);
114  
115 0 return @desc;
116 }
117  
118 # read_configfile: Return an array with the relevant lines of the config file
119 0 ▶sub read_configfile {
Subroutine read_configfile: not called CC=8 SCAR=42.8
Pod undocumented
120 0 my $configfile = $_[0];
121  
122 0 ▶ return slurp($configfile) if (defined($configfile) && -r $configfile);
Branch: 1 branch
BranchTrueFalseTotal
if defined $configfile and -r $configfile 0 0 0
Condition0%0/3
defined $configfile and -r $configfile
!ll&&!rl&&r
000
MC/DC0%0/2
defined $configfile and -r $configfile
defined $configfile -r $configfile
Truth table0%0/3
A and B
A= defined $configfileB= -r $configfile
ABresult
0X0
100
111
123  
124 # Configuration file not found. Try with ~/.clustersrc of cssh
125 0 $configfile = $_[0] = "$ENV{HOME}/.clustersrc";
126 0 ▶ return slurp($configfile) if (defined($configfile) && -r $configfile);
Branch: 1 branch
BranchTrueFalseTotal
if defined $configfile and -r $configfile 0 0 0
Condition0%0/3
defined $configfile and -r $configfile
!ll&&!rl&&r
000
MC/DC0%0/2
defined $configfile and -r $configfile
defined $configfile -r $configfile
Truth table0%0/3
A and B
A= defined $configfileB= -r $configfile
ABresult
0X0
100
111
127  
128 # Configuration file not found. Try with ~/.csshrc of cssh
129 0 $configfile = $_[0] = "$ENV{HOME}/.csshrc";
130 0 ▶ return read_csshrc($configfile) if (-r $configfile);
Branch: 1 branch
BranchTrueFalseTotal
if -r $configfile 0 0 0
131  
132 # Configuration file not found. Try with /etc/clusters of cssh
133 0 $configfile = $_[0] = "/etc/clusters";
134 0 ▶ return read_csshrc($configfile) if (-r $configfile);
Branch: 1 branch
BranchTrueFalseTotal
if -r $configfile 0 0 0
135  
136 0 ▶ warn("Warning. Configuration file not found!\n") if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::HostLanguage::VERBOSE 0 0 0
137  
138 0 return ();
139 }
140  
141 ############################################################
142 # limitation: label expansion isn't allowed. Like in:
143 # clusters = <tag1> <tag2> <tag3>
144 # <tag1> = host1 host2 host3
145 # <tag2> = user@host4 user@host5 host6
146 # <tag3> = <tag1> <tag2>
147 0 ▶sub parse_configfile {
Subroutine parse_configfile: not called CC=8 SCAR=42.8
Pod undocumented
148 0 my $configfile = $_[0];
149 0 my %cluster;
150  
151 0 my @desc = read_configfile($_[0]);
152  
153 0 for (@desc) {
154 0 ▶ next if /^\s*(#.*)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(#.*)?$/ 0 0 0
155  
156 0 my ($cluster, $members) = split /\s*=\s*/;
157 0 ▶ die "Error in configuration file $configfile invalid cluster name $cluster" unless $cluster =~ /^[\w.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cluster =~ /^[\w.]+$/ 0 0 0
158  
159 0 my @members = split /\s+/, $members;
160  
161 0 my @result;
162 0 for my $m (@members) {
163 0 ▶ die "Error in configuration file $_[0] invalid name $m" unless $m =~ /^[\@\w.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $m =~ /^[\@\w.]+$/ 0 0 0
164  
165 # Net::ParSCP admits cluster ranges as cc137..139
166 0 my $range = expand_ranges($m);
167 0 push @result, $range->members;
168 0 for my $r ($range->members) {
169 0 ▶ $cluster{$r} = Set::Scalar->new($r) unless exists $cluster{$r};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cluster{$r} 0 0 0
170 }
171  
172 }
173 0 $cluster{$cluster} = Set::Scalar->new(@result);
174 }
175  
176 # keys: machine and cluster names; values: name of the associated method
177 0 my $method = create_machine_alias(%cluster);
178  
179 0 return (\%cluster, $method);
180 }
181  
182 ############################################################
183 {
184 2 my $pc = 0;
185  
186 0 ▶ sub uniquename {
Subroutine uniquename: not called CC=1 SCAR=6.9
Pod undocumented
187 0 my $m = shift;
188  
189 0 $m =~ s/\W/_/g;
190 0 $pc++;
191 0 return "_$pc"."_$m";
192 }
193 }
194  
195 2 ▶sub warnundefined {
Subroutine warnundefined: not called CC=2 SCAR=15.1
Pod undocumented
196 0 my ($configfile, @errors) = @_;
197  
198 0 local $" = ", ";
199 0 ▶ my $prefix = (@errors > 1) ?
Branch: 1 branch
BranchTrueFalseTotal
@errors > 1 ? : 0 0 0
200 "Machine identifiers (@errors) do"
201 : "Machine identifier (@errors) does";
202 0 warn "$prefix not correspond to any cluster or machine defined in ".
203 " cluster description file '$configfile'.\n";
204 }
205  
206 # expand_ranges
207 # Receives a range (num...num) specifying a cluster like:
208 # cc124..125.a1..2
209 # and returns the Set::Scalar object containing the elements:
210 # cc124.a1 cc124.a2 cc125.a1 cc125.a2
211 0 ▶sub expand_ranges {
Subroutine expand_ranges: not called CC=3 SCAR=24.8
Pod undocumented
212 0 my $cluster = shift;
213  
214 0 my @result;
215 0 my @processing = ($cluster);
216 0 ▶ while (@processing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@processing) 0 0 0
217 0 my $c = shift @processing;
218 0 my ($b, $e) = $c =~ m{(\d+)\.\.+(\d+)};
219 0 ▶ if (defined($b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $b) { } 0 0 0
220 0 @processing = map { my $d = $c; $d =~ s/$b\.\.+$e/$_/; $d } $b..$e;
221 }
222 else {
223 0 push @result, $c;
224 }
225 }
226 0 return Set::Scalar->new(@result);
227 }
228  
229 0 ▶sub non_declared_machines {
Subroutine non_declared_machines: not called CC=3 SCAR=24.8
Pod undocumented
230 0 my $configfile = shift;
231 0 my $clusterexp = shift;
232 0 my %cluster = @_;
233  
234 0 my @unknown;
235 0 my @clusterexp = $clusterexp =~ m{([\w.\@]+)}g;
236 0 ▶ if (@unknown = grep { !exists($cluster{$_}) } @clusterexp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown = grep({not exists $cluster{$_};} @clusterexp)) 0 0 0
237 0 ▶ warnundefined($configfile, @unknown) if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::HostLanguage::VERBOSE 0 0 0
238 }
239 0 return @unknown;
240 }
241  
242 0 ▶sub translate {
Subroutine translate: not called CC=4 SCAR=30.0
Pod undocumented
243 0 my ($configfile, $clusterexp, $cluster, $method) = @_;
244  
245 # Autodeclare unknown machine identifiers
246 0 my @unknown = non_declared_machines($configfile, $clusterexp, %$cluster);
247 0 my %unknown = map { $_ => expand_ranges($_)} @unknown;
248 0 %$cluster = (%$cluster, %unknown); # union: add non declared machines
249 0 %$method = (%$method, %{create_machine_alias(%unknown)});
250  
251 # Translation: transform user's formula into a valid Perl expression
252 # Cluster names are translated into a call to the associated method
253 # The associated method returns the set of machines for that cluster
254 0 $clusterexp =~ s/(\w[\w.\@]*)/$method->{$1}()/g;
255  
256 0 my $set = eval $clusterexp;
257  
258 0 ▶ unless (defined($set) && ref($set) && $set->isa('Set::Scalar')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set and ref $set and $set->isa("Set::Scalar")) 0 0 0
Condition0%0/6
defined $set and ref $set and $set->isa("Set::Scalar")
!ll&&!rl&&r
000
defined $set and ref $set
!ll&&!rl&&r
000
MC/DC0%0/3
defined $set and ref $set and $set->isa("Set::Scalar")
defined $set ref $set $set->isa("Set::Scalar")
Truth table0%0/4
A and B and C
A= defined $setB= ref $setC= $set->isa("Set::Scalar")
ABCresult
0XX0
10X0
1100
1111
259 0 $clusterexp =~ s/_\d+_//g;
260 0 $clusterexp =~ s/()//g;
261 0 warn "Error. Expression '$clusterexp' has errors. Skipping.\n";
262 0 return;
263 }
264 0 return $set;
265 }
266  
267 2 1;
268  
269 __END__
« ParSCP.pm ↑ Summary