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

Last change on this file since 1507 was 1507, checked in by Bruno Cornec, 12 years ago
  • pb.conf: installing project-builder requires --force-yes since the keys don't get installed into the virtual environment. (Eric Anderson from d3145b5c2c63cf9cf00ffa818fa03a432a2e6e45)
  • pb.conf.pod: Fix lots of typos (coma -> comma) Document new ftp_proxy and http_proxy options. Document the rbsmirrorsrv option (already supported, not documented) Document that sshlogin and sshport are now optional parameters; both have reasonable defaults (not in that patch, will come later) (Eric Anderson from d3145b5c2c63cf9cf00ffa818fa03a432a2e6e45)
  • Conf.pm: Use confess so when failing to get a parameter we get a stack trace. (Eric Anderson from d3145b5c2c63cf9cf00ffa818fa03a432a2e6e45)
  • Distribution.pm: Fix typo (Donwloading -> Downloading); Fix code so that you can try to setup a VE multiple times. Existing debian code would fail the second time since pb.conf would already exist. Unclear on correct semantics here; the rpm code just force overwrites the file, but the old debian code was trying not to. (Eric Anderson from d3145b5c2c63cf9cf00ffa818fa03a432a2e6e45)
  • Distribution.pm: Adding the internal function pb_distro_compare_repo to avoid code duplication between apt and yum repos treatment (Bruno Cornec)
