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

Last change on this file since 1523 was 1509, checked in by Bruno Cornec, 12 years ago
  • Fix a build bug with the new prefix variable in pb-module. Only use if it exists !
  • pb_conf_init is called at low level on the default project to avoid PBPROJ to be undefined and creatin lots of error msgs. pbdistrocheck works again with it
  • Fix sbx2setupvm in order to call pb_conf_init correctly at start of the pbinit script.
File size: 9.7 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# $Id$
8#
9
10package ProjectBuilder::Conf;
11
12use strict;
13use Carp 'confess';
14use Data::Dumper;
15use ProjectBuilder::Base;
16use ProjectBuilder::Version;
17
18# Inherit from the "Exporter" module which handles exporting functions.
19
20use vars qw($VERSION $REVISION @ISA @EXPORT);
21use Exporter;
22
23# Export, by default, all the functions into the namespace of
24# any code which uses this module.
25
26our @ISA = qw(Exporter);
27our @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);
28($VERSION,$REVISION) = pb_version_init();
29
30# Global hash of conf files
31# Key is the conf file name
32# Value is its rank
33my %pbconffiles;
34
35# Global hash of conf file content
36# Key is the config keyword
37# Value is a hash whose key depends on the nature of the config keyword as documented
38# and value is the confguration value
39# We consider that values can not change during the life of pb
40my $h = ();
41
42=pod
43
44=head1 NAME
45
46ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
47
48=head1 DESCRIPTION
49
50This modules provides functions dealing with configuration files.
51
52=head1 SYNOPSIS
53
54 use ProjectBuilder::Conf;
55
56 #
57 # Read hash codes of values from a configuration file and return table of pointers
58 #
59 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
60 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
61
62=head1 USAGE
63
64=over 4
65
66=item B<pb_conf_init>
67
68This function setup the environment PBPROJ for project-builder function usage from other projects.
69The first parameter is the project name.
70It sets up environment variables (PBPROJ)
71
72=cut
73
74sub pb_conf_init {
75
76my $proj=shift || undef;
77
78pb_log(1,"Entering pb_conf_init\n");
79if (defined $proj) {
80 $ENV{'PBPROJ'} = $proj;
81} else {
82 $ENV{'PBPROJ'} = "default";
83}
84pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
85}
86
87
88=item B<pb_conf_cache>
89
90This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
91It returns the modified hash
92Can be used in correlation with the %h hash to store permanently values or not if temporarily.
93
94=cut
95
96sub pb_conf_cache {
97
98my $cf = shift;
99my $lh = shift;
100
101# Read the content of the config file and cache it in the %h hash further availble for queries
102open(CONF,$cf) || die "Unable to open $cf";
103while(<CONF>) {
104 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
105 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
106 $lh->{$1}->{$2}=$3;
107 }
108}
109close(CONF);
110return($lh);
111}
112
113=item B<pb_conf_add>
114
115This function adds the configuration file to the list last, and cache their content in the %h hash
116
117=cut
118
119sub pb_conf_add {
120
121pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
122my $lh;
123
124foreach my $cf (@_) {
125 if (! -r $cf) {
126 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
127 next;
128 }
129 # Skip already used conf files
130 return($lh) if (defined $pbconffiles{$cf});
131
132 # Add the new one at the end
133 my $num = keys %pbconffiles;
134 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
135 $pbconffiles{$cf} = $num;
136
137 # Read the content of the config file
138 $lh = pb_conf_cache($cf,$lh);
139 # and cache it in the %h hash for further queries but after the previous
140 # as we load conf files in reverse order (most precise first)
141 pb_conf_add_last_in_hash($lh)
142}
143}
144
145
146=item B<pb_conf_read_if>
147
148This function returns a table of pointers on hashes
149corresponding to the keys in a configuration file passed in parameter.
150If that file doesn't exist, it returns undef.
151
152The format of the configuration file is as follows:
153
154key tag = value1,value2,...
155
156Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
157
158 $ cat $HOME/.pbrc
159 pbver pb = 3
160 pbver default = 1
161 pblist pb = 12,25
162
163calling it like this:
164
165 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
166
167will allow to get the mapping:
168
169 $k1->{'pb'} contains 3
170 $k1->{'default'} contains 1
171 $k2->{'pb'} contains 12,25
172
173Valid chars for keys and tags are letters, numbers, '-' and '_'.
174
175The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
176
177=cut
178
179sub pb_conf_read_if {
180
181my $conffile = shift;
182my @param = @_;
183
184open(CONF,$conffile) || return((undef));
185close(CONF);
186return(pb_conf_read($conffile,@param));
187}
188
189=item B<pb_conf_read>
190
191This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
192
193=cut
194
195sub pb_conf_read {
196
197my $conffile = shift;
198my @param = @_;
199my @ptr;
200my $lh;
201
202$lh = pb_conf_cache($conffile,$lh);
203
204foreach my $param (@param) {
205 push @ptr,$lh->{$param};
206}
207return(@ptr);
208}
209
210
211
212=item B<pb_conf_get_in_hash_if>
213
214This 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.
215
216=cut
217
218sub pb_conf_get_in_hash_if {
219
220my $lh = shift || return(());
221my @params = @_;
222my @ptr = ();
223
224pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
225foreach my $k (@params) {
226 push @ptr,$lh->{$k};
227}
228
229pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
230return(@ptr);
231}
232
233
234
235=item B<pb_conf_get_if>
236
237This 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.
238
239The format of the configurations file is as follows:
240
241key tag = value1,value2,...
242
243It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
244
245 $ cat $HOME/.pbrc
246 pbver pb = 1
247 pblist pb = 4
248 $ cat $HOME/.pbrc2
249 pbver pb = 3
250 pblist default = 5
251
252calling it like this:
253
254 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
255 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
256
257will allow to get the mapping:
258
259 $k1->{'pb'} contains 3
260 $k2->{'pb'} contains 4
261
262Valid chars for keys and tags are letters, numbers, '-' and '_'.
263
264=cut
265
266sub pb_conf_get_if {
267
268my @params = @_;
269my @ptr = undef;
270
271return(pb_conf_get_in_hash_if($h,@params));
272}
273
274=item B<pb_conf_add_last_in_hash>
275
276This 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)
277
278It is used internally by pb_conf_add and is not exported.
279
280=cut
281
282sub pb_conf_add_last_in_hash {
283
284my $ptr = shift || undef;
285
286return if (not defined $ptr);
287# TODO: test $ptr is a hash pointer
288
289# When called without correct initialization, try to work anyway with default as project
290pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
291
292my @params = (sort keys %$ptr);
293
294# Everything is returned via @h
295# @h contains the values overloading what @ptr may contain.
296my @h = pb_conf_get_if(@params);
297my @ptr = pb_conf_get_in_hash_if($ptr,@params);
298
299my $p1;
300my $p2;
301
302pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
303pb_log(2,"DEBUG: pb_conf_add_last_in_hash hash: ".Dumper(@h)."\n");
304pb_log(2,"DEBUG: pb_conf_add_last_in_hash input: ".Dumper(@ptr)."\n");
305
306foreach my $i (0..$#params) {
307 $p1 = $h[$i];
308 $p2 = $ptr[$i];
309 # Always try to take the param from h
310 # in order to mask what could be defined already in ptr
311 if (not defined $p2) {
312 # exit if no p1 either
313 next if (not defined $p1);
314 # No ref in p2 so use p1
315 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
316 } else {
317 # Ref found in p2
318 if (not defined $p1) {
319 # No ref in p1 so use p2's value
320 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
321 $p1 = $p2;
322 } else {
323 # Both are defined - handling the overloading
324 if (not defined $p1->{'default'}) {
325 if (defined $p2->{'default'}) {
326 $p1->{'default'} = $p2->{'default'};
327 }
328 }
329
330 if (not defined $p1->{$ENV{'PBPROJ'}}) {
331 if (defined $p2->{$ENV{'PBPROJ'}}) {
332 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
333 } else {
334 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
335 }
336 }
337 # Now copy back into p1 all p2 content which doesn't exist in p1
338 # p1 content always has priority over p2
339 foreach my $k (keys %$p2) {
340 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
341 }
342 }
343 }
344 $h->{$params[$i]} = $p1;
345}
346pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
347}
348
349=item B<pb_conf_get>
350
351This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
352
353=cut
354
355sub pb_conf_get {
356
357my @param = @_;
358my @return = pb_conf_get_if(@param);
359my $proj = undef;
360
361if (not defined $ENV{'PBPROJ'}) {
362 $proj = "unknown";
363} else {
364 $proj = $ENV{'PBPROJ'};
365}
366
367die "No params found for $proj" if (not @return);
368
369foreach my $i (0..$#param) {
370 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
371}
372return(@return);
373}
374
375
376=item B<pb_conf_print>
377
378This function prints every configuration parameter in order to help debug stacking issues with conf files
379
380=cut
381
382sub pb_conf_print {
383
384pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
385pb_log(0,"====================================\n");
386foreach my $k (sort keys %$h) {
387 pb_log(0,"$k => ".Dumper($h->{$k})."\n");
388}
389}
390
391=back
392
393=head1 WEB SITES
394
395The 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/>.
396
397=head1 USER MAILING LIST
398
399None exists for the moment.
400
401=head1 AUTHORS
402
403The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
404
405=head1 COPYRIGHT
406
407Project-Builder.org is distributed under the GPL v2.0 license
408described in the file C<COPYING> included with the distribution.
409
410=cut
411
412
4131;
Note: See TracBrowser for help on using the repository browser.