[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 |
|
---|
| 14 | package ProjectBuilder::Conf;
|
---|
| 15 |
|
---|
| 16 | use strict;
|
---|
[2362] | 17 | use Carp qw/cluck confess/;
|
---|
[405] | 18 | use Data::Dumper;
|
---|
| 19 | use ProjectBuilder::Base;
|
---|
[1148] | 20 | use ProjectBuilder::Version;
|
---|
[405] | 21 |
|
---|
| 22 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
| 23 |
|
---|
[2279] | 24 | use vars qw($VERSION $REVISION $PBCONFVER @ISA @EXPORT);
|
---|
[405] | 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);
|
---|
[2426] | 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);
|
---|
[2254] | 32 | ($VERSION,$REVISION,$PBCONFVER) = pb_version_init();
|
---|
[405] | 33 |
|
---|
[898] | 34 | # Global hash of conf files
|
---|
| 35 | # Key is the conf file name
|
---|
| 36 | # Value is its rank
|
---|
| 37 | my %pbconffiles;
|
---|
[409] | 38 |
|
---|
[1495] | 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
|
---|
[898] | 43 | # We consider that values can not change during the life of pb
|
---|
[1495] | 44 | my $h = ();
|
---|
[898] | 45 |
|
---|
[405] | 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 | #
|
---|
[2252] | 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");
|
---|
[405] | 65 |
|
---|
| 66 | =head1 USAGE
|
---|
| 67 |
|
---|
[2152] | 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 |
|
---|
[2250] | 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:
|
---|
[2241] | 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
|
---|
[2152] | 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 |
|
---|
[2250] | 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
|
---|
[2257] | 93 | ---
|
---|
[2250] | 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 |
|
---|
[405] | 134 | =over 4
|
---|
| 135 |
|
---|
[505] | 136 | =item B<pb_conf_init>
|
---|
| 137 |
|
---|
[898] | 138 | This function setup the environment PBPROJ for project-builder function usage from other projects.
|
---|
[505] | 139 | The first parameter is the project name.
|
---|
[898] | 140 | It sets up environment variables (PBPROJ)
|
---|
[505] | 141 |
|
---|
| 142 | =cut
|
---|
| 143 |
|
---|
| 144 | sub pb_conf_init {
|
---|
| 145 |
|
---|
[1907] | 146 | my $proj=shift;
|
---|
[505] | 147 |
|
---|
[1495] | 148 | pb_log(1,"Entering pb_conf_init\n");
|
---|
[1584] | 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 |
|
---|
[505] | 161 | if (defined $proj) {
|
---|
| 162 | $ENV{'PBPROJ'} = $proj;
|
---|
| 163 | } else {
|
---|
| 164 | $ENV{'PBPROJ'} = "default";
|
---|
| 165 | }
|
---|
[1495] | 166 | pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
|
---|
[505] | 167 | }
|
---|
| 168 |
|
---|
| 169 |
|
---|
[1495] | 170 | =item B<pb_conf_cache>
|
---|
[505] | 171 |
|
---|
[2250] | 172 | This function caches the configuration file content passed as first parameter into the hash passed in second parameter
|
---|
[1495] | 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 |
|
---|
[2249] | 183 | my $ldfunc;
|
---|
| 184 |
|
---|
[2077] | 185 | # Read the content of the config file and cache it in the %h hash then available for queries
|
---|
[2279] | 186 | if ($PBCONFVER < 1) {
|
---|
[2434] | 187 | open(CONF,$cf) || (cluck "Unable to open $cf" && return($lh));
|
---|
[2176] | 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");
|
---|
[2253] | 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();
|
---|
[2256] | 206 | warn "unexpected line '$_' in $cf";
|
---|
[2176] | 207 | }
|
---|
[1495] | 208 | }
|
---|
[2176] | 209 | close(CONF);
|
---|
| 210 | } else {
|
---|
[2249] | 211 | eval {
|
---|
| 212 | require YAML;
|
---|
| 213 | YAML->import();
|
---|
| 214 | };
|
---|
| 215 | if ($@) {
|
---|
| 216 | eval {
|
---|
| 217 | # No YAML found using a more std but less complete one. Old perl only
|
---|
| 218 | require Module::Build::YAML;
|
---|
| 219 | Module::Build::YAML->import();
|
---|
| 220 | };
|
---|
| 221 | if ($@) {
|
---|
| 222 | die "Unable to handle YAML configuration files without a YAML.pm module\n";
|
---|
| 223 | } else {
|
---|
| 224 | $ldfunc = \&Module::Build::YAML::LoadFile;
|
---|
| 225 | }
|
---|
| 226 | } else {
|
---|
| 227 | $ldfunc = \&YAML::LoadFile;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[2261] | 230 | pb_log(1,"Loading YAML conf file $cf\n");
|
---|
[2402] | 231 | my $lh0;
|
---|
| 232 | eval { $lh0 = $ldfunc->($cf); };
|
---|
| 233 | if ($@) {
|
---|
| 234 | # Repeat to get the YAML error line
|
---|
| 235 | $lh0 = $ldfunc->($cf);
|
---|
| 236 | die "Unable to analyze YAML conf file $cf\n";
|
---|
| 237 | }
|
---|
[2263] | 238 | foreach my $k (keys %$lh0) {
|
---|
| 239 | if (defined $lh->{$k}) {
|
---|
| 240 | foreach my $k2 (keys %{$lh0->{$k}}) {
|
---|
| 241 | $lh->{$k}->{$k2} = $lh0->{$k}->{$k2};
|
---|
| 242 | }
|
---|
| 243 | } else {
|
---|
| 244 | $lh->{$k} = $lh0->{$k};
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
[1495] | 247 | }
|
---|
| 248 | return($lh);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[409] | 251 | =item B<pb_conf_add>
|
---|
| 252 |
|
---|
[1495] | 253 | This function adds the configuration file to the list last, and cache their content in the %h hash
|
---|
[409] | 254 |
|
---|
| 255 | =cut
|
---|
| 256 |
|
---|
| 257 | sub pb_conf_add {
|
---|
| 258 |
|
---|
[415] | 259 | pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
|
---|
[1495] | 260 | my $lh;
|
---|
[898] | 261 |
|
---|
| 262 | foreach my $cf (@_) {
|
---|
[1495] | 263 | if (! -r $cf) {
|
---|
| 264 | pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
|
---|
| 265 | next;
|
---|
| 266 | }
|
---|
[898] | 267 | # Skip already used conf files
|
---|
[1495] | 268 | return($lh) if (defined $pbconffiles{$cf});
|
---|
| 269 |
|
---|
[2154] | 270 | # The new conf file overload values already managed
|
---|
[898] | 271 | my $num = keys %pbconffiles;
|
---|
[1495] | 272 | pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
|
---|
[898] | 273 | $pbconffiles{$cf} = $num;
|
---|
[1495] | 274 |
|
---|
| 275 | # Read the content of the config file
|
---|
| 276 | $lh = pb_conf_cache($cf,$lh);
|
---|
| 277 | # and cache it in the %h hash for further queries but after the previous
|
---|
| 278 | # as we load conf files in reverse order (most precise first)
|
---|
| 279 | pb_conf_add_last_in_hash($lh)
|
---|
[409] | 280 | }
|
---|
[898] | 281 | }
|
---|
[409] | 282 |
|
---|
[1495] | 283 |
|
---|
[405] | 284 | =item B<pb_conf_read_if>
|
---|
| 285 |
|
---|
| 286 | This function returns a table of pointers on hashes
|
---|
| 287 | corresponding to the keys in a configuration file passed in parameter.
|
---|
| 288 | If that file doesn't exist, it returns undef.
|
---|
| 289 |
|
---|
[1495] | 290 | The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
|
---|
| 291 |
|
---|
[405] | 292 | =cut
|
---|
| 293 |
|
---|
| 294 | sub pb_conf_read_if {
|
---|
| 295 |
|
---|
| 296 | my $conffile = shift;
|
---|
| 297 | my @param = @_;
|
---|
| 298 |
|
---|
| 299 | open(CONF,$conffile) || return((undef));
|
---|
| 300 | close(CONF);
|
---|
| 301 | return(pb_conf_read($conffile,@param));
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | =item B<pb_conf_read>
|
---|
| 305 |
|
---|
| 306 | This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
|
---|
| 307 |
|
---|
| 308 | =cut
|
---|
| 309 |
|
---|
| 310 | sub pb_conf_read {
|
---|
| 311 |
|
---|
| 312 | my $conffile = shift;
|
---|
| 313 | my @param = @_;
|
---|
| 314 | my @ptr;
|
---|
[1495] | 315 | my $lh;
|
---|
[405] | 316 |
|
---|
[1495] | 317 | $lh = pb_conf_cache($conffile,$lh);
|
---|
| 318 |
|
---|
| 319 | foreach my $param (@param) {
|
---|
| 320 | push @ptr,$lh->{$param};
|
---|
[405] | 321 | }
|
---|
[1495] | 322 | return(@ptr);
|
---|
| 323 | }
|
---|
[405] | 324 |
|
---|
[1904] | 325 | =item B<pb_conf_write>
|
---|
[1495] | 326 |
|
---|
[2278] | 327 | This function writes in the file passed as first parameter the hash of values passed as second parameter
|
---|
[1495] | 328 |
|
---|
[1904] | 329 | =cut
|
---|
| 330 |
|
---|
| 331 | sub pb_conf_write {
|
---|
| 332 |
|
---|
| 333 | my $conffile = shift;
|
---|
[1905] | 334 | my $h = shift;
|
---|
[2249] | 335 | my $dpfunc;
|
---|
[1904] | 336 |
|
---|
[1905] | 337 | confess "No configuration file defined to write into !" if (not defined $conffile);
|
---|
| 338 | confess "No hash defined to read from !" if (not defined $h);
|
---|
[2434] | 339 | open(CONF,"> $conffile") || (cluck "Unable to write into $conffile" && return);
|
---|
[1904] | 340 |
|
---|
[2279] | 341 | if ($PBCONFVER < 1) {
|
---|
[2176] | 342 | # This is the original conf file format for versions up to 0.14
|
---|
| 343 | foreach my $p (sort keys %$h) {
|
---|
| 344 | my $j = $h->{$p};
|
---|
| 345 | foreach my $k (sort keys %$j) {
|
---|
| 346 | print CONF "$p $k = $j->{$k}\n";
|
---|
| 347 | }
|
---|
[1904] | 348 | }
|
---|
[2176] | 349 | } else {
|
---|
| 350 | # This is the new YAML format
|
---|
[2249] | 351 | eval {
|
---|
| 352 | require YAML;
|
---|
| 353 | YAML->import();
|
---|
| 354 | };
|
---|
| 355 | if ($@) {
|
---|
| 356 | eval {
|
---|
| 357 | # No YAML found using a more std but less complete one. Old perl only
|
---|
| 358 | require Module::Build::YAML;
|
---|
| 359 | Module::Build::YAML->import();
|
---|
| 360 | };
|
---|
| 361 | if ($@) {
|
---|
| 362 | die "Unable to handle YAML configuration files without a YAML.pm module\n";
|
---|
| 363 | } else {
|
---|
| 364 | $dpfunc = \&Module::Build::YAML::Dump;
|
---|
| 365 | }
|
---|
| 366 | } else {
|
---|
| 367 | $dpfunc = \&YAML::Dump;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[2261] | 370 | pb_log(1,"Writing YAML conf file $conffile\n");
|
---|
[2249] | 371 | print CONF $dpfunc->($h);
|
---|
[1904] | 372 | }
|
---|
| 373 | close(CONF);
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 |
|
---|
| 377 |
|
---|
[1495] | 378 | =item B<pb_conf_get_in_hash_if>
|
---|
| 379 |
|
---|
[1594] | 380 | 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.
|
---|
| 381 | It takes a table of keys as an input parameter.
|
---|
[1495] | 382 |
|
---|
| 383 | =cut
|
---|
| 384 |
|
---|
| 385 | sub pb_conf_get_in_hash_if {
|
---|
| 386 |
|
---|
| 387 | my $lh = shift || return(());
|
---|
| 388 | my @params = @_;
|
---|
| 389 | my @ptr = ();
|
---|
| 390 |
|
---|
| 391 | pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
|
---|
| 392 | foreach my $k (@params) {
|
---|
| 393 | push @ptr,$lh->{$k};
|
---|
[405] | 394 | }
|
---|
[1495] | 395 |
|
---|
| 396 | pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
|
---|
[405] | 397 | return(@ptr);
|
---|
| 398 | }
|
---|
| 399 |
|
---|
[1495] | 400 |
|
---|
| 401 |
|
---|
[409] | 402 | =item B<pb_conf_get_if>
|
---|
[405] | 403 |
|
---|
[1495] | 404 | 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.
|
---|
[405] | 405 |
|
---|
[409] | 406 | =cut
|
---|
| 407 |
|
---|
| 408 | sub pb_conf_get_if {
|
---|
| 409 |
|
---|
[2154] | 410 | my @param = @_;
|
---|
| 411 | my @return = pb_conf_get_in_hash_if($h,@_);
|
---|
| 412 | my $proj = undef;
|
---|
| 413 |
|
---|
| 414 | if (not defined $ENV{'PBPROJ'}) {
|
---|
| 415 | $proj = "unknown";
|
---|
| 416 | } else {
|
---|
| 417 | $proj = $ENV{'PBPROJ'};
|
---|
[405] | 418 | }
|
---|
[409] | 419 |
|
---|
[2154] | 420 | foreach my $i (0..$#param) {
|
---|
| 421 | if (not defined $return[$i]->{$proj}) {
|
---|
| 422 | $return[$i]->{$proj} = $return[$i]->{'default'} if (defined $return[$i]->{'default'});
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 | return(@return);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[1495] | 428 | =item B<pb_conf_add_last_in_hash>
|
---|
[405] | 429 |
|
---|
[1495] | 430 | 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)
|
---|
[405] | 431 |
|
---|
[1495] | 432 | It is used internally by pb_conf_add and is not exported.
|
---|
[409] | 433 |
|
---|
| 434 | =cut
|
---|
| 435 |
|
---|
[1495] | 436 | sub pb_conf_add_last_in_hash {
|
---|
[409] | 437 |
|
---|
[1907] | 438 | my $ptr = shift;
|
---|
[409] | 439 |
|
---|
[1495] | 440 | return if (not defined $ptr);
|
---|
| 441 | # TODO: test $ptr is a hash pointer
|
---|
[405] | 442 |
|
---|
[1509] | 443 | # When called without correct initialization, try to work anyway with default as project
|
---|
| 444 | pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
|
---|
| 445 |
|
---|
[1495] | 446 | my @params = (sort keys %$ptr);
|
---|
[405] | 447 |
|
---|
[1495] | 448 | # Everything is returned via @h
|
---|
| 449 | # @h contains the values overloading what @ptr may contain.
|
---|
[2154] | 450 | my @h = pb_conf_get_in_hash_if($h,@params);
|
---|
[1495] | 451 | my @ptr = pb_conf_get_in_hash_if($ptr,@params);
|
---|
[409] | 452 |
|
---|
[405] | 453 | my $p1;
|
---|
| 454 | my $p2;
|
---|
| 455 |
|
---|
[1495] | 456 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
|
---|
[2154] | 457 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash current hash: ".Dumper(@h)."\n");
|
---|
| 458 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash new inputs: ".Dumper(@ptr)."\n");
|
---|
[405] | 459 |
|
---|
[1495] | 460 | foreach my $i (0..$#params) {
|
---|
| 461 | $p1 = $h[$i];
|
---|
| 462 | $p2 = $ptr[$i];
|
---|
[2154] | 463 | # Always try to take the param from h in priority
|
---|
[1495] | 464 | # in order to mask what could be defined already in ptr
|
---|
[405] | 465 | if (not defined $p2) {
|
---|
[415] | 466 | # exit if no p1 either
|
---|
[1509] | 467 | next if (not defined $p1);
|
---|
[405] | 468 | } else {
|
---|
[409] | 469 | # Ref found in p2
|
---|
[405] | 470 | if (not defined $p1) {
|
---|
[409] | 471 | # No ref in p1 so use p2's value
|
---|
[405] | 472 | $p1 = $p2;
|
---|
| 473 | } else {
|
---|
| 474 | # Both are defined - handling the overloading
|
---|
[2154] | 475 | # Now copy back into p1 all p2 content
|
---|
| 476 | # as p1 content always has priority over p2
|
---|
[405] | 477 | if (not defined $p1->{$ENV{'PBPROJ'}}) {
|
---|
| 478 | if (defined $p2->{$ENV{'PBPROJ'}}) {
|
---|
[1594] | 479 | $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
|
---|
[405] | 480 | }
|
---|
| 481 | }
|
---|
| 482 | # Now copy back into p1 all p2 content which doesn't exist in p1
|
---|
[2154] | 483 | # # p1 content always has priority over p2
|
---|
[405] | 484 | foreach my $k (keys %$p2) {
|
---|
| 485 | $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
[1495] | 489 | $h->{$params[$i]} = $p1;
|
---|
[405] | 490 | }
|
---|
[1495] | 491 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
|
---|
[405] | 492 | }
|
---|
| 493 |
|
---|
[409] | 494 | =item B<pb_conf_get>
|
---|
[405] | 495 |
|
---|
[409] | 496 | 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.
|
---|
| 497 |
|
---|
| 498 | =cut
|
---|
| 499 |
|
---|
| 500 | sub pb_conf_get {
|
---|
| 501 |
|
---|
| 502 | my @param = @_;
|
---|
| 503 | my @return = pb_conf_get_if(@param);
|
---|
[932] | 504 | my $proj = undef;
|
---|
[409] | 505 |
|
---|
[932] | 506 | if (not defined $ENV{'PBPROJ'}) {
|
---|
| 507 | $proj = "unknown";
|
---|
| 508 | } else {
|
---|
| 509 | $proj = $ENV{'PBPROJ'};
|
---|
| 510 | }
|
---|
[409] | 511 |
|
---|
[1538] | 512 | confess "No params found for $proj" if (not @return);
|
---|
[932] | 513 |
|
---|
[409] | 514 | foreach my $i (0..$#param) {
|
---|
[1507] | 515 | confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
|
---|
[409] | 516 | }
|
---|
| 517 | return(@return);
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[1495] | 520 |
|
---|
[1694] | 521 | =item B<pb_conf_get_all>
|
---|
| 522 |
|
---|
[2077] | 523 | This function returns an array with all configuration parameters
|
---|
[1694] | 524 |
|
---|
| 525 | =cut
|
---|
| 526 |
|
---|
| 527 | sub pb_conf_get_all {
|
---|
| 528 |
|
---|
| 529 | return(sort keys %$h);
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[2077] | 532 |
|
---|
| 533 | =item B<pb_conf_get_hash>
|
---|
| 534 |
|
---|
| 535 | This function returns a pointer to the hash with all configuration parameters
|
---|
| 536 |
|
---|
| 537 | =cut
|
---|
| 538 |
|
---|
| 539 | sub pb_conf_get_hash {
|
---|
| 540 |
|
---|
| 541 | return($h);
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[2253] | 544 | =item B<pb_conf_update_v0>
|
---|
| 545 |
|
---|
| 546 | This function transform the old configuration v0 file as first param into a new v1 one as second param
|
---|
| 547 |
|
---|
| 548 | =cut
|
---|
| 549 |
|
---|
| 550 | sub pb_conf_update_v0 {
|
---|
| 551 |
|
---|
| 552 | my $orig = shift;
|
---|
| 553 | my $dest = shift;
|
---|
| 554 |
|
---|
[2434] | 555 | open(ORIG,$orig) || (cluck "Unable to open $orig" && return);
|
---|
[2253] | 556 | confess "Will not erase existing $dest while transforming $orig" if (-f $dest);
|
---|
[2434] | 557 | open(DEST,"> $dest") || (cluck "Unable to write into $dest" && close(ORIG) && return);
|
---|
[2257] | 558 | print DEST "---\n";
|
---|
[2253] | 559 | my $pbconfverbkp = $PBCONFVER;
|
---|
| 560 | # We force migration from v0 to v1
|
---|
| 561 | $PBCONFVER = 0;
|
---|
| 562 | my $lh0;
|
---|
[2257] | 563 | my $lh1;
|
---|
[2253] | 564 | $lh0 = pb_conf_cache($orig,$lh0);
|
---|
[2263] | 565 | pb_log(2,"lh0:\n".Dumper($lh0)."\n");
|
---|
[2253] | 566 | $PBCONFVER = $pbconfverbkp;
|
---|
| 567 |
|
---|
[2264] | 568 | pb_log(0,"Converting v0 conf file $orig to v1 conf file $dest\n");
|
---|
[2402] | 569 | # We can't just write the YAML if we want to keep comments !
|
---|
[2253] | 570 | while (<ORIG>) {
|
---|
| 571 | if ($_ =~ /^#/) {
|
---|
| 572 | # Keep comments
|
---|
| 573 | print DEST $_;
|
---|
[2257] | 574 | } elsif ($_ =~ /^\s*$/) {
|
---|
| 575 | # Replace empty lines by comments
|
---|
| 576 | print DEST "#\n";;
|
---|
[2253] | 577 | } else {
|
---|
| 578 | if (/^\s*([A-z0-9-_]+)\s+(.+)$/) {
|
---|
| 579 | # Handle parameters
|
---|
[2257] | 580 | my ($param,$void) = ($1, $2);
|
---|
| 581 | if (not defined $lh1->{$param}) {
|
---|
| 582 | pb_log(2,"Converting parameter $param\n");
|
---|
[2300] | 583 | my $param2 = $param;
|
---|
| 584 | # param pburl in v0 is now pbprojurl in v1
|
---|
| 585 | $param2 =~ s/pburl/pbprojurl/;
|
---|
| 586 | print DEST "$param2:\n";
|
---|
[2257] | 587 | foreach my $k (keys %{$lh0->{$param}}) {
|
---|
| 588 | pb_log(2,"Handling key $k\n");
|
---|
[2264] | 589 | if ($lh0->{$param}->{$k} =~ /^\s*$/) {
|
---|
| 590 | print DEST " $k: !!str \"\"\n";
|
---|
| 591 | } else {
|
---|
| 592 | print DEST " $k: $lh0->{$param}->{$k}\n";
|
---|
| 593 | }
|
---|
[2257] | 594 | }
|
---|
| 595 | $lh1->{$param} = 1;
|
---|
| 596 | }
|
---|
[2253] | 597 | } else {
|
---|
[2257] | 598 | pb_log(0,"Unable to convert line $_\n");
|
---|
[2253] | 599 | }
|
---|
| 600 | }
|
---|
| 601 | }
|
---|
| 602 | close(ORIG);
|
---|
| 603 | close(DEST);
|
---|
| 604 | return();
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[405] | 607 | =back
|
---|
| 608 |
|
---|
| 609 | =head1 WEB SITES
|
---|
| 610 |
|
---|
| 611 | 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/>.
|
---|
| 612 |
|
---|
| 613 | =head1 USER MAILING LIST
|
---|
| 614 |
|
---|
| 615 | None exists for the moment.
|
---|
| 616 |
|
---|
| 617 | =head1 AUTHORS
|
---|
| 618 |
|
---|
| 619 | The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
|
---|
| 620 |
|
---|
| 621 | =head1 COPYRIGHT
|
---|
| 622 |
|
---|
| 623 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
| 624 | described in the file C<COPYING> included with the distribution.
|
---|
| 625 |
|
---|
| 626 | =cut
|
---|
| 627 |
|
---|
| 628 |
|
---|
| 629 | 1;
|
---|