source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/Conf.pm@ 405

Last change on this file since 405 was 405, checked in by Bruno Cornec, 16 years ago

Split again function in modules to allow for usage with pbinit and easier reuse.

File size: 5.6 KB
Line 
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
10package ProjectBuilder::Conf;
11
12use strict;
13use Data::Dumper;
14use ProjectBuilder::Base;
15
16# Inherit from the "Exporter" module which handles exporting functions.
17
18use Exporter;
19
20# Export, by default, all the functions into the namespace of
21# any code which uses this module.
22
23our @ISA = qw(Exporter);
24our @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
30ProjectBuilder::Conf, part of the project-builder.org - module dealing with configuration files
31
32=head1 DESCRIPTION
33
34This 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
52This function initializes the configuration files analysis.
53Pass the configuration files to consider in order of importance (the most important first).
54
55=cut
56
57sub pb_conf_init {
58
59my @conffiles = @_;
60}
61
62=item B<pb_conf_read_if>
63
64This function returns a table of pointers on hashes
65corresponding to the keys in a configuration file passed in parameter.
66If that file doesn't exist, it returns undef.
67
68The format of the configuration file is as follows:
69
70key tag = value1,value2,...
71
72Supposing 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
79calling it like this:
80
81 my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
82
83will allow to get the mapping:
84
85 $k1->{'pb'} contains 3
86 $ka->{'default'} contains 1
87 $k2->{'pb'} contains 12,25
88
89Valid chars for keys and tags are letters, numbers, '-' and '_'.
90
91=cut
92
93sub pb_conf_read_if {
94
95my $conffile = shift;
96my @param = @_;
97
98open(CONF,$conffile) || return((undef));
99close(CONF);
100return(pb_conf_read($conffile,@param));
101}
102
103=item B<pb_conf_read>
104
105This 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
109sub pb_conf_read {
110
111my $conffile = shift;
112my @param = @_;
113my $trace;
114my @ptr;
115my %h;
116
117open(CONF,$conffile) || die "Unable to open $conffile";
118while(<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}
124close(CONF);
125
126for my $param (@param) {
127 push @ptr,$h{$param};
128}
129return(@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
136sub pb_conf_get {
137
138my @param = @_;
139my @return = pb_conf_get_if(@param);
140
141die "No params found for $ENV{'PBPROJ'}" if (not @return);
142
143foreach my $i (0..$#param) {
144 die "No $param[$i] defined for $ENV{'PBPROJ'}" if (not defined $return[$i]);
145}
146return(@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
152sub pb_conf_get_if {
153
154my @param = @_;
155
156# Everything is returned via ptr1
157my @ptr1 = ();
158my @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
162my $p1;
163my $p2;
164
165pb_log(2,"DEBUG: pb_conf_get param1: ".Dumper(@ptr1)."\n");
166pb_log(2,"DEBUG: pb_conf_get param2: ".Dumper(@ptr2)."\n");
167
168foreach 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}
206pb_log(2,"DEBUG: pb_conf_get param ptr1: ".Dumper(@ptr1)."\n");
207return(@ptr1);
208}
209
210
211=back
212
213=head1 WEB SITES
214
215The 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
219None exists for the moment.
220
221=head1 AUTHORS
222
223The 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
227Project-Builder.org is distributed under the GPL v2.0 license
228described in the file C<COPYING> included with the distribution.
229
230=cut
231
232
2331;
Note: See TracBrowser for help on using the repository browser.