File size: 9.6 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 Carp 'confess';
14use Data::Dumper;
15use ProjectBuilder::Base;
16use ProjectBuilder::Version;
17
18# Inherit from the "Exporter" module which handles exporting functions.
19
20use vars qw($VERSION $REVISION @ISA @EXPORT);
21use Exporter;
22
23# Export, by default, all the functions into the namespace of
24# any code which uses this module.
25
26our @ISA = qw(Exporter);
27our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if pb_conf_print);
28($VERSION,$REVISION) = pb_version_init();
29
30# Global hash of conf files
31# Key is the conf file name
32# Value is its rank
33my %pbconffiles;
34
35# Global hash of conf file content
36# Key is the config keyword
37# Value is a hash whose key depends on the nature of the config keyword as documented
38# and value is the confguration value
39# We consider that values can not change during the life of pb
40my $h = ();
41
42=pod
43
44=head1 NAME
45
46ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
47
48=head1 DESCRIPTION
49
50This modules provides functions dealing with configuration files.
51
52=head1 SYNOPSIS
53
54 use ProjectBuilder::Conf;
55
56 #
57 # Read hash codes of values from a configuration file and return table of pointers
58 #
59 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
60 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
61
62=head1 USAGE
63
64=over 4
65
66=item B<pb_conf_init>
67
68This function setup the environment PBPROJ for project-builder function usage from other projects.
69The first parameter is the project name.
70It sets up environment variables (PBPROJ)
71
72=cut
73
74sub pb_conf_init {
75
76my $proj=shift || undef;
77
78pb_log(1,"Entering pb_conf_init\n");
79if (defined $proj) {
80 $ENV{'PBPROJ'} = $proj;
81} else {
82 $ENV{'PBPROJ'} = "default";
83}
84pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
85}
86
87
88=item B<pb_conf_cache>
89
90This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
91It returns the modified hash
92Can be used in correlation with the %h hash to store permanently values or not if temporarily.
93
94=cut
95
96sub pb_conf_cache {
97
98my $cf = shift;
99my $lh = shift;
100
101# Read the content of the config file and cache it in the %h hash further availble for queries
102open(CONF,$cf) || die "Unable to open $cf";
103while(<CONF>) {
104 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.+)$/) {
105 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
106 $lh->{$1}->{$2}=$3;
107 }
108}
109close(CONF);
110return($lh);
111}
112
113=item B<pb_conf_add>
114
115This function adds the configuration file to the list last, and cache their content in the %h hash
116
117=cut
118
119sub pb_conf_add {
120
121pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
122my $lh;
123
124foreach my $cf (@_) {
125 if (! -r $cf) {
126 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
127 next;
128 }
129 # Skip already used conf files
130 return($lh) if (defined $pbconffiles{$cf});
131
132 # Add the new one at the end
133 my $num = keys %pbconffiles;
134 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
135 $pbconffiles{$cf} = $num;
136
137 # Read the content of the config file
138 $lh = pb_conf_cache($cf,$lh);
139 # and cache it in the %h hash for further queries but after the previous
140 # as we load conf files in reverse order (most precise first)
141 pb_conf_add_last_in_hash($lh)
142}
143}
144
145
146=item B<pb_conf_read_if>
147
148This function returns a table of pointers on hashes
149corresponding to the keys in a configuration file passed in parameter.
150If that file doesn't exist, it returns undef.
151
152The format of the configuration file is as follows:
153
154key tag = value1,value2,...
155
156Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
157
158 $ cat $HOME/.pbrc
159 pbver pb = 3
160 pbver default = 1
161 pblist pb = 12,25
162
163calling it like this:
164
165 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
166
167will allow to get the mapping:
168
169 $k1->{'pb'} contains 3
170 $k1->{'default'} contains 1
171 $k2->{'pb'} contains 12,25
172
173Valid chars for keys and tags are letters, numbers, '-' and '_'.
174
175The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
176
177=cut
178
179sub pb_conf_read_if {
180
181my $conffile = shift;
182my @param = @_;
183
184open(CONF,$conffile) || return((undef));
185close(CONF);
186return(pb_conf_read($conffile,@param));
187}
188
189=item B<pb_conf_read>
190
191This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
192
193=cut
194
195sub pb_conf_read {
196
197my $conffile = shift;
198my @param = @_;
199my @ptr;
200my $lh;
201
202$lh = pb_conf_cache($conffile,$lh);
203
204foreach my $param (@param) {
205 push @ptr,$lh->{$param};
206}
207return(@ptr);
208}
209
210
211
212=item B<pb_conf_get_in_hash_if>
213
214This function returns a table, corresponding to a set of values queried in the hash passe in parameter or undef if it doen't exist. It takes a table of keys as an input parameter.
215
216=cut
217
218sub pb_conf_get_in_hash_if {
219
220my $lh = shift || return(());
221my @params = @_;
222my @ptr = ();
223
224pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
225foreach my $k (@params) {
226 push @ptr,$lh->{$k};
227}
228
229pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
230return(@ptr);
231}
232
233
234
235=item B<pb_conf_get_if>
236
237This function returns a table, corresponding to a set of values queried in the %h hash or undef if it doen't exist. It takes a table of keys as an input parameter.
238
239The format of the configurations file is as follows:
240
241key tag = value1,value2,...
242
243It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
244
245 $ cat $HOME/.pbrc
246 pbver pb = 1
247 pblist pb = 4
248 $ cat $HOME/.pbrc2
249 pbver pb = 3
250 pblist default = 5
251
252calling it like this:
253
254 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
255 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
256
257will allow to get the mapping:
258
259 $k1->{'pb'} contains 3
260 $k2->{'pb'} contains 4
261
262Valid chars for keys and tags are letters, numbers, '-' and '_'.
263
264=cut
265
266sub pb_conf_get_if {
267
268my @params = @_;
269my @ptr = undef;
270
271return(pb_conf_get_in_hash_if($h,@params));
272}
273
274=item B<pb_conf_add_last_in_hash>
275
276This function merges the values passed in the hash parameter into the %h hash, but only if itdoesn't already contain a value, or if the value is more precise (real value instead of default)
277
278It is used internally by pb_conf_add and is not exported.
279
280=cut
281
282sub pb_conf_add_last_in_hash {
283
284my $ptr = shift || undef;
285
286return if (not defined $ptr);
287# TODO: test $ptr is a hash pointer
288
289my @params = (sort keys %$ptr);
290
291# Everything is returned via @h
292# @h contains the values overloading what @ptr may contain.
293my @h = pb_conf_get_if(@params);
294my @ptr = pb_conf_get_in_hash_if($ptr,@params);
295
296my $p1;
297my $p2;
298
299pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
300pb_log(2,"DEBUG: pb_conf_add_last_in_hash hash: ".Dumper(@h)."\n");
301pb_log(2,"DEBUG: pb_conf_add_last_in_hash input: ".Dumper(@ptr)."\n");
302
303foreach my $i (0..$#params) {
304 $p1 = $h[$i];
305 $p2 = $ptr[$i];
306 # Always try to take the param from h
307 # in order to mask what could be defined already in ptr
308 if (not defined $p2) {
309 # exit if no p1 either
310 next if ((not defined $p1) || (not defined $ENV{'PBPROJ'}));
311 # No ref in p2 so use p1
312 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
313 } else {
314 # Ref found in p2
315 if (not defined $p1) {
316 # No ref in p1 so use p2's value
317 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
318 $p1 = $p2;
319 } else {
320 # Both are defined - handling the overloading
321 if (not defined $p1->{'default'}) {
322 if (defined $p2->{'default'}) {
323 $p1->{'default'} = $p2->{'default'};
324 }
325 }
326
327 if (not defined $p1->{$ENV{'PBPROJ'}}) {
328 if (defined $p2->{$ENV{'PBPROJ'}}) {
329 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
330 } else {
331 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
332 }
333 }
334 # Now copy back into p1 all p2 content which doesn't exist in p1
335 # p1 content always has priority over p2
336 foreach my $k (keys %$p2) {
337 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
338 }
339 }
340 }
341 $h->{$params[$i]} = $p1;
342}
343pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
344}
345
346=item B<pb_conf_get>
347
348This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
349
350=cut
351
352sub pb_conf_get {
353
354my @param = @_;
355my @return = pb_conf_get_if(@param);
356my $proj = undef;
357
358if (not defined $ENV{'PBPROJ'}) {
359 $proj = "unknown";
360} else {
361 $proj = $ENV{'PBPROJ'};
362}
363
364die "No params found for $proj" if (not @return);
365
366foreach my $i (0..$#param) {
367 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
368}
369return(@return);
370}
371
372
373=item B<pb_conf_print>
374
375This function prints every configuration parameter in order to help debug stacking issues with conf files
376
377=cut
378
379sub pb_conf_print {
380
381pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
382pb_log(0,"====================================\n");
383foreach my $k (sort keys %$h) {
384 pb_log(0,"$k => ".Dumper($h->{$k})."\n");
385}
386}
387
388=back
389
390=head1 WEB SITES
391
392The 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/>.
393
394=head1 USER MAILING LIST
395
396None exists for the moment.
397
398=head1 AUTHORS
399
400The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
401
402=head1 COPYRIGHT
403
404Project-Builder.org is distributed under the GPL v2.0 license
405described in the file C<COPYING> included with the distribution.
406
407=cut
408
409
4101;
Note: See TracBrowser for help on using the repository browser.