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