forked from oar-team/oargrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoargrid_conflib.pm
111 lines (102 loc) · 3.01 KB
/
oargrid_conflib.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
###############################################################################
## *** ConfLib: ***
##
## - Description:
## Home brewed module managing configuration file for oar_grid
##
## - Usage: init_conf(<filename>);
## Read the first file matching <filename> in
## . current directory
## . $OARGRIDDIR directory
## . /etc directory
##
## - Configuration file format:
## A line of the configuration file looks like that:
## > truc = 45 machin chose bidule 23 # any comment
## "truc" is a configuration entry being assigned "45 machin chose bidule 23"
## Anything placed after a dash (#) is ignored
## (for instance lines begining with a dash are comment lines then ignored)
## Any line not matching the regexp defined below are also ignored
##
## Module must be initialized using init_conf(<filename>), then
## any entry is retrieved using get_conf(<entry>).
## is_conf(<entry>) may be used to check if any entry actually exists.
##
## - Example:
## > use ConfLib qw(init_conf get_conf is_conf);
## > init_conf("oargrid.conf");
## > print "toto = ".get_conf("toto")."\n" if is_conf("toto");
##
###############################################################################
# $Id: oargrid_conflib.pm,v 1.3 2006/04/14 09:18:37 capitn Exp $
package oargrid_conflib;
use strict;
use warnings;
require Exporter;
our (@ISA,@EXPORT,@EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(init_conf get_conf is_conf dump_conf reset_conf);
## the configuration file.
my $file = undef;
## parameters container...
my %params;
## configuration file regexp (one line).
my $regex = qr{^\s*([^#=\s]+)\s*=\s*([^#]*)};
## Initialization of the configuration
# param: configuration file pathname
# Result: 0 if conf was already loaded
# 1 if conf was actually loaded
# 2 if conf was not found
sub init_conf ($){
# If file already loaded, exit immediately
(defined $file) and return 0;
$file = shift;
(defined $file) or return 2;
if ( defined $ENV{OARGRIDDIR} and -r $ENV{OARGRIDDIR}."/".$file ) {
$file = $ENV{OARGRIDDIR}."/".$file;
} elsif ( -r "/etc/".$file ) {
$file = "/etc/".$file;
} else {
warn "Configuration file not found.";
$file = undef;
return 2;
}
open CONF, $file or die "Open configuration file";
%params = ();
foreach my $line (<CONF>) {
if ($line =~ $regex) {
my ($key,$val) = ($1,$2);
$val =~ s/\s*$//;
$params{$key}=$val;
}
}
close CONF;
return 1;
}
## retrieve a parameter
sub get_conf ( $ ) {
my $key = shift;
(defined $key) or die "missing a key!";
return $params{$key};
}
## check if a parameter is defined
sub is_conf ( $ ) {
my $key = shift;
(defined $key) or die "missing a key!";
return exists $params{$key};
}
## debug: dump parameters
sub dump_conf () {
print "Config file is: ".$file."\n";
while (my ($key,$val) = each %params) {
print " ".$key." = ".$val."\n";
}
return 1;
}
## reset the module state
sub reset_conf () {
$file = undef;
%params = ();
return 1;
}
return 1;