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

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

pbdistrocheck + YAML works

  • Fix for yml main conf file to avoid duplicates
  • Fix for require analysis for YAML module
  • pbdistrocheck works with the main pb.yml file.
  • Need to convert all other configuration files into YAML format
File size: 12.3 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#
[2032]7# Copyright B. Cornec 2007-2016
[1528]8# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
9# Provided under the GPL v2
10#
[405]11# $Id$
12#
13
14package ProjectBuilder::Conf;
15
16use strict;
[1507]17use Carp 'confess';
[405]18use Data::Dumper;
19use ProjectBuilder::Base;
[1148]20use ProjectBuilder::Version;
[2241]21#use YAML;
[405]22
23# Inherit from the "Exporter" module which handles exporting functions.
24
[2241]25use vars qw($VERSION $REVISION @ISA @EXPORT);
[405]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);
[2154]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);
[2241]33($VERSION,$REVISION) = pb_version_init();
[405]34
[898]35# Global hash of conf files
36# Key is the conf file name
37# Value is its rank
38my %pbconffiles;
[409]39
[1495]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
[898]44# We consider that values can not change during the life of pb
[1495]45my $h = ();
[898]46
[405]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
[2152]69The configuration files are loaded in a specific order from most generic to the most specific
70to allow for overwrite to work:
71
[2241]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
[2152]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
[405]78=over 4
79
[505]80=item B<pb_conf_init>
81
[898]82This function setup the environment PBPROJ for project-builder function usage from other projects.
[505]83The first parameter is the project name.
[898]84It sets up environment variables (PBPROJ)
[505]85
86=cut
87
88sub pb_conf_init {
89
[1907]90my $proj=shift;
[505]91
[1495]92pb_log(1,"Entering pb_conf_init\n");
[1584]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
[505]105if (defined $proj) {
106 $ENV{'PBPROJ'} = $proj;
107} else {
108 $ENV{'PBPROJ'} = "default";
109}
[1495]110pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
[505]111}
112
113
[1495]114=item B<pb_conf_cache>
[505]115
[1495]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;
126
[2249]127my $ldfunc;
128
[2077]129# Read the content of the config file and cache it in the %h hash then available for queries
[2241]130if ($confver < 0.15) {
[2176]131 open(CONF,$cf) || confess "Unable to open $cf";
132 # This is the original conf file format for versions up to 0.14
133 while(<CONF>) {
134 next if (/^#/);
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 $lh->{$1}->{$2}=$3;
138 }
[1495]139 }
[2176]140 close(CONF);
141} else {
[2249]142 eval {
143 require YAML;
144 YAML->import();
145 };
146 if ($@) {
147 eval {
148 # No YAML found using a more std but less complete one. Old perl only
149 require Module::Build::YAML;
150 Module::Build::YAML->import();
151 };
152 if ($@) {
153 die "Unable to handle YAML configuration files without a YAML.pm module\n";
154 } else {
155 $ldfunc = \&Module::Build::YAML::LoadFile;
156 }
157 } else {
158 $ldfunc = \&YAML::LoadFile;
159 }
160
161 $lh = $ldfunc->($cf);
[1495]162}
163return($lh);
164}
165
[409]166=item B<pb_conf_add>
167
[1495]168This function adds the configuration file to the list last, and cache their content in the %h hash
[409]169
170=cut
171
172sub pb_conf_add {
173
[415]174pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
[1495]175my $lh;
[898]176
177foreach my $cf (@_) {
[1495]178 if (! -r $cf) {
179 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
180 next;
181 }
[898]182 # Skip already used conf files
[1495]183 return($lh) if (defined $pbconffiles{$cf});
184
[2154]185 # The new conf file overload values already managed
[898]186 my $num = keys %pbconffiles;
[1495]187 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
[898]188 $pbconffiles{$cf} = $num;
[1495]189
190 # Read the content of the config file
191 $lh = pb_conf_cache($cf,$lh);
192 # and cache it in the %h hash for further queries but after the previous
193 # as we load conf files in reverse order (most precise first)
194 pb_conf_add_last_in_hash($lh)
[409]195}
[898]196}
[409]197
[1495]198
[405]199=item B<pb_conf_read_if>
200
201This function returns a table of pointers on hashes
202corresponding to the keys in a configuration file passed in parameter.
203If that file doesn't exist, it returns undef.
204
205The format of the configuration file is as follows:
206
207key tag = value1,value2,...
208
209Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
210
211 $ cat $HOME/.pbrc
212 pbver pb = 3
213 pbver default = 1
214 pblist pb = 12,25
215
216calling it like this:
217
218 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
219
220will allow to get the mapping:
221
222 $k1->{'pb'} contains 3
[409]223 $k1->{'default'} contains 1
[405]224 $k2->{'pb'} contains 12,25
225
226Valid chars for keys and tags are letters, numbers, '-' and '_'.
227
[1495]228The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
229
[405]230=cut
231
232sub pb_conf_read_if {
233
234my $conffile = shift;
235my @param = @_;
236
237open(CONF,$conffile) || return((undef));
238close(CONF);
239return(pb_conf_read($conffile,@param));
240}
241
242=item B<pb_conf_read>
243
244This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
245
246=cut
247
248sub pb_conf_read {
249
250my $conffile = shift;
251my @param = @_;
252my @ptr;
[1495]253my $lh;
[405]254
[1495]255$lh = pb_conf_cache($conffile,$lh);
256
257foreach my $param (@param) {
258 push @ptr,$lh->{$param};
[405]259}
[1495]260return(@ptr);
261}
[405]262
[1904]263=item B<pb_conf_write>
[1495]264
[1904]265This function writes in the file passed ias first parameter the hash of values passed as second parameter
[1495]266
[1904]267=cut
268
269sub pb_conf_write {
270
271my $conffile = shift;
[1905]272my $h = shift;
[2249]273my $dpfunc;
[1904]274
[1905]275confess "No configuration file defined to write into !" if (not defined $conffile);
276confess "No hash defined to read from !" if (not defined $h);
277open(CONF,"> $conffile") || confess "Unable to write into $conffile";
[1904]278
[2241]279if ($confver < 0.15) {
[2176]280 # This is the original conf file format for versions up to 0.14
281 foreach my $p (sort keys %$h) {
282 my $j = $h->{$p};
283 foreach my $k (sort keys %$j) {
284 print CONF "$p $k = $j->{$k}\n";
285 }
[1904]286 }
[2176]287} else {
288 # This is the new YAML format
[2249]289 eval {
290 require YAML;
291 YAML->import();
292 };
293 if ($@) {
294 eval {
295 # No YAML found using a more std but less complete one. Old perl only
296 require Module::Build::YAML;
297 Module::Build::YAML->import();
298 };
299 if ($@) {
300 die "Unable to handle YAML configuration files without a YAML.pm module\n";
301 } else {
302 $dpfunc = \&Module::Build::YAML::Dump;
303 }
304 } else {
305 $dpfunc = \&YAML::Dump;
306 }
307
308 print CONF $dpfunc->($h);
[1904]309}
310close(CONF);
311}
312
313
314
[1495]315=item B<pb_conf_get_in_hash_if>
316
[1594]317This function returns a table, corresponding to a set of values queried in the hash passed in parameter or undef if it doesn't exist.
318It takes a table of keys as an input parameter.
[1495]319
320=cut
321
322sub pb_conf_get_in_hash_if {
323
324my $lh = shift || return(());
325my @params = @_;
326my @ptr = ();
327
328pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
329foreach my $k (@params) {
330 push @ptr,$lh->{$k};
[405]331}
[1495]332
333pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
[405]334return(@ptr);
335}
336
[1495]337
338
[409]339=item B<pb_conf_get_if>
[405]340
[1495]341This 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.
[405]342
[409]343The format of the configurations file is as follows:
344
345key tag = value1,value2,...
346
[1495]347It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
[409]348
349 $ cat $HOME/.pbrc
350 pbver pb = 1
351 pblist pb = 4
352 $ cat $HOME/.pbrc2
353 pbver pb = 3
354 pblist default = 5
355
356calling it like this:
357
[505]358 pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
[409]359 my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
360
361will allow to get the mapping:
362
363 $k1->{'pb'} contains 3
364 $k2->{'pb'} contains 4
365
366Valid chars for keys and tags are letters, numbers, '-' and '_'.
367
368=cut
369
370sub pb_conf_get_if {
371
[2154]372my @param = @_;
373my @return = pb_conf_get_in_hash_if($h,@_);
374my $proj = undef;
375
376if (not defined $ENV{'PBPROJ'}) {
377 $proj = "unknown";
378} else {
379 $proj = $ENV{'PBPROJ'};
[405]380}
[409]381
[2154]382foreach my $i (0..$#param) {
383 if (not defined $return[$i]->{$proj}) {
384 $return[$i]->{$proj} = $return[$i]->{'default'} if (defined $return[$i]->{'default'});
385 }
386}
387return(@return);
388}
389
[1495]390=item B<pb_conf_add_last_in_hash>
[405]391
[1495]392This 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)
[405]393
[1495]394It is used internally by pb_conf_add and is not exported.
[409]395
396=cut
397
[1495]398sub pb_conf_add_last_in_hash {
[409]399
[1907]400my $ptr = shift;
[409]401
[1495]402return if (not defined $ptr);
403# TODO: test $ptr is a hash pointer
[405]404
[1509]405# When called without correct initialization, try to work anyway with default as project
406pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
407
[1495]408my @params = (sort keys %$ptr);
[405]409
[1495]410# Everything is returned via @h
411# @h contains the values overloading what @ptr may contain.
[2154]412my @h = pb_conf_get_in_hash_if($h,@params);
[1495]413my @ptr = pb_conf_get_in_hash_if($ptr,@params);
[409]414
[405]415my $p1;
416my $p2;
417
[1495]418pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
[2154]419pb_log(2,"DEBUG: pb_conf_add_last_in_hash current hash: ".Dumper(@h)."\n");
420pb_log(2,"DEBUG: pb_conf_add_last_in_hash new inputs: ".Dumper(@ptr)."\n");
[405]421
[1495]422foreach my $i (0..$#params) {
423 $p1 = $h[$i];
424 $p2 = $ptr[$i];
[2154]425 # Always try to take the param from h in priority
[1495]426 # in order to mask what could be defined already in ptr
[405]427 if (not defined $p2) {
[415]428 # exit if no p1 either
[1509]429 next if (not defined $p1);
[405]430 } else {
[409]431 # Ref found in p2
[405]432 if (not defined $p1) {
[409]433 # No ref in p1 so use p2's value
[405]434 $p1 = $p2;
435 } else {
436 # Both are defined - handling the overloading
[2154]437 # Now copy back into p1 all p2 content
438 # as p1 content always has priority over p2
[405]439 if (not defined $p1->{$ENV{'PBPROJ'}}) {
440 if (defined $p2->{$ENV{'PBPROJ'}}) {
[1594]441 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
[405]442 }
443 }
444 # Now copy back into p1 all p2 content which doesn't exist in p1
[2154]445 # # p1 content always has priority over p2
[405]446 foreach my $k (keys %$p2) {
447 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
448 }
449 }
450 }
[1495]451 $h->{$params[$i]} = $p1;
[405]452}
[1495]453pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
[405]454}
455
[409]456=item B<pb_conf_get>
[405]457
[409]458This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
459
460=cut
461
462sub pb_conf_get {
463
464my @param = @_;
465my @return = pb_conf_get_if(@param);
[932]466my $proj = undef;
[409]467
[932]468if (not defined $ENV{'PBPROJ'}) {
469 $proj = "unknown";
470} else {
471 $proj = $ENV{'PBPROJ'};
472}
[409]473
[1538]474confess "No params found for $proj" if (not @return);
[932]475
[409]476foreach my $i (0..$#param) {
[1507]477 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
[409]478}
479return(@return);
480}
481
[1495]482
[1694]483=item B<pb_conf_get_all>
484
[2077]485This function returns an array with all configuration parameters
[1694]486
487=cut
488
489sub pb_conf_get_all {
490
491return(sort keys %$h);
492}
493
[2077]494
495=item B<pb_conf_get_hash>
496
497This function returns a pointer to the hash with all configuration parameters
498
499=cut
500
501sub pb_conf_get_hash {
502
503return($h);
504}
505
[405]506=back
507
508=head1 WEB SITES
509
510The 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/>.
511
512=head1 USER MAILING LIST
513
514None exists for the moment.
515
516=head1 AUTHORS
517
518The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
519
520=head1 COPYRIGHT
521
522Project-Builder.org is distributed under the GPL v2.0 license
523described in the file C<COPYING> included with the distribution.
524
525=cut
526
527
5281;
Note: See TracBrowser for help on using the repository browser.