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

Last change on this file since 2249 was 2249, checked in by bruno, 23 months 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
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;
126
127my $ldfunc;
128
129# Read the content of the config file and cache it in the %h hash then available for queries
130if ($confver < 0.15) {
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        }
139    }
140    close(CONF);
141} else {
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);
162}
163return($lh);
164}
165
166=item B<pb_conf_add>
167
168This function adds the configuration file to the list last, and cache their content in the %h hash
169
170=cut
171
172sub pb_conf_add {
173
174pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
175my $lh;
176
177foreach my $cf (@_) {
178    if (! -r $cf) {
179        pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
180        next;
181    }
182    # Skip already used conf files
183    return($lh) if (defined $pbconffiles{$cf});
184   
185    # The new conf file overload values already managed
186    my $num = keys %pbconffiles;
187    pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
188    $pbconffiles{$cf} = $num;
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)
195}
196}
197
198
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
223  $k1->{'default'} contains 1
224  $k2->{'pb'} contains 12,25
225
226Valid chars for keys and tags are letters, numbers, '-' and '_'.
227
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
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;
253my $lh;
254
255$lh = pb_conf_cache($conffile,$lh);
256
257foreach my $param (@param) {
258    push @ptr,$lh->{$param};
259}
260return(@ptr);
261}
262
263=item B<pb_conf_write>
264
265This function writes in the file passed ias first parameter the hash of values passed as second parameter
266
267=cut
268
269sub pb_conf_write {
270
271my $conffile = shift;
272my $h = shift;
273my $dpfunc;
274
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";
278
279if ($confver < 0.15) {
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        }
286    }
287} else {
288    # This is the new YAML format
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);
309}
310close(CONF);
311}
312
313
314
315=item B<pb_conf_get_in_hash_if>
316
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.
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};
331}
332
333pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
334return(@ptr);
335}
336
337
338
339=item B<pb_conf_get_if>
340
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.
342
343The format of the configurations file is as follows:
344
345key tag = value1,value2,...
346
347It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
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
358  pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
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
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'};
380}
381
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
390=item B<pb_conf_add_last_in_hash>
391
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)
393
394It is used internally by pb_conf_add and is not exported.
395
396=cut
397
398sub pb_conf_add_last_in_hash {
399
400my $ptr = shift;
401
402return if (not defined $ptr);
403# TODO: test $ptr is a hash pointer
404
405# When called without correct initialization, try to work anyway with default as project
406pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
407
408my @params = (sort keys %$ptr);
409
410# Everything is returned via @h
411# @h contains the values overloading what @ptr may contain.
412my @h = pb_conf_get_in_hash_if($h,@params);
413my @ptr = pb_conf_get_in_hash_if($ptr,@params);
414
415my $p1;
416my $p2;
417
418pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
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");
421
422foreach my $i (0..$#params) {
423    $p1 = $h[$i];
424    $p2 = $ptr[$i];
425    # Always try to take the param from h in priority
426    # in order to mask what could be defined already in ptr
427    if (not defined $p2) {
428        # exit if no p1 either
429        next if (not defined $p1);
430    } else {
431        # Ref found in p2
432        if (not defined $p1) {
433            # No ref in p1 so use p2's value
434            $p1 = $p2;
435        } else {
436            # Both are defined - handling the overloading
437            # Now copy back into p1 all p2 content
438            # as p1 content always has priority over p2
439            if (not defined $p1->{$ENV{'PBPROJ'}}) {
440                if (defined $p2->{$ENV{'PBPROJ'}}) {
441                    $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
442                }
443            }
444            # Now copy back into p1 all p2 content which doesn't exist in p1
445            # # p1 content always has priority over p2
446            foreach my $k (keys %$p2) {
447                $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
448            }
449        }
450    }
451    $h->{$params[$i]} = $p1;
452}
453pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
454}
455
456=item B<pb_conf_get>
457
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);
466my $proj = undef;
467
468if (not defined $ENV{'PBPROJ'}) {
469    $proj = "unknown";
470} else {
471    $proj = $ENV{'PBPROJ'};
472}
473
474confess "No params found for $proj" if (not @return);
475
476foreach my $i (0..$#param) {
477    confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
478}
479return(@return);
480}
481
482
483=item B<pb_conf_get_all>
484
485This function returns an array with all configuration parameters
486
487=cut
488
489sub pb_conf_get_all {
490
491return(sort keys %$h);
492}
493
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
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.