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 |
|
---|
14 | package ProjectBuilder::Conf;
|
---|
15 |
|
---|
16 | use strict;
|
---|
17 | use Carp 'confess';
|
---|
18 | use Data::Dumper;
|
---|
19 | use ProjectBuilder::Base;
|
---|
20 | use ProjectBuilder::Version;
|
---|
21 | #use YAML;
|
---|
22 |
|
---|
23 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
24 |
|
---|
25 | use vars qw($VERSION $REVISION @ISA @EXPORT);
|
---|
26 | use Exporter;
|
---|
27 |
|
---|
28 | # Export, by default, all the functions into the namespace of
|
---|
29 | # any code which uses this module.
|
---|
30 |
|
---|
31 | our @ISA = qw(Exporter);
|
---|
32 | our @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
|
---|
38 | my %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
|
---|
45 | my $h = ();
|
---|
46 |
|
---|
47 | =pod
|
---|
48 |
|
---|
49 | =head1 NAME
|
---|
50 |
|
---|
51 | ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
|
---|
52 |
|
---|
53 | =head1 DESCRIPTION
|
---|
54 |
|
---|
55 | This 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 |
|
---|
69 | The configuration files are loaded in a specific order from most generic to the most specific
|
---|
70 | to allow for overwrite to work:
|
---|
71 |
|
---|
72 | 1. /usr/share/pb/pb.conf - the read-only system conf file provided by install
|
---|
73 | 2. /etc/pb/pb.conf - the same global conf file given to the sysadmin in order to make system wide modifications
|
---|
74 | 3. /path/to/project.pb - Configuration file for the project we're building for
|
---|
75 | 4. /(vm|ve|rm)path/to/.pbrc - configuration file for VM, VE or RM specific parameters. Cumulative should be orthogonal
|
---|
76 | 5. $HOME/.pbrc - user's configuration file
|
---|
77 |
|
---|
78 | =over 4
|
---|
79 |
|
---|
80 | =item B<pb_conf_init>
|
---|
81 |
|
---|
82 | This function setup the environment PBPROJ for project-builder function usage from other projects.
|
---|
83 | The first parameter is the project name.
|
---|
84 | It sets up environment variables (PBPROJ)
|
---|
85 |
|
---|
86 | =cut
|
---|
87 |
|
---|
88 | sub pb_conf_init {
|
---|
89 |
|
---|
90 | my $proj=shift;
|
---|
91 |
|
---|
92 | pb_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 | #
|
---|
99 | if ((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 |
|
---|
105 | if (defined $proj) {
|
---|
106 | $ENV{'PBPROJ'} = $proj;
|
---|
107 | } else {
|
---|
108 | $ENV{'PBPROJ'} = "default";
|
---|
109 | }
|
---|
110 | pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | =item B<pb_conf_cache>
|
---|
115 |
|
---|
116 | This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
|
---|
117 | It returns the modified hash
|
---|
118 | Can be used in correlation with the %h hash to store permanently values or not if temporarily.
|
---|
119 |
|
---|
120 | =cut
|
---|
121 |
|
---|
122 | sub pb_conf_cache {
|
---|
123 |
|
---|
124 | my $cf = shift;
|
---|
125 | my $lh = shift;
|
---|
126 | my $confver = "0.14";
|
---|
127 |
|
---|
128 | # Read the content of the config file and cache it in the %h hash then available for queries
|
---|
129 | if ($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 | }
|
---|
143 | return($lh);
|
---|
144 | }
|
---|
145 |
|
---|
146 | =item B<pb_conf_add>
|
---|
147 |
|
---|
148 | This function adds the configuration file to the list last, and cache their content in the %h hash
|
---|
149 |
|
---|
150 | =cut
|
---|
151 |
|
---|
152 | sub pb_conf_add {
|
---|
153 |
|
---|
154 | pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
|
---|
155 | my $lh;
|
---|
156 |
|
---|
157 | foreach 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 |
|
---|
181 | This function returns a table of pointers on hashes
|
---|
182 | corresponding to the keys in a configuration file passed in parameter.
|
---|
183 | If that file doesn't exist, it returns undef.
|
---|
184 |
|
---|
185 | The format of the configuration file is as follows:
|
---|
186 |
|
---|
187 | key tag = value1,value2,...
|
---|
188 |
|
---|
189 | Supposing 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 |
|
---|
196 | calling it like this:
|
---|
197 |
|
---|
198 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
|
---|
199 |
|
---|
200 | will allow to get the mapping:
|
---|
201 |
|
---|
202 | $k1->{'pb'} contains 3
|
---|
203 | $k1->{'default'} contains 1
|
---|
204 | $k2->{'pb'} contains 12,25
|
---|
205 |
|
---|
206 | Valid chars for keys and tags are letters, numbers, '-' and '_'.
|
---|
207 |
|
---|
208 | The 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 |
|
---|
212 | sub pb_conf_read_if {
|
---|
213 |
|
---|
214 | my $conffile = shift;
|
---|
215 | my @param = @_;
|
---|
216 |
|
---|
217 | open(CONF,$conffile) || return((undef));
|
---|
218 | close(CONF);
|
---|
219 | return(pb_conf_read($conffile,@param));
|
---|
220 | }
|
---|
221 |
|
---|
222 | =item B<pb_conf_read>
|
---|
223 |
|
---|
224 | This 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 |
|
---|
228 | sub pb_conf_read {
|
---|
229 |
|
---|
230 | my $conffile = shift;
|
---|
231 | my @param = @_;
|
---|
232 | my @ptr;
|
---|
233 | my $lh;
|
---|
234 |
|
---|
235 | $lh = pb_conf_cache($conffile,$lh);
|
---|
236 |
|
---|
237 | foreach my $param (@param) {
|
---|
238 | push @ptr,$lh->{$param};
|
---|
239 | }
|
---|
240 | return(@ptr);
|
---|
241 | }
|
---|
242 |
|
---|
243 | =item B<pb_conf_write>
|
---|
244 |
|
---|
245 | This function writes in the file passed ias first parameter the hash of values passed as second parameter
|
---|
246 |
|
---|
247 | =cut
|
---|
248 |
|
---|
249 | sub pb_conf_write {
|
---|
250 |
|
---|
251 | my $conffile = shift;
|
---|
252 | my $h = shift;
|
---|
253 | my $confver = "0.14";
|
---|
254 |
|
---|
255 | confess "No configuration file defined to write into !" if (not defined $conffile);
|
---|
256 | confess "No hash defined to read from !" if (not defined $h);
|
---|
257 | open(CONF,"> $conffile") || confess "Unable to write into $conffile";
|
---|
258 |
|
---|
259 | if ($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 | }
|
---|
271 | close(CONF);
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 |
|
---|
276 | =item B<pb_conf_get_in_hash_if>
|
---|
277 |
|
---|
278 | This function returns a table, corresponding to a set of values queried in the hash passed in parameter or undef if it doesn't exist.
|
---|
279 | It takes a table of keys as an input parameter.
|
---|
280 |
|
---|
281 | =cut
|
---|
282 |
|
---|
283 | sub pb_conf_get_in_hash_if {
|
---|
284 |
|
---|
285 | my $lh = shift || return(());
|
---|
286 | my @params = @_;
|
---|
287 | my @ptr = ();
|
---|
288 |
|
---|
289 | pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
|
---|
290 | foreach my $k (@params) {
|
---|
291 | push @ptr,$lh->{$k};
|
---|
292 | }
|
---|
293 |
|
---|
294 | pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
|
---|
295 | return(@ptr);
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 |
|
---|
300 | =item B<pb_conf_get_if>
|
---|
301 |
|
---|
302 | This 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 |
|
---|
304 | The format of the configurations file is as follows:
|
---|
305 |
|
---|
306 | key tag = value1,value2,...
|
---|
307 |
|
---|
308 | It 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 |
|
---|
317 | calling it like this:
|
---|
318 |
|
---|
319 | pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
|
---|
320 | my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
|
---|
321 |
|
---|
322 | will allow to get the mapping:
|
---|
323 |
|
---|
324 | $k1->{'pb'} contains 3
|
---|
325 | $k2->{'pb'} contains 4
|
---|
326 |
|
---|
327 | Valid chars for keys and tags are letters, numbers, '-' and '_'.
|
---|
328 |
|
---|
329 | =cut
|
---|
330 |
|
---|
331 | sub pb_conf_get_if {
|
---|
332 |
|
---|
333 | my @param = @_;
|
---|
334 | my @return = pb_conf_get_in_hash_if($h,@_);
|
---|
335 | my $proj = undef;
|
---|
336 |
|
---|
337 | if (not defined $ENV{'PBPROJ'}) {
|
---|
338 | $proj = "unknown";
|
---|
339 | } else {
|
---|
340 | $proj = $ENV{'PBPROJ'};
|
---|
341 | }
|
---|
342 |
|
---|
343 | foreach 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 | }
|
---|
348 | return(@return);
|
---|
349 | }
|
---|
350 |
|
---|
351 | =item B<pb_conf_add_last_in_hash>
|
---|
352 |
|
---|
353 | This 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 |
|
---|
355 | It is used internally by pb_conf_add and is not exported.
|
---|
356 |
|
---|
357 | =cut
|
---|
358 |
|
---|
359 | sub pb_conf_add_last_in_hash {
|
---|
360 |
|
---|
361 | my $ptr = shift;
|
---|
362 |
|
---|
363 | return 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
|
---|
367 | pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
|
---|
368 |
|
---|
369 | my @params = (sort keys %$ptr);
|
---|
370 |
|
---|
371 | # Everything is returned via @h
|
---|
372 | # @h contains the values overloading what @ptr may contain.
|
---|
373 | my @h = pb_conf_get_in_hash_if($h,@params);
|
---|
374 | my @ptr = pb_conf_get_in_hash_if($ptr,@params);
|
---|
375 |
|
---|
376 | my $p1;
|
---|
377 | my $p2;
|
---|
378 |
|
---|
379 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
|
---|
380 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash current hash: ".Dumper(@h)."\n");
|
---|
381 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash new inputs: ".Dumper(@ptr)."\n");
|
---|
382 |
|
---|
383 | foreach 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 | }
|
---|
414 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
|
---|
415 | }
|
---|
416 |
|
---|
417 | =item B<pb_conf_get>
|
---|
418 |
|
---|
419 | This 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 |
|
---|
423 | sub pb_conf_get {
|
---|
424 |
|
---|
425 | my @param = @_;
|
---|
426 | my @return = pb_conf_get_if(@param);
|
---|
427 | my $proj = undef;
|
---|
428 |
|
---|
429 | if (not defined $ENV{'PBPROJ'}) {
|
---|
430 | $proj = "unknown";
|
---|
431 | } else {
|
---|
432 | $proj = $ENV{'PBPROJ'};
|
---|
433 | }
|
---|
434 |
|
---|
435 | confess "No params found for $proj" if (not @return);
|
---|
436 |
|
---|
437 | foreach my $i (0..$#param) {
|
---|
438 | confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
|
---|
439 | }
|
---|
440 | return(@return);
|
---|
441 | }
|
---|
442 |
|
---|
443 |
|
---|
444 | =item B<pb_conf_get_all>
|
---|
445 |
|
---|
446 | This function returns an array with all configuration parameters
|
---|
447 |
|
---|
448 | =cut
|
---|
449 |
|
---|
450 | sub pb_conf_get_all {
|
---|
451 |
|
---|
452 | return(sort keys %$h);
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | =item B<pb_conf_get_hash>
|
---|
457 |
|
---|
458 | This function returns a pointer to the hash with all configuration parameters
|
---|
459 |
|
---|
460 | =cut
|
---|
461 |
|
---|
462 | sub pb_conf_get_hash {
|
---|
463 |
|
---|
464 | return($h);
|
---|
465 | }
|
---|
466 |
|
---|
467 | =back
|
---|
468 |
|
---|
469 | =head1 WEB SITES
|
---|
470 |
|
---|
471 | The 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 |
|
---|
475 | None exists for the moment.
|
---|
476 |
|
---|
477 | =head1 AUTHORS
|
---|
478 |
|
---|
479 | The 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 |
|
---|
483 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
484 | described in the file C<COPYING> included with the distribution.
|
---|
485 |
|
---|
486 | =cut
|
---|
487 |
|
---|
488 |
|
---|
489 | 1;
|
---|