From 068317048409a88f73a205199f95a1a855a17104 Mon Sep 17 00:00:00 2001 From: Mikhail S Medvedev Date: Tue, 22 Sep 2015 11:00:56 -0500 Subject: [PATCH] Allow global ciwatch conf file We want to be able to store the configuration in /etc, as opposed to source directory. Try to load configuration from source dir first, and fallback to /etc/ciwatch/ci-watch.conf. Wrap everything into a function and add exception if no configuration is found. We want to move away from having globals. Original Change-Id: I91f2adc6e90b6252c5839a5ef8dde0fe7cd137f8 Change-Id: Ic939caeff538e290418cff2a6438d63a024994b0 --- ciwatch/config.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ciwatch/config.py b/ciwatch/config.py index 3d36e35..d71b284 100644 --- a/ciwatch/config.py +++ b/ciwatch/config.py @@ -16,9 +16,19 @@ import os from iniparse import INIConfig -_fdir = os.path.dirname(os.path.realpath(__file__)) -_conf_dir = os.path.dirname(_fdir) -cfg = INIConfig(open(_conf_dir + '/ci-watch.conf')) + +def get_config(): + this_file = os.path.dirname(os.path.realpath(__file__)) + this_dir = os.path.dirname(this_file) + conf_files = [os.path.join(this_dir, 'ci-watch.conf'), + '/etc/ciwatch/ci-watch.conf'] + # Read first existing conf file, ignore the rest + for conf_file in conf_files: + if os.path.exists(conf_file): + return INIConfig(open(conf_file)) + raise Exception('Could not read configuration from %s' % conf_files) + +cfg = get_config() def get_projects():