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

Last change on this file since 2152 was 2152, checked in by bruno, 3 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
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_print 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    # Add the new one at the end
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
319return(pb_conf_get_in_hash_if($h,@_));
320}
321
322=item B<pb_conf_add_last_in_hash>
323
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)
325
326It is used internally by pb_conf_add and is not exported.
327
328=cut
329
330sub pb_conf_add_last_in_hash {
331
332my $ptr = shift;
333
334return if (not defined $ptr);
335# TODO: test $ptr is a hash pointer
336
337# When called without correct initialization, try to work anyway with default as project
338pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
339
340my @params = (sort keys %$ptr);
341
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);
346
347my $p1;
348my $p2;
349
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");
353
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
359    if (not defined $p2) {
360        # exit if no p1 either
361        next if (not defined $p1);
362        # No ref in p2 so use p1
363        $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
364    } else {
365        # Ref found in p2
366        if (not defined $p1) {
367            # No ref in p1 so use p2's value
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'}}) {
380                    $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
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
386            # p1 content always has priority over p2
387            foreach my $k (keys %$p2) {
388                $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
389            }
390        }
391    }
392    $h->{$params[$i]} = $p1;
393}
394pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
395}
396
397=item B<pb_conf_get>
398
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);
407my $proj = undef;
408
409if (not defined $ENV{'PBPROJ'}) {
410    $proj = "unknown";
411} else {
412    $proj = $ENV{'PBPROJ'};
413}
414
415confess "No params found for $proj" if (not @return);
416
417foreach my $i (0..$#param) {
418    confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
419}
420return(@return);
421}
422
423
424=item B<pb_conf_get_all>
425
426This function returns an array with all configuration parameters
427
428=cut
429
430sub pb_conf_get_all {
431
432return(sort keys %$h);
433}
434
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
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.