source: ProjectBuilder/0.15.3/pb-modules/lib/ProjectBuilder/Conf.pm@ 2604

Last change on this file since 2604 was 2604, checked in by Bruno Cornec, 4 years ago

Avoid usage of Module::Build::YAML as creating issues on old distros and prefer Tiny:YAML embedded

File size: 15.7 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-today
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 qw/cluck 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(@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 pb_conf_update_v0 pb_conf_get_in_hash_if);
32
33# Global hash of conf files
34# Key is the conf file name
35# Value is its rank
36my %pbconffiles;
37
38# Global hash of conf file content
39# Key is the config keyword
40# Value is a hash whose key depends on the nature of the config keyword as documented
41# and value is the confguration value
42# We consider that values can not change during the life of pb
43my $h = ();
44
45my $dpfunc;
46my $ldfunc;
47our ($VERSION,$REVISION,$PBCONFVER) = pb_version_init();
48
49=pod
50
51=head1 NAME
52
53ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
54
55=head1 DESCRIPTION
56
57This modules provides functions dealing with configuration files.
58
59=head1 SYNOPSIS
60
61 use ProjectBuilder::Conf;
62
63 #
64 # Read hash codes of values from a configuration file and return table of pointers
65 #
66 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc.yml","key1","key2");
67 my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc.yml","key");
68
69=head1 USAGE
70
71The configuration files are loaded in a specific order from most generic to the most specific
72to allow for overwrite to work:
73
74For recent versions of pb (>= 0.15):
751. /usr/share/pb/pb.yml - the read-only system conf file provided by install
762. /etc/pb/pb.yml - the same global conf file given to the sysadmin in order to make system wide modifications
773. /path/to/project.yml - Configuration file for the project we're building for
784. /vm|vepath/to/.pbrc.yml - configuration file for VM, VE or RM specific parameters. Cumulative should be orthogonal
795. $HOME/.pbrc.yml - user's configuration file
80
81For versions of pb up to 0.14:
821. /usr/share/pb/pb.conf - the read-only system conf file provided by install
832. /etc/pb/pb.conf - the same global conf file given to the sysadmin in order to make system wide modifications
843. /path/to/project.pb - Configuration file for the project we're building for
854. /(vm|ve|rm)path/to/.pbrc - configuration file for VM, VE or RM specific parameters. Cumulative should be orthogonal
865. $HOME/.pbrc - user's configuration file
87
88The format of the configuration file is as follows:
89
90For recent versions of pb (>= 0.15):
91YAML format is now used - The version of the configuration files is
92
93Supposing the file is called "$ENV{'HOME'}/.pbrc.yml", containing the following:
94
95 $ cat $HOME/.pbrc.yml
96 ---
97 pbver:
98 - pb: 3
99 - default: 1
100 pblist:
101 - pb: 12,25
102
103calling it like this:
104
105 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc.yml","pbver","pblist");
106
107will allow to get the mapping:
108
109 $k1->{'pb'} contains 3
110 $k1->{'default'} contains 1
111 $k2->{'pb'} contains 12,25
112
113For versions of pb up to 0.14:
114An own format was used - The version of the configuration files is 0
115
116key tag = value1,value2,...
117
118Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
119
120 $ cat $HOME/.pbrc
121 pbver pb = 3
122 pbver default = 1
123 pblist pb = 12,25
124
125calling it like this:
126
127 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
128
129will allow to get the mapping:
130
131 $k1->{'pb'} contains 3
132 $k1->{'default'} contains 1
133 $k2->{'pb'} contains 12,25
134
135Valid chars for keys and tags are letters, numbers, '-' and '_'.
136
137=over 4
138
139=item B<pb_conf_init>
140
141This function setup the environment PBPROJ for project-builder function usage from other projects.
142The first parameter is the project name.
143It sets up environment variables (PBPROJ)
144
145=cut
146
147sub pb_conf_init {
148
149my $proj=shift;
150
151pb_log(2,"Entering pb_conf_init\n");
152#
153# Check project name
154# Could be with env var PBPROJ
155# or option -p
156# if not defined take the first in conf file
157#
158if ((defined $ENV{'PBPROJ'}) &&
159 (not defined $proj)) {
160 pb_log(2,"PBPROJ env var setup ($ENV{'PBPROJ'}) so using it\n");
161 $proj = $ENV{'PBPROJ'};
162}
163
164if (defined $proj) {
165 $ENV{'PBPROJ'} = $proj;
166} else {
167 $ENV{'PBPROJ'} = "default";
168}
169pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
170
171# Manage YAML modules to use
172
173if ($PBCONFVER >= 1) {
174 eval {
175 require YAML::XS;
176 YAML::XS->import();
177 };
178 if ($@) {
179 eval {
180 require YAML;
181 YAML->import();
182 };
183 if ($@) {
184 eval {
185 # No YAML found using a more std but less complete one. Old perl only
186 require YAML::Tiny;
187 YAML::Tiny->import();
188 };
189 if ($@) {
190 # Here we use an embedded YAML code for distro lacking any YAML module
191 if (not defined ($ENV{'PBSETUPV'})) {
192 # Don't do that for setupv only for other scripts
193 # as the module is already embedded and would conflict
194 require ProjectBuilder::YAML;
195 ProjectBuilder::YAML->import();
196 };
197 $dpfunc = \&pb_Dump;
198 $ldfunc = \&pb_LoadFile;
199 } else {
200 $dpfunc = \&YAML::Tiny::Dump;
201 $ldfunc = \&YAML::Tiny::LoadFile;
202 }
203 } else {
204 $dpfunc = \&YAML::Dump;
205 $ldfunc = \&YAML::LoadFile;
206 }
207 } else {
208 $dpfunc = \&YAML::XS::Dump;
209 $ldfunc = \&YAML::XS::LoadFile;
210 }
211}
212}
213
214
215=item B<pb_conf_cache>
216
217This function caches the configuration file content passed as first parameter into the hash passed in second parameter
218It returns the modified hash
219Can be used in correlation with the %h hash to store permanently values or not if temporarily.
220
221=cut
222
223sub pb_conf_cache {
224
225my $cf = shift;
226my $lh = shift;
227
228# Read the content of the config file and cache it in the %h hash then available for queries
229if ($PBCONFVER < 1) {
230 open(CONF,$cf) || (cluck "Unable to open $cf" && return($lh));
231 # This is the original conf file format for versions up to 0.14
232 while(<CONF>) {
233 next if (/^#/);
234 if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.\?\[\]\*\+\\]+)\s*=\s*(.*)$/) {
235 pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
236 my ($what, $var, $value) = ($1, $2, $3);
237 # Add support for multi-lines
238 while ($value =~ s/\\\s*$//o) {
239 $_ = <CONF>;
240 die "Still processing continuations for $what $var at EOF" if (not defined $_);
241 s/[\r\n]//go;
242 $value .= "\n$_";
243 }
244 $lh->{$what}->{$var}=$value;
245 } elsif ((/^\s*#/o) || (/^\s*$/o)) {
246 # ignore
247 } else {
248 chomp();
249 warn "unexpected line '$_' in $cf";
250 }
251 }
252 close(CONF);
253
254} else {
255 # Have we already handled that conf file ?
256 next if ((defined $lh) && (defined $lh->{'__cf'}) && (defined $lh->{'__cf'}->{$cf}) && ($lh->{'__cf'}->{$cf} eq 1));
257
258 pb_log(1,"Loading YAML conf file $cf\n");
259 my $lh0;
260 eval { $lh0 = $ldfunc->($cf); };
261 if ($@) {
262 # Repeat to get the YAML error line
263 $lh0 = $ldfunc->($cf);
264 die "Unable to analyze YAML conf file $cf\n";
265 }
266 foreach my $k (keys %$lh0) {
267 if (defined $lh->{$k}) {
268 foreach my $k2 (keys %{$lh0->{$k}}) {
269 $lh->{$k}->{$k2} = $lh0->{$k}->{$k2};
270 }
271 } else {
272 $lh->{$k} = $lh0->{$k};
273 }
274 }
275 # Remember we've seen that conf file
276 $lh->{'__cf'}->{$cf} = 1;
277}
278return($lh);
279}
280
281=item B<pb_conf_add>
282
283This function adds the configuration file to the list last, and cache their content in the %h hash
284
285=cut
286
287sub pb_conf_add {
288
289my $lh;
290
291foreach my $cf (@_) {
292 if (! -r $cf) {
293 pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
294 next;
295 }
296 # Skip already used conf files
297 return($lh) if (defined $pbconffiles{$cf});
298
299 pb_log(2,"DEBUG: pb_conf_add with $cf\n");
300 # The new conf file overload values already managed
301 my $num = keys %pbconffiles;
302 pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
303 $pbconffiles{$cf} = $num;
304
305 # Read the content of the config file
306 $lh = pb_conf_cache($cf,$lh);
307 # and cache it in the %h hash for further queries but after the previous
308 # as we load conf files in reverse order (most precise first)
309 pb_conf_add_last_in_hash($lh)
310}
311}
312
313
314=item B<pb_conf_read_if>
315
316This function returns a table of pointers on hashes
317corresponding to the keys in a configuration file passed in parameter.
318If that file doesn't exist, it returns undef.
319
320The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
321
322=cut
323
324sub pb_conf_read_if {
325
326my $conffile = shift;
327my @param = @_;
328
329open(CONF,$conffile) || return((undef));
330close(CONF);
331return(pb_conf_read($conffile,@param));
332}
333
334=item B<pb_conf_read>
335
336This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
337
338=cut
339
340sub pb_conf_read {
341
342my $conffile = shift;
343my @param = @_;
344my @ptr;
345my $lh;
346
347$lh = pb_conf_cache($conffile,$lh);
348
349foreach my $param (@param) {
350 push @ptr,$lh->{$param};
351}
352return(@ptr);
353}
354
355=item B<pb_conf_write>
356
357This function writes in the file passed as first parameter the hash of values passed as second parameter
358
359=cut
360
361sub pb_conf_write {
362
363my $conffile = shift;
364my $h = shift;
365
366confess "No configuration file defined to write into !" if (not defined $conffile);
367confess "No hash defined to read from !" if (not defined $h);
368open(CONF,"> $conffile") || (cluck "Unable to write into $conffile" && return);
369
370if ($PBCONFVER < 1) {
371 # This is the original conf file format for versions up to 0.14
372 foreach my $p (sort keys %$h) {
373 my $j = $h->{$p};
374 foreach my $k (sort keys %$j) {
375 print CONF "$p $k = $j->{$k}\n";
376 }
377 }
378} else {
379 pb_log(1,"Writing YAML conf file $conffile\n");
380 delete $h->{'__cf'};
381 print CONF $dpfunc->($h);
382}
383close(CONF);
384}
385
386
387
388=item B<pb_conf_get_in_hash_if>
389
390This function returns a table, corresponding to a set of values queried in the hash passed in parameter or undef if it doesn't exist.
391It takes a table of keys as an input parameter.
392
393=cut
394
395sub pb_conf_get_in_hash_if {
396
397my $lh = shift || return(());
398my @params = @_;
399my @ptr = ();
400
401pb_log(3,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
402foreach my $k (@params) {
403 push @ptr,$lh->{$k};
404}
405
406pb_log(3,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
407return(@ptr);
408}
409
410
411
412=item B<pb_conf_get_if>
413
414This 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.
415
416=cut
417
418sub pb_conf_get_if {
419
420my @param = @_;
421my @return = pb_conf_get_in_hash_if($h,@_);
422my $proj = undef;
423
424if (not defined $ENV{'PBPROJ'}) {
425 $proj = "unknown";
426} else {
427 $proj = $ENV{'PBPROJ'};
428}
429
430foreach my $i (0..$#param) {
431 if (not defined $return[$i]->{$proj}) {
432 $return[$i]->{$proj} = $return[$i]->{'default'} if (defined $return[$i]->{'default'});
433 }
434}
435return(@return);
436}
437
438=item B<pb_conf_add_last_in_hash>
439
440This 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)
441
442It is used internally by pb_conf_add and is not exported.
443
444=cut
445
446sub pb_conf_add_last_in_hash {
447
448my $ptr = shift;
449
450return if (not defined $ptr);
451# TODO: test $ptr is a hash pointer
452
453# When called without correct initialization, try to work anyway with default as project
454pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
455
456my @params = (sort keys %$ptr);
457
458# Everything is returned via @h
459# @h contains the values overloading what @ptr may contain.
460my @h = pb_conf_get_in_hash_if($h,@params);
461my @ptr = pb_conf_get_in_hash_if($ptr,@params);
462
463my $p1;
464my $p2;
465
466pb_log(3,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
467pb_log(3,"DEBUG: pb_conf_add_last_in_hash current hash: ".Dumper(@h)."\n");
468pb_log(3,"DEBUG: pb_conf_add_last_in_hash new inputs: ".Dumper(@ptr)."\n");
469
470foreach my $i (0..$#params) {
471 $p1 = $h[$i];
472 $p2 = $ptr[$i];
473 # Always try to take the param from h in priority
474 # in order to mask what could be defined already in ptr
475 if (not defined $p2) {
476 # exit if no p1 either
477 next if (not defined $p1);
478 } else {
479 # Ref found in p2
480 if (not defined $p1) {
481 # No ref in p1 so use p2's value
482 $p1 = $p2;
483 } else {
484 # Both are defined - handling the overloading
485 # Now copy back into p1 all p2 content
486 # as p1 content always has priority over p2
487 if (not defined $p1->{$ENV{'PBPROJ'}}) {
488 if (defined $p2->{$ENV{'PBPROJ'}}) {
489 $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
490 }
491 }
492 # Now copy back into p1 all p2 content which doesn't exist in p1
493 # # p1 content always has priority over p2
494 foreach my $k (keys %$p2) {
495 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
496 }
497 }
498 }
499 $h->{$params[$i]} = $p1;
500}
501pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
502}
503
504=item B<pb_conf_get>
505
506This function is the same B<pb_conf_get_if>, except that it tests each returned value as they need to exist in that case.
507
508=cut
509
510sub pb_conf_get {
511
512my @param = @_;
513my @return = pb_conf_get_if(@param);
514my $proj = undef;
515
516if (not defined $ENV{'PBPROJ'}) {
517 $proj = "unknown";
518} else {
519 $proj = $ENV{'PBPROJ'};
520}
521
522confess "No params found for $proj" if (not @return);
523
524foreach my $i (0..$#param) {
525 confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
526}
527return(@return);
528}
529
530
531=item B<pb_conf_get_all>
532
533This function returns an array with all configuration parameters
534
535=cut
536
537sub pb_conf_get_all {
538
539my $ah;
540foreach my $i (sort keys %$h) {
541 $ah->{$i} = $h->{$i} if ($i !~ /^__cf/);
542}
543return(sort keys %$ah);
544}
545
546
547=item B<pb_conf_get_hash>
548
549This function returns a pointer to the hash with all configuration parameters
550
551=cut
552
553sub pb_conf_get_hash {
554
555return($h);
556}
557
558=item B<pb_conf_update_v0>
559
560This function transform the old configuration v0 file as first param into a new v1 one as second param
561
562=cut
563
564sub pb_conf_update_v0 {
565
566my $orig = shift;
567my $dest = shift;
568
569open(ORIG,$orig) || (cluck "Unable to open $orig" && return);
570confess "Will not erase existing $dest while transforming $orig" if (-f $dest);
571open(DEST,"> $dest") || (cluck "Unable to write into $dest" && close(ORIG) && return);
572print DEST "---\n";
573my $pbconfverbkp = $PBCONFVER;
574# We force migration from v0 to v1
575$PBCONFVER = 0;
576my $lh0;
577my $lh1;
578$lh0 = pb_conf_cache($orig,$lh0);
579pb_log(2,"lh0:\n".Dumper($lh0)."\n");
580$PBCONFVER = $pbconfverbkp;
581
582pb_log(0,"Converting v0 conf file $orig to v1 conf file $dest\n");
583# We can't just write the YAML if we want to keep comments !
584while (<ORIG>) {
585 if ($_ =~ /^#/) {
586 # Keep comments
587 print DEST $_;
588 } elsif ($_ =~ /^\s*$/) {
589 # Replace empty lines by comments
590 print DEST "#\n";;
591 } else {
592 if (/^\s*([A-z0-9-_]+)\s+(.+)$/) {
593 # Handle parameters
594 my ($param,$void) = ($1, $2);
595 if (not defined $lh1->{$param}) {
596 pb_log(2,"Converting parameter $param\n");
597 my $param2 = $param;
598 # param pburl in v0 is now pbprojurl in v1
599 $param2 =~ s/pburl/pbprojurl/;
600 print DEST "$param2:\n";
601 foreach my $k (keys %{$lh0->{$param}}) {
602 pb_log(2,"Handling key $k\n");
603 if ($lh0->{$param}->{$k} =~ /^\s*$/) {
604 print DEST " $k: !!str \"\"\n";
605 } else {
606 print DEST " $k: $lh0->{$param}->{$k}\n";
607 }
608 }
609 $lh1->{$param} = 1;
610 }
611 } else {
612 pb_log(0,"Unable to convert line $_\n");
613 }
614 }
615}
616close(ORIG);
617close(DEST);
618return();
619}
620
621=back
622
623=head1 WEB SITES
624
625The 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/>.
626
627=head1 USER MAILING LIST
628
629None exists for the moment.
630
631=head1 AUTHORS
632
633The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
634
635=head1 COPYRIGHT
636
637Project-Builder.org is distributed under the GPL v2.0 license
638described in the file C<COPYING> included with the distribution.
639
640=cut
641
642
6431;
Note: See TracBrowser for help on using the repository browser.