~launchpad-pqm/python-oops-tools/trunk

« back to all changes in this revision

Viewing changes to src/oopstools/oops/prefixloader.py

  • Committer: Robert Collins
  • Date: 2011-10-13 20:18:51 UTC
  • Revision ID: robertc@robertcollins.net-20111013201851-ym8jmdhoeol3p83s
Export of cruft-deleted tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2005-2011 Canonical Ltd.  All rights reserved.
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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/>.
 
15
 
 
16
 
 
17
import re
 
18
import os
 
19
import os.path
 
20
 
 
21
from ConfigParser import MissingSectionHeaderError
 
22
 
 
23
from django.conf import settings
 
24
from lazr.config import ConfigSchema
 
25
from lazr.config.interfaces import NoConfigError
 
26
 
 
27
from oopstools.oops.models import AppInstance, Prefix
 
28
 
 
29
 
 
30
class PrefixLoader:
 
31
    """Load OOPS prefixes into the database from a lazr.config file or dir."""
 
32
 
 
33
    def __init__(self, lazr_config):
 
34
        """Initialize the loader from the lazr.config file or dir."""
 
35
        self._schemas = []
 
36
        if os.path.isfile(lazr_config):
 
37
            self._schemas.append(ConfigSchema(lazr_config))
 
38
        elif os.path.isdir(lazr_config):
 
39
            config_files = []
 
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:
 
45
                try:
 
46
                    self._schemas.append(ConfigSchema(config_file))
 
47
                except MissingSectionHeaderError:
 
48
                    # Oops, not a lazr.config file, go to the next one.
 
49
                    continue
 
50
        else:
 
51
            raise NoConfigError
 
52
        self._prefixes = []
 
53
        for schema in self._schemas:
 
54
            self._prefixes.extend(self._get_prefixes(schema))
 
55
 
 
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('/', '')
 
65
        if not appinstance:
 
66
            return ''
 
67
        else:
 
68
            # Remove the digit from the appinstance (e.g. edge1, edge2, etc).
 
69
            appinstance = re.sub('\d', '', appinstance)
 
70
            return appinstance
 
71
 
 
72
    def _get_prefixes(self, schema):
 
73
        """Return prefixes for a given ConfigSchema object."""
 
74
        prefixes = []
 
75
        for section in schema:
 
76
            appinstance = self._get_appinstance_from_schema(schema)
 
77
            for key in section:
 
78
                if key == 'oops_prefix':
 
79
                    prefixes.append((appinstance, section['oops_prefix']))
 
80
        return prefixes
 
81
 
 
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]
 
85
 
 
86
    def load_prefixes_into_database(self):
 
87
        """Load prefixes into the database."""
 
88
        prefixes = []
 
89
        for appinstance, prefix in self._prefixes:
 
90
            try:
 
91
                prefix = Prefix.objects.get(value=prefix)
 
92
            except Prefix.DoesNotExist:
 
93
                try:
 
94
                    appinstance = AppInstance.objects.get(title=appinstance)
 
95
                except AppInstance.DoesNotExist:
 
96
                    appinstance = AppInstance(title=appinstance)
 
97
                    appinstance.save()
 
98
                prefix = Prefix(value=prefix, appinstance=appinstance)
 
99
                prefix.save()
 
100
                prefixes.append(prefix.value)
 
101
        # Stabilize the order so we can reliably test the return value.
 
102
        return sorted(prefixes)