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