Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / settings / __init__.py @ c323f81a

History | View | Annotate | Download (4.4 kB)

1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import os
35
from sys import modules, stderr
36
_module = modules[__name__]
37

    
38
# set synnefo package __file__ to fix django related bug
39
import synnefo
40
synnefo.__file__ = os.path.join(synnefo.__path__[0], '__init__.py')
41

    
42
from .setup import Setting
43
synnefo_settings = {}
44
# insert global default synnefo settings
45
from .default import *
46
for name in dir(_module):
47
    if not Setting.is_valid_setting_name(name):
48
        continue
49
    synnefo_settings[name] = getattr(_module, name)
50

    
51
# autodetect default settings provided by synnefo applications
52
from synnefo.util.entry_points import get_entry_points
53
for e in get_entry_points('synnefo', 'default_settings'):
54
    m = e.load()
55
    for name in dir(m):
56
        if name.startswith('__'):
57
            continue
58
        synnefo_settings[name] = getattr(m, name)
59

    
60
# set strict to True to require annotation of all settings
61
Setting.initialize_settings(synnefo_settings, strict=False)
62
_module.__dict__.update(Setting.Catalogs['defaults'])
63

    
64
# extend default settings with settings provided within *.conf user files
65
# located in directory specified in the SYNNEFO_SETTINGS_DIR
66
# environment variable
67
import re
68
system_conf_re = re.compile('^([0-9]\+-)?system.conf$')
69

    
70
SYNNEFO_SETTINGS_DIR = os.environ.get('SYNNEFO_SETTINGS_DIR', "/etc/synnefo/")
71
if os.path.exists(SYNNEFO_SETTINGS_DIR):
72
    try:
73
        entries = [os.path.join(SYNNEFO_SETTINGS_DIR, x) for x in
74
                   os.listdir(SYNNEFO_SETTINGS_DIR)]
75
        conffiles = [f for f in entries if os.path.isfile(f) and
76
                     f.endswith(".conf")]
77
    except Exception as e:
78
        print >> stderr, "Failed to list *.conf files under %s" % \
79
            SYNNEFO_SETTINGS_DIR
80
        raise SystemExit(1)
81
    conffiles.sort()
82
    for f in conffiles:
83
        if system_conf_re.match(f):
84
            allow_known = False
85
            allow_unknown = True
86
        else:
87
            allow_known = True
88
            allow_unknown = False
89

    
90
        # FIXME: Hack until all settings have been annotated properly
91
        allow_unknown = True
92
        allow_override = True
93

    
94
        try:
95
            path = os.path.abspath(f)
96
            old_settings = Setting.Catalogs['defaults']
97
            new_settings = Setting.load_settings_from_file(path, old_settings)
98

    
99
            Setting.load_configuration(new_settings,
100
                                       source=path,
101
                                       allow_known=allow_known,
102
                                       allow_unknown=allow_unknown,
103
                                       allow_override=allow_override)
104
        except Exception as e:
105
            print >> stderr, "Failed to read settings file: %s [%r]" % \
106
                (path, e)
107
            raise
108
            raise SystemExit(1)
109

    
110
Setting.configure_settings()
111
_module.__dict__.update(Setting.Catalogs['runtime'])
112

    
113
# cleanup module namespace
114
for _name in dir(_module):
115
    if _name.startswith('_') or _name.isupper():
116
        continue
117
    delattr(_module, _name)
118
del _name
119
del _module