blib/lib/HPCI/SuperSub.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 130 CC 15 SCAR 28.5
CC
15
Cov
0%
_build_stage_list
42.0
_find_named_stage
12.0
_match_stages
12.0
CRAP 17.3
1 package HPCI::SuperSub;
2  
3 ### INCLUDES ##################################################################
4  
5 # safe Perl
6 use warnings;
7 use strict;
8  
9 use Moose::Role;
10  
11 ## no critic BoutrosLab::HangingComma
12 ## no critic BoutrosLab::IndentationCheck
13  
14 =head1 NAME
15  
16 HPCI::SuperSub
17  
18 =head1 SYNOPSIS
19  
20 A role containing attibutes and methods common to all of group,
21 subgroup, and stage classes.
22  
23 =head1 ATTRIBUTES
24  
25 The attributes for this class are pulled in from HPCI::Super
26 and HPCI::Sub. Super contains all the characteristics common to
27 container classes (Group and Subgroup), while Sub contains all
28 the characteristics common to contained classes (Subgroup and Stage).
29  
30 =head2 _full_name (composed internally)
31  
32 The name of a group/subgroup/stage is expanded into a directory-like
33 name that is composed of the parent subgroups' and the final object's names
34 all joined together with double underscores. E.g. 'subgroupA__subgroupB__stage1'
35  
36 =cut
37  
38 has '_full_name' => (
39 is => 'ro',
40 isa => 'Str',
41 lazy => 1,
42 default => sub {
43 my $self = shift;
44 my $name = $self->name;
45 if ($self->does('HPCI::Sub') ) {
46 return $self->group->_sub_name( $name );
47 }
48 return $name;
49 },
50 );
51  
52 sub _sub_name {
53 my $self = shift;
54 my $name = shift;
55 return join '__', $self->_group_names, $name;
56 }
57  
58 sub _group_names {
59 my $self = shift;
60 if ($self->does('HPCI::Sub')) {
61 return ( $self->group->_group_names, $self->name );
62 }
63 return ();
64 }
65  
66 sub _build_stage_list {
67 my $self = shift;
68 my $sgtarget = shift;
69 my $stages = $self->_stages;
70 my $subgrps = $self->_subgroups;
71 return
72 map { $self->_croak( "Unknown stage name ($_) passed to add_deps" )
73 unless exists $stages->{$_};
74 $_
75 }
76 map { $subgrps->{$_} ? ($_ . '__' . $sgtarget) : $_ }
77 map { $self->_find_named_stage( $_ ) }
78 # grep { defined }
79 map { ref($_) ? $_->_full_name : $_ }
80 map { ref($_) eq 'Regexp' ? ($self->_match_stages( $_ )) : ($_) }
81 map { ref($_) eq 'ARRAY' ? @{$_} : ($_) }
82 grep { defined }
83 @_
84 };
85  
86 sub _find_named_stage {
87 my $self = shift;
88 my $name = shift;
89 return $name if $name =~ m{/}; # return if already a full name
90 return $self->_namemap->{$name} # or if short name is in this subgroup
91 || ( $self->does('HPCI::Sub')
92 ? $self->group->_find_named_stage($name) # search parent group
93 : $name ); # at top, return short name for failure message
94 };
95  
96 sub _match_stages {
97 my $self = shift;
98 my $pat = shift;
99 my @matches = grep { m/$pat/ } keys %{ $self->_namemap };
100 return @matches if @matches;
101 return $self->does('HPCI::Sub')
102 ? $self->group->_match_stages($pat)
103 : ()
104 ;
105 };
106  
107 =head2 $group->add_deps
108  
109 $group->add_deps(
110 dep => 'a_dep', ## one of these two
111 deps => ['dep1', 'dep2', ...],
112 pre_req => 'a_pre_req', ## and one of these two
113 pre_reqs => ['pre_req1', 'pre_req2', ...],
114 );
115  
116 # A scalar value, either provided alone or in a list, can be
117 # any of:
118 # - an existing stage object
119 # - a string - the exact value of some existing stage's name
120 # - a regexp - all of the stages whose name matches the regexp
121  
122 The add_deps method marks the pre_req (or all of the pre_reqs)
123 as being pre-requisites to the dep (or all of the deps). When the
124 group is executed, stages may be run in parallel, but a dependent
125 stage will not be permitted to start executing until all of its
126 prerequisites stages have completed successfully.
127  
128 It is permitted to list the same dependency multiple times. This can
129 be convenient in that you do not need to be careful about providing
130 non-overlapping groups when you specify sets of prerequisites.
131  
132 So, you could write:
133  
134 $group->add_deps( pre_req=>'stage1', deps=>[qw(stage2 stage3)] );
135 $group->add_deps( pre_reqs=>[qw(stage1 stage2)], dep=>'stage3' );
136  
137 instead of:
138  
139 $group->add_deps( pre_req=>'stage1', deps=>qr(^stage[23]$) );
140 $group->add_deps( pre_req=>'stage2', dep=>'stage3' );
141  
142 or:
143  
144 $group->add_deps( pre_req=>'stage1', dep=>'stage2' );
145 $group->add_deps( pre_req=>'stage2', dep=>'stage3' );
146  
147  
148 All three forms will provide the same ordering, the last is
149 clearer for this simple sequence, but when there are many
150 stages that have it may be easier to specify collections of
151 dependencies at once.
152  
153 However, you B<must> be careful to avoid dependency loops.
154 That would be a chain of dependencies stages that include the
155 same stage multiple times (stage1 -> stage2 -> stage1). Since a
156 dependency indicates that the prerequisite stage must be finished
157 executing before the dependent stage can start executing, this loop
158 would mean that the stage1 cannot start until stage2 has completed,
159 but also that stage2 cannot start until stage1 has completed. So,
160 neither one can ever start and they will both never complete.
161  
162 Such a loop will eventually be detected, when the group has reached
163 a point where there are no stages running, and no stages can be
164 started - but there could have been a lot of time wasted executing
165 stages that were not part of the loop before this is noticed and
166 the run aborted.
167  
168 Each stage argument passed can be either a reference to the stage
169 object or the name of the stage, or a regexp that select all of the
170 stages whose name matches the regexp. (If no stage name matches a
171 regexp, then no stages are selected. This allows using a regexp to
172 match against an optional stage without having to check whether that
173 optional stage was actually used in this run. The downside is that
174 a mistyped regexp will give no complaint when it matches nothing,
175 but it is certainly not possibly to give a complaint if a mistyped
176 regexp matches more stages than the user intended so checking the
177 regexp carefully is necessary in any case.)
178  
179 =cut
180  
181 sub add_deps {
182 my $self = shift;
183 my %defargs = (
184 dep => undef,
185 deps => [],
186 pre_req => undef,
187 pre_reqs => [],
188 );
189 my %args = ( %defargs, scalar(@_) == 1 ? %{ $_[0] } : @_ );
190 my $badargs = join ' ', grep { ! exists $defargs{$_} } keys %args;
191 $self->_croak( "Unknown arg($badargs) provided to add_deps" ) if $badargs;
192 my @deps = $self->_build_stage_list( '__START__', $args{dep}, $args{deps});
193 my @pre_reqs = $self->_build_stage_list( '__END__', $args{pre_req}, $args{pre_reqs});
194 for my $dep (@deps) {
195 for my $pre_req (@pre_reqs) {
196 $self->_add_dep( $pre_req, $dep );
197 }
198 }
199 };
200  
201 =head2 group_dir (optional)
202  
203 The directory which will contain all output pertaining to the entire
204 group. By default, this is a new directory under the parent group's
205 B<group_dir> named for this subgroup, or, for the top level group,
206 under B<base_dir> a name combining the name of the group and the timestamp
207 when the group was created (e.g. EXAMPLEGROUP-YYMMDD-HHMMSS).
208  
209 =cut
210  
211 has 'group_dir' => (
212 is => 'ro',
213 isa => 'Path::Class::Dir',
214 lazy => 1,
215 coerce => 1,
216 default => sub {
217 my $self = shift;
218 my $target =
219 $self->does('HPCI::Sub')
220 ? $self->group->group_dir->subdir($self->name)
221 : $self->base_dir->subdir($self->_unique_name);
222 HPCI::_trigger_mkdir( $self, $target );
223 return $target;
224 },
225 trigger => \&HPCI::_trigger_mkdir,
226 );
227  
228 =head2 file_params
229  
230 A hash containing the param list for files that need params is
231 kept in the internal attribute C<_file_info>. That attribute
232 is initialized to the value of the patent (sub)group (or to
233 an empty hash for the top level group).
234  
235 The internal method C<_add_file_params> is used to add any values
236 passed in this C<file_params> attribute into the C<_file_info>
237 hash.
238  
239 Providing file info this way means that it does not have to be written
240 out in full every time the file is used through the program.
241  
242 Defaults to an empty hash.
243  
244 =cut
245  
246 has 'file_params' => (
247 is => 'bare',
248 isa => 'HashRef',
249 trigger => sub {
250 my $self = shift;
251 $self->add_file_params( @_ );
252 },
253 );
254  
255 has '_file_info' => (
256 is => 'ro',
257 isa => 'HashRef',
258 lazy => 1,
259 builder => '_default_file_info',
260 );
261  
262 =head2 $group->add_file_params
263  
264 Augment the file_params list with additioal files.
265 Provide either a hashref or a list of value pairs,
266 in either case, the pairs are filename as the key,
267 and params as the value.
268  
269 =cut
270  
271 sub add_file_params {
272 my $self = shift;
273 my $fi = $self->_file_info;
274 my $args = $_[0];
275 $args = { @_ } unless ref $args eq 'HASH';
276 while ( my ($k,$v) = each %$args ) {
277 $self->_croak( "file params value for $k must be a hash ref" )
278 unless (ref($v) eq 'HASH');
279 $k = File::Spec->rel2abs($k);
280 $fi->{$k}{params} = $v;
281 }
282 }
283  
284  
285 =head1 AUTHOR
286  
287 John Macdonald - Boutros Lab
288  
289 =head1 ACKNOWLEDGEMENTS
290  
291 Paul Boutros, Phd, PI - Boutros http://www.omgubuntu.co.uk/2016/03/vineyard-wine-configuration-tool-linuxLab
292  
293 The Ontario Institute for Cancer Research
294  
295 =cut
296  
297 1;
298