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