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