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

Last change on this file since 1528 was 1528, checked in by bruno, 7 years ago
  • Add Copyrights specified by HP Open Source Review Board (Eric Anderson)
  • Clarification of copyrights, dates and licences (Bruno Cornec)
File size: 9.9 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-2012
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_get pb_conf_get_if pb_conf_print);
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
68=over 4
69
70=item B<pb_conf_init>
71
72This function setup the environment PBPROJ for project-builder function usage from other projects.
73The first parameter is the project name.
74It sets up environment variables (PBPROJ)
75
76=cut
77
78sub pb_conf_init {
79
80my $proj=shift || undef;
81
82pb_log(1,"Entering pb_conf_init\n");
83if (defined $proj) {
84    $ENV{'PBPROJ'} = $proj;
85} else {
86    $ENV{'PBPROJ'} = "default";
87}
88pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
89}
90
91
92=item B<pb_conf_cache>
93
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
106open(CONF,$cf) || die "Unable to open $cf";
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
117=item B<pb_conf_add>
118
119This function adds the configuration file to the list last, and cache their content in the %h hash
120
121=cut
122
123sub pb_conf_add {
124
125pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
126my $lh;
127
128foreach my $cf (@_) {
129    if (! -r $cf) {
130        pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
131        next;
132    }
133    # Skip already used conf files
134    return($lh) if (defined $pbconffiles{$cf});
135   
136    # Add the new one at the end
137    my $num = keys %pbconffiles;
138    pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
139    $pbconffiles{$cf} = $num;
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)
146}
147}
148
149
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
174  $k1->{'default'} contains 1
175  $k2->{'pb'} contains 12,25
176
177Valid chars for keys and tags are letters, numbers, '-' and '_'.
178
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
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;
204my $lh;
205
206$lh = pb_conf_cache($conffile,$lh);
207
208foreach my $param (@param) {
209    push @ptr,$lh->{$param};
210}
211return(@ptr);
212}
213
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};
231}
232
233pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
234return(@ptr);
235}
236
237
238
239=item B<pb_conf_get_if>
240
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.
242
243The format of the configurations file is as follows:
244
245key tag = value1,value2,...
246
247It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
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
258  pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
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
272my @params = @_;
273my @ptr = undef;
274
275return(pb_conf_get_in_hash_if($h,@params));
276}
277
278=item B<pb_conf_add_last_in_hash>
279
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)
281
282It is used internally by pb_conf_add and is not exported.
283
284=cut
285
286sub pb_conf_add_last_in_hash {
287
288my $ptr = shift || undef;
289
290return if (not defined $ptr);
291# TODO: test $ptr is a hash pointer
292
293# When called without correct initialization, try to work anyway with default as project
294pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
295
296my @params = (sort keys %$ptr);
297
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);
302
303my $p1;
304my $p2;
305
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");
309
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
315    if (not defined $p2) {
316        # exit if no p1 either
317        next if (not defined $p1);
318        # No ref in p2 so use p1
319        $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
320    } else {
321        # Ref found in p2
322        if (not defined $p1) {
323            # No ref in p1 so use p2's value
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
342            # p1 content always has priority over p2
343            foreach my $k (keys %$p2) {
344                $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
345            }
346        }
347    }
348    $h->{$params[$i]} = $p1;
349}
350pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
351}
352
353=item B<pb_conf_get>
354
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);
363my $proj = undef;
364
365if (not defined $ENV{'PBPROJ'}) {
366    $proj = "unknown";
367} else {
368    $proj = $ENV{'PBPROJ'};
369}
370
371die "No params found for $proj" if (not @return);
372
373foreach my $i (0..$#param) {
374    confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
375}
376return(@return);
377}
378
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
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.