source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/Conf.pm@ 1538

Last change on this file since 1538 was 1538, checked in by Bruno Cornec, 12 years ago
  • Use more confess instead of die in Conf.pm to debug a calling issue
File size: 9.9 KB
RevLine 
[405]1#!/usr/bin/perl -w
2#
3# ProjectBuilder Conf module
4# Conf files subroutines brought by the the Project-Builder project
5# which can be easily used by wahtever perl project
6#
[1528]7# Copyright B. Cornec 2007-2012
8# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
9# Provided under the GPL v2
10#
[405]11# $Id$
12#
13
14package ProjectBuilder::Conf;
15
16use strict;
[1507]17use Carp 'confess';
[405]18use Data::Dumper;
19use ProjectBuilder::Base;
[1148]20use ProjectBuilder::Version;
[405]21
22# Inherit from the "Exporter" module which handles exporting functions.
23
[1156]24use vars qw($VERSION $REVISION @ISA @EXPORT);
[405]25use Exporter;
26
27# Export, by default, all the functions into the namespace of
28# any code which uses this module.
29
30our @ISA = qw(Exporter);
[1495]31our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if pb_conf_print);
[1156]32($VERSION,$REVISION) = pb_version_init();
[405]33
[898]34# Global hash of conf files
35# Key is the conf file name
36# Value is its rank
37my %pbconffiles;
[409]38
[1495]39# Global hash of conf file content
40# Key is the config keyword
41# Value is a hash whose key depends on the nature of the config keyword as documented
42# and value is the confguration value
[898]43# We consider that values can not change during the life of pb
[1495]44my $h = ();
[898]45
[405]46=pod
47
48=head1 NAME
49
50ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
51
52=head1 DESCRIPTION
53
54This modules provides functions dealing with configuration files.
55
56=head1 SYNOPSIS
57
58 use ProjectBuilder::Conf;
59
60 #
61 # Read hash codes of values from a configuration file and return table of pointers
62 #
63 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
64 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
65
66=head1 USAGE
67
68=over 4
69
[505]70=item B<pb_conf_init>
71
[898]72This function setup the environment PBPROJ for project-builder function usage from other projects.
[505]73The first parameter is the project name.
[898]74It sets up environment variables (PBPROJ)
[505]75
76=cut
77
78sub pb_conf_init {
79
80my $proj=shift || undef;
81
[1495]82pb_log(1,"Entering pb_conf_init\n");
[505]83if (defined $proj) {
84 $ENV{'PBPROJ'} = $proj;
85} else {
86 $ENV{'PBPROJ'} = "default";
87}
[1495]88pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
[505]89}
90
91
[1495]92=item B<pb_conf_cache>
[505]93
[1495]94This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
95It returns the modified hash
96Can be used in correlation with the %h hash to store permanently values or not if temporarily.
97
98=cut
99
100sub pb_conf_cache {
101
102my $cf = shift;
103my $lh = shift;
104
105# Read the content of the config file and cache it in the %h hash further availble for queries
[1538]106open(CONF,$cf) || confess "Unable to open $cf";
[1495]107while(<CONF>) {
108 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
109 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
110 $lh->{$1}->{$2}=$3;
111 }
112}
113close(CONF);
114return($lh);
115}
116
[409]117=item B<pb_conf_add>
118
[1495]119This function adds the configuration file to the list last, and cache their content in the %h hash
[409]120
121=cut
122
123sub pb_conf_add {
124
[415]125pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
[1495]126my $lh;
[898]127
128foreach my $cf (@_) {
[1495]129 if (! -r $cf) {
130 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
131 next;
132 }
[898]133 # Skip already used conf files
[1495]134 return($lh) if (defined $pbconffiles{$cf});
135
[898]136 # Add the new one at the end
137 my $num = keys %pbconffiles;
[1495]138 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
[898]139 $pbconffiles{$cf} = $num;
[1495]140
141 # Read the content of the config file
142 $lh = pb_conf_cache($cf,$lh);
143 # and cache it in the %h hash for further queries but after the previous
144 # as we load conf files in reverse order (most precise first)
145 pb_conf_add_last_in_hash($lh)
[409]146}
[898]147}
[409]148
[1495]149
[405]150=item B<pb_conf_read_if>
151
152This function returns a table of pointers on hashes
153corresponding to the keys in a configuration file passed in parameter.
154If that file doesn't exist, it returns undef.
155
156The format of the configuration file is as follows:
157
158key tag = value1,value2,...
159
160Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
161
162 $ cat $HOME/.pbrc
163 pbver pb = 3
164 pbver default = 1
165 pblist pb = 12,25
166
167calling it like this:
168
169 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
170
171will allow to get the mapping:
172
173 $k1->{'pb'} contains 3
[409]174 $k1->{'default'} contains 1
[405]175 $k2->{'pb'} contains 12,25
176
177Valid chars for keys and tags are letters, numbers, '-' and '_'.
178
[1495]179The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
180
[405]181=cut
182
183sub pb_conf_read_if {
184
185my $conffile = shift;
186my @param = @_;
187
188open(CONF,$conffile) || return((undef));
189close(CONF);
190return(pb_conf_read($conffile,@param));
191}
192
193=item B<pb_conf_read>
194
195This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
196
197=cut
198
199sub pb_conf_read {
200
201my $conffile = shift;
202my @param = @_;
203my @ptr;
[1495]204my $lh;
[405]205
[1495]206$lh = pb_conf_cache($conffile,$lh);
207
208foreach my $param (@param) {
209 push @ptr,$lh->{$param};
[405]210}
[1495]211return(@ptr);
212}
[405]213
[1495]214
215
216=item B<pb_conf_get_in_hash_if>
217
218This function returns a table, corresponding to a set of values queried in the hash passe in parameter or undef if it doen't exist. It takes a table of keys as an input parameter.
219
220=cut
221
222sub pb_conf_get_in_hash_if {
223
224my $lh = shift || return(());
225my @params = @_;
226my @ptr = ();
227
228pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
229foreach my $k (@params) {
230 push @ptr,$lh->{$k};
[405]231}
[1495]232
233pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
[405]234return(@ptr);
235}
236
[1495]237
238
[409]239=item B<pb_conf_get_if>
[405]240
[1495]241This function returns a table, corresponding to a set of values queried in the %h hash or undef if it doen't exist. It takes a table of keys as an input parameter.
[405]242
[409]243The format of the configurations file is as follows:
244
245key tag = value1,value2,...
246
[1495]247It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
[409]248
249 $ cat $HOME/.pbrc
250 pbver pb = 1
251 pblist pb = 4
252 $ cat $HOME/.pbrc2
253 pbver pb = 3
254 pblist default = 5
255
256calling it like this:
257
[505]258 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
[409]259 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
260
261will allow to get the mapping:
262
263 $k1->{'pb'} contains 3
264 $k2->{'pb'} contains 4
265
266Valid chars for keys and tags are letters, numbers, '-' and '_'.
267
268=cut
269
270sub pb_conf_get_if {
271
[1495]272my @params = @_;
273my @ptr = undef;
[405]274
[1495]275return(pb_conf_get_in_hash_if($h,@params));
[405]276}
[409]277
[1495]278=item B<pb_conf_add_last_in_hash>
[405]279
[1495]280This function merges the values passed in the hash parameter into the %h hash, but only if itdoesn't already contain a value, or if the value is more precise (real value instead of default)
[405]281
[1495]282It is used internally by pb_conf_add and is not exported.
[409]283
284=cut
285
[1495]286sub pb_conf_add_last_in_hash {
[409]287
[1495]288my $ptr = shift || undef;
[409]289
[1495]290return if (not defined $ptr);
291# TODO: test $ptr is a hash pointer
[405]292
[1509]293# When called without correct initialization, try to work anyway with default as project
294pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
295
[1495]296my @params = (sort keys %$ptr);
[405]297
[1495]298# Everything is returned via @h
299# @h contains the values overloading what @ptr may contain.
300my @h = pb_conf_get_if(@params);
301my @ptr = pb_conf_get_in_hash_if($ptr,@params);
[409]302
[405]303my $p1;
304my $p2;
305
[1495]306pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
307pb_log(2,"DEBUG: pb_conf_add_last_in_hash hash: ".Dumper(@h)."\n");
308pb_log(2,"DEBUG: pb_conf_add_last_in_hash input: ".Dumper(@ptr)."\n");
[405]309
[1495]310foreach my $i (0..$#params) {
311 $p1 = $h[$i];
312 $p2 = $ptr[$i];
313 # Always try to take the param from h
314 # in order to mask what could be defined already in ptr
[405]315 if (not defined $p2) {
[415]316 # exit if no p1 either
[1509]317 next if (not defined $p1);
[409]318 # No ref in p2 so use p1
[405]319 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
320 } else {
[409]321 # Ref found in p2
[405]322 if (not defined $p1) {
[409]323 # No ref in p1 so use p2's value
[405]324 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
325 $p1 = $p2;
326 } else {
327 # Both are defined - handling the overloading
328 if (not defined $p1->{'default'}) {
329 if (defined $p2->{'default'}) {
330 $p1->{'default'} = $p2->{'default'};
331 }
332 }
333
334 if (not defined $p1->{$ENV{'PBPROJ'}}) {
335 if (defined $p2->{$ENV{'PBPROJ'}}) {
336 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
337 } else {
338 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
339 }
340 }
341 # Now copy back into p1 all p2 content which doesn't exist in p1
[409]342 # p1 content always has priority over p2
[405]343 foreach my $k (keys %$p2) {
344 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
345 }
346 }
347 }
[1495]348 $h->{$params[$i]} = $p1;
[405]349}
[1495]350pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
[405]351}
352
[409]353=item B<pb_conf_get>
[405]354
[409]355This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
356
357=cut
358
359sub pb_conf_get {
360
361my @param = @_;
362my @return = pb_conf_get_if(@param);
[932]363my $proj = undef;
[409]364
[932]365if (not defined $ENV{'PBPROJ'}) {
366 $proj = "unknown";
367} else {
368 $proj = $ENV{'PBPROJ'};
369}
[409]370
[1538]371confess "No params found for $proj" if (not @return);
[932]372
[409]373foreach my $i (0..$#param) {
[1507]374 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
[409]375}
376return(@return);
377}
378
[1495]379
380=item B<pb_conf_print>
381
382This function prints every configuration parameter in order to help debug stacking issues with conf files
383
384=cut
385
386sub pb_conf_print {
387
388pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
389pb_log(0,"====================================\n");
390foreach my $k (sort keys %$h) {
391 pb_log(0,"$k => ".Dumper($h->{$k})."\n");
392}
393}
394
[405]395=back
396
397=head1 WEB SITES
398
399The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
400
401=head1 USER MAILING LIST
402
403None exists for the moment.
404
405=head1 AUTHORS
406
407The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
408
409=head1 COPYRIGHT
410
411Project-Builder.org is distributed under the GPL v2.0 license
412described in the file C<COPYING> included with the distribution.
413
414=cut
415
416
4171;
Note: See TracBrowser for help on using the repository browser.