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
Line 
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#
7# Copyright B. Cornec 2007-2016
8# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
9# Provided under the GPL v2
10#
11# $Id$
12#
13
14package ProjectBuilder::Conf;
15
16use strict;
17use Carp 'confess';
18use Data::Dumper;
19use ProjectBuilder::Base;
20use ProjectBuilder::Version;
21
22# Inherit from the "Exporter" module which handles exporting functions.
23
24use vars qw($VERSION $REVISION @ISA @EXPORT);
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);
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);
32($VERSION,$REVISION) = pb_version_init();
33
34# Global hash of conf files
35# Key is the conf file name
36# Value is its rank
37my %pbconffiles;
38
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
43# We consider that values can not change during the life of pb
44my $h = ();
45
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
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
77=over 4
78
79=item B<pb_conf_init>
80
81This function setup the environment PBPROJ for project-builder function usage from other projects.
82The first parameter is the project name.
83It sets up environment variables (PBPROJ)
84
85=cut
86
87sub pb_conf_init {
88
89my $proj=shift;
90
91pb_log(1,"Entering pb_conf_init\n");
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
104if (defined $proj) {
105 $ENV{'PBPROJ'} = $proj;
106} else {
107 $ENV{'PBPROJ'} = "default";
108}
109pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
110}
111
112
113=item B<pb_conf_cache>
114
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
126# Read the content of the config file and cache it in the %h hash then available for queries
127open(CONF,$cf) || confess "Unable to open $cf";
128while(<CONF>) {
129 next if (/^#/);
130 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.\?\[\]\*\+\\]+)\s*=\s*(.*)$/) {
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
139=item B<pb_conf_add>
140
141This function adds the configuration file to the list last, and cache their content in the %h hash
142
143=cut
144
145sub pb_conf_add {
146
147pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
148my $lh;
149
150foreach my $cf (@_) {
151 if (! -r $cf) {
152 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
153 next;
154 }
155 # Skip already used conf files
156 return($lh) if (defined $pbconffiles{$cf});
157
158 # The new conf file overload values already managed
159 my $num = keys %pbconffiles;
160 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
161 $pbconffiles{$cf} = $num;
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)
168}
169}
170
171
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
196 $k1->{'default'} contains 1
197 $k2->{'pb'} contains 12,25
198
199Valid chars for keys and tags are letters, numbers, '-' and '_'.
200
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
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;
226my $lh;
227
228$lh = pb_conf_cache($conffile,$lh);
229
230foreach my $param (@param) {
231 push @ptr,$lh->{$param};
232}
233return(@ptr);
234}
235
236=item B<pb_conf_write>
237
238This function writes in the file passed ias first parameter the hash of values passed as second parameter
239
240=cut
241
242sub pb_conf_write {
243
244my $conffile = shift;
245my $h = shift;
246
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";
250
251foreach my $p (sort keys %$h) {
252 my $j = $h->{$p};
253 foreach my $k (sort keys %$j) {
254 print CONF "$p $k = $j->{$k}\n";
255 }
256}
257close(CONF);
258}
259
260
261
262=item B<pb_conf_get_in_hash_if>
263
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.
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};
278}
279
280pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
281return(@ptr);
282}
283
284
285
286=item B<pb_conf_get_if>
287
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.
289
290The format of the configurations file is as follows:
291
292key tag = value1,value2,...
293
294It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
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
305 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
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
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'};
327}
328
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
337=item B<pb_conf_add_last_in_hash>
338
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)
340
341It is used internally by pb_conf_add and is not exported.
342
343=cut
344
345sub pb_conf_add_last_in_hash {
346
347my $ptr = shift;
348
349return if (not defined $ptr);
350# TODO: test $ptr is a hash pointer
351
352# When called without correct initialization, try to work anyway with default as project
353pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
354
355my @params = (sort keys %$ptr);
356
357# Everything is returned via @h
358# @h contains the values overloading what @ptr may contain.
359my @h = pb_conf_get_in_hash_if($h,@params);
360my @ptr = pb_conf_get_in_hash_if($ptr,@params);
361
362my $p1;
363my $p2;
364
365pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
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");
368
369foreach my $i (0..$#params) {
370 $p1 = $h[$i];
371 $p2 = $ptr[$i];
372 # Always try to take the param from h in priority
373 # in order to mask what could be defined already in ptr
374 if (not defined $p2) {
375 # exit if no p1 either
376 next if (not defined $p1);
377 } else {
378 # Ref found in p2
379 if (not defined $p1) {
380 # No ref in p1 so use p2's value
381 $p1 = $p2;
382 } else {
383 # Both are defined - handling the overloading
384 # Now copy back into p1 all p2 content
385 # as p1 content always has priority over p2
386 if (not defined $p1->{$ENV{'PBPROJ'}}) {
387 if (defined $p2->{$ENV{'PBPROJ'}}) {
388 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
389 }
390 }
391 # Now copy back into p1 all p2 content which doesn't exist in p1
392 # # p1 content always has priority over p2
393 foreach my $k (keys %$p2) {
394 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
395 }
396 }
397 }
398 $h->{$params[$i]} = $p1;
399}
400pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
401}
402
403=item B<pb_conf_get>
404
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);
413my $proj = undef;
414
415if (not defined $ENV{'PBPROJ'}) {
416 $proj = "unknown";
417} else {
418 $proj = $ENV{'PBPROJ'};
419}
420
421confess "No params found for $proj" if (not @return);
422
423foreach my $i (0..$#param) {
424 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
425}
426return(@return);
427}
428
429
430=item B<pb_conf_get_all>
431
432This function returns an array with all configuration parameters
433
434=cut
435
436sub pb_conf_get_all {
437
438return(sort keys %$h);
439}
440
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
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.