blib/lib/HPCI/Super.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 199 CC 14 SCAR 20.5
CC
14
Cov
0%
_deps_args
20.0
_register_descendent
12.0
_stage
12.0
CRAP 7.8
1 package HPCI::Super;
2  
3 ### INCLUDES ##################################################################
4  
5 # safe Perl
6 use warnings;
7 use strict;
8 use Carp;
9 use Data::Dumper;
10 use File::ShareDir;
11 use File::Path qw(make_path);
12 use File::Spec;
13 use Time::HiRes qw(usleep gettimeofday);
14 use Try::Tiny;
15 use DateTime;
16 use Module::Load;
17  
18 use Moose::Role;
19 use MooseX::Types::Path::Class qw(Dir File);
20 use Moose::Util::TypeConstraints;
21  
22 use HPCI::File;
23 use HPCI::Subgroup;
24  
25 ## no critic BoutrosLab::HangingComma
26 ## no critic BoutrosLab::IndentationCheck
27  
28 sub name; # tell other roles we provide a name attribute
29  
30 =head1 NAME
31  
32 HPCI::Super
33  
34 =head1 SYNOPSIS
35  
36 Role for attributes and methods that are common to group and subgroup
37 classes (i.e. those superior to a stage-like class).
38  
39 =head1 ATTRIBUTES
40  
41 =cut
42  
43 has '_driver' => (
44 is => 'ro',
45 isa => 'Str',
46 lazy => 1,
47 init_arg => undef,
48 default => sub { 'HPCD::'.$_[0]->cluster },
49 );
50  
51 has '_stageClass' => (
52 is => 'ro',
53 isa => 'Str',
54 lazy => 1,
55 init_arg => undef,
56 default => sub { $_[0]->_driver . '::Stage' },
57 );
58  
59 has '_namemap' => (
60 is => 'ro',
61 isa => 'HashRef',
62 lazy => 1,
63 init_arg => undef,
64 default => sub { {} },
65 );
66  
67 # method _register_descendent => sub {
68 sub _register_descendent {
69 my $self = shift;
70 my $child = shift;
71 my $name = $child->name;
72 my $fullname = $child->_full_name;
73 my $namemap = $self->_namemap;
74 $self->_croak("Duplicate subobject (stage or subgroup) name ($fullname)")
75 if exists $namemap->{$name};
76 $namemap->{$name} = $fullname;
77 $child->does('HPCI::Super')
78 ? $self->_register_subgroup($child)
79 : $self->_register_stage($child);
80 };
81  
82 =head2 stage_defaults
83  
84 This attribute can be given a hash reference containing values that
85 will be passed to every stage created. Subgroups that are not
86 provided with their own specific hash use their parent groups' hash.
87 The top level group defaults to an empty hash.
88  
89 =cut
90  
91 has 'stage_defaults' => (
92 is => 'ro',
93 isa => 'HashRef',
94 lazy => 1,
95 default => sub {
96 my $self = shift;
97 $self->does('HPCI::Sub')
98 ? $self->group->stage_defaults
99 : {};
100 },
101 );
102  
103 =head1 METHODS
104  
105 =head2 $group->stage( name=>'stagename', ... )
106  
107 Creates a stage and adds it to the group. See HPCI::Stage
108 for the generic parameters you may provide for a stage; and see
109 HPCD::$cluster::Stage for the cluster-specific parameters for the
110 actual type of cluster you are using.
111  
112 Note: this is the only way to add a stage object to the group.
113 In particular, you cannot create a stage object separately and add
114 it to the group - this is done to ensure that the created stage
115 object is consistant with the actual group object and that you don't
116 have to change code in multiple places if you switch to using a
117 different cluster type for the group. (If you want to mix stages
118 for multiple cluster types within your program, you should either
119 create two groups that execute independently, or else create a
120 stage that itself creates a group and manages the stages for the second
121 type of cluster.)
122  
123 The name parameter is required and must be unique - two stages
124 within the same group may not have the same name.
125  
126 The method returns the stage object that was created, although
127 most code will not need it directly. (Whenever you need to refer
128 to a stage to add dependencies, you can use its name instead of a
129 reference to the object.)
130  
131 =cut
132  
133 # method stage => sub {
134 sub stage {
135 my $self = shift;
136 my $stage = $self->_stage( @_ );
137 if ($self->does('HPCI::Sub')) {
138 $self->add_deps( pre_req => '__START__', dep => $stage );
139 $self->add_deps( pre_req => $stage, dep => '__END__' );
140 }
141 return $stage;
142 };
143  
144 # method _stage => sub {
145 sub _stage {
146 my $self = shift;
147 $self->_croak("Cannot define new stages after execution has started!")
148 if $self->_execution_started;
149 my $use_args = { };
150 # $self->debug( "Creating stage. Provided args: " . Dumper( \@_ ));
151 HPCI::_merge_hash( $use_args, $self->stage_defaults );
152 my $args = { @_ };
153 my $cluster = $self->cluster;
154 for my $arg_set ($use_args, $args) {
155 if (my $spec_args = delete $arg_set->{cluster_specific}) {
156 HPCI::_merge_hash( $use_args, $spec_args->{$cluster} // {} );
157 }
158 }
159 HPCI::_merge_hash( $use_args, $args );
160  
161 my $deps = $self->_extract_deps( $use_args );
162  
163 $self->debug( "Creating stage. Composed args: " . Dumper( \@_ ));
164 # my $stage = $StageClass->new(
165 my $stage = ($self->_stageClass)->new(
166 %$use_args,
167 cluster => $self->cluster,
168 group => $self,
169 );
170 $self->_deps_args( $stage, $deps );
171 # $self->_namemap->{$stage->name} = $stage;
172 return $stage;
173 };
174  
175 # method subgroup => sub {
176 sub subgroup {
177 my $self = shift;
178 $self->_croak("Cannot define new subgroups after execution has started!")
179 if $self->_execution_started;
180 my $use_args = { @_ };
181 # $self->debug( "Creating stage. Provided args: " . Dumper( \@_ ));
182 my $cluster = $self->cluster;
183 if (my $spec_args = delete $use_args->{cluster_specific}) {
184 HPCI::_merge_hash( $use_args, $spec_args->{$cluster} // {} );
185 }
186  
187 my $deps = $self->_extract_deps( $use_args );
188  
189 $self->debug( "Creating subgroup. Composed args: " . Dumper( \@_ ));
190 my $subgroup = HPCI::Subgroup->new(
191 %$use_args,
192 cluster => $self->cluster,
193 group => $self,
194 );
195
196 $subgroup->_stage(
197 name => '__START__',
198 code => sub { return 1 },
199 );
200
201 $subgroup->_stage(
202 name => '__END__',
203 code => sub { return 1 },
204 pre_req => '__START__',
205 );
206  
207 $self->_deps_args( $subgroup, $deps );
208  
209 return $subgroup;
210 };
211  
212 # method _extract_deps => sub {
213 sub _extract_deps {
214 my $self = shift;
215 my $args = shift;
216 my $reqs = {};
217 for my $key (qw(pre_req pre_reqs)) {
218 if (my $list = delete $args->{$key}) {
219 $reqs->{pre_req}{$key} = $list;
220 }
221 }
222 for my $key (qw(dep deps)) {
223 if (my $list = delete $args->{$key}) {
224 $reqs->{dep}{$key} = $list;
225 }
226 }
227 return $reqs;
228 };
229  
230 # method _deps_args => sub {
231 sub _deps_args {
232 my $self = shift;
233 my $child = shift;
234 my $deps = shift;
235 return unless %$deps;
236 if (my $pre = $deps->{pre_req}) {
237 $self->add_deps( %{$pre}, dep => $child );
238 }
239 if (my $dep = $deps->{dep}) {
240 $self->add_deps( %{$dep}, pre_req => $child );
241 }
242 };
243  
244 # method _add_dep => sub {
245 sub _add_dep {
246 # little cost/no damage if called again with same dep and pre_req
247 my ( $self, $pre_req, $dep ) = @_;
248 return if $self->_deps->{$pre_req}{$dep};
249 $self->_deps->{$pre_req}{$dep} = 1;
250 $self->_pre_reqs->{$dep}{$pre_req} = 1;
251 $self->_blocked->{$dep} = 1;
252 delete $self->_ready->{$dep};
253 };
254  
255  
256 =head1 AUTHOR
257  
258 Christopher Lalansingh - Boutros Lab
259  
260 John Macdonald - Boutros Lab
261  
262 =head1 ACKNOWLEDGEMENTS
263  
264 Paul Boutros, Phd, PI - Boutros http://www.omgubuntu.co.uk/2016/03/vineyard-wine-configuration-tool-linuxLab
265  
266 The Ontario Institute for Cancer Research
267  
268 =cut
269  
270 1;
271