OpenInteract2/Config/Bootstrap.pm

filter: Statementstmt 78.3% 83 / 106 Branchbran 43.7% 14 / 32 Conditioncond 28.5% 6 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 94.1% 16 / 17 Podpod 14.2% 1 / 7 totaltotal 61.2% 120 / 196 CC 26 SCAR 22.1
CC
26
Cov
65%
new
21.6
save_config
20.0
read_config
5.8
CRAP 9.1
« OpenInteract2/Setup/InitializeActions.pm ↑ Summary OpenInteract2/Manage/Package/CreatePackage.pm »
1 package OpenInteract2::Config::Bootstrap;
2  
3 # $Id: Bootstrap.pm,v 1.2 2005/03/18 04:09:50 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use base qw( Exporter Class::Accessor::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use File::Spec::Functions qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Config::Ini;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Constants qw( :log BOOTSTRAP_CONF_DIR BOOTSTRAP_CONF_FILE );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 87 $OpenInteract2::Config::Bootstrap::VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
16  
17 87 my ( $log );
18  
19 87 my @CONFIG_FIELDS = qw( website_dir temp_lib_dir package_dir
20 config_type config_class config_dir config_file );
21 87 my @FIELDS = ( @CONFIG_FIELDS, 'filename' );
22 87 OpenInteract2::Config::Bootstrap->mk_accessors( @FIELDS );
23  
24 ########################################
25 # CLASS METHODS
26  
27 1 ▶sub new {
Subroutine new: called CC=10 SCAR=30.7
Pod documented
28 1 my ( $class, $params ) = @_;
29 1 ▶ $log ||= get_logger( LOG_CONFIG );
Condition33%1/3
$log ||= get_logger("OI2.CONFIG")
l!l&&r!l&&!r
010
MC/DC0%0/2
$log ||= get_logger("OI2.CONFIG")
$log get_logger("OI2.CONFIG")
Truth table33%1/3
A ||= B
A= $logB= get_logger("OI2.CONFIG")
ABresult
1X1
011
000
30  
31 1 my $self = bless( {}, $class );
32  
33 1 my $filename = $params->{filename};
34 1 my $website_dir = $params->{website_dir};
35 1 ▶ if ( ! $filename and $website_dir ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $filename and $website_dir) { } 1 0 1
elsif ($filename and not $website_dir) { } 0 0 0
Condition33%1/3
not $filename and $website_dir
!ll&&!rl&&r
001
MC/DC0%0/2
not $filename and $website_dir
not $filename $website_dir
Truth table33%1/3
not(A) and B
A= not $filenameB= $website_dir
ABresult
0X0
100
111
36 1 $filename = $self->create_website_filename( $website_dir );
37 1 $self->website_dir( $website_dir );
38 }
39  
40 # If the last directory of the specified file is 'conf', assume
41 # that everything else is the website dir
42  
43 ▶ elsif ( $filename and ! $website_dir ) {
Condition0%0/3
$filename and not $website_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$filename and not $website_dir
$filename not $website_dir
Truth table0%0/3
A and not(B)
A= $filenameB= not $website_dir
ABresult
0X0
100
111
44 0 $filename = rel2abs( $filename );
45 0 my @dirs = splitdir( dirname( $filename ) );
46 0 ▶ if ( $dirs[-1] eq BOOTSTRAP_CONF_DIR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirs[-1] eq "conf") 0 0 0
47 0 pop @dirs;
48 0 $self->website_dir( catdir( @dirs ) );
49 }
50 }
51  
52 1 ▶ if ( $filename and -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename and -f $filename) 1 0 1
Condition33%1/3
$filename and -f $filename
!ll&&!rl&&r
001
MC/DC0%0/2
$filename and -f $filename
$filename -f $filename
Truth table33%1/3
A and B
A= $filenameB= -f $filename
ABresult
0X0
100
111
53 1 $params = $self->read_config( $filename );
54 1 $self->filename( $filename );
55 }
56 1 ▶ unless ( $self->config_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config_dir) 1 0 1
57 1 $self->config_dir( BOOTSTRAP_CONF_DIR );
58 }
59 1 $self->_initialize( $params );
60 1 $log->debug( "Read bootstrap configuration ok, returning" );
61 1 return $self;
62 }
63  
64  
65 1 ▶sub read_config {
Subroutine read_config: called CC=5 SCAR=17.6
Pod undocumented
66 1 my ( $class, $filename ) = @_;
67 1 ▶ $log ||= get_logger( LOG_CONFIG );
Condition33%1/3
$log ||= get_logger("OI2.CONFIG")
l!l&&r!l&&!r
100
MC/DC0%0/2
$log ||= get_logger("OI2.CONFIG")
$log get_logger("OI2.CONFIG")
Truth table33%1/3
A ||= B
A= $logB= get_logger("OI2.CONFIG")
ABresult
1X1
011
000
68 1 ▶ unless ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 0 1 1
69 0 my $msg = "Cannot open '$filename' for bootstrap server " .
70 "configuration: file does not exist";
71 0 $log->fatal( $msg );
72 0 oi_error $msg;
73 }
74 1 my $ini = OpenInteract2::Config::Ini->new({ filename => $filename });
75 1 ▶ return {} unless ( $ini->{bootstrap} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ini->{"bootstrap"} 0 1 1
76 1 my %params = ();
77 1 ▶ while ( my ( $key, $value ) = each %{ $ini->{bootstrap} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$ini{"bootstrap"};}) 7 1 8
78 7 $params{ $key } = $value;
79 }
80 1 return \%params;
81 }
82  
83 1 ▶sub create_website_filename {
Subroutine create_website_filename: called CC=2 SCAR=7.6
Pod undocumented
84 1 my ( $class, $dir ) = @_;
85 1 ▶ unless ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 0 1 1
86 0 oi_error "Must pass in website directory to create ",
87 "bootstrap filename";
88 }
89 1 return $class->create_filename( catdir( $dir, BOOTSTRAP_CONF_DIR ) );
90 }
91  
92 1 ▶sub create_filename {
Subroutine create_filename: called CC=2 SCAR=7.6
Pod undocumented
93 1 my ( $class, $dir ) = @_;
94 1 ▶ unless ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 0 1 1
95 0 oi_error "Must pass in directory to create bootstrap filename";
96 }
97 1 return catfile( $dir, BOOTSTRAP_CONF_FILE );
98 }
99  
100  
101 ########################################
102 # OBJECT METHODS
103  
104 1 ▶sub _initialize {
Subroutine _initialize: called CC=4 SCAR=13.9
105 1 my ( $self, $params ) = @_;
106 1 foreach my $field ( @CONFIG_FIELDS ) {
107 7 ▶ if ( $params->{ $field } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{$field}) 7 0 7
108 7 $self->$field( $params->{ $field } );
109 7 ▶ if ( $field =~ /dir$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /dir$/) 4 3 7
110 4 $self->clean_dir( $field );
111 }
112 }
113 }
114 1 return $self;
115 }
116  
117  
118 6 ▶sub clean_dir {
Subroutine clean_dir: called CC=2 SCAR=7.0
Pod undocumented
119 6 my ( $self, $prop ) = @_;
120 6 my $dir = $self->$prop();
121 6 ▶ if ( $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 6 0 6
122 6 $dir =~ s|/$||;
123 6 $self->$prop( $dir );
124 }
125 6 return $dir;
126 }
127  
128  
129 1 ▶sub get_server_config_file {
Subroutine get_server_config_file: called CC=4 SCAR=16.6
Pod undocumented
130 1 my ( $self ) = @_;
131 1 ▶ unless ( $self->website_dir and $self->config_dir and
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->website_dir and $self->config_dir and $self->config_file) 0 1 1
Condition33%2/6
$self->website_dir and $self->config_dir and $self->config_file
!ll&&!rl&&r
001
$self->website_dir and $self->config_dir
!ll&&!rl&&r
001
MC/DC0%0/3
$self->website_dir and $self->config_dir and $self->config_file
$self->website_dir $self->config_dir $self->config_file
Truth table25%1/4
A and B and C
A= $self->website_dirB= $self->config_dirC= $self->config_file
ABCresult
0XX0
10X0
1100
1111
132 $self->config_file ) {
133 0 oi_error sprintf(
134 "Properties 'website_dir', 'config_dir' and 'config_file' must " .
135 "be defined to retrieve the config filename. Currently assigned " .
136 "as '%s', '%s' and '%s'",
137 $self->website_dir, $self->config_dir, $self->config_file
138 );
139 }
140 1 $self->clean_dir( 'website_dir' );
141 1 $self->clean_dir( 'config_dir' );
142 1 return catfile( $self->website_dir,
143 $self->config_dir,
144 $self->config_file );
145 }
146  
147  
148 0 ▶sub save_config {
Subroutine save_config: not called CC=4 SCAR=30.0
Pod undocumented
149 0 my ( $self, ) = @_;
150  
151 # First ensure required fields are set
152 0 my @empty_fields = grep { ! $self->$_() } @CONFIG_FIELDS;
153 0 ▶ if ( scalar @empty_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @empty_fields) 0 0 0
154 0 oi_error "Cannot save bootstrap config: the following fields ",
155 "must be defined: ", join( ", ", @empty_fields );
156 }
157  
158 # If no filename create one from the website_dir
159 0 ▶ unless ( $self->filename() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filename) 0 0 0
160 0 $self->filename(
161 $self->create_website_filename( $self->website_dir )
162 );
163 }
164  
165 # Now store data into the INI and write it out
166 0 my $ini = OpenInteract2::Config::Ini->new();
167 0 foreach my $field ( @CONFIG_FIELDS ) {
168 0 $ini->set( 'bootstrap', $field, $self->$field() );
169 }
170 0 $ini->write_file( $self->filename() );
171 0 return $self->filename();
172 }
173  
174 87 1;
175  
176 __END__
« OpenInteract2/Setup/InitializeActions.pm ↑ Summary OpenInteract2/Manage/Package/CreatePackage.pm »