Added support to read configs from /etc/gerrithooks.d/*.conf
authorAlexander Kanevskiy <alexander.kanevskiy@intel.com>
Thu, 15 Aug 2013 19:43:54 +0000 (22:43 +0300)
committerAlexander Kanevskiy <alexander.kanevskiy@intel.com>
Thu, 15 Aug 2013 19:43:54 +0000 (22:43 +0300)
gerrithooks/common.py
gerrithooks/misc.py

index 23c9ef1ea55433271a40467cbdd763743a485f9a..aa69bf2e4e83a50b328a5d54ac5a6942aab11ee3 100644 (file)
@@ -19,6 +19,7 @@
 Common code to run hooks from plugins
 """
 import os
+import glob
 import sys
 import types
 import ConfigParser
@@ -54,7 +55,7 @@ def run_plugin_hooks(hook, parser):
 
     logger.debug("looking for config")
     confname = "gerrithooks.conf"
-    conf = find_config(confname, subdir="gerrithooks")
+    conf, confd = find_config(confname, subdir="gerrithooks")
     if not conf:
         logger.error("Log configuration file %s not found or not readable",
                         confname)
@@ -64,11 +65,17 @@ def run_plugin_hooks(hook, parser):
     config = ConfigParser.RawConfigParser()
     config.optionxform = str
 
-    logger.debug("runner: reading config from %s", conf)
+    logger.debug("Reading main config from %s", conf)
     config.read(conf)
 
+    if confd:
+        conf = glob.glob(os.path.join(confd,"*.conf"))
+        if conf:
+            logger.debug("Reading additional configs from %s", conf)
+            conf = config.read(conf)
+            logger.debug("read from %s", conf)
+
     entry_point_group = "gerrithooks"
-    logger.debug("runner: looking for plugins in entry %s", entry_point_group)
 
     for entry_point in iter_entry_points(group=entry_point_group, name=hook):
         logger.debug("Loading plugin %s from %s",
index 6528912e71ba887af9b9e9d33d0b572170112e5e..b6081fe43dd0aea40027d35df49da2cd71c28d48 100644 (file)
@@ -64,7 +64,7 @@ def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
 
 
 def cmd_inout(command, args, cmd_input=None, extra_env=None, cwd=None,
-        capture_stderr=False, output_f=None, filter_fn=None, 
+        capture_stderr=False, output_f=None, filter_fn=None,
         filter_kwargs={}, logger=None):
     """
     Run command in subprocess
@@ -130,7 +130,7 @@ def find_config(name, dirs=None, subdir=None):
         homedir = pwd.getpwuid(os.getuid()).pw_dir
         dirs = []
         if subdir:
-            dirs.append(os.path.join(homedir,"."+subdir))
+            dirs.append(os.path.join(homedir, "."+subdir))
             dirs.append(os.path.join("/etc", subdir))
         else:
             dirs.append(homedir)
@@ -138,19 +138,18 @@ def find_config(name, dirs=None, subdir=None):
     for dirname in dirs:
         confpath = os.path.join(dirname, name)
         if os.access(confpath, os.R_OK):
-            return confpath
+            return (confpath, dirname+'.d')
     if os.getenv('VIRTUAL_ENV'):
         resource_conf = resource_filename(__package__, name)
         if resource_conf and os.access(resource_conf, os.R_OK):
-            return resource_conf
-    return None
+            return (resource_conf, None)
+    return (None, None)
 
 def configure_logging(appname):
     """Setup logging"""
-    homedir = pwd.getpwuid(os.getuid()).pw_dir
 
     log_confname = "gerrithooks-log.conf"
-    log_config = find_config(log_confname, subdir="gerrithooks")
+    log_config, _ = find_config(log_confname, subdir="gerrithooks")
     if not log_config:
         raise SystemExit("Log configuration file %s not found or not readable" \
                 % log_confname)