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

Last change on this file since 2154 was 2154, checked in by Bruno Cornec, 7 years ago

Fix conf file analysis:

  • pb_conf_get_if now returns the value and theone for default if it doesn't exist. pb_conf_get inherits it
  • Fix pb_conf_add_last_in_hash by analyzing correctly the 2 hashes without taking in account default values
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);
[2154]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_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
[2154]158 # The new conf file overload values already managed
[898]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
[2154]319my @param = @_;
320my @return = pb_conf_get_in_hash_if($h,@_);
321my $proj = undef;
322
323if (not defined $ENV{'PBPROJ'}) {
324 $proj = "unknown";
325} else {
326 $proj = $ENV{'PBPROJ'};
[405]327}
[409]328
[2154]329foreach my $i (0..$#param) {
330 if (not defined $return[$i]->{$proj}) {
331 $return[$i]->{$proj} = $return[$i]->{'default'} if (defined $return[$i]->{'default'});
332 }
333}
334return(@return);
335}
336
[1495]337=item B<pb_conf_add_last_in_hash>
[405]338
[1495]339This 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]340
[1495]341It is used internally by pb_conf_add and is not exported.
[409]342
343=cut
344
[1495]345sub pb_conf_add_last_in_hash {
[409]346
[1907]347my $ptr = shift;
[409]348
[1495]349return if (not defined $ptr);
350# TODO: test $ptr is a hash pointer
[405]351
[1509]352# When called without correct initialization, try to work anyway with default as project
353pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
354
[1495]355my @params = (sort keys %$ptr);
[405]356
[1495]357# Everything is returned via @h
358# @h contains the values overloading what @ptr may contain.
[2154]359my @h = pb_conf_get_in_hash_if($h,@params);
[1495]360my @ptr = pb_conf_get_in_hash_if($ptr,@params);
[409]361
[405]362my $p1;
363my $p2;
364
[1495]365pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
[2154]366pb_log(2,"DEBUG: pb_conf_add_last_in_hash current hash: ".Dumper(@h)."\n");
367pb_log(2,"DEBUG: pb_conf_add_last_in_hash new inputs: ".Dumper(@ptr)."\n");
[405]368
[1495]369foreach my $i (0..$#params) {
370 $p1 = $h[$i];
371 $p2 = $ptr[$i];
[2154]372 # Always try to take the param from h in priority
[1495]373 # in order to mask what could be defined already in ptr
[405]374 if (not defined $p2) {
[415]375 # exit if no p1 either
[1509]376 next if (not defined $p1);
[405]377 } else {
[409]378 # Ref found in p2
[405]379 if (not defined $p1) {
[409]380 # No ref in p1 so use p2's value
[405]381 $p1 = $p2;
382 } else {
383 # Both are defined - handling the overloading
[2154]384 # Now copy back into p1 all p2 content
385 # as p1 content always has priority over p2
[405]386 if (not defined $p1->{$ENV{'PBPROJ'}}) {
387 if (defined $p2->{$ENV{'PBPROJ'}}) {
[1594]388 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
[405]389 }
390 }
391 # Now copy back into p1 all p2 content which doesn't exist in p1
[2154]392 # # p1 content always has priority over p2
[405]393 foreach my $k (keys %$p2) {
394 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
395 }
396 }
397 }
[1495]398 $h->{$params[$i]} = $p1;
[405]399}
[1495]400pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
[405]401}
402
[409]403=item B<pb_conf_get>
[405]404
[409]405This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
406
407=cut
408
409sub pb_conf_get {
410
411my @param = @_;
412my @return = pb_conf_get_if(@param);
[932]413my $proj = undef;
[409]414
[932]415if (not defined $ENV{'PBPROJ'}) {
416 $proj = "unknown";
417} else {
418 $proj = $ENV{'PBPROJ'};
419}
[409]420
[1538]421confess "No params found for $proj" if (not @return);
[932]422
[409]423foreach my $i (0..$#param) {
[1507]424 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
[409]425}
426return(@return);
427}
428
[1495]429
[1694]430=item B<pb_conf_get_all>
431
[2077]432This function returns an array with all configuration parameters
[1694]433
434=cut
435
436sub pb_conf_get_all {
437
438return(sort keys %$h);
439}
440
[2077]441
442=item B<pb_conf_get_hash>
443
444This function returns a pointer to the hash with all configuration parameters
445
446=cut
447
448sub pb_conf_get_hash {
449
450return($h);
451}
452
[405]453=back
454
455=head1 WEB SITES
456
457The 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/>.
458
459=head1 USER MAILING LIST
460
461None exists for the moment.
462
463=head1 AUTHORS
464
465The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
466
467=head1 COPYRIGHT
468
469Project-Builder.org is distributed under the GPL v2.0 license
470described in the file C<COPYING> included with the distribution.
471
472=cut
473
474
4751;
Note: See TracBrowser for help on using the repository browser.