OpenInteract2/Setup.pm

filter: Statementstmt 67.2% 76 / 113 Branchbran 35.7% 5 / 14 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 80.7% 21 / 26 Podpod 0.0% 0 / 13 totaltotal 55.6% 104 / 187 CC 14 SCAR 17.0
CC
14
Cov
59%
run_setup_for
20.0
_read_ini
12.0
run_all_actions
9.2
CRAP 5.5
« OpenInteract2/SessionManager/DBI.pm ↑ Summary OpenInteract2/ContentGenerator.pm »
1 package OpenInteract2::Setup;
2  
3 # $Id: Setup.pm,v 1.61 2005/03/18 04:09:48 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( Class::Factory OpenInteract2::ParamContainer );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Algorithm::Dependency::Ordered;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Config::IniFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 87 ▶use OpenInteract2::Setup::DependencySource;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 87 ▶use OpenInteract2::URL; # don't get rid of this...
Subroutine BEGIN: called CC=1 SCAR=0.0
15 87 ▶use OpenInteract2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 87 $OpenInteract2::Setup::VERSION = sprintf("%d.%02d", q$Revision: 1.61 $ =~ /(\d+)\.(\d+)/);
18  
19 87 my ( $DEP ); # Algorithm::Dependency object
20 my ( $DEFAULT_DEPENDENCY ); # Name of server config dep, set after we find all actions
21  
22 87 my ( $log );
23  
24 ########################################
25 # IMPLEMENTATION METHODS
26  
27 21 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
28 21 my ( $class, $setup_type, %params ) = @_;
29 21 my $impl_class = $class->get_factory_class( $setup_type );
30 21 my $self = bless( {}, $impl_class );
31 21 $self->param_assign( \%params );
32 21 $self->init();
33 21 return $self;
34 }
35  
36 # lifecycle to the outside world
37  
38 1 ▶sub run {
Subroutine run: called CC=2 SCAR=7.3
Pod undocumented
39 1 my ( $self, $ctx ) = @_;
40 1 eval {
41 1 $self->setup( $ctx );
42 1 $self->execute( $ctx );
43 1 $self->tear_down( $ctx );
44 };
45 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
46 0 my $name = $self->get_name;
47 0 oi_error "Caught exception during '$name' execution: $@";
48 }
49 1 return $self;
50 }
51  
52 # Subclasses MUST override
53  
54 0 ▶sub get_name { _must_implement( 'get_name', @_ ) }
Subroutine get_name: not called CC=1 SCAR=6.9
Pod undocumented
55 0 ▶sub execute { _must_implement( 'execute', @_ ) }
Subroutine execute: not called CC=1 SCAR=6.9
Pod undocumented
56  
57 0 ▶sub _must_implement {
Subroutine _must_implement: not called CC=2 SCAR=17.9
58 0 my ( $method, $item ) = @_;
59 0 ▶ my $class = ref( $item ) || $item;
Condition0%0/3
ref $item || $item
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $item || $item
ref $item $item
Truth table0%0/3
A || B
A= ref $itemB= $item
ABresult
1X1
011
000
60 0 oi_error "Class '$class' must implement method '$method'";
61 }
62  
63  
64 # Subclasses MAY override
65  
66 21 ▶sub init {}
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
67 7 ▶sub get_dependencies { return ( $DEFAULT_DEPENDENCY ) }
Subroutine get_dependencies: called CC=1 SCAR=0.0
Pod undocumented
68 1 ▶sub setup {}
Subroutine setup: called CC=1 SCAR=0.0
Pod undocumented
69 1 ▶sub tear_down {}
Subroutine tear_down: called CC=1 SCAR=0.0
Pod undocumented
70  
71 # Commonly used code
72  
73 0 ▶sub _read_ini {
Subroutine _read_ini: not called CC=3 SCAR=24.8
74 0 my ( $self, $ini_file ) = @_;
75 0 ▶ $log ||= get_logger( LOG_INIT );
Condition0%0/3
$log ||= get_logger("OI2.INITIALIZE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log get_logger("OI2.INITIALIZE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
ABresult
1X1
011
000
76 0 my $config = eval {
77 0 OpenInteract2::Config::IniFile->read_config({
78 filename => $ini_file
79 })
80 };
81 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
82 0 $log->error( "Failed to read configuration '$ini_file': $@" );
83 0 $config = undef;
84 }
85 0 return $config;
86 }
87  
88  
89 ########################################
90 # FACTORY/COORDINATOR METHODS
91  
92 1 ▶sub list_actions {
Subroutine list_actions: called CC=1 SCAR=0.0
Pod undocumented
93 1 my ( $class ) = @_;
94 1 return __PACKAGE__->get_registered_types;
95 }
96  
97  
98 1 ▶sub get_setup_dependencies {
Subroutine get_setup_dependencies: called CC=2 SCAR=7.4
Pod undocumented
99 1 my ( $class ) = @_;
100 1 $DEP = Algorithm::Dependency::Ordered->new(
101 source => OpenInteract2::Setup::DependencySource->new(),
102 selected => [ $DEFAULT_DEPENDENCY ],
103 );
104 1 ▶ unless ( $DEP ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DEP) 0 1 1
105 0 oi_error "Failed to create setup dependency object (no error_given)";
106 }
107 1 return $DEP;
108 }
109  
110  
111 1 ▶sub run_all_actions {
Subroutine run_all_actions: called CC=6 SCAR=22.2
Pod undocumented
112 1 my ( $class, $ctx, @skip_actions ) = @_;
113 1 ▶ $log ||= get_logger( LOG_INIT );
Condition33%1/3
$log ||= get_logger("OI2.INITIALIZE")
l!l&&r!l&&!r
010
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log get_logger("OI2.INITIALIZE")
Truth table33%1/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
ABresult
1X1
011
000
114  
115 # First, read the server configuration...
116  
117 1 my $server_config_setup = $class->new( $DEFAULT_DEPENDENCY );
118 1 $server_config_setup->run( $ctx );
119 1 $ctx->_initialize_singleton;
120  
121 # ...next, get the rest of the setup items in execution
122 # order. This order might not be pretty, but it ensures that an
123 # action won't get executed before any of its dependencies
124  
125 1 $class->get_setup_dependencies;
126 1 ▶ my $ordered_names = $DEP->schedule_all || [];
Condition50%1/2
$DEP->schedule_all || []
l!l
10
MC/DC0%0/1
$DEP->schedule_all || []
$DEP->schedule_all
Truth table50%1/2
A
A= $DEP->schedule_all
Aresult
11
00
127 1 ▶ unless ( scalar @{ $ordered_names } > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$ordered_names;} > 0) 0 1 1
128 0 oi_error "Failed to get setup dependency listing: cannot start server";
129 }
130 1 ▶ if ( scalar @skip_actions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @skip_actions) 1 0 1
131 1 $log->info( "Removing these actions and dependencies: ",
132 join( ', ', @skip_actions ) );
133 1 $ordered_names = $class->remove_skip_actions( @skip_actions );
134 }
135 $log->info( "Running the following setup actions, in order: ",
136 0 join( ', ', @{ $ordered_names } ) );
137 0 foreach my $name ( @{ $ordered_names } ) {
138 0 my $setup = OpenInteract2::Setup->new( $name );
139 0 $setup->run( $ctx );
140 0 $log->debug( "Ran setup action '$name' ok" );
141 }
142 }
143  
144 0 ▶sub run_setup_for {
Subroutine run_setup_for: not called CC=4 SCAR=30.0
Pod undocumented
145 0 my ( $class, $run_action ) = @_;
146 0 ▶ unless ( $DEP ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DEP) 0 0 0
147 0 oi_error "Before running specific setup actions you must ",
148 "first initialize the OpenInteract2::Context object ",
149 "with 'OpenInteract2::Context->create(...)'";
150 }
151 0 ▶ my $actions = $DEP->dependent_on( $run_action ) || [];
Condition0%0/2
$DEP->dependent_on($run_action) || []
l!l
00
MC/DC0%0/1
$DEP->dependent_on($run_action) || []
$DEP->dependent_on($run_action)
Truth table0%0/2
A
A= $DEP->dependent_on($run_action)
Aresult
11
00
152 0 unshift @{ $actions }, $run_action;
153 $log->info( "Asked to run setup for '$run_action', will execute ",
154 0 "actions (including deps): ", join( ', ', @{ $actions } ) );
155 0 foreach my $action ( @{ $actions } ) {
156 0 my $setup = OpenInteract2::Setup->new( $action );
157 0 $setup->run( CTX );
158 }
159 }
160  
161 1 ▶sub remove_skip_actions {
Subroutine remove_skip_actions: called CC=1 SCAR=0.0
Pod undocumented
162 1 my ( $class, @to_skip_items ) = @_;
163 1 return $DEP->without( @to_skip_items );
164 }
165  
166 87 OpenInteract2::Util->find_factory_subclasses(
167 'OpenInteract2::Setup', @INC
168 );
169 87 $DEFAULT_DEPENDENCY = OpenInteract2::Setup::ReadServerConfig->get_name;
170  
171 # This subroutine has been submitted as a patch to A::D but not yet
172 # accepted/released; once it is we'll just change the dependency
173 # version in Build.PL/Makefile.PL and get rid of this.
174  
175 {
176 87 require Algorithm::Dependency;
177 87 ▶ if ( Algorithm::Dependency->VERSION <= 1.03 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Algorithm::Dependency"->VERSION <= "1.03") 0 87 87
178 0 eval <<'WITHOUT';
179  
180 sub Algorithm::Dependency::without {
181 my $self = shift;
182 my @without = @_;
183 my $all_items = $self->schedule_all();
184 unless ( scalar @without ) {
185 return $all_items;
186 }
187 my %to_skip = map { $_ => 1 } @without;
188 my @good_items = ();
189  
190 ITEM:
191 foreach my $item ( @{ $all_items } ) {
192 next ITEM if ( $to_skip{ $item } );
193 my $all_item_dep = $self->depends( $item );
194 foreach my $item_dep ( @{ $all_item_dep } ) {
195 next ITEM if ( $to_skip{ $item_dep } );
196 }
197 push @good_items, $item;
198 }
199 return \@good_items;
200 }
201  
202 sub Algorithm::Dependency::dependent_on {
203 my $self = shift;
204 my $parent = shift;
205  
206 my $all_items = $self->schedule_all();
207 my @deps = ();
208  
209 foreach my $item ( @{ $all_items } ) {
210 next if ( $item eq $parent );
211 my $all_item_dep = $self->depends( $item );
212 foreach my $item_dep ( @{ $all_item_dep } ) {
213 push @deps, $item if ( $item_dep eq $parent );
214 }
215 }
216 return \@deps;
217 }
218  
219 WITHOUT
220  
221 }
222 }
223  
224 87 1;
225  
226 __END__
« OpenInteract2/SessionManager/DBI.pm ↑ Summary OpenInteract2/ContentGenerator.pm »