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

Last change on this file since 1102 was 1052, checked in by Bruno Cornec, 14 years ago

r3866@localhost: bruno | 2010-06-09 23:43:23 +0200

  • Add support for LSB 3.2
File size: 8.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# $Id$
8#
9
10package ProjectBuilder::Conf;
11
12use strict;
13use Data::Dumper;
14use ProjectBuilder::Base;
15
16# Inherit from the "Exporter" module which handles exporting functions.
17
18use Exporter;
19
20# Export, by default, all the functions into the namespace of
21# any code which uses this module.
22
23our @ISA = qw(Exporter);
24our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if);
25
26# Global hash of conf files
27# Key is the conf file name
28# Value is its rank
29my %pbconffiles;
30
31# Global hash of cached values.
32# We consider that values can not change during the life of pb
33# my %cachedval;
34
35=pod
36
37=head1 NAME
38
39ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
40
41=head1 DESCRIPTION
42
43This modules provides functions dealing with configuration files.
44
45=head1 SYNOPSIS
46
47 use ProjectBuilder::Conf;
48
49 #
50 # Read hash codes of values from a configuration file and return table of pointers
51 #
52 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
53 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
54
55=head1 USAGE
56
57=over 4
58
59=item B<pb_conf_init>
60
61This function setup the environment PBPROJ for project-builder function usage from other projects.
62The first parameter is the project name.
63It sets up environment variables (PBPROJ)
64
65=cut
66
67sub pb_conf_init {
68
69my $proj=shift || undef;
70
71if (defined $proj) {
72 $ENV{'PBPROJ'} = $proj;
73} else {
74 $ENV{'PBPROJ'} = "default";
75}
76}
77
78
79
80=item B<pb_conf_add>
81
82This function adds the configuration file to the list last.
83
84=cut
85
86sub pb_conf_add {
87
88pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
89
90foreach my $cf (@_) {
91 # Skip already used conf files
92 next if (defined $pbconffiles{$cf});
93 # Add the new one at the end
94 my $num = keys %pbconffiles;
95 pb_log(2,"DEBUG: pb_conf_add $cf at position $num\n");
96 $pbconffiles{$cf} = $num;
97 pb_log(0,"WARNING: pb_conf_add can not read $cf\n") if (! -r $cf);
98}
99}
100
101=item B<pb_conf_read_if>
102
103This function returns a table of pointers on hashes
104corresponding to the keys in a configuration file passed in parameter.
105If that file doesn't exist, it returns undef.
106
107The format of the configuration file is as follows:
108
109key tag = value1,value2,...
110
111Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
112
113 $ cat $HOME/.pbrc
114 pbver pb = 3
115 pbver default = 1
116 pblist pb = 12,25
117
118calling it like this:
119
120 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
121
122will allow to get the mapping:
123
124 $k1->{'pb'} contains 3
125 $k1->{'default'} contains 1
126 $k2->{'pb'} contains 12,25
127
128Valid chars for keys and tags are letters, numbers, '-' and '_'.
129
130=cut
131
132sub pb_conf_read_if {
133
134my $conffile = shift;
135my @param = @_;
136
137open(CONF,$conffile) || return((undef));
138close(CONF);
139return(pb_conf_read($conffile,@param));
140}
141
142=item B<pb_conf_read>
143
144This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
145
146=cut
147
148sub pb_conf_read {
149
150my $conffile = shift;
151my @param = @_;
152my $trace;
153my @ptr;
154my %h;
155
156open(CONF,$conffile) || die "Unable to open $conffile";
157while(<CONF>) {
158 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
159 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
160 $h{$1}{$2}=$3;
161 }
162}
163close(CONF);
164
165for my $param (@param) {
166 push @ptr,$h{$param};
167}
168return(@ptr);
169}
170
171=item B<pb_conf_get_if>
172
173This function returns a table, corresponding to a set of values queried in the conf files or undef if it doen't exist. It takes a table of keys as an input parameter.
174
175The format of the configurations file is as follows:
176
177key tag = value1,value2,...
178
179It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys, taking in account the order of conf files, to manage overloading.
180
181 $ cat $HOME/.pbrc
182 pbver pb = 1
183 pblist pb = 4
184 $ cat $HOME/.pbrc2
185 pbver pb = 3
186 pblist default = 5
187
188calling it like this:
189
190 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
191 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
192
193will allow to get the mapping:
194
195 $k1->{'pb'} contains 3
196 $k2->{'pb'} contains 4
197
198Valid chars for keys and tags are letters, numbers, '-' and '_'.
199
200=cut
201
202sub pb_conf_get_if {
203
204my @param = @_;
205
206my $ptr = undef;
207
208# the most important conf file is first, so read them in reverse order
209foreach my $f (reverse sort { $pbconffiles{$a} <=> $pbconffiles{$b} } keys %pbconffiles) {
210 pb_log(2,"DEBUG: pb_conf_get_if in file $f\n");
211 $ptr = pb_conf_get_fromfile_if("$f",$ptr,@param);
212}
213
214return(@$ptr);
215}
216
217=item B<pb_conf_fromfile_if>
218
219This function returns a pointer on a table, corresponding to a merge of values queried in the conf file and the pointer on another table passed as parameter. It takes a table of keys as last input parameter.
220
221 my ($k1) = pb_conf_fromfile_if("$HOME/.pbrc",undef,"pbver","pblist");
222 my ($k2) = pb_conf_fromfile_if("$HOME/.pbrc3",$k1,"pbver","pblist");
223
224It is used internally by pb_conf_get_if and is not exported yet.
225
226=cut
227
228
229sub pb_conf_get_fromfile_if {
230
231my $conffile = shift;
232my $ptr2 = shift || undef;
233my @param = @_;
234
235# Everything is returned via ptr1
236my @ptr1 = ();
237my @ptr2 = ();
238
239# @ptr1 contains the values overloading what @ptr2 may contain.
240@ptr1 = pb_conf_read_if("$conffile", @param) if (defined $conffile);
241@ptr2 = @$ptr2 if (defined $ptr2);
242
243my $p1;
244my $p2;
245
246pb_log(2,"DEBUG: pb_conf_get_from_file $conffile: ".Dumper(@ptr1)."\n");
247pb_log(2,"DEBUG: pb_conf_get_from_file input: ".Dumper(@ptr2)."\n");
248pb_log(2,"DEBUG: pb_conf_get_from_file param: ".Dumper(@param)."\n");
249
250foreach my $i (0..$#param) {
251 $p1 = $ptr1[$i];
252 # Optimisation doesn't seem useful
253 # if ((defined $p1) && (defined $cachedval{$p1})) {
254 # $ptr1[$i] = $cachedval{$p1};
255 # next;
256 # }
257 $p2 = $ptr2[$i];
258 # Always try to take the param from ptr1
259 # in order to mask what could be defined already in ptr2
260 if (not defined $p2) {
261 # exit if no p1 either
262 next if ((not defined $p1) || (not defined $ENV{'PBPROJ'}));
263 # No ref in p2 so use p1
264 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
265 } else {
266 # Ref found in p2
267 if (not defined $p1) {
268 # No ref in p1 so use p2's value
269 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
270 $p1 = $p2;
271 } else {
272 # Both are defined - handling the overloading
273 if (not defined $p1->{'default'}) {
274 if (defined $p2->{'default'}) {
275 $p1->{'default'} = $p2->{'default'};
276 }
277 }
278
279 if (not defined $p1->{$ENV{'PBPROJ'}}) {
280 if (defined $p2->{$ENV{'PBPROJ'}}) {
281 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
282 } else {
283 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
284 }
285 }
286 # Now copy back into p1 all p2 content which doesn't exist in p1
287 # p1 content always has priority over p2
288 foreach my $k (keys %$p2) {
289 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
290 }
291 }
292 }
293 $ptr1[$i] = $p1;
294 # Cache values to avoid redoing all that analyze when asked again on a known value
295 # $cachedval{$p1} = $p1;
296}
297pb_log(2,"DEBUG: pb_conf_get output: ".Dumper(@ptr1)."\n");
298return(\@ptr1);
299}
300
301=item B<pb_conf_get>
302
303This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
304
305=cut
306
307sub pb_conf_get {
308
309my @param = @_;
310my @return = pb_conf_get_if(@param);
311my $proj = undef;
312
313if (not defined $ENV{'PBPROJ'}) {
314 $proj = "unknown";
315} else {
316 $proj = $ENV{'PBPROJ'};
317}
318
319die "No params found for $proj" if (not @return);
320
321foreach my $i (0..$#param) {
322 die "No $param[$i] defined for $proj" if (not defined $return[$i]);
323}
324return(@return);
325}
326
327=back
328
329=head1 WEB SITES
330
331The 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/>.
332
333=head1 USER MAILING LIST
334
335None exists for the moment.
336
337=head1 AUTHORS
338
339The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
340
341=head1 COPYRIGHT
342
343Project-Builder.org is distributed under the GPL v2.0 license
344described in the file C<COPYING> included with the distribution.
345
346=cut
347
348
3491;
Note: See TracBrowser for help on using the repository browser.