1
# Copyright 2005-2011 Canonical Ltd. All rights reserved.
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU Affero General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
from ConfigParser import MissingSectionHeaderError
23
from django.conf import settings
24
from lazr.config import ConfigSchema
25
from lazr.config.interfaces import NoConfigError
27
from oopstools.oops.models import AppInstance, Prefix
31
"""Load OOPS prefixes into the database from a lazr.config file or dir."""
33
def __init__(self, lazr_config):
34
"""Initialize the loader from the lazr.config file or dir."""
36
if os.path.isfile(lazr_config):
37
self._schemas.append(ConfigSchema(lazr_config))
38
elif os.path.isdir(lazr_config):
40
for root, dirs, files in os.walk(lazr_config):
41
for filename in files:
42
if filename.endswith('.conf'):
43
config_files.append(os.path.join(root, filename))
44
for config_file in config_files:
46
self._schemas.append(ConfigSchema(config_file))
47
except MissingSectionHeaderError:
48
# Oops, not a lazr.config file, go to the next one.
53
for schema in self._schemas:
54
self._prefixes.extend(self._get_prefixes(schema))
56
def _get_appinstance_from_schema(self, schema):
57
"""Figure out AppInstance from the lazr.conf filename."""
58
# Remove the common part from the filename.
59
appinstance_and_conf_file = re.sub(
60
settings.LAZR_CONFIG, '', schema.filename)
61
# Remove the .conf part of the filename.
62
appinstance = appinstance_and_conf_file.replace(schema.name, '')
63
# Remove trailing slash, if any.
64
appinstance = appinstance.replace('/', '')
68
# Remove the digit from the appinstance (e.g. edge1, edge2, etc).
69
appinstance = re.sub('\d', '', appinstance)
72
def _get_prefixes(self, schema):
73
"""Return prefixes for a given ConfigSchema object."""
75
for section in schema:
76
appinstance = self._get_appinstance_from_schema(schema)
78
if key == 'oops_prefix':
79
prefixes.append((appinstance, section['oops_prefix']))
82
def iter_prefixes(self):
83
"""Return a list of prefixes found in the lazr.config file(s)."""
84
return [prefix for (appinstance, prefix) in self._prefixes]
86
def load_prefixes_into_database(self):
87
"""Load prefixes into the database."""
89
for appinstance, prefix in self._prefixes:
91
prefix = Prefix.objects.get(value=prefix)
92
except Prefix.DoesNotExist:
94
appinstance = AppInstance.objects.get(title=appinstance)
95
except AppInstance.DoesNotExist:
96
appinstance = AppInstance(title=appinstance)
98
prefix = Prefix(value=prefix, appinstance=appinstance)
100
prefixes.append(prefix.value)
101
# Stabilize the order so we can reliably test the return value.
102
return sorted(prefixes)