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

Last change on this file since 2176 was 2176, checked in by Bruno Cornec, 7 years ago

Fix package based installation of pb

File size: 11.5 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# Copyright B. Cornec 2007-2016
8# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
9# Provided under the GPL v2
10#
11# $Id$
12#
13
14package ProjectBuilder::Conf;
15
16use strict;
17use Carp 'confess';
18use Data::Dumper;
19use ProjectBuilder::Base;
20use ProjectBuilder::Version;
21#use YAML;
22
23# Inherit from the "Exporter" module which handles exporting functions.
24
25use vars qw($VERSION $REVISION @ISA @EXPORT);
26use Exporter;
27
28# Export, by default, all the functions into the namespace of
29# any code which uses this module.
30
31our @ISA = qw(Exporter);
32our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_write pb_conf_get pb_conf_get_if pb_conf_get_all pb_conf_get_hash pb_conf_cache);
33($VERSION,$REVISION) = pb_version_init();
34
35# Global hash of conf files
36# Key is the conf file name
37# Value is its rank
38my %pbconffiles;
39
40# Global hash of conf file content
41# Key is the config keyword
42# Value is a hash whose key depends on the nature of the config keyword as documented
43# and value is the confguration value
44# We consider that values can not change during the life of pb
45my $h = ();
46
47=pod
48
49=head1 NAME
50
51ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
52
53=head1 DESCRIPTION
54
55This modules provides functions dealing with configuration files.
56
57=head1 SYNOPSIS
58
59 use ProjectBuilder::Conf;
60
61 #
62 # Read hash codes of values from a configuration file and return table of pointers
63 #
64 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
65 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
66
67=head1 USAGE
68
69The configuration files are loaded in a specific order from most generic to the most specific
70to allow for overwrite to work:
71
721. /usr/share/pb/pb.conf - the read-only system conf file provided by install
732. /etc/pb/pb.conf - the same global conf file given to the sysadmin in order to make system wide modifications
743. /path/to/project.pb - Configuration file for the project we're building for
754. /(vm|ve|rm)path/to/.pbrc - configuration file for VM, VE or RM specific parameters. Cumulative should be orthogonal
765. $HOME/.pbrc - user's configuration file
77
78=over 4
79
80=item B<pb_conf_init>
81
82This function setup the environment PBPROJ for project-builder function usage from other projects.
83The first parameter is the project name.
84It sets up environment variables (PBPROJ)
85
86=cut
87
88sub pb_conf_init {
89
90my $proj=shift;
91
92pb_log(1,"Entering pb_conf_init\n");
93#
94# Check project name
95# Could be with env var PBPROJ
96# or option -p
97# if not defined take the first in conf file
98#
99if ((defined $ENV{'PBPROJ'}) &&
100 (not defined $proj)) {
101 pb_log(2,"PBPROJ env var setup ($ENV{'PBPROJ'}) so using it\n");
102 $proj = $ENV{'PBPROJ'};
103}
104
105if (defined $proj) {
106 $ENV{'PBPROJ'} = $proj;
107} else {
108 $ENV{'PBPROJ'} = "default";
109}
110pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
111}
112
113
114=item B<pb_conf_cache>
115
116This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
117It returns the modified hash
118Can be used in correlation with the %h hash to store permanently values or not if temporarily.
119
120=cut
121
122sub pb_conf_cache {
123
124my $cf = shift;
125my $lh = shift;
126my $confver = "0.14";
127
128# Read the content of the config file and cache it in the %h hash then available for queries
129if ($confver < 0.15) {
130 open(CONF,$cf) || confess "Unable to open $cf";
131 # This is the original conf file format for versions up to 0.14
132 while(<CONF>) {
133 next if (/^#/);
134 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.\?\[\]\*\+\\]+)\s*=\s*(.*)$/) {
135 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
136 $lh->{$1}->{$2}=$3;
137 }
138 }
139 close(CONF);
140} else {
141 $lh = LoadFile($cf);
142}
143return($lh);
144}
145
146=item B<pb_conf_add>
147
148This function adds the configuration file to the list last, and cache their content in the %h hash
149
150=cut
151
152sub pb_conf_add {
153
154pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
155my $lh;
156
157foreach my $cf (@_) {
158 if (! -r $cf) {
159 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
160 next;
161 }
162 # Skip already used conf files
163 return($lh) if (defined $pbconffiles{$cf});
164
165 # The new conf file overload values already managed
166 my $num = keys %pbconffiles;
167 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
168 $pbconffiles{$cf} = $num;
169
170 # Read the content of the config file
171 $lh = pb_conf_cache($cf,$lh);
172 # and cache it in the %h hash for further queries but after the previous
173 # as we load conf files in reverse order (most precise first)
174 pb_conf_add_last_in_hash($lh)
175}
176}
177
178
179=item B<pb_conf_read_if>
180
181This function returns a table of pointers on hashes
182corresponding to the keys in a configuration file passed in parameter.
183If that file doesn't exist, it returns undef.
184
185The format of the configuration file is as follows:
186
187key tag = value1,value2,...
188
189Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
190
191 $ cat $HOME/.pbrc
192 pbver pb = 3
193 pbver default = 1
194 pblist pb = 12,25
195
196calling it like this:
197
198 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
199
200will allow to get the mapping:
201
202 $k1->{'pb'} contains 3
203 $k1->{'default'} contains 1
204 $k2->{'pb'} contains 12,25
205
206Valid chars for keys and tags are letters, numbers, '-' and '_'.
207
208The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
209
210=cut
211
212sub pb_conf_read_if {
213
214my $conffile = shift;
215my @param = @_;
216
217open(CONF,$conffile) || return((undef));
218close(CONF);
219return(pb_conf_read($conffile,@param));
220}
221
222=item B<pb_conf_read>
223
224This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
225
226=cut
227
228sub pb_conf_read {
229
230my $conffile = shift;
231my @param = @_;
232my @ptr;
233my $lh;
234
235$lh = pb_conf_cache($conffile,$lh);
236
237foreach my $param (@param) {
238 push @ptr,$lh->{$param};
239}
240return(@ptr);
241}
242
243=item B<pb_conf_write>
244
245This function writes in the file passed ias first parameter the hash of values passed as second parameter
246
247=cut
248
249sub pb_conf_write {
250
251my $conffile = shift;
252my $h = shift;
253my $confver = "0.14";
254
255confess "No configuration file defined to write into !" if (not defined $conffile);
256confess "No hash defined to read from !" if (not defined $h);
257open(CONF,"> $conffile") || confess "Unable to write into $conffile";
258
259if ($confver < 0.15) {
260 # This is the original conf file format for versions up to 0.14
261 foreach my $p (sort keys %$h) {
262 my $j = $h->{$p};
263 foreach my $k (sort keys %$j) {
264 print CONF "$p $k = $j->{$k}\n";
265 }
266 }
267} else {
268 # This is the new YAML format
269 print CONF Dump($h);
270}
271close(CONF);
272}
273
274
275
276=item B<pb_conf_get_in_hash_if>
277
278This function returns a table, corresponding to a set of values queried in the hash passed in parameter or undef if it doesn't exist.
279It takes a table of keys as an input parameter.
280
281=cut
282
283sub pb_conf_get_in_hash_if {
284
285my $lh = shift || return(());
286my @params = @_;
287my @ptr = ();
288
289pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
290foreach my $k (@params) {
291 push @ptr,$lh->{$k};
292}
293
294pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
295return(@ptr);
296}
297
298
299
300=item B<pb_conf_get_if>
301
302This 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.
303
304The format of the configurations file is as follows:
305
306key tag = value1,value2,...
307
308It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
309
310 $ cat $HOME/.pbrc
311 pbver pb = 1
312 pblist pb = 4
313 $ cat $HOME/.pbrc2
314 pbver pb = 3
315 pblist default = 5
316
317calling it like this:
318
319 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
320 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
321
322will allow to get the mapping:
323
324 $k1->{'pb'} contains 3
325 $k2->{'pb'} contains 4
326
327Valid chars for keys and tags are letters, numbers, '-' and '_'.
328
329=cut
330
331sub pb_conf_get_if {
332
333my @param = @_;
334my @return = pb_conf_get_in_hash_if($h,@_);
335my $proj = undef;
336
337if (not defined $ENV{'PBPROJ'}) {
338 $proj = "unknown";
339} else {
340 $proj = $ENV{'PBPROJ'};
341}
342
343foreach my $i (0..$#param) {
344 if (not defined $return[$i]->{$proj}) {
345 $return[$i]->{$proj} = $return[$i]->{'default'} if (defined $return[$i]->{'default'});
346 }
347}
348return(@return);
349}
350
351=item B<pb_conf_add_last_in_hash>
352
353This 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)
354
355It is used internally by pb_conf_add and is not exported.
356
357=cut
358
359sub pb_conf_add_last_in_hash {
360
361my $ptr = shift;
362
363return if (not defined $ptr);
364# TODO: test $ptr is a hash pointer
365
366# When called without correct initialization, try to work anyway with default as project
367pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
368
369my @params = (sort keys %$ptr);
370
371# Everything is returned via @h
372# @h contains the values overloading what @ptr may contain.
373my @h = pb_conf_get_in_hash_if($h,@params);
374my @ptr = pb_conf_get_in_hash_if($ptr,@params);
375
376my $p1;
377my $p2;
378
379pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
380pb_log(2,"DEBUG: pb_conf_add_last_in_hash current hash: ".Dumper(@h)."\n");
381pb_log(2,"DEBUG: pb_conf_add_last_in_hash new inputs: ".Dumper(@ptr)."\n");
382
383foreach my $i (0..$#params) {
384 $p1 = $h[$i];
385 $p2 = $ptr[$i];
386 # Always try to take the param from h in priority
387 # in order to mask what could be defined already in ptr
388 if (not defined $p2) {
389 # exit if no p1 either
390 next if (not defined $p1);
391 } else {
392 # Ref found in p2
393 if (not defined $p1) {
394 # No ref in p1 so use p2's value
395 $p1 = $p2;
396 } else {
397 # Both are defined - handling the overloading
398 # Now copy back into p1 all p2 content
399 # as p1 content always has priority over p2
400 if (not defined $p1->{$ENV{'PBPROJ'}}) {
401 if (defined $p2->{$ENV{'PBPROJ'}}) {
402 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
403 }
404 }
405 # Now copy back into p1 all p2 content which doesn't exist in p1
406 # # p1 content always has priority over p2
407 foreach my $k (keys %$p2) {
408 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
409 }
410 }
411 }
412 $h->{$params[$i]} = $p1;
413}
414pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
415}
416
417=item B<pb_conf_get>
418
419This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
420
421=cut
422
423sub pb_conf_get {
424
425my @param = @_;
426my @return = pb_conf_get_if(@param);
427my $proj = undef;
428
429if (not defined $ENV{'PBPROJ'}) {
430 $proj = "unknown";
431} else {
432 $proj = $ENV{'PBPROJ'};
433}
434
435confess "No params found for $proj" if (not @return);
436
437foreach my $i (0..$#param) {
438 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
439}
440return(@return);
441}
442
443
444=item B<pb_conf_get_all>
445
446This function returns an array with all configuration parameters
447
448=cut
449
450sub pb_conf_get_all {
451
452return(sort keys %$h);
453}
454
455
456=item B<pb_conf_get_hash>
457
458This function returns a pointer to the hash with all configuration parameters
459
460=cut
461
462sub pb_conf_get_hash {
463
464return($h);
465}
466
467=back
468
469=head1 WEB SITES
470
471The 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/>.
472
473=head1 USER MAILING LIST
474
475None exists for the moment.
476
477=head1 AUTHORS
478
479The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
480
481=head1 COPYRIGHT
482
483Project-Builder.org is distributed under the GPL v2.0 license
484described in the file C<COPYING> included with the distribution.
485
486=cut
487
488
4891;
Note: See TracBrowser for help on using the repository browser.