[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 | #
|
---|
[1528] | 7 | # Copyright B. Cornec 2007-2012
|
---|
| 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;
|
---|
[1507] | 17 | use Carp '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 |
|
---|
[1156] | 24 | use vars qw($VERSION $REVISION @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);
|
---|
[1694] | 31 | our @EXPORT = qw(pb_conf_init pb_conf_add pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if pb_conf_print pb_conf_get_all);
|
---|
[1156] | 32 | ($VERSION,$REVISION) = 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 | #
|
---|
| 63 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
|
---|
| 64 | my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
|
---|
| 65 |
|
---|
| 66 | =head1 USAGE
|
---|
| 67 |
|
---|
| 68 | =over 4
|
---|
| 69 |
|
---|
[505] | 70 | =item B<pb_conf_init>
|
---|
| 71 |
|
---|
[898] | 72 | This function setup the environment PBPROJ for project-builder function usage from other projects.
|
---|
[505] | 73 | The first parameter is the project name.
|
---|
[898] | 74 | It sets up environment variables (PBPROJ)
|
---|
[505] | 75 |
|
---|
| 76 | =cut
|
---|
| 77 |
|
---|
| 78 | sub pb_conf_init {
|
---|
| 79 |
|
---|
| 80 | my $proj=shift || undef;
|
---|
| 81 |
|
---|
[1495] | 82 | pb_log(1,"Entering pb_conf_init\n");
|
---|
[1584] | 83 | #
|
---|
| 84 | # Check project name
|
---|
| 85 | # Could be with env var PBPROJ
|
---|
| 86 | # or option -p
|
---|
| 87 | # if not defined take the first in conf file
|
---|
| 88 | #
|
---|
| 89 | if ((defined $ENV{'PBPROJ'}) &&
|
---|
| 90 | (not defined $proj)) {
|
---|
| 91 | pb_log(2,"PBPROJ env var setup ($ENV{'PBPROJ'}) so using it\n");
|
---|
| 92 | $proj = $ENV{'PBPROJ'};
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[505] | 95 | if (defined $proj) {
|
---|
| 96 | $ENV{'PBPROJ'} = $proj;
|
---|
| 97 | } else {
|
---|
| 98 | $ENV{'PBPROJ'} = "default";
|
---|
| 99 | }
|
---|
[1495] | 100 | pb_log(1,"PBPROJ = $ENV{'PBPROJ'}\n");
|
---|
[505] | 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
[1495] | 104 | =item B<pb_conf_cache>
|
---|
[505] | 105 |
|
---|
[1495] | 106 | This function caches the configuration file content passed as first parameter into the a hash passed in second parameter
|
---|
| 107 | It returns the modified hash
|
---|
| 108 | Can be used in correlation with the %h hash to store permanently values or not if temporarily.
|
---|
| 109 |
|
---|
| 110 | =cut
|
---|
| 111 |
|
---|
| 112 | sub pb_conf_cache {
|
---|
| 113 |
|
---|
| 114 | my $cf = shift;
|
---|
| 115 | my $lh = shift;
|
---|
| 116 |
|
---|
| 117 | # Read the content of the config file and cache it in the %h hash further availble for queries
|
---|
[1538] | 118 | open(CONF,$cf) || confess "Unable to open $cf";
|
---|
[1495] | 119 | while(<CONF>) {
|
---|
[1644] | 120 | if (/^\s*([A-z0-9-_.]+)\s+([[A-z0-9-_.]+)\s*=\s*(.*)$/) {
|
---|
[1495] | 121 | pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
|
---|
| 122 | $lh->{$1}->{$2}=$3;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | close(CONF);
|
---|
| 126 | return($lh);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[409] | 129 | =item B<pb_conf_add>
|
---|
| 130 |
|
---|
[1495] | 131 | This function adds the configuration file to the list last, and cache their content in the %h hash
|
---|
[409] | 132 |
|
---|
| 133 | =cut
|
---|
| 134 |
|
---|
| 135 | sub pb_conf_add {
|
---|
| 136 |
|
---|
[415] | 137 | pb_log(2,"DEBUG: pb_conf_add with ".Dumper(@_)."\n");
|
---|
[1495] | 138 | my $lh;
|
---|
[898] | 139 |
|
---|
| 140 | foreach my $cf (@_) {
|
---|
[1495] | 141 | if (! -r $cf) {
|
---|
| 142 | pb_log(0,"WARNING: pb_conf_add can not read $cf\n");
|
---|
| 143 | next;
|
---|
| 144 | }
|
---|
[898] | 145 | # Skip already used conf files
|
---|
[1495] | 146 | return($lh) if (defined $pbconffiles{$cf});
|
---|
| 147 |
|
---|
[898] | 148 | # Add the new one at the end
|
---|
| 149 | my $num = keys %pbconffiles;
|
---|
[1495] | 150 | pb_log(2,"DEBUG: pb_conf_cache of $cf at position $num\n");
|
---|
[898] | 151 | $pbconffiles{$cf} = $num;
|
---|
[1495] | 152 |
|
---|
| 153 | # Read the content of the config file
|
---|
| 154 | $lh = pb_conf_cache($cf,$lh);
|
---|
| 155 | # and cache it in the %h hash for further queries but after the previous
|
---|
| 156 | # as we load conf files in reverse order (most precise first)
|
---|
| 157 | pb_conf_add_last_in_hash($lh)
|
---|
[409] | 158 | }
|
---|
[898] | 159 | }
|
---|
[409] | 160 |
|
---|
[1495] | 161 |
|
---|
[405] | 162 | =item B<pb_conf_read_if>
|
---|
| 163 |
|
---|
| 164 | This function returns a table of pointers on hashes
|
---|
| 165 | corresponding to the keys in a configuration file passed in parameter.
|
---|
| 166 | If that file doesn't exist, it returns undef.
|
---|
| 167 |
|
---|
| 168 | The format of the configuration file is as follows:
|
---|
| 169 |
|
---|
| 170 | key tag = value1,value2,...
|
---|
| 171 |
|
---|
| 172 | Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
|
---|
| 173 |
|
---|
| 174 | $ cat $HOME/.pbrc
|
---|
| 175 | pbver pb = 3
|
---|
| 176 | pbver default = 1
|
---|
| 177 | pblist pb = 12,25
|
---|
| 178 |
|
---|
| 179 | calling it like this:
|
---|
| 180 |
|
---|
| 181 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
|
---|
| 182 |
|
---|
| 183 | will allow to get the mapping:
|
---|
| 184 |
|
---|
| 185 | $k1->{'pb'} contains 3
|
---|
[409] | 186 | $k1->{'default'} contains 1
|
---|
[405] | 187 | $k2->{'pb'} contains 12,25
|
---|
| 188 |
|
---|
| 189 | Valid chars for keys and tags are letters, numbers, '-' and '_'.
|
---|
| 190 |
|
---|
[1495] | 191 | The file read is forgotten after its usage. If you want permanent caching of the data, use pb_conf_add then pb_conf_get
|
---|
| 192 |
|
---|
[405] | 193 | =cut
|
---|
| 194 |
|
---|
| 195 | sub pb_conf_read_if {
|
---|
| 196 |
|
---|
| 197 | my $conffile = shift;
|
---|
| 198 | my @param = @_;
|
---|
| 199 |
|
---|
| 200 | open(CONF,$conffile) || return((undef));
|
---|
| 201 | close(CONF);
|
---|
| 202 | return(pb_conf_read($conffile,@param));
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | =item B<pb_conf_read>
|
---|
| 206 |
|
---|
| 207 | This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
|
---|
| 208 |
|
---|
| 209 | =cut
|
---|
| 210 |
|
---|
| 211 | sub pb_conf_read {
|
---|
| 212 |
|
---|
| 213 | my $conffile = shift;
|
---|
| 214 | my @param = @_;
|
---|
| 215 | my @ptr;
|
---|
[1495] | 216 | my $lh;
|
---|
[405] | 217 |
|
---|
[1495] | 218 | $lh = pb_conf_cache($conffile,$lh);
|
---|
| 219 |
|
---|
| 220 | foreach my $param (@param) {
|
---|
| 221 | push @ptr,$lh->{$param};
|
---|
[405] | 222 | }
|
---|
[1495] | 223 | return(@ptr);
|
---|
| 224 | }
|
---|
[405] | 225 |
|
---|
[1495] | 226 |
|
---|
| 227 |
|
---|
| 228 | =item B<pb_conf_get_in_hash_if>
|
---|
| 229 |
|
---|
[1594] | 230 | 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.
|
---|
| 231 | It takes a table of keys as an input parameter.
|
---|
[1495] | 232 |
|
---|
| 233 | =cut
|
---|
| 234 |
|
---|
| 235 | sub pb_conf_get_in_hash_if {
|
---|
| 236 |
|
---|
| 237 | my $lh = shift || return(());
|
---|
| 238 | my @params = @_;
|
---|
| 239 | my @ptr = ();
|
---|
| 240 |
|
---|
| 241 | pb_log(2,"DEBUG: pb_conf_get_in_hash_if on params ".join(' ',@params)."\n");
|
---|
| 242 | foreach my $k (@params) {
|
---|
| 243 | push @ptr,$lh->{$k};
|
---|
[405] | 244 | }
|
---|
[1495] | 245 |
|
---|
| 246 | pb_log(2,"DEBUG: pb_conf_get_in_hash_if returns\n".Dumper(@ptr));
|
---|
[405] | 247 | return(@ptr);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[1495] | 250 |
|
---|
| 251 |
|
---|
[409] | 252 | =item B<pb_conf_get_if>
|
---|
[405] | 253 |
|
---|
[1495] | 254 | 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] | 255 |
|
---|
[409] | 256 | The format of the configurations file is as follows:
|
---|
| 257 |
|
---|
| 258 | key tag = value1,value2,...
|
---|
| 259 |
|
---|
[1495] | 260 | It will gather the values from all the configurations files passed to pb_conf_add, and return the values for the keys
|
---|
[409] | 261 |
|
---|
| 262 | $ cat $HOME/.pbrc
|
---|
| 263 | pbver pb = 1
|
---|
| 264 | pblist pb = 4
|
---|
| 265 | $ cat $HOME/.pbrc2
|
---|
| 266 | pbver pb = 3
|
---|
| 267 | pblist default = 5
|
---|
| 268 |
|
---|
| 269 | calling it like this:
|
---|
| 270 |
|
---|
[505] | 271 | pb_conf_add("$HOME/.pbrc","$HOME/.pbrc2");
|
---|
[409] | 272 | my ($k1, $k2) = pb_conf_get_if("pbver","pblist");
|
---|
| 273 |
|
---|
| 274 | will allow to get the mapping:
|
---|
| 275 |
|
---|
| 276 | $k1->{'pb'} contains 3
|
---|
| 277 | $k2->{'pb'} contains 4
|
---|
| 278 |
|
---|
| 279 | Valid chars for keys and tags are letters, numbers, '-' and '_'.
|
---|
| 280 |
|
---|
| 281 | =cut
|
---|
| 282 |
|
---|
| 283 | sub pb_conf_get_if {
|
---|
| 284 |
|
---|
[1594] | 285 | return(pb_conf_get_in_hash_if($h,@_));
|
---|
[405] | 286 | }
|
---|
[409] | 287 |
|
---|
[1495] | 288 | =item B<pb_conf_add_last_in_hash>
|
---|
[405] | 289 |
|
---|
[1495] | 290 | 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] | 291 |
|
---|
[1495] | 292 | It is used internally by pb_conf_add and is not exported.
|
---|
[409] | 293 |
|
---|
| 294 | =cut
|
---|
| 295 |
|
---|
[1495] | 296 | sub pb_conf_add_last_in_hash {
|
---|
[409] | 297 |
|
---|
[1495] | 298 | my $ptr = shift || undef;
|
---|
[409] | 299 |
|
---|
[1495] | 300 | return if (not defined $ptr);
|
---|
| 301 | # TODO: test $ptr is a hash pointer
|
---|
[405] | 302 |
|
---|
[1509] | 303 | # When called without correct initialization, try to work anyway with default as project
|
---|
| 304 | pb_conf_init("default") if (not defined $ENV{'PBPROJ'});
|
---|
| 305 |
|
---|
[1495] | 306 | my @params = (sort keys %$ptr);
|
---|
[405] | 307 |
|
---|
[1495] | 308 | # Everything is returned via @h
|
---|
| 309 | # @h contains the values overloading what @ptr may contain.
|
---|
| 310 | my @h = pb_conf_get_if(@params);
|
---|
| 311 | my @ptr = pb_conf_get_in_hash_if($ptr,@params);
|
---|
[409] | 312 |
|
---|
[405] | 313 | my $p1;
|
---|
| 314 | my $p2;
|
---|
| 315 |
|
---|
[1495] | 316 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash params: ".Dumper(@params)."\n");
|
---|
| 317 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash hash: ".Dumper(@h)."\n");
|
---|
| 318 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash input: ".Dumper(@ptr)."\n");
|
---|
[405] | 319 |
|
---|
[1495] | 320 | foreach my $i (0..$#params) {
|
---|
| 321 | $p1 = $h[$i];
|
---|
| 322 | $p2 = $ptr[$i];
|
---|
| 323 | # Always try to take the param from h
|
---|
| 324 | # in order to mask what could be defined already in ptr
|
---|
[405] | 325 | if (not defined $p2) {
|
---|
[415] | 326 | # exit if no p1 either
|
---|
[1509] | 327 | next if (not defined $p1);
|
---|
[409] | 328 | # No ref in p2 so use p1
|
---|
[405] | 329 | $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
|
---|
| 330 | } else {
|
---|
[409] | 331 | # Ref found in p2
|
---|
[405] | 332 | if (not defined $p1) {
|
---|
[409] | 333 | # No ref in p1 so use p2's value
|
---|
[405] | 334 | $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
|
---|
| 335 | $p1 = $p2;
|
---|
| 336 | } else {
|
---|
| 337 | # Both are defined - handling the overloading
|
---|
| 338 | if (not defined $p1->{'default'}) {
|
---|
| 339 | if (defined $p2->{'default'}) {
|
---|
| 340 | $p1->{'default'} = $p2->{'default'};
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | if (not defined $p1->{$ENV{'PBPROJ'}}) {
|
---|
| 345 | if (defined $p2->{$ENV{'PBPROJ'}}) {
|
---|
[1594] | 346 | $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}};
|
---|
[405] | 347 | } else {
|
---|
| 348 | $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 | # Now copy back into p1 all p2 content which doesn't exist in p1
|
---|
[409] | 352 | # p1 content always has priority over p2
|
---|
[405] | 353 | foreach my $k (keys %$p2) {
|
---|
| 354 | $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
[1495] | 358 | $h->{$params[$i]} = $p1;
|
---|
[405] | 359 | }
|
---|
[1495] | 360 | pb_log(2,"DEBUG: pb_conf_add_last_in_hash output: ".Dumper($h)."\n");
|
---|
[405] | 361 | }
|
---|
| 362 |
|
---|
[409] | 363 | =item B<pb_conf_get>
|
---|
[405] | 364 |
|
---|
[409] | 365 | 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.
|
---|
| 366 |
|
---|
| 367 | =cut
|
---|
| 368 |
|
---|
| 369 | sub pb_conf_get {
|
---|
| 370 |
|
---|
| 371 | my @param = @_;
|
---|
| 372 | my @return = pb_conf_get_if(@param);
|
---|
[932] | 373 | my $proj = undef;
|
---|
[409] | 374 |
|
---|
[932] | 375 | if (not defined $ENV{'PBPROJ'}) {
|
---|
| 376 | $proj = "unknown";
|
---|
| 377 | } else {
|
---|
| 378 | $proj = $ENV{'PBPROJ'};
|
---|
| 379 | }
|
---|
[409] | 380 |
|
---|
[1538] | 381 | confess "No params found for $proj" if (not @return);
|
---|
[932] | 382 |
|
---|
[409] | 383 | foreach my $i (0..$#param) {
|
---|
[1507] | 384 | confess "No $param[$i] defined for $proj" if (not defined $return[$i]);
|
---|
[409] | 385 | }
|
---|
| 386 | return(@return);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[1495] | 389 |
|
---|
[1694] | 390 | =item B<pb_conf_get_all>
|
---|
| 391 |
|
---|
| 392 | This function returns an array wirh all configuration parameters
|
---|
| 393 |
|
---|
| 394 | =cut
|
---|
| 395 |
|
---|
| 396 | sub pb_conf_get_all {
|
---|
| 397 |
|
---|
| 398 | return(sort keys %$h);
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[405] | 401 | =back
|
---|
| 402 |
|
---|
| 403 | =head1 WEB SITES
|
---|
| 404 |
|
---|
| 405 | 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/>.
|
---|
| 406 |
|
---|
| 407 | =head1 USER MAILING LIST
|
---|
| 408 |
|
---|
| 409 | None exists for the moment.
|
---|
| 410 |
|
---|
| 411 | =head1 AUTHORS
|
---|
| 412 |
|
---|
| 413 | The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
|
---|
| 414 |
|
---|
| 415 | =head1 COPYRIGHT
|
---|
| 416 |
|
---|
| 417 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
| 418 | described in the file C<COPYING> included with the distribution.
|
---|
| 419 |
|
---|
| 420 | =cut
|
---|
| 421 |
|
---|
| 422 |
|
---|
| 423 | 1;
|
---|