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

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