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

Last change on this file since 416 was 416, checked in by Bruno Cornec, 16 years ago

move the pb_env_init function to a separate module to allow pbinit usage

File size: 6.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 list of conf files
27our @pbconffiles = ();
28
29=pod
30
31=head1 NAME
32
33ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
34
35=head1 DESCRIPTION
36
37This modules provides functions dealing with configuration files.
38
39=head1 SYNOPSIS
40
41 use ProjectBuilder::Conf;
42
43 #
44 # Read hash codes of values from a configuration file and return table of pointers
45 #
46 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
47 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
48
49=head1 USAGE
50
51=over 4
52
53=item B<pb_conf_add>
54
55This function adds the configuration file to the list last.
56
57=cut
58
59sub pb_conf_add {
60
61pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
62push(@pbconffiles,@_);
63}
64
65=item B<pb_conf_read_if>
66
67This function returns a table of pointers on hashes
68corresponding to the keys in a configuration file passed in parameter.
69If that file doesn't exist, it returns undef.
70
71The format of the configuration file is as follows:
72
73key tag = value1,value2,...
74
75Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
76
77 $ cat $HOME/.pbrc
78 pbver pb = 3
79 pbver default = 1
80 pblist pb = 12,25
81
82calling it like this:
83
84 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
85
86will allow to get the mapping:
87
88 $k1->{'pb'} contains 3
89 $k1->{'default'} contains 1
90 $k2->{'pb'} contains 12,25
91
92Valid chars for keys and tags are letters, numbers, '-' and '_'.
93
94=cut
95
96sub pb_conf_read_if {
97
98my $conffile = shift;
99my @param = @_;
100
101open(CONF,$conffile) || return((undef));
102close(CONF);
103return(pb_conf_read($conffile,@param));
104}
105
106=item B<pb_conf_read>
107
108This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
109
110=cut
111
112sub pb_conf_read {
113
114my $conffile = shift;
115my @param = @_;
116my $trace;
117my @ptr;
118my %h;
119
120open(CONF,$conffile) || die "Unable to open $conffile";
121while(<CONF>) {
122 if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
123 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
124 $h{$1}{$2}=$3;
125 }
126}
127close(CONF);
128
129for my $param (@param) {
130 push @ptr,$h{$param};
131}
132return(@ptr);
133}
134
135=item B<pb_conf_get_if>
136
137This 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.
138
139The format of the configurations file is as follows:
140
141key tag = value1,value2,...
142
143It will gather the values from all the configurations files passed to pb_conf_init, and return the values for the keys, taking in account the order of conf files, to manage overloading.
144
145 $ cat $HOME/.pbrc
146 pbver pb = 1
147 pblist pb = 4
148 $ cat $HOME/.pbrc2
149 pbver pb = 3
150 pblist default = 5
151
152calling it like this:
153
154 pb_conf_init("$HOME/.pbrc","$HOME/.pbrc2");
155 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
156
157will allow to get the mapping:
158
159 $k1->{'pb'} contains 3
160 $k2->{'pb'} contains 4
161
162Valid chars for keys and tags are letters, numbers, '-' and '_'.
163
164=cut
165
166sub pb_conf_get_if {
167
168my @param = @_;
169
170my $ptr = undef;
171
172# the most important conf file is first, so read them in revers order
173foreach my $f (reverse @pbconffiles) {
174 $ptr = pb_conf_get_fromfile_if("$f",$ptr,@param);
175}
176
177return(@$ptr);
178}
179
180=item B<pb_conf_fromfile_if>
181
182This 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.
183
184 my ($k1) = pb_conf_fromfile_if("$HOME/.pbrc",undef,"pbver","pblist");
185 my ($k2) = pb_conf_fromfile_if("$HOME/.pbrc3",$k1,"pbver","pblist");
186
187It is used internally by pb_conf_get_if and is not exported yet.
188
189=cut
190
191
192sub pb_conf_get_fromfile_if {
193
194my $conffile = shift;
195my $ptr2 = shift || undef;
196my @param = @_;
197
198# Everything is returned via ptr1
199my @ptr1 = ();
200my @ptr2 = ();
201
202# @ptr1 contains the values overloading what @ptr2 may contain.
203@ptr1 = pb_conf_read_if("$conffile", @param) if (defined $conffile);
204@ptr2 = @$ptr2 if (defined $ptr2);
205
206my $p1;
207my $p2;
208
209pb_log(2,"DEBUG: pb_conf_get $conffile: ".Dumper(@ptr1)."\n");
210pb_log(2,"DEBUG: pb_conf_get input: ".Dumper(@ptr2)."\n");
211
212foreach my $i (0..$#param) {
213 $p1 = $ptr1[$i];
214 $p2 = $ptr2[$i];
215 # Always try to take the param from ptr1
216 # in order to mask what could be defined already in ptr2
217 if (not defined $p2) {
218 # exit if no p1 either
219 last if ((not defined $p1) || (not defined $ENV{'PBPROJ'}));
220 # No ref in p2 so use p1
221 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
222 } else {
223 # Ref found in p2
224 if (not defined $p1) {
225 # No ref in p1 so use p2's value
226 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
227 $p1 = $p2;
228 } else {
229 # Both are defined - handling the overloading
230 if (not defined $p1->{'default'}) {
231 if (defined $p2->{'default'}) {
232 $p1->{'default'} = $p2->{'default'};
233 }
234 }
235
236 if (not defined $p1->{$ENV{'PBPROJ'}}) {
237 if (defined $p2->{$ENV{'PBPROJ'}}) {
238 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
239 } else {
240 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
241 }
242 }
243 # Now copy back into p1 all p2 content which doesn't exist in p1
244 # p1 content always has priority over p2
245 foreach my $k (keys %$p2) {
246 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
247 }
248 }
249 }
250 $ptr1[$i] = $p1;
251}
252pb_log(2,"DEBUG: pb_conf_get output: ".Dumper(@ptr1)."\n");
253return(\@ptr1);
254}
255
256=item B<pb_conf_get>
257
258This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
259
260=cut
261
262sub pb_conf_get {
263
264my @param = @_;
265my @return = pb_conf_get_if(@param);
266
267die "No params found for $ENV{'PBPROJ'}" if (not @return);
268
269foreach my $i (0..$#param) {
270 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);
271}
272return(@return);
273}
274
275=back
276
277=head1 WEB SITES
278
279The 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/>.
280
281=head1 USER MAILING LIST
282
283None exists for the moment.
284
285=head1 AUTHORS
286
287The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
288
289=head1 COPYRIGHT
290
291Project-Builder.org is distributed under the GPL v2.0 license
292described in the file C<COPYING> included with the distribution.
293
294=cut
295
296
2971;
Note: See TracBrowser for help on using the repository browser.