[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);
|
---|
| 24 | our @EXPORT = qw(pb_conf_init pb_conf_read pb_conf_read_if pb_conf_get pb_conf_get_if);
|
---|
| 25 |
|
---|
| 26 | =pod
|
---|
| 27 |
|
---|
| 28 | =head1 NAME
|
---|
| 29 |
|
---|
| 30 | ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
|
---|
| 31 |
|
---|
| 32 | =head1 DESCRIPTION
|
---|
| 33 |
|
---|
| 34 | This modules provides functions dealing with configuration files.
|
---|
| 35 |
|
---|
| 36 | =head1 SYNOPSIS
|
---|
| 37 |
|
---|
| 38 | use ProjectBuilder::Conf;
|
---|
| 39 |
|
---|
| 40 | #
|
---|
| 41 | # Read hash codes of values from a configuration file and return table of pointers
|
---|
| 42 | #
|
---|
| 43 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
|
---|
| 44 | my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
|
---|
| 45 |
|
---|
| 46 | =head1 USAGE
|
---|
| 47 |
|
---|
| 48 | =over 4
|
---|
| 49 |
|
---|
| 50 | =item B<pb_conf_init>
|
---|
| 51 |
|
---|
| 52 | This function initializes the configuration files analysis.
|
---|
| 53 | Pass the configuration files to consider in order of importance (the most important first).
|
---|
| 54 |
|
---|
| 55 | =cut
|
---|
| 56 |
|
---|
| 57 | sub pb_conf_init {
|
---|
| 58 |
|
---|
| 59 | my @conffiles = @_;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | =item B<pb_conf_read_if>
|
---|
| 63 |
|
---|
| 64 | This function returns a table of pointers on hashes
|
---|
| 65 | corresponding to the keys in a configuration file passed in parameter.
|
---|
| 66 | If that file doesn't exist, it returns undef.
|
---|
| 67 |
|
---|
| 68 | The format of the configuration file is as follows:
|
---|
| 69 |
|
---|
| 70 | key tag = value1,value2,...
|
---|
| 71 |
|
---|
| 72 | Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
|
---|
| 73 |
|
---|
| 74 | $ cat $HOME/.pbrc
|
---|
| 75 | pbver pb = 3
|
---|
| 76 | pbver default = 1
|
---|
| 77 | pblist pb = 12,25
|
---|
| 78 |
|
---|
| 79 | calling it like this:
|
---|
| 80 |
|
---|
| 81 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
|
---|
| 82 |
|
---|
| 83 | will allow to get the mapping:
|
---|
| 84 |
|
---|
| 85 | $k1->{'pb'} contains 3
|
---|
| 86 | $ka->{'default'} contains 1
|
---|
| 87 | $k2->{'pb'} contains 12,25
|
---|
| 88 |
|
---|
| 89 | Valid chars for keys and tags are letters, numbers, '-' and '_'.
|
---|
| 90 |
|
---|
| 91 | =cut
|
---|
| 92 |
|
---|
| 93 | sub pb_conf_read_if {
|
---|
| 94 |
|
---|
| 95 | my $conffile = shift;
|
---|
| 96 | my @param = @_;
|
---|
| 97 |
|
---|
| 98 | open(CONF,$conffile) || return((undef));
|
---|
| 99 | close(CONF);
|
---|
| 100 | return(pb_conf_read($conffile,@param));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | =item B<pb_conf_read>
|
---|
| 104 |
|
---|
| 105 | This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
|
---|
| 106 |
|
---|
| 107 | =cut
|
---|
| 108 |
|
---|
| 109 | sub pb_conf_read {
|
---|
| 110 |
|
---|
| 111 | my $conffile = shift;
|
---|
| 112 | my @param = @_;
|
---|
| 113 | my $trace;
|
---|
| 114 | my @ptr;
|
---|
| 115 | my %h;
|
---|
| 116 |
|
---|
| 117 | open(CONF,$conffile) || die "Unable to open $conffile";
|
---|
| 118 | while(<CONF>) {
|
---|
| 119 | if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
|
---|
| 120 | pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
|
---|
| 121 | $h{$1}{$2}=$3;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | close(CONF);
|
---|
| 125 |
|
---|
| 126 | for my $param (@param) {
|
---|
| 127 | push @ptr,$h{$param};
|
---|
| 128 | }
|
---|
| 129 | return(@ptr);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | # Function which returns a pointer on a table
|
---|
| 134 | # corresponding to a set of values queried in the conf file
|
---|
| 135 | # and test the returned vaue as they need to exist in that case
|
---|
| 136 | sub pb_conf_get {
|
---|
| 137 |
|
---|
| 138 | my @param = @_;
|
---|
| 139 | my @return = pb_conf_get_if(@param);
|
---|
| 140 |
|
---|
| 141 | die "No params found for $ENV{'PBPROJ'}" if (not @return);
|
---|
| 142 |
|
---|
| 143 | foreach my $i (0..$#param) {
|
---|
| 144 | die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);
|
---|
| 145 | }
|
---|
| 146 | return(@return);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | # Function which returns a pointer on a table
|
---|
| 150 | # corresponding to a set of values queried in the conf file
|
---|
| 151 | # Those value may be undef if they do not exist
|
---|
| 152 | sub pb_conf_get_if {
|
---|
| 153 |
|
---|
| 154 | my @param = @_;
|
---|
| 155 |
|
---|
| 156 | # Everything is returned via ptr1
|
---|
| 157 | my @ptr1 = ();
|
---|
| 158 | my @ptr2 = ();
|
---|
| 159 | @ptr1 = pb_conf_read_if("$ENV{'PBETC'}", @param) if (defined $ENV{'PBETC'});
|
---|
| 160 | @ptr2 = pb_conf_read_if("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.pb", @param) if ((defined $ENV{'PBROOTDIR'}) and (defined $ENV{'PBPROJ'}));
|
---|
| 161 |
|
---|
| 162 | my $p1;
|
---|
| 163 | my $p2;
|
---|
| 164 |
|
---|
| 165 | pb_log(2,"DEBUG: pb_conf_get param1: ".Dumper(@ptr1)."\n");
|
---|
| 166 | pb_log(2,"DEBUG: pb_conf_get param2: ".Dumper(@ptr2)."\n");
|
---|
| 167 |
|
---|
| 168 | foreach my $i (0..$#param) {
|
---|
| 169 | $p1 = $ptr1[$i];
|
---|
| 170 | $p2 = $ptr2[$i];
|
---|
| 171 | # Always try to take the param from the home dir conf file in priority
|
---|
| 172 | # in order to mask what could be defined under the CMS to allow for overloading
|
---|
| 173 | if (not defined $p2) {
|
---|
| 174 | # No ref in CMS project conf file so use the home dir one.
|
---|
| 175 | $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
|
---|
| 176 | } else {
|
---|
| 177 | # Ref found in CMS project conf file
|
---|
| 178 | if (not defined $p1) {
|
---|
| 179 | # No ref in home dir project conf file so use the CMS one.
|
---|
| 180 | $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
|
---|
| 181 | $p1 = $p2;
|
---|
| 182 | } else {
|
---|
| 183 | # Both are defined - handling the overloading
|
---|
| 184 | if (not defined $p1->{'default'}) {
|
---|
| 185 | if (defined $p2->{'default'}) {
|
---|
| 186 | $p1->{'default'} = $p2->{'default'};
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | if (not defined $p1->{$ENV{'PBPROJ'}}) {
|
---|
| 191 | if (defined $p2->{$ENV{'PBPROJ'}}) {
|
---|
| 192 | $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
|
---|
| 193 | } else {
|
---|
| 194 | $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | # Now copy back into p1 all p2 content which doesn't exist in p1
|
---|
| 198 | # p1 content (local) always has priority over p2 (project)
|
---|
| 199 | foreach my $k (keys %$p2) {
|
---|
| 200 | $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | $ptr1[$i] = $p1;
|
---|
| 205 | }
|
---|
| 206 | pb_log(2,"DEBUG: pb_conf_get param ptr1: ".Dumper(@ptr1)."\n");
|
---|
| 207 | return(@ptr1);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 | =back
|
---|
| 212 |
|
---|
| 213 | =head1 WEB SITES
|
---|
| 214 |
|
---|
| 215 | 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/>.
|
---|
| 216 |
|
---|
| 217 | =head1 USER MAILING LIST
|
---|
| 218 |
|
---|
| 219 | None exists for the moment.
|
---|
| 220 |
|
---|
| 221 | =head1 AUTHORS
|
---|
| 222 |
|
---|
| 223 | The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
|
---|
| 224 |
|
---|
| 225 | =head1 COPYRIGHT
|
---|
| 226 |
|
---|
| 227 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
| 228 | described in the file C<COPYING> included with the distribution.
|
---|
| 229 |
|
---|
| 230 | =cut
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 | 1;
|
---|