| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MToken::Config; # $Id: Config.pm 57 2019-06-06 13:46:47Z minus $ |
|
2
|
1
|
|
|
1
|
|
7
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
30
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use utf8; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=encoding utf-8 |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
MToken::Config - MToken global and local configuration |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 1.01 |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use MToken::Config; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $config = new MToken::Config( |
|
20
|
|
|
|
|
|
|
global_file => '/foo/bar/global.conf', |
|
21
|
|
|
|
|
|
|
local_file => '/foo/bar/local.conf', |
|
22
|
|
|
|
|
|
|
name => 'foo', # Optional |
|
23
|
|
|
|
|
|
|
); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $foo = $config->get('foo'); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $foo = $config->set('foo' => 'bar'); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $status = $config->save(); # Local file only |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
The module works with the configuration data |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 METHODS |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=over 8 |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=item B |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $config = new WWW::MLite::Config( |
|
42
|
|
|
|
|
|
|
global_file => '/foo/bar/global.conf', |
|
43
|
|
|
|
|
|
|
local_file => '/foo/bar/local.conf', |
|
44
|
|
|
|
|
|
|
name => 'foo', # Optional |
|
45
|
|
|
|
|
|
|
); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Returns configuration object |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item B |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $value = $config->get( 'key' ); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Returns value by keyname |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item B |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my %config = $config->getall; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Returns all allowed pairs - key and value |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item B |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$config->set( 'key', $value ); |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Set new value for key. Returns status of the operation |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item B |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$config->save; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Save current configuration to local_file and returns status of the operation |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=back |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 HISTORY |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
See C file |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 AUTHOR |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 LICENSE |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
|
90
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
See C file and L |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
|
95
|
|
|
|
|
|
|
|
|
96
|
1
|
|
|
1
|
|
36
|
use Carp; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
50
|
|
|
97
|
1
|
|
|
1
|
|
461
|
use File::HomeDir; |
|
|
1
|
|
|
|
|
5360
|
|
|
|
1
|
|
|
|
|
52
|
|
|
98
|
1
|
|
|
1
|
|
731
|
use Config::General; |
|
|
1
|
|
|
|
|
11841
|
|
|
|
1
|
|
|
|
|
62
|
|
|
99
|
1
|
|
|
1
|
|
561
|
use Try::Tiny; |
|
|
1
|
|
|
|
|
1986
|
|
|
|
1
|
|
|
|
|
58
|
|
|
100
|
1
|
|
|
1
|
|
8
|
use Cwd; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
118
|
|
|
101
|
1
|
|
|
1
|
|
7
|
use File::Spec; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
30
|
|
|
102
|
1
|
|
|
1
|
|
5
|
use MToken::Const qw/ :GENERAL :MATH /; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
212
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
1
|
|
|
1
|
|
8
|
use vars qw/$VERSION/; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
59
|
|
|
105
|
|
|
|
|
|
|
$VERSION = '1.01'; |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
use constant { |
|
108
|
1
|
|
|
|
|
1111
|
ALLOWED_KEYS => [qw/ |
|
109
|
|
|
|
|
|
|
name distname project |
|
110
|
|
|
|
|
|
|
server_url |
|
111
|
|
|
|
|
|
|
gpgbin opensslbin |
|
112
|
|
|
|
|
|
|
/], |
|
113
|
1
|
|
|
1
|
|
6
|
}; |
|
|
1
|
|
|
|
|
2
|
|
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub new { |
|
116
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
117
|
0
|
|
|
|
|
|
my %args = (@_); |
|
118
|
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
my $global_file = $args{global_file}; |
|
120
|
0
|
|
|
|
|
|
my $local_file = $args{local_file}; |
|
121
|
0
|
|
|
|
|
|
my $project = $args{project}; |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# global_file |
|
124
|
0
|
0
|
|
|
|
|
unless (defined $global_file) { |
|
125
|
0
|
|
|
|
|
|
$global_file = File::Spec->catfile(cwd(), DIR_ETC, GLOBAL_CONF_FILE); |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# local_file |
|
129
|
0
|
0
|
0
|
|
|
|
my $locex = (defined($project) && $project ne PROJECT) ? TRUE : FALSE; |
|
130
|
0
|
0
|
|
|
|
|
if (defined $local_file) { |
|
131
|
0
|
|
|
|
|
|
$locex = TRUE; |
|
132
|
|
|
|
|
|
|
} else { |
|
133
|
0
|
0
|
|
|
|
|
my $lcf = $locex |
|
134
|
|
|
|
|
|
|
? sprintf("%s_%s", LOCAL_CONF_FILE, $project) |
|
135
|
|
|
|
|
|
|
: LOCAL_CONF_FILE; |
|
136
|
0
|
|
|
|
|
|
$local_file = File::Spec->catfile(home(), $lcf); |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
unless ($locex) { |
|
140
|
0
|
|
|
|
|
|
my %tmp = _loadconfig($global_file); |
|
141
|
0
|
|
|
|
|
|
my $project = $tmp{project}; |
|
142
|
0
|
0
|
|
|
|
|
croak("Can't get PROJECT param from $global_file file. Please reinitialize this project") unless $project; |
|
143
|
0
|
|
|
|
|
|
$local_file = File::Spec->catfile(home(), sprintf("%s_%s", LOCAL_CONF_FILE, $project)); |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my %cfg = ( |
|
147
|
|
|
|
|
|
|
global_conf_file => $global_file, |
|
148
|
|
|
|
|
|
|
local_conf_file => $local_file, |
|
149
|
|
|
|
|
|
|
_loadconfig($global_file, $local_file) |
|
150
|
|
|
|
|
|
|
); |
|
151
|
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my $self = bless { %cfg }, $class; |
|
153
|
0
|
|
|
|
|
|
return $self; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
sub save { |
|
156
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
157
|
0
|
|
|
|
|
|
return _saveconfig($self->get("local_conf_file"), {($self->getall)}); |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
sub get { |
|
160
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
161
|
0
|
|
|
|
|
|
my $key = shift; |
|
162
|
0
|
0
|
|
|
|
|
unless ($self) { |
|
163
|
0
|
|
|
|
|
|
carp("Object not specified"); |
|
164
|
0
|
|
|
|
|
|
return undef; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
0
|
0
|
|
|
|
|
return undef unless $key; |
|
167
|
0
|
|
|
|
|
|
return $self->{$key}; |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
sub getall { |
|
170
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
171
|
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my %svh = (); |
|
173
|
0
|
|
|
|
|
|
foreach my $k (@{(ALLOWED_KEYS)}) { |
|
|
0
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my $v = $self->get($k); |
|
175
|
0
|
0
|
|
|
|
|
$svh{$k} = $v if defined $v; |
|
176
|
|
|
|
|
|
|
} |
|
177
|
0
|
|
|
|
|
|
return (%svh); |
|
178
|
|
|
|
|
|
|
} |
|
179
|
0
|
|
|
0
|
1
|
|
sub conf { goto &getall }; |
|
180
|
0
|
|
|
0
|
1
|
|
sub config { goto &getall }; |
|
181
|
|
|
|
|
|
|
sub set { |
|
182
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
183
|
0
|
|
|
|
|
|
my $key = shift; |
|
184
|
0
|
|
|
|
|
|
my $val = shift; |
|
185
|
0
|
0
|
|
|
|
|
unless ($self) { |
|
186
|
0
|
|
|
|
|
|
carp("Object not specified"); |
|
187
|
0
|
|
|
|
|
|
return FALSE; |
|
188
|
|
|
|
|
|
|
} |
|
189
|
0
|
0
|
|
|
|
|
unless ($key) { |
|
190
|
0
|
|
|
|
|
|
carp("Key not specified"); |
|
191
|
0
|
|
|
|
|
|
return FALSE; |
|
192
|
|
|
|
|
|
|
} |
|
193
|
0
|
|
|
|
|
|
$self->{$key} = $val; |
|
194
|
0
|
|
|
|
|
|
return TRUE; |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub _loadconfig { |
|
198
|
0
|
|
|
0
|
|
|
my $gfile = shift; |
|
199
|
0
|
|
|
|
|
|
my $lfile = shift; |
|
200
|
|
|
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
my %config = ( |
|
202
|
|
|
|
|
|
|
loadstatus_global => FALSE, |
|
203
|
|
|
|
|
|
|
loadstatus_local => FALSE, |
|
204
|
|
|
|
|
|
|
configfiles => [], |
|
205
|
|
|
|
|
|
|
); |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# Global |
|
208
|
0
|
0
|
0
|
|
|
|
if ($gfile && -e $gfile) { |
|
209
|
0
|
|
|
|
|
|
my $gconf; |
|
210
|
|
|
|
|
|
|
try { |
|
211
|
0
|
|
|
0
|
|
|
$gconf = new Config::General( |
|
212
|
|
|
|
|
|
|
-ConfigFile => $gfile, |
|
213
|
|
|
|
|
|
|
#-ConfigPath => $cdirs, |
|
214
|
|
|
|
|
|
|
-ApacheCompatible => TRUE, |
|
215
|
|
|
|
|
|
|
-LowerCaseNames => TRUE, |
|
216
|
|
|
|
|
|
|
-AutoTrue => TRUE, |
|
217
|
|
|
|
|
|
|
); |
|
218
|
|
|
|
|
|
|
} catch { |
|
219
|
0
|
|
|
0
|
|
|
carp($_); |
|
220
|
0
|
|
|
|
|
|
}; |
|
221
|
0
|
0
|
0
|
|
|
|
if ($gconf && $gconf->can('getall')) { |
|
222
|
0
|
|
|
|
|
|
%config = (%config, $gconf->getall); |
|
223
|
0
|
|
|
|
|
|
$config{loadstatus_global} = 1; |
|
224
|
|
|
|
|
|
|
} |
|
225
|
0
|
0
|
0
|
|
|
|
$config{configfiles} = [$gconf->files] if $gconf && $gconf->can('files'); |
|
226
|
|
|
|
|
|
|
} |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# Local |
|
229
|
0
|
0
|
0
|
|
|
|
if ($lfile && -e $lfile) { |
|
230
|
0
|
|
|
|
|
|
my $lconf; |
|
231
|
|
|
|
|
|
|
try { |
|
232
|
0
|
|
|
0
|
|
|
$lconf = new Config::General( |
|
233
|
|
|
|
|
|
|
-ConfigFile => $lfile, |
|
234
|
|
|
|
|
|
|
-ApacheCompatible => TRUE, |
|
235
|
|
|
|
|
|
|
-LowerCaseNames => TRUE, |
|
236
|
|
|
|
|
|
|
-AutoTrue => TRUE, |
|
237
|
|
|
|
|
|
|
); |
|
238
|
|
|
|
|
|
|
} catch { |
|
239
|
0
|
|
|
0
|
|
|
carp($_); |
|
240
|
0
|
|
|
|
|
|
}; |
|
241
|
0
|
0
|
0
|
|
|
|
if ($lconf && $lconf->can('getall')) { |
|
242
|
0
|
|
|
|
|
|
my %rplc = $lconf->getall; |
|
243
|
0
|
|
|
|
|
|
while (my ($k,$v) = each %rplc) { |
|
244
|
0
|
0
|
|
|
|
|
$config{$k} = $v if defined $v; |
|
245
|
|
|
|
|
|
|
} |
|
246
|
0
|
|
|
|
|
|
$config{loadstatus_local} = 1; |
|
247
|
|
|
|
|
|
|
} |
|
248
|
0
|
|
|
|
|
|
my $cfs = $config{configfiles}; |
|
249
|
0
|
0
|
0
|
|
|
|
push(@$cfs, ($lconf->files)) if $lconf && $lconf->can('files'); |
|
250
|
|
|
|
|
|
|
} |
|
251
|
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
return %config; |
|
253
|
|
|
|
|
|
|
} |
|
254
|
|
|
|
|
|
|
sub _saveconfig { |
|
255
|
0
|
|
|
0
|
|
|
my $file = shift; |
|
256
|
0
|
|
|
|
|
|
my $config = shift; |
|
257
|
0
|
0
|
|
|
|
|
unless ($file) { |
|
258
|
0
|
|
|
|
|
|
carp("Filename not specified!"); |
|
259
|
0
|
|
|
|
|
|
return 0; |
|
260
|
|
|
|
|
|
|
} |
|
261
|
0
|
0
|
0
|
|
|
|
unless ($config && ref($config) eq 'HASH') { |
|
262
|
0
|
|
|
|
|
|
carp("No configuration data for saving"); |
|
263
|
0
|
|
|
|
|
|
return 0; |
|
264
|
|
|
|
|
|
|
} |
|
265
|
|
|
|
|
|
|
|
|
266
|
0
|
|
|
|
|
|
my $conf = new Config::General( |
|
267
|
|
|
|
|
|
|
-ConfigHash => $config, |
|
268
|
|
|
|
|
|
|
-ApacheCompatible => TRUE, |
|
269
|
|
|
|
|
|
|
-LowerCaseNames => TRUE, |
|
270
|
|
|
|
|
|
|
-AutoTrue => TRUE, |
|
271
|
|
|
|
|
|
|
); |
|
272
|
0
|
|
|
|
|
|
$conf->save_file($file); |
|
273
|
0
|
|
|
|
|
|
return 1; |
|
274
|
|
|
|
|
|
|
} |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
1; |