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

Last change on this file since 2152 was 2152, checked in by Bruno Cornec, 7 years ago
  • Fix a bug in the loading order of the 2 generic conf files files which were reversed
  • Document in Conf.pm the order in which all conf files are loaded
File size: 11.2 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#
[2032]7# Copyright B. Cornec 2007-2016
[1528]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);
[2077]31our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_write pb_conf_get pb_conf_get_if pb_conf_print pb_conf_get_all pb_conf_get_hash pb_conf_cache);
[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
[2152]68The configuration files are loaded in a specific order from most generic to the most specific
69to allow for overwrite to work:
70
711. /usr/share/pb/pb.conf - the read-only system conf file provided by install
722. /etc/pb/pb.conf - the same global conf file given to the sysadmin in order to make system wide modifications
733. /path/to/project.pb - Configuration file for the project we're building for
744. /(vm|ve|rm)path/to/.pbrc - configuration file for VM, VE or RM specific parameters. Cumulative should be orthogonal
755. $HOME/.pbrc - user's configuration file
76
[405]77=over 4
78
[505]79=item B<pb_conf_init>
80
[898]81This function setup the environment PBPROJ for project-builder function usage from other projects.
[505]82The first parameter is the project name.
[898]83It sets up environment variables (PBPROJ)
[505]84
85=cut
86
87sub pb_conf_init {
88
[1907]89my $proj=shift;
[505]90
[1495]91pb_log(1,"Entering pb_conf_init\n");
[1584]92#
93# Check project name
94# Could be with env var PBPROJ
95# or option -p
96# if not defined take the first in conf file
97#
98if ((defined $ENV{'PBPROJ'}) &&
99 (not defined $proj)) {
100 pb_log(2,"PBPROJ env var setup ($ENV{'PBPROJ'}) so using it\n");
101 $proj = $ENV{'PBPROJ'};
102}
103
[505]104if (defined $proj) {
105 $ENV{'PBPROJ'} = $proj;
106} else {
107 $ENV{'PBPROJ'} = "default";
108}
[1495]109pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
[505]110}
111
112
[1495]113=item B<pb_conf_cache>
[505]114
[1495]115This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
116It returns the modified hash
117Can be used in correlation with the %h hash to store permanently values or not if temporarily.
118
119=cut
120
121sub pb_conf_cache {
122
123my $cf = shift;
124my $lh = shift;
125
[2077]126# Read the content of the config file and cache it in the %h hash then available for queries
[1538]127open(CONF,$cf) || confess "Unable to open $cf";
[1495]128while(<CONF>) {
[1905]129 next if (/^#/);
[2077]130 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.\?\[\]\*\+\\]+)\s*=\s*(.*)$/) {
[1495]131 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
132 $lh->{$1}->{$2}=$3;
133 }
134}
135close(CONF);
136return($lh);
137}
138
[409]139=item B<pb_conf_add>
140
[1495]141This function adds the configuration file to the list last, and cache their content in the %h hash
[409]142
143=cut
144
145sub pb_conf_add {
146
[415]147pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
[1495]148my $lh;
[898]149
150foreach my $cf (@_) {
[1495]151 if (! -r $cf) {
152 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
153 next;
154 }
[898]155 # Skip already used conf files
[1495]156 return($lh) if (defined $pbconffiles{$cf});
157
[898]158 # Add the new one at the end
159 my $num = keys %pbconffiles;
[1495]160 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
[898]161 $pbconffiles{$cf} = $num;
[1495]162
163 # Read the content of the config file
164 $lh = pb_conf_cache($cf,$lh);
165 # and cache it in the %h hash for further queries but after the previous
166 # as we load conf files in reverse order (most precise first)
167 pb_conf_add_last_in_hash($lh)
[409]168}
[898]169}
[409]170
[1495]171
[405]172=item B<pb_conf_read_if>
173
174This function returns a table of pointers on hashes
175corresponding to the keys in a configuration file passed in parameter.
176If that file doesn't exist, it returns undef.
177
178The format of the configuration file is as follows:
179
180key tag = value1,value2,...
181
182Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
183
184 $ cat $HOME/.pbrc
185 pbver pb = 3
186 pbver default = 1
187 pblist pb = 12,25
188
189calling it like this:
190
191 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
192
193will allow to get the mapping:
194
195 $k1->{'pb'} contains 3
[409]196 $k1->{'default'} contains 1
[405]197 $k2->{'pb'} contains 12,25
198
199Valid chars for keys and tags are letters, numbers, '-' and '_'.
200
[1495]201The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
202
[405]203=cut
204
205sub pb_conf_read_if {
206
207my $conffile = shift;
208my @param = @_;
209
210open(CONF,$conffile) || return((undef));
211close(CONF);
212return(pb_conf_read($conffile,@param));
213}
214
215=item B<pb_conf_read>
216
217This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
218
219=cut
220
221sub pb_conf_read {
222
223my $conffile = shift;
224my @param = @_;
225my @ptr;
[1495]226my $lh;
[405]227
[1495]228$lh = pb_conf_cache($conffile,$lh);
229
230foreach my $param (@param) {
231 push @ptr,$lh->{$param};
[405]232}
[1495]233return(@ptr);
234}
[405]235
[1904]236=item B<pb_conf_write>
[1495]237
[1904]238This function writes in the file passed ias first parameter the hash of values passed as second parameter
[1495]239
[1904]240=cut
241
242sub pb_conf_write {
243
244my $conffile = shift;
[1905]245my $h = shift;
[1904]246
[1905]247confess "No configuration file defined to write into !" if (not defined $conffile);
248confess "No hash defined to read from !" if (not defined $h);
249open(CONF,"> $conffile") || confess "Unable to write into $conffile";
[1904]250
[1907]251foreach my $p (sort keys %$h) {
[1905]252 my $j = $h->{$p};
[1907]253 foreach my $k (sort keys %$j) {
[1905]254 print CONF "$p $k = $j->{$k}\n";
[1904]255 }
256}
257close(CONF);
258}
259
260
261
[1495]262=item B<pb_conf_get_in_hash_if>
263
[1594]264This function returns a table, corresponding to a set of values queried in the hash passed in parameter or undef if it doesn't exist.
265It takes a table of keys as an input parameter.
[1495]266
267=cut
268
269sub pb_conf_get_in_hash_if {
270
271my $lh = shift || return(());
272my @params = @_;
273my @ptr = ();
274
275pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
276foreach my $k (@params) {
277 push @ptr,$lh->{$k};
[405]278}
[1495]279
280pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
[405]281return(@ptr);
282}
283
[1495]284
285
[409]286=item B<pb_conf_get_if>
[405]287
[1495]288This 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]289
[409]290The format of the configurations file is as follows:
291
292key tag = value1,value2,...
293
[1495]294It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
[409]295
296 $ cat $HOME/.pbrc
297 pbver pb = 1
298 pblist pb = 4
299 $ cat $HOME/.pbrc2
300 pbver pb = 3
301 pblist default = 5
302
303calling it like this:
304
[505]305 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
[409]306 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
307
308will allow to get the mapping:
309
310 $k1->{'pb'} contains 3
311 $k2->{'pb'} contains 4
312
313Valid chars for keys and tags are letters, numbers, '-' and '_'.
314
315=cut
316
317sub pb_conf_get_if {
318
[1594]319return(pb_conf_get_in_hash_if($h,@_));
[405]320}
[409]321
[1495]322=item B<pb_conf_add_last_in_hash>
[405]323
[1495]324This 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]325
[1495]326It is used internally by pb_conf_add and is not exported.
[409]327
328=cut
329
[1495]330sub pb_conf_add_last_in_hash {
[409]331
[1907]332my $ptr = shift;
[409]333
[1495]334return if (not defined $ptr);
335# TODO: test $ptr is a hash pointer
[405]336
[1509]337# When called without correct initialization, try to work anyway with default as project
338pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
339
[1495]340my @params = (sort keys %$ptr);
[405]341
[1495]342# Everything is returned via @h
343# @h contains the values overloading what @ptr may contain.
344my @h = pb_conf_get_if(@params);
345my @ptr = pb_conf_get_in_hash_if($ptr,@params);
[409]346
[405]347my $p1;
348my $p2;
349
[1495]350pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
351pb_log(2,"DEBUG: pb_conf_add_last_in_hash hash: ".Dumper(@h)."\n");
352pb_log(2,"DEBUG: pb_conf_add_last_in_hash input: ".Dumper(@ptr)."\n");
[405]353
[1495]354foreach my $i (0..$#params) {
355 $p1 = $h[$i];
356 $p2 = $ptr[$i];
357 # Always try to take the param from h
358 # in order to mask what could be defined already in ptr
[405]359 if (not defined $p2) {
[415]360 # exit if no p1 either
[1509]361 next if (not defined $p1);
[409]362 # No ref in p2 so use p1
[405]363 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
364 } else {
[409]365 # Ref found in p2
[405]366 if (not defined $p1) {
[409]367 # No ref in p1 so use p2's value
[405]368 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
369 $p1 = $p2;
370 } else {
371 # Both are defined - handling the overloading
372 if (not defined $p1->{'default'}) {
373 if (defined $p2->{'default'}) {
374 $p1->{'default'} = $p2->{'default'};
375 }
376 }
377
378 if (not defined $p1->{$ENV{'PBPROJ'}}) {
379 if (defined $p2->{$ENV{'PBPROJ'}}) {
[1594]380 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
[405]381 } else {
382 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
383 }
384 }
385 # Now copy back into p1 all p2 content which doesn't exist in p1
[409]386 # p1 content always has priority over p2
[405]387 foreach my $k (keys %$p2) {
388 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
389 }
390 }
391 }
[1495]392 $h->{$params[$i]} = $p1;
[405]393}
[1495]394pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
[405]395}
396
[409]397=item B<pb_conf_get>
[405]398
[409]399This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
400
401=cut
402
403sub pb_conf_get {
404
405my @param = @_;
406my @return = pb_conf_get_if(@param);
[932]407my $proj = undef;
[409]408
[932]409if (not defined $ENV{'PBPROJ'}) {
410 $proj = "unknown";
411} else {
412 $proj = $ENV{'PBPROJ'};
413}
[409]414
[1538]415confess "No params found for $proj" if (not @return);
[932]416
[409]417foreach my $i (0..$#param) {
[1507]418 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
[409]419}
420return(@return);
421}
422
[1495]423
[1694]424=item B<pb_conf_get_all>
425
[2077]426This function returns an array with all configuration parameters
[1694]427
428=cut
429
430sub pb_conf_get_all {
431
432return(sort keys %$h);
433}
434
[2077]435
436=item B<pb_conf_get_hash>
437
438This function returns a pointer to the hash with all configuration parameters
439
440=cut
441
442sub pb_conf_get_hash {
443
444return($h);
445}
446
[405]447=back
448
449=head1 WEB SITES
450
451The 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/>.
452
453=head1 USER MAILING LIST
454
455None exists for the moment.
456
457=head1 AUTHORS
458
459The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
460
461=head1 COPYRIGHT
462
463Project-Builder.org is distributed under the GPL v2.0 license
464described in the file C<COPYING> included with the distribution.
465
466=cut
467
468
4691;
Note: See TracBrowser for help on using the repository browser.