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

Last change on this file since 898 was 898, checked in by Bruno Cornec, 14 years ago
  • Do not add conf files if already present in the list (changing pbconffiles into a hash for that to still keep order as this is mandatory)
  • Small optimisations for pb_conf function usage in pb
File size: 7.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# $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 $pbconffiles{$cf} = $num;
96}
97}
98
99=item B<pb_conf_read_if>
100
101This function returns a table of pointers on hashes
102corresponding to the keys in a configuration file passed in parameter.
103If that file doesn't exist, it returns undef.
104
105The format of the configuration file is as follows:
106
107key tag = value1,value2,...
108
109Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
110
111 $ cat $HOME/.pbrc
112 pbver pb = 3
113 pbver default = 1
114 pblist pb = 12,25
115
116calling it like this:
117
118 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
119
120will allow to get the mapping:
121
122 $k1->{'pb'} contains 3
123 $k1->{'default'} contains 1
124 $k2->{'pb'} contains 12,25
125
126Valid chars for keys and tags are letters, numbers, '-' and '_'.
127
128=cut
129
130sub pb_conf_read_if {
131
132my $conffile = shift;
133my @param = @_;
134
135open(CONF,$conffile) || return((undef));
136close(CONF);
137return(pb_conf_read($conffile,@param));
138}
139
140=item B<pb_conf_read>
141
142This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
143
144=cut
145
146sub pb_conf_read {
147
148my $conffile = shift;
149my @param = @_;
150my $trace;
151my @ptr;
152my %h;
153
154open(CONF,$conffile) || die "Unable to open $conffile";
155while(<CONF>) {
156 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
157 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
158 $h{$1}{$2}=$3;
159 }
160}
161close(CONF);
162
163for my $param (@param) {
164 push @ptr,$h{$param};
165}
166return(@ptr);
167}
168
169=item B<pb_conf_get_if>
170
171This function returns a table, corresponding to a set of values querried in the conf files or undef if it doen't exist. It takes a table of keys as an input parameter.
172
173The format of the configurations file is as follows:
174
175key tag = value1,value2,...
176
177It 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.
178
179 $ cat $HOME/.pbrc
180 pbver pb = 1
181 pblist pb = 4
182 $ cat $HOME/.pbrc2
183 pbver pb = 3
184 pblist default = 5
185
186calling it like this:
187
188 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
189 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
190
191will allow to get the mapping:
192
193 $k1->{'pb'} contains 3
194 $k2->{'pb'} contains 4
195
196Valid chars for keys and tags are letters, numbers, '-' and '_'.
197
198=cut
199
200sub pb_conf_get_if {
201
202my @param = @_;
203
204my $ptr = undef;
205
206# the most important conf file is first, so read them in reverse order
207foreach my $f (reverse sort { $pbconffiles{$a} <=> $pbconffiles{$b} } keys %pbconffiles) {
208 $ptr = pb_conf_get_fromfile_if("$f",$ptr,@param);
209}
210
211return(@$ptr);
212}
213
214=item B<pb_conf_fromfile_if>
215
216This function returns a pointer on a table, corresponding to a merge of values querried in the conf file and the pointer on another table passed as parameter. It takes a table of keys as last input parameter.
217
218 my ($k1) = pb_conf_fromfile_if("$HOME/.pbrc",undef,"pbver","pblist");
219 my ($k2) = pb_conf_fromfile_if("$HOME/.pbrc3",$k1,"pbver","pblist");
220
221It is used internally by pb_conf_get_if and is not exported yet.
222
223=cut
224
225
226sub pb_conf_get_fromfile_if {
227
228my $conffile = shift;
229my $ptr2 = shift || undef;
230my @param = @_;
231
232# Everything is returned via ptr1
233my @ptr1 = ();
234my @ptr2 = ();
235
236# @ptr1 contains the values overloading what @ptr2 may contain.
237@ptr1 = pb_conf_read_if("$conffile", @param) if (defined $conffile);
238@ptr2 = @$ptr2 if (defined $ptr2);
239
240my $p1;
241my $p2;
242
243pb_log(2,"DEBUG: pb_conf_get $conffile: ".Dumper(@ptr1)."\n");
244pb_log(2,"DEBUG: pb_conf_get input: ".Dumper(@ptr2)."\n");
245pb_log(2,"DEBUG: pb_conf_get param: ".Dumper(@param)."\n");
246
247foreach my $i (0..$#param) {
248 $p1 = $ptr1[$i];
249 # Optimisation doesn't seem useful
250 # if ((defined $p1) && (defined $cachedval{$p1})) {
251 # $ptr1[$i] = $cachedval{$p1};
252 # next;
253 # }
254 $p2 = $ptr2[$i];
255 # Always try to take the param from ptr1
256 # in order to mask what could be defined already in ptr2
257 if (not defined $p2) {
258 # exit if no p1 either
259 next if ((not defined $p1) || (not defined $ENV{'PBPROJ'}));
260 # No ref in p2 so use p1
261 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
262 } else {
263 # Ref found in p2
264 if (not defined $p1) {
265 # No ref in p1 so use p2's value
266 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
267 $p1 = $p2;
268 } else {
269 # Both are defined - handling the overloading
270 if (not defined $p1->{'default'}) {
271 if (defined $p2->{'default'}) {
272 $p1->{'default'} = $p2->{'default'};
273 }
274 }
275
276 if (not defined $p1->{$ENV{'PBPROJ'}}) {
277 if (defined $p2->{$ENV{'PBPROJ'}}) {
278 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
279 } else {
280 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
281 }
282 }
283 # Now copy back into p1 all p2 content which doesn't exist in p1
284 # p1 content always has priority over p2
285 foreach my $k (keys %$p2) {
286 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
287 }
288 }
289 }
290 $ptr1[$i] = $p1;
291 # Cache values to avoid redoing all that analyze when asked again on a known value
292 # $cachedval{$p1} = $p1;
293}
294pb_log(2,"DEBUG: pb_conf_get output: ".Dumper(@ptr1)."\n");
295return(\@ptr1);
296}
297
298=item B<pb_conf_get>
299
300This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
301
302=cut
303
304sub pb_conf_get {
305
306my @param = @_;
307my @return = pb_conf_get_if(@param);
308
309die "No params found for $ENV{'PBPROJ'}" if (not @return);
310
311foreach my $i (0..$#param) {
312 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);
313}
314return(@return);
315}
316
317=back
318
319=head1 WEB SITES
320
321The 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/>.
322
323=head1 USER MAILING LIST
324
325None exists for the moment.
326
327=head1 AUTHORS
328
329The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
330
331=head1 COPYRIGHT
332
333Project-Builder.org is distributed under the GPL v2.0 license
334described in the file C<COPYING> included with the distribution.
335
336=cut
337
338
3391;
Note: See TracBrowser for help on using the repository browser.