Changeset 409 in ProjectBuilder for devel/pb-modules
- Timestamp:
- Apr 25, 2008, 3:13:55 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
devel/pb-modules/lib/ProjectBuilder/Conf.pm
r405 r409 22 22 23 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); 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 list of conf files 27 our @pbconffiles = (); 25 28 26 29 =pod … … 57 60 sub pb_conf_init { 58 61 59 my @conffiles = @_; 62 @pbconffiles = @_; 63 } 64 65 =item B<pb_conf_add> 66 67 This function adds the configuration file to the list last. 68 69 =cut 70 71 sub pb_conf_add { 72 73 my $f = shift; 74 75 push(@pbconffiles,"$f"); 60 76 } 61 77 … … 84 100 85 101 $k1->{'pb'} contains 3 86 $k a->{'default'} contains 1102 $k1->{'default'} contains 1 87 103 $k2->{'pb'} contains 12,25 88 104 … … 130 146 } 131 147 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 148 =item B<pb_conf_get_if> 149 150 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. 151 152 The format of the configurations file is as follows: 153 154 key tag = value1,value2,... 155 156 It will gather the values from all the configurations files passed to pb_conf_init, and return the values for the keys, taking in account the order of conf files, to manage overloading. 157 158 $ cat $HOME/.pbrc 159 pbver pb = 1 160 pblist pb = 4 161 $ cat $HOME/.pbrc2 162 pbver pb = 3 163 pblist default = 5 164 165 calling it like this: 166 167 pb_conf_init("$HOME/.pbrc","$HOME/.pbrc2"); 168 my ($k1, $k2) = pb_conf_get_if("pbver","pblist"); 169 170 will allow to get the mapping: 171 172 $k1->{'pb'} contains 3 173 $k2->{'pb'} contains 4 174 175 Valid chars for keys and tags are letters, numbers, '-' and '_'. 176 177 =cut 178 152 179 sub pb_conf_get_if { 153 180 181 my @param = @_; 182 183 my $ptr = undef; 184 185 # the most important conf file is first, so read them in revers order 186 foreach my $f (reverse @pbconffiles) { 187 $ptr = pb_conf_get_fromfile_if("$f",$ptr,@param); 188 } 189 190 return(@$ptr); 191 } 192 193 =item B<pb_conf_fromfile_if> 194 195 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. 196 197 my ($k1) = pb_conf_fromfile_if("$HOME/.pbrc",undef,"pbver","pblist"); 198 my ($k2) = pb_conf_fromfile_if("$HOME/.pbrc3",$k1,"pbver","pblist"); 199 200 It is used internally by pb_conf_get_if and is not exported yet. 201 202 =cut 203 204 205 sub pb_conf_get_fromfile_if { 206 207 my $conffile = shift; 208 my $ptr2 = shift || undef; 154 209 my @param = @_; 155 210 … … 157 212 my @ptr1 = (); 158 213 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'})); 214 215 # @ptr1 contains the values overloading what @ptr2 may contain. 216 @ptr1 = pb_conf_read_if("$conffile", @param) if (defined $conffile); 217 @ptr2 = @$ptr2 if (defined $ptr2); 161 218 162 219 my $p1; … … 169 226 $p1 = $ptr1[$i]; 170 227 $p2 = $ptr2[$i]; 171 # Always try to take the param from the home dir conf file in priority172 # in order to mask what could be defined under the CMS to allow for overloading228 # Always try to take the param from ptr1 229 # in order to mask what could be defined already in ptr2 173 230 if (not defined $p2) { 174 # No ref in CMS project conf file so use the home dir one.231 # No ref in p2 so use p1 175 232 $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'})); 176 233 } else { 177 # Ref found in CMS project conf file234 # Ref found in p2 178 235 if (not defined $p1) { 179 # No ref in home dir project conf file so use the CMS one.236 # No ref in p1 so use p2's value 180 237 $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'})); 181 238 $p1 = $p2; … … 196 253 } 197 254 # Now copy back into p1 all p2 content which doesn't exist in p1 198 # p1 content (local) always has priority over p2 (project)255 # p1 content always has priority over p2 199 256 foreach my $k (keys %$p2) { 200 257 $p1->{$k} = $p2->{$k} if (not defined $p1->{$k}); … … 205 262 } 206 263 pb_log(2,"DEBUG: pb_conf_get param ptr1: ".Dumper(@ptr1)."\n"); 207 return(@ptr1); 208 } 209 264 return(\@ptr1); 265 } 266 267 =item B<pb_conf_get> 268 269 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. 270 271 =cut 272 273 sub pb_conf_get { 274 275 my @param = @_; 276 my @return = pb_conf_get_if(@param); 277 278 die "No params found for $ENV{'PBPROJ'}" if (not @return); 279 280 foreach my $i (0..$#param) { 281 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]); 282 } 283 return(@return); 284 } 210 285 211 286 =back
Note:
See TracChangeset
for help on using the changeset viewer.