| 1 |
otavio |
7 |
# debpartial-mirror - partial debian mirror package tool
|
| 2 |
natbudin-guest |
121 |
# (c) 2004 Otavio Salvador <otavio@debian.org>, Nat Budin <natb@brandeis.edu>
|
| 3 |
otavio |
7 |
#
|
| 4 |
|
|
# This program is free software; you can redistribute it and/or modify
|
| 5 |
|
|
# it under the terms of the GNU General Public License as published by
|
| 6 |
|
|
# the Free Software Foundation; either version 2 of the License, or
|
| 7 |
|
|
# (at your option) any later version.
|
| 8 |
|
|
#
|
| 9 |
|
|
# This program is distributed in the hope that it will be useful,
|
| 10 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
|
|
# GNU General Public License for more details.
|
| 13 |
|
|
#
|
| 14 |
|
|
# You should have received a copy of the GNU General Public License
|
| 15 |
|
|
# along with this program; if not, write to the Free Software
|
| 16 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 17 |
|
|
# $Id$
|
| 18 |
|
|
|
| 19 |
otavio |
119 |
from logging import *
|
| 20 |
natbudin-guest |
125 |
from ConfigParser import ConfigParser, DEFAULTSECT
|
| 21 |
natbudin-guest |
123 |
from sys import exit
|
| 22 |
otavio |
2 |
|
| 23 |
otavio |
124 |
class InvalidOption(Exception):
|
| 24 |
|
|
"""
|
| 25 |
|
|
Exception called when a invalid option is found in configuration
|
| 26 |
|
|
file.
|
| 27 |
|
|
|
| 28 |
|
|
Attributes:
|
| 29 |
|
|
section -- Where the invalid option was set;
|
| 30 |
|
|
option -- The name of invalid option.
|
| 31 |
|
|
"""
|
| 32 |
|
|
def __init__(self, section, option):
|
| 33 |
|
|
self.section = section
|
| 34 |
|
|
self.option = option
|
| 35 |
|
|
|
| 36 |
otavio |
119 |
class Config(dict):
|
| 37 |
|
|
"""
|
| 38 |
|
|
Store the configurations used by our system.
|
| 39 |
|
|
"""
|
| 40 |
henrique |
30 |
|
| 41 |
otavio |
119 |
required_in_default = [
|
| 42 |
|
|
'mirror_dir',
|
| 43 |
|
|
'architectures',
|
| 44 |
|
|
'sections',
|
| 45 |
|
|
'distributions',
|
| 46 |
|
|
'get_suggests',
|
| 47 |
|
|
'get_recommends',
|
| 48 |
|
|
'get_provides',
|
| 49 |
|
|
]
|
| 50 |
|
|
|
| 51 |
|
|
allowed_in_backend = [
|
| 52 |
|
|
'server',
|
| 53 |
|
|
'architectures',
|
| 54 |
|
|
'sections',
|
| 55 |
|
|
'distributions',
|
| 56 |
|
|
'filter',
|
| 57 |
|
|
'filter_@BACKEND@',
|
| 58 |
|
|
'get_suggests',
|
| 59 |
|
|
'get_recommends',
|
| 60 |
|
|
'get_provides',
|
| 61 |
natbudin-guest |
123 |
'include_from_task',
|
| 62 |
|
|
'exclude_from_task',
|
| 63 |
|
|
'backends',
|
| 64 |
|
|
'name',
|
| 65 |
|
|
'resolve_deps_using',
|
| 66 |
otavio |
119 |
]
|
| 67 |
otavio |
49 |
|
| 68 |
otavio |
2 |
def __init__(self, filename):
|
| 69 |
otavio |
119 |
conf = ConfigParser()
|
| 70 |
|
|
conf.read(filename)
|
| 71 |
natbudin-guest |
123 |
|
| 72 |
|
|
# Read the default section.
|
| 73 |
|
|
self['DEFAULT'] = {}
|
| 74 |
natbudin-guest |
125 |
for item, value in conf.items(DEFAULTSECT):
|
| 75 |
natbudin-guest |
123 |
if item not in Config.required_in_default:
|
| 76 |
otavio |
124 |
debug("[%s] is not allowed in default section." % (item))
|
| 77 |
|
|
raise InvalidOption('DEFAULT', item)
|
| 78 |
natbudin-guest |
123 |
self['DEFAULT'][item] = value
|
| 79 |
natbudin-guest |
121 |
|
| 80 |
|
|
for section in conf.sections():
|
| 81 |
|
|
self[section] = {}
|
| 82 |
|
|
for item, value in conf.items(section):
|
| 83 |
natbudin-guest |
125 |
# is this item from the default section? if so, go on to the
|
| 84 |
|
|
# next item
|
| 85 |
|
|
if self['DEFAULT'].has_key(item):
|
| 86 |
|
|
if self['DEFAULT'][item] == value:
|
| 87 |
|
|
continue
|
| 88 |
natbudin-guest |
123 |
if item not in Config.allowed_in_backend:
|
| 89 |
|
|
actually_its_ok = False
|
| 90 |
|
|
for allowed_key in Config.allowed_in_backend:
|
| 91 |
otavio |
124 |
# check the allowed_in_backend keys with
|
| 92 |
|
|
# @VARIABLES@
|
| 93 |
natbudin-guest |
123 |
if allowed_key.find('@') != -1:
|
| 94 |
|
|
nonvar_length = allowed_key.find('@')
|
| 95 |
|
|
if allowed_key[:nonvar_length] == item[:nonvar_length]:
|
| 96 |
|
|
# found it!
|
| 97 |
|
|
actually_its_ok = True
|
| 98 |
|
|
break
|
| 99 |
|
|
if not actually_its_ok:
|
| 100 |
otavio |
124 |
debug("[%s] is not allowed in a backend section (it was found in [%s])."
|
| 101 |
natbudin-guest |
123 |
% (item, section))
|
| 102 |
otavio |
124 |
raise InvalidOption(section, item)
|
| 103 |
natbudin-guest |
121 |
self[section][item] = value
|
| 104 |
|
|
|
| 105 |
natbudin-guest |
123 |
def getValue(self, key, section='DEFAULT'):
|
| 106 |
otavio |
124 |
# get a config value for a certain section. if it's not
|
| 107 |
|
|
# specified, fall back to DEFAULT
|
| 108 |
natbudin-guest |
121 |
if not self.has_key(section):
|
| 109 |
natbudin-guest |
123 |
error("no config section called [%s]." % (section))
|
| 110 |
natbudin-guest |
121 |
exit(1)
|
| 111 |
|
|
if self[section].has_key(key):
|
| 112 |
|
|
return self[section][key]
|
| 113 |
natbudin-guest |
123 |
if not self['DEFAULT'].has_key(key):
|
| 114 |
|
|
error("[%s] is not present in section [%s] or the default section of config file." % (key, section))
|
| 115 |
natbudin-guest |
121 |
exit(1)
|
| 116 |
natbudin-guest |
123 |
return self['DEFAULT'][key]
|
| 117 |
natbudin-guest |
121 |
|
| 118 |
otavio |
119 |
def dump(self):
|
| 119 |
natbudin-guest |
123 |
print 'mirror_dir =', self.getValue('mirror_dir')
|
| 120 |
|
|
print 'architectures =', self.getValue('architectures')
|
| 121 |
|
|
print 'sections = ', self.getValue('sections')
|
| 122 |
|
|
print 'distributions =', self.getValue('distributions')
|
| 123 |
|
|
print 'get_provides = ', self.getValue('get_provides')
|
| 124 |
|
|
print 'get_recommends = ', self.getValue('get_recommends')
|
| 125 |
|
|
print 'get_suggests = ', self.getValue('get_suggests')
|
| 126 |
otavio |
2 |
|
| 127 |
|
|
|
| 128 |
natbudin-guest |
125 |
getLogger().setLevel(DEBUG)
|
| 129 |
otavio |
119 |
conf = Config('../etc/debpartial-mirror.conf')
|
| 130 |
|
|
conf.dump()
|