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

Last change on this file since 963 was 963, checked in by bruno, 10 years ago

r3658@localhost: bruno | 2010-02-07 00:15:25 +0100

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