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