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 initializes the configuration files analysis.
|
---|
56 | Pass the configuration files to consider in order of importance (the most important first).
|
---|
57 |
|
---|
58 | =cut
|
---|
59 |
|
---|
60 | sub pb_conf_init {
|
---|
61 |
|
---|
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");
|
---|
76 | }
|
---|
77 |
|
---|
78 | =item B<pb_conf_read_if>
|
---|
79 |
|
---|
80 | This function returns a table of pointers on hashes
|
---|
81 | corresponding to the keys in a configuration file passed in parameter.
|
---|
82 | If that file doesn't exist, it returns undef.
|
---|
83 |
|
---|
84 | The format of the configuration file is as follows:
|
---|
85 |
|
---|
86 | key tag = value1,value2,...
|
---|
87 |
|
---|
88 | Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
|
---|
89 |
|
---|
90 | $ cat $HOME/.pbrc
|
---|
91 | pbver pb = 3
|
---|
92 | pbver default = 1
|
---|
93 | pblist pb = 12,25
|
---|
94 |
|
---|
95 | calling it like this:
|
---|
96 |
|
---|
97 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
|
---|
98 |
|
---|
99 | will allow to get the mapping:
|
---|
100 |
|
---|
101 | $k1->{'pb'} contains 3
|
---|
102 | $k1->{'default'} contains 1
|
---|
103 | $k2->{'pb'} contains 12,25
|
---|
104 |
|
---|
105 | Valid chars for keys and tags are letters, numbers, '-' and '_'.
|
---|
106 |
|
---|
107 | =cut
|
---|
108 |
|
---|
109 | sub pb_conf_read_if {
|
---|
110 |
|
---|
111 | my $conffile = shift;
|
---|
112 | my @param = @_;
|
---|
113 |
|
---|
114 | open(CONF,$conffile) || return((undef));
|
---|
115 | close(CONF);
|
---|
116 | return(pb_conf_read($conffile,@param));
|
---|
117 | }
|
---|
118 |
|
---|
119 | =item B<pb_conf_read>
|
---|
120 |
|
---|
121 | This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
|
---|
122 |
|
---|
123 | =cut
|
---|
124 |
|
---|
125 | sub pb_conf_read {
|
---|
126 |
|
---|
127 | my $conffile = shift;
|
---|
128 | my @param = @_;
|
---|
129 | my $trace;
|
---|
130 | my @ptr;
|
---|
131 | my %h;
|
---|
132 |
|
---|
133 | open(CONF,$conffile) || die "Unable to open $conffile";
|
---|
134 | while(<CONF>) {
|
---|
135 | if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
|
---|
136 | pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
|
---|
137 | $h{$1}{$2}=$3;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | close(CONF);
|
---|
141 |
|
---|
142 | for my $param (@param) {
|
---|
143 | push @ptr,$h{$param};
|
---|
144 | }
|
---|
145 | return(@ptr);
|
---|
146 | }
|
---|
147 |
|
---|
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 |
|
---|
179 | sub pb_conf_get_if {
|
---|
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;
|
---|
209 | my @param = @_;
|
---|
210 |
|
---|
211 | # Everything is returned via ptr1
|
---|
212 | my @ptr1 = ();
|
---|
213 | my @ptr2 = ();
|
---|
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);
|
---|
218 |
|
---|
219 | my $p1;
|
---|
220 | my $p2;
|
---|
221 |
|
---|
222 | pb_log(2,"DEBUG: pb_conf_get param1: ".Dumper(@ptr1)."\n");
|
---|
223 | pb_log(2,"DEBUG: pb_conf_get param2: ".Dumper(@ptr2)."\n");
|
---|
224 |
|
---|
225 | foreach my $i (0..$#param) {
|
---|
226 | $p1 = $ptr1[$i];
|
---|
227 | $p2 = $ptr2[$i];
|
---|
228 | # Always try to take the param from ptr1
|
---|
229 | # in order to mask what could be defined already in ptr2
|
---|
230 | if (not defined $p2) {
|
---|
231 | # No ref in p2 so use p1
|
---|
232 | $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if ((not defined $p1->{$ENV{'PBPROJ'}}) && (defined $p1->{'default'}));
|
---|
233 | } else {
|
---|
234 | # Ref found in p2
|
---|
235 | if (not defined $p1) {
|
---|
236 | # No ref in p1 so use p2's value
|
---|
237 | $p2->{$ENV{'PBPROJ'}} = $p2->{'default'} if ((not defined $p2->{$ENV{'PBPROJ'}}) && (defined $p2->{'default'}));
|
---|
238 | $p1 = $p2;
|
---|
239 | } else {
|
---|
240 | # Both are defined - handling the overloading
|
---|
241 | if (not defined $p1->{'default'}) {
|
---|
242 | if (defined $p2->{'default'}) {
|
---|
243 | $p1->{'default'} = $p2->{'default'};
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (not defined $p1->{$ENV{'PBPROJ'}}) {
|
---|
248 | if (defined $p2->{$ENV{'PBPROJ'}}) {
|
---|
249 | $p1->{$ENV{'PBPROJ'}} = $p2->{$ENV{'PBPROJ'}} if (defined $p2->{$ENV{'PBPROJ'}});
|
---|
250 | } else {
|
---|
251 | $p1->{$ENV{'PBPROJ'}} = $p1->{'default'} if (defined $p1->{'default'});
|
---|
252 | }
|
---|
253 | }
|
---|
254 | # Now copy back into p1 all p2 content which doesn't exist in p1
|
---|
255 | # p1 content always has priority over p2
|
---|
256 | foreach my $k (keys %$p2) {
|
---|
257 | $p1->{$k} = $p2->{$k} if (not defined $p1->{$k});
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 | $ptr1[$i] = $p1;
|
---|
262 | }
|
---|
263 | pb_log(2,"DEBUG: pb_conf_get param ptr1: ".Dumper(@ptr1)."\n");
|
---|
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 | }
|
---|
285 |
|
---|
286 | =back
|
---|
287 |
|
---|
288 | =head1 WEB SITES
|
---|
289 |
|
---|
290 | 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/>.
|
---|
291 |
|
---|
292 | =head1 USER MAILING LIST
|
---|
293 |
|
---|
294 | None exists for the moment.
|
---|
295 |
|
---|
296 | =head1 AUTHORS
|
---|
297 |
|
---|
298 | The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
|
---|
299 |
|
---|
300 | =head1 COPYRIGHT
|
---|
301 |
|
---|
302 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
303 | described in the file C<COPYING> included with the distribution.
|
---|
304 |
|
---|
305 | =cut
|
---|
306 |
|
---|
307 |
|
---|
308 | 1;
|
---|