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