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

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

Start move to YAML conf files

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