diff --git a/colab/utils/conf.py b/colab/utils/conf.py index f61d10b..b090ee0 100644 --- a/colab/utils/conf.py +++ b/colab/utils/conf.py @@ -38,7 +38,7 @@ def _load_yaml_file(yaml_path): def load_yaml_settings(): settings_dir = '/etc/colab/settings.d' - yaml_path = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.yaml') + yaml_path = os.getenv('COLAB_YAML_SETTINGS', '/etc/colab/settings.yaml') if os.path.exists(yaml_path): global USING_YAML_SETTINGS @@ -71,24 +71,37 @@ class InaccessiblePySettings(ImproperlyConfigured): Check if the file exists and if you have read permissions.""" -def _load_py_file(py_path): +def _load_py_file(py_path, path): + original_path = sys.path + + sys.path = [path] try: py_settings = importlib.import_module(py_path) - except: + except IOError: msg = ('Could not open settings file {}. Please ' 'check if the file exists and if user ' 'has read rights.').format(py_path) raise InaccessiblePySettings(msg) - return py_settings + except SyntaxError as excpt: + msg = ('Syntax Error: {}'.format(excpt)) + raise InaccessiblePySettings(msg) + + finally: + sys.path = original_path + + py_setting = {var: getattr(py_settings, var) for var in dir(py_settings) + if not var.startswith('__')} + + return py_setting def load_py_settings(): settings_dir = '/etc/colab/settings.d' - settings_module = 'settings' - py_path = os.getenv('COLAB_SETTINGS', - "/etc/colab/{}.py".format(settings_module)) + settings_file = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.py') + settings_module = settings_file.split('.')[-2].split('/')[-1] + py_path = "/".join(settings_file.split('/')[:-1]) global USING_YAML_SETTINGS if not os.path.exists(py_path) and not USING_YAML_SETTINGS: @@ -97,34 +110,28 @@ def load_py_settings(): elif USING_YAML_SETTINGS: return {} - sys.path.insert(0, '/etc/colab/') - sys.path.insert(0, settings_dir) - - py_settings = _load_py_file(settings_module).__dict__ + py_settings = _load_py_file(settings_module, py_path) # Try to read settings from settings.d + if os.path.exists(settings_dir): + return py_settings for file_name in os.listdir(settings_dir): if file_name.endswith('.py'): file_module = file_name.split('.')[0] - py_settings_d = _load_py_file(file_module).__dict__ + py_settings_d = _load_py_file(file_module, settings_dir) py_settings.update(py_settings_d) - sys.path.remove('/etc/colab/') - sys.path.remove(settings_dir) - - return py_settings or {} + return py_settings def load_colab_apps(): - plugins_dir = '/etc/colab/plugins.d/' + plugins_dir = os.getenv('COLAB_PLUGINS', '/etc/colab/plugins.d/') global USING_YAML_SETTINGS if USING_YAML_SETTINGS: return {} - sys.path.insert(0, plugins_dir) - COLAB_APPS = {} # Try to read settings from plugins.d @@ -132,7 +139,7 @@ def load_colab_apps(): for file_name in os.listdir(plugins_dir): if file_name.endswith('.py'): file_module = file_name.split('.')[0] - py_settings_d = _load_py_file(file_module) + py_settings_d = _load_py_file(file_module, plugins_dir) fields = ['urls', 'menu', 'upstream', 'middlewares', 'dependencies', 'context_processors'] @@ -147,8 +154,6 @@ def load_colab_apps(): if value: COLAB_APPS[app_name][key] = value - sys.path.remove(plugins_dir) - return {'COLAB_APPS': COLAB_APPS} diff --git a/tests/config_settings.py b/tests/config_settings.py new file mode 100644 index 0000000..c7c6f26 --- /dev/null +++ b/tests/config_settings.py @@ -0,0 +1,11 @@ +SECRET_KEY = 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddaddddddddd' + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'HOST': 'localhost', + 'NAME': 'colab', + 'USER': 'colab', + 'PASSWORD': 'colab', + } +} diff --git a/tests/plugins.d/gitlab.py b/tests/plugins.d/gitlab.py new file mode 100644 index 0000000..b0f9cb9 --- /dev/null +++ b/tests/plugins.d/gitlab.py @@ -0,0 +1,39 @@ +from django.utils.translation import ugettext_lazy as _ + +name = 'colab.plugins.gitlab' +verbose_name = 'Gitlab Proxy' + +upstream = 'localhost' +#middlewares = [] + +menu = { +'title': _('Code'), +'links': ( + (_('Public Projects'), 'public/projects'), +), +'auth_links': ( + (_('Profile'), 'profile'), + (_('New Project'), 'projects/new'), + (_('Projects'), 'dashboard/projects'), + (_('Groups'), 'profile/groups'), + (_('Issues'), 'dashboard/issues'), + (_('Merge Requests'), 'dashboard/merge_requests'), + +), +} + + +# dpaste: +# dependencies: +# - 'mptt' +# urls: +# include: 'dpaste.urls.dpaste' +# prefix: '^paste/' +# namespace: 'dpaste' +# menu: +# title: 'Dpaste' +# links: +# Public Projects: '/paste' +# auth_links: +# Profile: '/projects' +# New Project: '/projects/new' diff --git a/tests/run.py b/tests/run.py index b2366bb..f57962e 100755 --- a/tests/run.py +++ b/tests/run.py @@ -4,7 +4,8 @@ import os import sys os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' -os.environ['COLAB_SETTINGS'] = 'tests/settings.yaml' +os.environ['COLAB_SETTINGS'] = 'tests/config_settings.py' +os.environ['PLUGINS_SETTINGS'] = 'tests/plugins.d' os.environ['COVERAGE_PROCESS_START'] = '.coveragerc' os.environ['REUSE_DB'] = '0' -- libgit2 0.21.2