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