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

Last change on this file since 424 was 424, checked in by Bruno Cornec, 16 years ago
  • Fix a bug in Env.pm (forgot a use)
  • Fix a bug in pb_conf_get_fromfile_if (using last instead of next) and fix #24
File size: 7.0 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");
211pb_log(2,"DEBUG: pb_conf_get param: ".Dumper(@param)."\n");
212
213foreach my $i (0..$#param) {
214 $p1 = $ptr1[$i];
215 $p2 = $ptr2[$i];
216 # Always try to take the param from ptr1
217 # in order to mask what could be defined already in ptr2
218 if (not defined $p2) {
219 # exit if no p1 either
220 next if ((not defined $p1) || (not defined $ENV{'PBPROJ'}));
221 # No ref in p2 so use p1
222 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
223 } else {
224 # Ref found in p2
225 if (not defined $p1) {
226 # No ref in p1 so use p2's value
227 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
228 $p1 = $p2;
229 } else {
230 # Both are defined - handling the overloading
231 if (not defined $p1->{'default'}) {
232 if (defined $p2->{'default'}) {
233 $p1->{'default'} = $p2->{'default'};
234 }
235 }
236
237 if (not defined $p1->{$ENV{'PBPROJ'}}) {
238 if (defined $p2->{$ENV{'PBPROJ'}}) {
239 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
240 } else {
241 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
242 }
243 }
244 # Now copy back into p1 all p2 content which doesn't exist in p1
245 # p1 content always has priority over p2
246 foreach my $k (keys %$p2) {
247 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
248 }
249 }
250 }
251 $ptr1[$i] = $p1;
252}
253pb_log(2,"DEBUG: pb_conf_get output: ".Dumper(@ptr1)."\n");
254return(\@ptr1);
255}
256
257=item B<pb_conf_get>
258
259This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
260
261=cut
262
263sub pb_conf_get {
264
265my @param = @_;
266my @return = pb_conf_get_if(@param);
267
268die "No params found for $ENV{'PBPROJ'}" if (not @return);
269
270foreach my $i (0..$#param) {
271 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);
272}
273return(@return);
274}
275
276=back
277
278=head1 WEB SITES
279
280The 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/>.
281
282=head1 USER MAILING LIST
283
284None exists for the moment.
285
286=head1 AUTHORS
287
288The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
289
290=head1 COPYRIGHT
291
292Project-Builder.org is distributed under the GPL v2.0 license
293described in the file C<COPYING> included with the distribution.
294
295=cut
296
297
2981;
Note: See TracBrowser for help on using the repository browser.