blib/lib/App/CELL/Load.pm

filter: Statementstmt 88.3% 272 / 308 Branchbran 75.0% 87 / 116 Conditioncond 55.1% 16 / 29 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 96.5% 28 / 29 Podpod 100% 7 / 7 totaltotal 82.1% 415 / 505 CC 75 SCAR 23.7
CC
75
Cov
83%
init
24.6
get_sitedir
12.7
__ANON__
11.6
CRAP 10.7
« blib/lib/App/CELL/Config.pm ↑ Summary blib/lib/App/CELL/Log.pm »
1 # *************************************************************************
2 # Copyright (c) 2014-2020, SUSE LLC
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 # *************************************************************************
32  
33 package App::CELL::Load;
34  
35 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 15 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 15 ▶use App::CELL::Config qw( $meta $core $site );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 15 ▶use App::CELL::Log qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
41 15 ▶use App::CELL::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 15 ▶use App::CELL::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 15 ▶use App::CELL::Test qw( cmp_arrays );
Subroutine BEGIN: called CC=1 SCAR=0.0
44 15 ▶use App::CELL::Util qw( stringify_args is_directory_viable );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 15 ▶use File::Next;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 15 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 15 ▶use Params::Validate qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 =head1 NAME
51  
52 App::CELL::Load -- find and load message files and config files
53  
54  
55  
56 =head1 SYNOPSIS
57
58 use App::CELL::Load;
59  
60 # Load App::CELL's internal messages and config params and then
61 # attempt to load the application's messages and config params
62 $status = App::CELL::Load::init();
63 return $status if $status->not_ok;
64  
65 # attempt to determine the site configuration directory
66 my $resulthash = App::CELL::Load::get_sitedir();
67  
68 # get a reference to a list of configuration files (full paths) of a
69 # given type under a given directory
70 my $metafiles = App::CELL::Load::find_files( '/etc/CELL', 'meta' );
71
72 # load messages from all message file in a given directory and all its
73 # subdirectories
74 $status = message_files( '/etc/CELL' );
75  
76 # load meta, core, and site params from all meta, core, and site
77 # configuration files in a given directory and all its subdirectories
78 $status = meta_core_site_files( '/etc/CELL' );
79  
80  
81  
82 =head1 DESCRIPTION
83  
84 The purpose of the App::CELL::Load module is to provide message and config
85 file finding and loading functionality to the App::CELL::Message and
86 App::CELL::Config modules.
87  
88  
89  
90 =head1 PACKAGE VARIABLES
91  
92 This module provides the following package variables
93  
94 =over
95  
96 =item C<$sharedir> - the full path of the sharedir
97  
98 =item C<$sharedir_loaded> - whether it has been loaded or not
99  
100 =item C<@sitedir> - the full path of the site configuration directory
101  
102 =back
103  
104 =cut
105  
106 15 our $sharedir = '';
107 15 our $sharedir_loaded = 0;
108 15 our @sitedir = ();
109  
110  
111 =head1 MODULES
112  
113 =head2 init
114  
115 Idempotent initialization function.
116  
117 Optionally takes a PARAMHASH. The following arguments are recognized:
118  
119 =over
120  
121 =item C<sitedir> -- full path to the/a site dir
122  
123 =item C<enviro> -- name of environment variable containing sitedir path
124  
125 =item C<verbose> -- increase logging verbosity of the load routine
126  
127 =back
128  
129 E.g.:
130  
131 my $status = App::CELL::Load::init(
132 sitedir => '/etc/foo',
133 verbose => 1
134 );
135  
136 See L<App::CELL::Guide> for details.
137  
138 =cut
139  
140 13 ▶sub init {
Subroutine init: called CC=21 SCAR=32.0
Pod documented
141 13 my %ARGS = validate( @_, {
142 enviro => { type => SCALAR, optional => 1 },
143 sitedir => { type => SCALAR, optional => 1 },
144 verbose => { type => SCALAR, default => 0 },
145 } );
146  
147 # determine verbosity level
148 13 my $args_string;
149 13 ▶ if ( %ARGS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%ARGS) { } 13 0 13
150 13 $args_string = "with arguments: " . stringify_args( \%ARGS );
151 } else {
152 0 $args_string = "without arguments";
153 }
154 13 ▶ $meta->set('CELL_META_LOAD_VERBOSE', $ARGS{'verbose'} || 0);
Condition100%2/2
$ARGS{'verbose'} || 0
l!l
67
MC/DC100%1/1
$ARGS{'verbose'} || 0
$ARGS{'verbose'}
Truth table100%2/2
A
A= $ARGS{'verbose'}
Aresult
11
00
155  
156 13 ▶ $log->info(
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 6 7 13
157 "Entering App::CELL::Load::init from " . (caller)[0] . " $args_string",
158 cell => 1
159 ) if $meta->CELL_META_LOAD_VERBOSE;
160  
161 # check for taint mode
162 13 ▶ if ( ${^TAINT} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${^TAINT} != 0) 0 13 13
163 0 return App::CELL::Status->new( level => "FATAL",
164 code => "Attempt to load while in taint mode (-T)" );
165 }
166  
167 # look up sharedir
168 13 ▶ if ( not $sharedir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($App::CELL::Load::sharedir) 9 4 13
169 9 my $tmp_sharedir = File::ShareDir::dist_dir('App-CELL');
170 9 ▶ if ( ! is_directory_viable( $tmp_sharedir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_directory_viable($tmp_sharedir)) 0 9 9
171 0 return App::CELL::Status->new(
172 level => 'ERR',
173 code => 'CELL_SHAREDIR_NOT_VIABLE',
174 args => [ $tmp_sharedir, $App::CELL::Util::not_viable_reason ],
175 );
176 }
177 9 ▶ $log->info( "Found viable CELL configuration directory " .
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 6 3 9
178 $tmp_sharedir . " in App::CELL distro", cell => 1 ) if $meta->CELL_META_LOAD_VERBOSE;
179 9 $site->set( 'CELL_SHAREDIR_FULLPATH', $tmp_sharedir );
180 9 $sharedir = $tmp_sharedir;
181 }
182  
183 # walk sharedir
184 13 ▶ if ( $sharedir and not $sharedir_loaded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::CELL::Load::sharedir and not $App::CELL::Load::sharedir_loaded) 9 4 13
Condition66%2/3
$App::CELL::Load::sharedir and not $App::CELL::Load::sharedir_loaded
!ll&&!rl&&r
049
MC/DC50%1/2
$App::CELL::Load::sharedir and not $App::CELL::Load::sharedir_loaded
$App::CELL::Load::sharedir not $App::CELL::Load::sharedir_loaded
Truth table66%2/3
A and not(B)
A= $App::CELL::Load::sharedirB= not $App::CELL::Load::sharedir_loaded
ABresult
0X0
100
111
185 9 my $status = message_files( $sharedir );
186 9 my $load_status = _report_load_status( $sharedir, 'sharedir', 'message', $status );
187 9 ▶ return $load_status if $load_status->not_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $load_status->not_ok 0 9 9
188 9 $status = meta_core_site_files( $sharedir );
189 9 $load_status = _report_load_status( $sharedir, 'sharedir', 'config params', $status );
190 9 ▶ return $load_status if $load_status->not_ok;
Branch: 1 branch
BranchTrueFalseTotal
if $load_status->not_ok 0 9 9
191 9 $site->set( 'CELL_SHAREDIR_LOADED', 1 );
192 9 $sharedir_loaded = 1;
193 }
194  
195 13 ▶ if ( $meta->CELL_META_LOAD_VERBOSE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::CELL::Load::meta->CELL_META_LOAD_VERBOSE) 6 7 13
196 6 ▶ if ( @sitedir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@App::CELL::Load::sitedir) { } 0 6 6
197 0 $log->debug( "sitedir package variable contains ->" .
198 join( ':', @sitedir ) . "<-", cell => 1 );
199 } else {
200 6 $log->debug( "sitedir package variable is empty", cell => 1 );
201 }
202 }
203  
204 # get sitedir from args or environment
205 13 my $status = get_sitedir( %ARGS );
206 13 ▶ return $status unless $status->ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $status->ok 11 2 13
207 2 my $sitedir_candidate = $status->payload;
208  
209 # walk sitedir
210 2 ▶ if ( $sitedir_candidate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sitedir_candidate) 2 0 2
211 2 my $status = message_files( $sitedir_candidate );
212 2 my $messages_loaded = _report_load_status( $sitedir_candidate, 'sitedir', 'message', $status );
213 2 $status = meta_core_site_files( $sitedir_candidate );
214 2 my $params_loaded = _report_load_status( $sitedir_candidate, 'sitedir', 'config params', $status );
215 #
216 # sitedir candidate is accepted only if something is actually
217 # loaded
218 #
219 2 ▶ if ( $messages_loaded->ok or $params_loaded->ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($messages_loaded->ok or $params_loaded->ok) 2 0 2
Condition33%1/3
$messages_loaded->ok or $params_loaded->ok
l!l&&r!l&&!r
200
MC/DC0%0/2
$messages_loaded->ok or $params_loaded->ok
$messages_loaded->ok $params_loaded->ok
Truth table33%1/3
A or B
A= $messages_loaded->okB= $params_loaded->ok
ABresult
1X1
011
000
220 2 $meta->set( 'CELL_META_SITEDIR_LOADED',
221 ( $meta->CELL_META_SITEDIR_LOADED + 1 ) );
222 2 push @sitedir, $sitedir_candidate;
223 2 $meta->set( 'CELL_META_SITEDIR_LIST', \@sitedir );
224 }
225 }
226  
227 # check that at least sharedir has really been loaded
228 SANITY: {
229 2 my $results = [];
230  
231 # remember, message constructor returns a status object
232 2 my $status = App::CELL::Message->new( code => 'CELL_LOAD_SANITY_MESSAGE' );
233  
234 2 ▶ if ( $status->ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->ok) 2 0 2
235 2 my $msgobj = $status->payload;
236 2 push @$results, (
237 $meta->CELL_LOAD_SANITY_META,
238 $core->CELL_LOAD_SANITY_CORE,
239 $site->CELL_LOAD_SANITY_SITE,
240 $msgobj->text(),
241 );
242 2 my $cmp_arrays_result = cmp_arrays(
243 $results,
244 [ 'Baz', 'Bar', 'Foo', 'This is a sanity testing message' ],
245 );
246 2 ▶ last SANITY if $cmp_arrays_result;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp_arrays_result 2 0 2
247 }
248 0 return App::CELL::Status->new(
249 level => 'ERR',
250 code => 'CELL_LOAD_FAILED_SANITY',
251 );
252 }
253
254 2 ▶ $log->debug( "Leaving App::CELL::Load::init", cell => 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 0 2 2
255 if $meta->CELL_META_LOAD_VERBOSE;
256  
257 2 return App::CELL::Status->ok;
258 }
259  
260  
261 22 ▶sub _report_load_status {
Subroutine _report_load_status: called CC=7 SCAR=20.1
262 22 my ( $dir_path, $dir_desc, $what, $status ) = @_;
263 22 my $return_status = App::CELL::Status->ok;
264 22 ▶ my $quantitems = ${ $status->payload }{quantitems} || 0;
Condition50%1/2
do { $status->payload }->{'quantitems'} || 0
l!l
220
MC/DC0%0/1
do { $status->payload }->{'quantitems'} || 0
do { $status->payload }->{'quantitems'}
Truth table50%1/2
A
A= do { $status->payload }->{'quantitems'}
Aresult
11
00
265 22 ▶ my $quantfiles = ${ $status->payload }{quantfiles} || 0;
Condition50%1/2
do { $status->payload }->{'quantfiles'} || 0
l!l
220
MC/DC0%0/1
do { $status->payload }->{'quantfiles'} || 0
do { $status->payload }->{'quantfiles'}
Truth table50%1/2
A
A= do { $status->payload }->{'quantfiles'}
Aresult
11
00
266 22 ▶ if ( $quantitems == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quantitems == 0) 0 22 22
267 0 $return_status = App::CELL::Status->new(
268 level => 'WARN',
269 code => 'CELL_DIR_WALKED_NOTHING_FOUND',
270 args => [ $what, $dir_desc, $dir_path, $quantfiles ],
271 caller => [ CORE::caller() ],
272 cell => 1,
273 );
274 }
275 # trigger a log message: note that we can't use an OK status here
276 # because log messages for those are suppressed
277 App::CELL::Status->new (
278 22 ▶ level => 'INFO',
Branch: 1 branch
BranchTrueFalseTotal
if $dir_desc eq "sitedir" or $dir_desc eq "sharedir" and $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 16 6 22
Condition83%5/6
$dir_desc eq "sitedir" or $dir_desc eq "sharedir" and $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
l!l&&r!l&&!r
4126
$dir_desc eq "sharedir" and $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
!ll&&!rl&&r
0612
MC/DC66%2/3
$dir_desc eq "sitedir" or $dir_desc eq "sharedir" and $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
$dir_desc eq "sitedir" $dir_desc eq "sharedir" $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
Truth table75%3/4
A or B and C
A= $dir_desc eq "sitedir"B= $dir_desc eq "sharedir"C= $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
ABCresult
1XX1
0111
00X0
0100
279 code => 'CELL_DIR_WALKED_ITEMS_LOADED',
280 args => [ $quantitems, $what, $quantfiles, $dir_desc, $dir_path ],
281 caller => [ CORE::caller() ],
282 cell => 1,
283 ) if ( $dir_desc eq 'sitedir' ) or ( $dir_desc eq 'sharedir' and $meta->CELL_META_LOAD_VERBOSE );
284 22 return $return_status;
285 }
286  
287 =head2 message_files
288  
289 Loads message files from the given directory. Takes: full path to
290 configuration directory. Returns: result hash containing 'quantfiles'
291 (total number of files processed) and 'count' (total number of
292 messages loaded).
293  
294 =cut
295  
296 11 ▶sub message_files {
Subroutine message_files: called CC=6 SCAR=18.7
Pod documented
297  
298 11 my $confdir = shift;
299 11 my %reshash;
300 11 $reshash{quantfiles} = 0;
301 11 $reshash{quantitems} = 0;
302  
303 11 my $file_list = find_files( 'message', $confdir );
304  
305 11 ▶ if ( @$file_list ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$file_list) { } 11 0 11
306 11 ▶ $log->info( "Found message files: " . join( ',', @$file_list ),
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 6 5 11
307 cell => 1 ) if $meta->CELL_META_LOAD_VERBOSE;
308 } else {
309 0 ▶ $log->warn( "No message files found in $confdir", cell => 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 0 0 0
310 if $meta->CELL_META_LOAD_VERBOSE;
311 }
312  
313 11 foreach my $file ( @$file_list ) {
314 12 $reshash{quantfiles} += 1;
315 12 ▶ die "INTERNAL ERROR (App::CELL::Message::mesg is not a reference)" if not ref( $App::CELL::Message::mesg );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $App::CELL::Message::mesg 0 12 12
316 12 $reshash{quantitems} += parse_message_file(
317 File => $file,
318 Dest => $App::CELL::Message::mesg,
319 );
320 }
321  
322 11 return App::CELL::Status->new(
323 level => 'OK',
324 payload => \%reshash,
325 );
326 }
327  
328  
329 =head2 meta_core_site_files
330  
331 Loads meta, core, and site config files from the given directory. Takes:
332 full path to configuration directory. Returns: result hash containing
333 'quantfiles' (total number of files processed) and 'count' (total number of
334 configuration parameters loaded).
335  
336 =cut
337  
338 11 ▶sub meta_core_site_files {
Subroutine meta_core_site_files: called CC=3 SCAR=11.0
Pod documented
339  
340 11 my $confdir = shift;
341 11 my %reshash;
342 11 $reshash{quantfiles} = 0;
343 11 $reshash{quantitems} = 0;
344  
345 11 foreach my $type ( 'meta', 'core', 'site' ) {
346 33 my $fulltype = 'App::CELL::Config::' . $type;
347 #$log->debug( "\$fulltype is $fulltype", cell => 1 );
348 33 my $file_list = find_files( $type, $confdir );
349 33 foreach my $file ( @$file_list ) {
350 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
351 29 $reshash{quantfiles} += 1;
352 29 $reshash{quantitems} += parse_config_file(
353 File => $file,
354 Dest => $$fulltype,
355 );
356 }
357 }
358  
359 11 return App::CELL::Status->new(
360 level => 'OK',
361 payload => \%reshash,
362 );
363 }
364  
365  
366 =head2 get_sitedir
367  
368 This function implements the algorithm described in
369 L<App::CELL::Guide/Sitedir search algorithm> to find a sitedir candidate.
370 configuration directory.
371  
372 On success -- i.e., as soon as the algorithm finds a viable sitedir
373 candidate -- the sitedir (full path) is added to CELL_META_SITEDIR_LIST and
374 an OK status object is returned, with the sitedir in the payload.
375  
376 On failure, the function returns an ERR or WARN status object containing
377 a description of what went wrong.
378  
379 =cut
380  
381 13 ▶sub get_sitedir {
Subroutine get_sitedir: called CC=10 SCAR=25.4
Pod documented
382  
383 13 my %paramhash = @_;
384 13 my $reason;
385  
386 13 my ( $sitedir, $log_message, $status );
387 GET_CANDIDATE_DIR: {
388  
389 # look in paramhash for sitedir
390 13 $log->debug( "SITEDIR SEARCH, ROUND 1 (sitedir parameter):", cell => 1 );
391 13 ▶ if ( $sitedir = $paramhash{sitedir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sitedir = $paramhash{"sitedir"}) 3 10 13
392 3 $log_message = "Viable sitedir passed as argument";
393 3 ▶ last GET_CANDIDATE_DIR if is_directory_viable( $sitedir );
Branch: 1 branch
BranchTrueFalseTotal
if is_directory_viable($sitedir) 2 1 3
394 1 $reason = "CELL load routine received 'sitedir' argument ->$sitedir<- " .
395 "but this is not a viable directory ($App::CELL::Util::not_viable_reason)";
396 1 $log->err( $reason, cell => 1 );
397 1 return App::CELL::Status->new( level => 'ERR', code => $reason );
398 }
399 10 $log->debug( "looked at function arguments but they do not " .
400 "contain a literal site dir path", cell => 1 );
401  
402 # look in paramhash for name of environment variable
403 10 $log->debug( "SITEDIR SEARCH, ROUND 2 (enviro parameter):", cell => 1 );
404 10 ▶ if ( $paramhash{enviro} )
Branch: 1 branch
BranchTrueFalseTotal
if ($paramhash{"enviro"}) 1 9 10
405 {
406 1 ▶ if ( $sitedir = $ENV{ $paramhash{enviro} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sitedir = $ENV{$paramhash{'enviro'}}) { } 0 1 1
407 $log_message = "Found viable sitedir in " . $paramhash{enviro}
408 0 . " environment variable";
409 0 ▶ last GET_CANDIDATE_DIR if is_directory_viable( $sitedir );
Branch: 1 branch
BranchTrueFalseTotal
if is_directory_viable($sitedir) 0 0 0
410 0 $reason = "CELL load routine received 'enviro' argument ->$paramhash{enviro}<- " .
411 "which expanded to ->$sitedir<- but this is not a viable directory " .
412 "($App::CELL::Util::not_viable_reason)";
413 0 return App::CELL::Status->new( level => 'ERR', code => $reason );
414 } else {
415 1 $reason = "CELL load routine: enviro argument contained ->$paramhash{enviro}<- " .
416 "but no such variable found in the environment";
417 1 return App::CELL::Status->new( level => 'ERR', code => $reason );
418 }
419 }
420  
421 # fall back to hard-coded environment variable
422 9 $log->debug( "SITEDIR SEARCH, ROUND 3 (fallback to CELL_SITEDIR " .
423 "environment variable):", cell => 1 );
424 9 $sitedir = undef;
425 9 ▶ if ( $sitedir = $ENV{ 'CELL_SITEDIR' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sitedir = $ENV{"CELL_SITEDIR"}) 1 8 9
426 1 $log_message = "Found viable sitedir in CELL_SITEDIR environment variable";
427 1 ▶ last GET_CANDIDATE_DIR if is_directory_viable( $sitedir );
Branch: 1 branch
BranchTrueFalseTotal
if is_directory_viable($sitedir) 0 1 1
428 1 $reason = "CELL load routine: no 'sitedir', 'enviro' arguments specified; " .
429 "fell back to CELL_SITEDIR environment variable, which exists " .
430 "with value ->$sitedir<- but this is not a viable directory" .
431 "($App::CELL::Util::not_viable_reason)";
432 1 ▶ if ( $meta->CELL_META_SITEDIR_LOADED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::CELL::Load::meta->CELL_META_SITEDIR_LOADED) 0 1 1
433 0 $log->warn( $reason, cell => 1 );
434 $log->notice( "The following sitedirs have been loaded already " .
435 0 join( ' ', @{ $meta->CELL_META_SITEDIR_LIST }),
436 cell => 1 );
437 0 return App::CELL::Status->ok;
438 }
439 1 return App::CELL::Status->new( level => 'WARN', code => $reason );
440 }
441
442 # failed to find a sitedir
443 8 $reason = "CELL load routine gave up (no sitedir argument, no enviro " .
444 "argument, no CELL_SITEDIR environment variable)";
445 8 ▶ if ( $meta->CELL_META_SITEDIR_LOADED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::CELL::Load::meta->CELL_META_SITEDIR_LOADED) 0 8 8
446 0 $log->warn( $reason, cell => 1 );
447 $log->notice( "The following sitedirs have been loaded already " .
448 0 join( ' ', @{ $meta->CELL_META_SITEDIR_LIST } ),
449 cell => 1 );
450 0 return App::CELL::Status->ok;
451 }
452 8 return App::CELL::Status->new( level => 'WARN', code => $reason );
453 }
454  
455 # SUCCEED
456 2 $log->info( $log_message, cell => 1 );
457 2 return App::CELL::Status->ok( $sitedir );
458 }
459  
460  
461 =head2 find_files
462  
463 Takes two arguments: full directory path and config file type.
464  
465 Always returns an array reference. On "failure", the array reference will
466 be empty.
467  
468 How it works: first, the function checks a state variable to see if the
469 "work" of walking the configuration directory has already been done. If
470 so, then the function simply returns the corresponding array reference from
471 its cache (the state hash C<%resultlist>). If this is the first invocation
472 for this directory, the function walks the directory (and all its
473 subdirectories) to find files matching one of the four regular expressions
474 corresponding to the four types of configuration files('meta', 'core',
475 'site', 'message'). For each matching file, the full path is pushed onto
476 the corresponding array in the cache.
477  
478 Note that there is a ceiling on the number of files that will be considered
479 while walking the directory tree. This ceiling is defined in the package
480 variable C<$max_files> (see below).
481  
482 =cut
483  
484 # regular expressions for each file type
485 15 our $typeregex = {
486 'meta' => qr/^.+_MetaConfig.pm$/ ,
487 'core' => qr/^.+_Config.pm$/ ,
488 'site' => qr/^.+_SiteConfig.pm$/ ,
489 'message' => qr/^.+_Message(_[^_]+){0,1}.conf$/ ,
490 };
491  
492 # C<$max_files> puts a limit on how many files we will look at in our directory
493 # tree walk
494 15 our $max_files = 1000;
495  
496 50 ▶sub find_files {
Subroutine find_files: called CC=11 SCAR=24.2
Pod documented
497 50 my ( $type, $dirpath ) = @_;
498  
499 #
500 # FIXME: convert $dirpath into an absolute path so it's always the same
501 #
502  
503 # re-entrant function
504 15 ▶ use feature "state";
Subroutine BEGIN: called CC=1 SCAR=0.0
505 50 state $resultcache = {};
506  
507 # If $dirpath key exists in %resultcache, we are re-entering.
508 # In other words, $dirpath has already been walked and all the
509 # filepaths are already in the array stored within %resultcache
510 50 ▶ if ( exists $resultcache->{ $dirpath } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $resultcache->{$dirpath}) { } 38 12 50
511 38 ▶ $log->debug( "Re-entering find_files for $dirpath (type '$type')",
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 18 20 38
512 cell => 1) if $meta->CELL_META_LOAD_VERBOSE;
513 38 return $resultcache->{ $dirpath }->{ $type };
514 } else { # create it
515 12 $resultcache->{ $dirpath } = {
516 'meta' => [],
517 'core' => [],
518 'site' => [],
519 'message' => [],
520 };
521 }
522  
523 # walk the directory (do we need some error checking here?)
524 12 $log->debug( "Preparing to walk $dirpath", cell => 1 );
525 12 my $iter = File::Next::files( $dirpath );
526  
527 # while we are walking, go ahead and populate the result cache for _all
528 # four_ types (even though we were asked for just one type)
529 12 my $walk_counter = 0;
530 12 ▶ ITER_LOOP: while ( defined ( my $file = $iter->() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = &$iter())) 57 12 69
531 57 $log->debug( "Now considering $file", cell => 1 );
532 57 $walk_counter += 1;
533 57 ▶ if ( $walk_counter > $max_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($walk_counter > $App::CELL::Load::max_files) 0 57 57
534 0 App::CELL::Status->new (
535 level => 'ERROR',
536 code => 'Maximum number of configuration file candidates ->%s<- exceeded in %s',
537 args => [ $max_files, $dirpath ],
538 );
539 0 last ITER_LOOP; # stop looping if there are so many files
540 }
541 57 ▶ if ( not -r $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 57 57
542 0 App::CELL::Status->new (
543 level => 'WARN',
544 code => 'Load operation passed over file ->%s<- (not readable)',
545 args => [ $file ],
546 );
547 0 next ITER_LOOP; # jump to next file
548 }
549 # $file is now a "candidate"
550 57 my $counter = 0;
551 57 foreach my $type ( 'meta', 'core', 'site', 'message' ) {
552 163 ▶ if ( $file =~ /${ $typeregex }{ $type }/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /do { $App::CELL::Load::typeregex }->{$type}/u) 47 116 163
553 47 push @{ $resultcache->{ $dirpath}->{ $type } }, $file;
554 47 $counter += 1;
555 47 next ITER_LOOP;
556 }
557 }
558 10 ▶ $log->info( "Load operation passed over file $file (type not " .
Branch: 1 branch
BranchTrueFalseTotal
if not $counter and $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 6 4 10
Condition66%2/3
not $counter and $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
!ll&&!rl&&r
046
MC/DC50%1/2
not $counter and $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
not $counter $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
Truth table66%2/3
not(A) and B
A= not $counterB= $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE
ABresult
0X0
100
111
559 "recognized)", cell => 1 ) if not $counter and $meta->CELL_META_LOAD_VERBOSE;
560 }
561 12 ▶ $log->debug( "Returning " . join( ',', @{ $resultcache->{ $dirpath }->{ $type } } ),
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 6 6 12
562 cell => 1 ) if $meta->CELL_META_LOAD_VERBOSE;
563 12 return $resultcache->{ $dirpath }->{ $type };
564 }
565  
566  
567 =head2 parse_message_file
568  
569 This function is where message files are parsed. It takes a PARAMHASH
570 consisting of:
571  
572 =over
573  
574 =item C<File> - filename (full path)
575  
576 =item C<Dest> - hash reference (where to store the message templates).
577  
578 =back
579  
580 Returns: number of stanzas successfully parsed and loaded
581  
582 =cut
583  
584 13 ▶sub parse_message_file {
Subroutine parse_message_file: called CC=9 SCAR=22.3
Pod documented
585 13 my @ARGS = @_;
586 13 my %ARGS = (
587 'File' => undef,
588 'Dest' => undef,
589 @ARGS,
590 );
591  
592 184 ▶ my $process_stanza_sub = sub {
Subroutine __ANON__: called CC=9 SCAR=24.5
593  
594 # get arguments
595 184 my ( $file, $line, $lang, $stanza, $destref ) = @_;
596  
597 # put first token on first line into $code
598 184 my ( $code ) = $stanza->[0] =~ m/^\s*(\S+)/;
599 184 ▶ if ( not $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 184 184
600 0 $log->info(
601 "ERROR: Could not process stanza ->" . join( " ", @$stanza ) . "<- in $file",
602 cell => 1,
603 );
604 0 return 0;
605 }
606  
607 # The rest of the lines are the message template
608 184 my $text = '';
609 184 foreach ( @$stanza[1 .. $#{ $stanza }] ) {
610 193 chomp;
611 193 $text = $text . " " . $_;
612 }
613 184 $text =~ s/^\s+//g;
614 184 ▶ if ( $code and $lang and $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code and $lang and $text) 184 0 184
Condition33%2/6
$code and $lang and $text
!ll&&!rl&&r
00184
$code and $lang
!ll&&!rl&&r
00184
MC/DC0%0/3
$code and $lang and $text
$code $lang $text
Truth table25%1/4
A and B and C
A= $codeB= $langC= $text
ABCresult
0XX0
10X0
1100
1111
615 184 $log->debug(
616 "Parsed message CODE ->$code<- LANG ->$lang<- TEXT ->$text<-",
617 cell => 1,
618 );
619 # we have a candidate, but we don't want to overwrite
620 # an existing entry with the same $code-$lang pair
621 184 ▶ if ( $destref->{ $code }->{ $lang } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destref->{$code}{$lang}) { } 1 183 184
622 1 my $existing_text = $destref->{ $code }->{ $lang }->{ 'Text' };
623 $log->error(
624 "ERROR: not loading code-lang pair ->$code" .
625 "/$lang<- with text ->$text<- because this would" .
626 1 " overwrite existing pair from " . $destref->{$code}->{$lang}->{'File'},
627 cell => 1,
628 );
629 1 return 0;
630 } else {
631 183 ▶ $log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 114 69 183
632 "OK: loading code-lang pair ->$code/$lang<- with text ->$text<-",
633 cell => 1,
634 ) if $meta->CELL_META_LOAD_VERBOSE;
635 183 $destref->{ $code }->{ $lang } = {
636 'Text' => $text,
637 'File' => $file,
638 'Line' => $line,
639 };
640 183 return 1;
641 }
642 }
643 $log->error(
644 0 ▶ "Parsed " . ( $code || "<NO_CODE>" ) . " but something missing!!",
Condition0%0/2
$code || '<NO_CODE>'
l!l
00
MC/DC0%0/1
$code || '<NO_CODE>'
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
645 cell => 1,
646 );
647 0 return 0;
648 13 };
649  
650 # determine language from file name
651 13 my ( $lang ) = $ARGS{'File'} =~ m/_Message_([^_]+).conf$/;
652 13 ▶ if ( not $lang ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang) 1 12 13
653 1 $log->warn(
654 "Could not determine language from filename $ARGS{'File'} " .
655 "-- reverting to default language ->en<-",
656 cell => 1,
657 );
658 1 $lang = 'en';
659 }
660  
661 # open the file for reading
662 13 ▶ open( my $fh, "<", $ARGS{'File'} )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $ARGS{"File"} or die qq[cannot open < $ARGS{"File"}: $!] 13 0 13
663 or die "cannot open < $ARGS{'File'}: $!";
664  
665 13 my @stanza = ();
666 13 my $index = 0;
667 13 my $count = 0;
668 13 my $line = 0;
669 13 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1036 13 1049
670 1036 chomp( $_ );
671 1036 $line += 1;
672 #$log->debug( "Read line =>$_<= from $ARGS{'File'}", cell => 1 );
673 1036 ▶ $_ = '' if /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s+$/u 11 1025 1036
674 1036 ▶ if ( $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) { } 840 196 1036
675 840 ▶ if ( ! /^\s*#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s*#/u) 377 463 840
676 377 s/^\s*//g;
677 377 s/\s*$//g;
678 377 $stanza[ $index++ ] = $_;
679 }
680 } else {
681 $count += &$process_stanza_sub( $ARGS{'File'}, $line, $lang, \@stanza,
682 196 ▶ $ARGS{'Dest'} ) if @stanza;
Branch: 1 branch
BranchTrueFalseTotal
if @stanza 175 21 196
683 196 @stanza = ();
684 196 $index = 0;
685 }
686 }
687 # There might be one stanza left at the end
688 $count += &$process_stanza_sub( $ARGS{'File'}, $line, $lang, \@stanza,
689 13 ▶ $ARGS{'Dest'} ) if @stanza;
Branch: 1 branch
BranchTrueFalseTotal
if @stanza 9 4 13
690  
691 13 close $fh;
692  
693 # $log->info( "Parsed and loaded $count configuration stanzas "
694 # . "from $ARGS{'File'}", cell => 1 );
695
696 13 return $count;
697 };
698  
699  
700 =head2 parse_config_file
701  
702 Parses a configuration file and adds the parameters found to the hashref
703 provided. If a parameter already exists in the hashref, a warning is
704 generated, the existing parameter is not overwritten, and processing
705 continues.
706  
707 This function doesn't care what type of configuration parameters
708 are in the file, except that they must be scalar values. Since the
709 configuration files are actually Perl modules, the value can even be
710 a reference (to an array, a hash, or a subroutine, or any other complex
711 data structure).
712  
713 The technique used in the C<eval>, derived from Request Tracker, can be
714 described as follows: a local typeglob "set" is defined, containing a
715 reference to an anonymous subroutine. Subsequently, a config file (Perl
716 module) consisting of calls to this "set" subroutine is C<require>d.
717  
718 Note: If even one call to C<set> fails to compile, the entire file will be
719 rejected and no configuration parameters from that file will be loaded.
720  
721 The C<parse_config_file> function takes a PARAMHASH consisting of:
722  
723 =over
724  
725 =item C<File> - filename (full path)
726  
727 =item C<Dest> - hash reference (where to store the config params).
728  
729 =back
730  
731 Returns: number of configuration parameters parsed/loaded
732  
733 (IMPORTANT NOTE: If even one call to C<set> fails to compile, the entire
734 file will be rejected and no configuration parameters from that file will
735 be loaded.)
736  
737 =cut
738  
739 31 ▶sub parse_config_file {
Subroutine parse_config_file: called CC=2 SCAR=7.2
Pod documented
740 31 my %ARGS = (
741 'File' => undef,
742 'Dest' => undef,
743 @_,
744 );
745  
746 # This is so we can use the C<$self> variable (in the C<try>
747 # statement, below) to reach the C<_conf_from_config> functions from
748 # the configuration file.
749 31 my $self = {};
750 31 bless $self, 'App::CELL::Load';
751  
752 31 my $count = 0;
753
754 # ideally this should be 'debug' for sharedir and 'info' for sitedir
755 # but in this routine I have no easy way of telling one from the other
756 31 $log->debug( "Loading =>$ARGS{'File'}<=", cell => 1 );
757 31 ▶ if ( not ref( $ARGS{'Dest'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ARGS{"Dest"}) 0 31 31
758 0 $log->warn(
759 "Something strange happened: destination is not a reference?!?",
760 cell => 1,
761 );
762 }
763  
764 {
765 15 ▶ use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
766 31 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
767 156 ▶ local *set = sub(@) {
Subroutine __ANON__: called CC=4 SCAR=14.1
768 156 my $number_of_params = scalar @_;
769 156 my @params = @_;
770 156 my $param;
771 my $value;
772 156 ▶ if ( $number_of_params == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($number_of_params == 0) { } 0 156 156
elsif ($number_of_params == 1) { } 1 155 156
elsif ($number_of_params == 2) { } 154 1 155
773 0 my $msg = "set() called with no parameters";
774 0 $log->crit( $msg, cell => 1 );
775 0 die $msg;
776 } elsif ( $number_of_params == 1 ) {
777 1 $param = $params[0];
778 1 $log->warn(
779 "set() called with parameter $param but no value - set to \"\"",
780 cell => 1,
781 );
782 } elsif ( $number_of_params == 2 ) {
783 154 $param = $params[0];
784 154 $value = $params[1];
785 154 $log->debug(
786 "set() called with parameter $param and one value",
787 cell => 1,
788 );
789 } else {
790 1 $param = $params[0];
791 1 $value = $params[1];
792 1 $log->warn(
793 "set() called with $number_of_params parameters. Only " .
794 "the first two were used; the rest were ignored.",
795 cell => 1,
796 );
797 }
798 156 my ( undef, $file, $line ) = caller;
799 $count += $self->_conf_from_config(
800 156 'Dest' => $ARGS{'Dest'},
801 'Param' => $param,
802 'Value' => $value,
803 'File' => $file,
804 'Line' => $line,
805 );
806 31 };
807 31 require $ARGS{'File'};
808 }
809 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
810 0 my $errmsg = $_;
811 0 $errmsg =~ s/\012/ -- /g;
812 0 $log->err(
813 "CELL_CONFIG_LOAD_FAIL on file $ARGS{File} with error message: $errmsg",
814 cell => 1,
815 );
816 0 $log->debug( "The count is $count", cell => 1 );
817 0 return $count;
818 31 };
819 }
820 #$log->info( "Successfully loaded $count configuration parameters "
821 # . "from $ARGS{'File'}", cell => 1 );
822  
823 31 return $count;
824 }
825  
826  
827 =head2 _conf_from_config
828  
829 This function takes a target hashref (which points to one of the 'meta',
830 'core', or 'site' package hashes in C<App::CELL::Config>), a config parameter
831 (i.e. a string), config value, config file name, and line number.
832  
833 Let's imagine that the configuration parameter is "FOO_BAR". The function
834 first checks if a key named "FOO_BAR" already exists in the package hash
835 (which is passed into the function as C<%ARGS{'Dest'}>). If there isn't
836 one, it creates that key. If there is one, it leaves it untouched and
837 triggers a warning.
838  
839 Although the arguments are passed to the function in the form of a
840 PARAMHASH, the function converts them into ordinary private variables.
841 This was necessary to avoid extreme notational ugliness.
842  
843 =cut
844  
845 156 ▶sub _conf_from_config {
Subroutine _conf_from_config: called CC=3 SCAR=11.0
846 156 my $self = shift;
847 156 my ( %ARGS ) = validate( @_, {
848 Dest => { type => HASHREF },
849 Param => { type => SCALAR },
850 Value => { type => SCALAR|SCALARREF|ARRAYREF|HASHREF|CODEREF|UNDEF },
851 File => { type => SCALAR },
852 Line => { type => SCALAR },
853 } );
854 # convert PARAMHASH into private variables
855 156 my $desthash = $ARGS{'Dest'};
856 156 my $param = $ARGS{'Param'};
857 156 my $value = $ARGS{'Value'};
858 156 my $file = $ARGS{'File'};
859 156 my $line = $ARGS{'Line'};
860  
861 156 ▶ if ( keys( %{ $desthash->{ $param } } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$desthash{$param};}) { } 2 154 156
862 {
863 $log->warn(
864 "ignoring duplicate definition of config parameter $param in line $line " .
865 "of config file $file because it conflicts with a similar parameter in " .
866 2 $desthash->{ $param }->{'File'},
867 cell => 1,
868 );
869 2 return 0;
870 } else {
871 154 $desthash->{ $param } = {
872 'Value' => $value,
873 'File' => $file,
874 'Line' => $line,
875 };
876 154 ▶ $log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $App::CELL::Load::meta->CELL_META_LOAD_VERBOSE 96 58 154
877 "Parsed parameter $param from $file, line $line",
878 cell => 1,
879 suppress_caller => 1
880 ) if $meta->CELL_META_LOAD_VERBOSE;
881 154 return 1;
882 }
883 }
884  
885 15 1;
« blib/lib/App/CELL/Config.pm ↑ Summary blib/lib/App/CELL/Log.pm »