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