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