File: ConfBase.pm

package info (click to toggle)
sbuild 0.60.0-2squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,136 kB
  • ctags: 640
  • sloc: perl: 14,039; sh: 1,173; sql: 772; makefile: 406
file content (291 lines) | stat: -rw-r--r-- 6,372 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#
# ConfBase.pm: configuration library (base functionality) for sbuild
# Copyright © 2005      Ryan Murray <rmurray@debian.org>
# Copyright © 2006-2008 Roger Leigh <rleigh@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

package Sbuild::ConfBase;

use strict;
use warnings;

use Cwd qw(cwd);
use Sbuild qw(isin);
use Sbuild::Log;

BEGIN {
    use Exporter ();
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter);

    @EXPORT = qw();
}

sub init_allowed_keys {
    my $self = shift;

    my $validate_program = sub {
	my $self = shift;
	my $entry = shift;
	my $key = $entry->{'NAME'};
	my $program = $self->get($key);

	die "$key binary is not defined"
	    if !defined($program);

	die "$key binary $program does not exist or is not executable"
	    if !-x $program;
    };

    my $validate_directory = sub {
	my $self = shift;
	my $entry = shift;
	my $key = $entry->{'NAME'};
	my $directory = $self->get($key);

	die "$key directory is not defined"
	    if !defined($directory);

	die "$key directory $directory does not exist"
	    if !-d $directory;
    };

    my $home = $ENV{'HOME'}
        or die "HOME not defined in environment!\n";
    my @pwinfo = getpwuid($<);
    my $username = $pwinfo[0] || $ENV{'LOGNAME'} || $ENV{'USER'};
    my $fullname = $pwinfo[6];
    $fullname =~ s/,.*$//;

    chomp(my $hostname = `$Sbuild::Sysconfig::programs{'HOSTNAME'} -f`);

    # Not user-settable.
    chomp(my $host_arch =
	  readpipe("$Sbuild::Sysconfig::programs{'DPKG'} --print-architecture"));

    my %common_keys = (
	'DISTRIBUTION'				=> {
	    SET => sub {
		my $self = shift;
		my $entry = shift;
		my $value = shift;
		my $key = $entry->{'NAME'};

		$self->_set_value($key, $value);

		my $override = ($self->get($key)) ? 1 : 0;
		$self->set('OVERRIDE_DISTRIBUTION', $override);

		#Now, we might need to adjust the MAILTO based on the
		#config data. We shouldn't do this if it was already
		#explicitly set by the command line option:
		if (!$self->get('MAILTO_FORCED_BY_CLI') 
		    && defined($self->get('DISTRIBUTION')) 
		    && $self->get('DISTRIBUTION') 
		    && $self->get('MAILTO_HASH')->{$self->get('DISTRIBUTION')}) {
		    $self->set('MAILTO',
		        $self->get('MAILTO_HASH')->{$self->get('DISTRIBUTION')});
		}
	    }
	},
	'OVERRIDE_DISTRIBUTION'			=> {
	    DEFAULT => 0
	},
	'MAILPROG'				=> {
	    CHECK => $validate_program,
	    DEFAULT => $Sbuild::Sysconfig::programs{'SENDMAIL'}
	},
	# TODO: Check if defaulted in code assuming undef
	'ARCH'					=> {
	    DEFAULT => $host_arch
	},
	'HOST_ARCH'				=> {
	    DEFAULT => $host_arch
	},
	'HOSTNAME'				=> {
	    DEFAULT => $hostname
	},
	'HOME'					=> {
	    DEFAULT => $home
	},
	'USERNAME'				=> {
	    DEFAULT => $username
	},
	'FULLNAME'				=> {
	    DEFAULT => $fullname
	},
	'CWD'					=> {
	    DEFAULT => cwd()
	},
	'VERBOSE'				=> {
	    DEFAULT => 0
	},
	'DEBUG'					=> {
	    DEFAULT => 0
	},
	'DPKG'					=> {
	    CHECK => $validate_program,
	    DEFAULT => $Sbuild::Sysconfig::programs{'DPKG'}
	},
    );

    $self->set_allowed_keys(\%common_keys);
}

sub new {
    my $class = shift;

    my $self  = {};
    $self->{'config'} = {};
    bless($self, $class);

    $self->init_allowed_keys();
    $self->read_config();

    return $self;
}

sub is_default {
    my $self = shift;
    my $key = shift;

    return ($self->_get_value($key) == undef);
}

sub _get_property_value {
    my $self = shift;
    my $key = shift;
    my $property = shift;

    my $entry = $self->{'KEYS'}->{$key};

    return $entry->{$property};
}

sub _get_value {
    my $self = shift;
    my $key = shift;

    return $self->_get_property_value($key, 'VALUE');
}

sub _get_default {
    my $self = shift;
    my $key = shift;

    return $self->_get_property_value($key, 'DEFAULT');
}

sub get {
    my $self = shift;
    my $key = shift;

    my $entry = $self->{'KEYS'}->{$key};

    my $value = undef;
    if ($entry) {
	if (defined($entry->{'GET'})) {
	    $value = $entry->{'GET'}->($self, $entry);
	} else {
	    $value = $self->_get_value($key);
	    $value = $self->_get_default($key)
		if (!defined($value));
	}
    }

    return $value;
}

sub _set_property_value {
    my $self = shift;
    my $key = shift;
    my $property = shift;
    my $value = shift;

    my $entry = $self->{'KEYS'}->{$key};

    return $entry->{$property} = $value;
}

sub _set_value {
    my $self = shift;
    my $key = shift;
    my $value = shift;

    return $self->_set_property_value($key, 'VALUE', $value);
}

sub _set_default {
    my $self = shift;
    my $key = shift;
    my $value = shift;

    return $self->_set_property_value($key, 'DEFAULT', $value);
}

sub set {
    my $self = shift;
    my $key = shift;
    my $value = shift;

    # Set global debug level.
    $Sbuild::debug_level = $value
	if ($key eq 'DEBUG');

    my $entry = $self->{'KEYS'}->{$key};

    if (defined($entry)) {
	if (defined($entry->{'SET'})) {
	    $value = $entry->{'SET'}->($self, $entry, $value);
	} else {
	    $value = $self->_set_value($key, $value);
	}
	if (defined($entry->{'CHECK'})) {
	    $entry->{'CHECK'}->($self, $entry);
	}
	$entry->{'NAME'} = $key;
	return $value;
    } else {
	warn "W: key \"$key\" is not allowed in configuration";
	return undef;
    }
}

sub set_allowed_keys {
    my $self = shift;
    my $allowed_keys = shift;

    foreach (keys %{$allowed_keys}) {
	$allowed_keys->{$_}->{'NAME'} = $_;
	$self->{'KEYS'}->{$_} = $allowed_keys->{$_}
    }

}

sub warn_deprecated {
    my $oldtype = shift;
    my $oldopt = shift;
    my $newtype = shift;
    my $newopt = shift;

    warn "W: Obsolete $oldtype option '$oldopt' used in configuration";
    warn "I: The replacement is $newtype option '$newopt'"
}

1;