Common code for all hooks to find and execute plugins.
authorAlexander Kanevskiy <alexander.kanevskiy@intel.com>
Tue, 13 Aug 2013 09:15:16 +0000 (12:15 +0300)
committerAlexander Kanevskiy <alexander.kanevskiy@intel.com>
Tue, 13 Aug 2013 09:15:16 +0000 (12:15 +0300)
gerrithooks/common.py [new file with mode: 0644]

diff --git a/gerrithooks/common.py b/gerrithooks/common.py
new file mode 100644 (file)
index 0000000..6580bae
--- /dev/null
@@ -0,0 +1,67 @@
+#!/usr/bin/env
+# -*- coding: UTF-8 -*-
+# vim: sw=4 ts=4 expandtab ai
+#
+# Copyright (c) 2013 Intel, Inc.
+# License: GPLv2
+# Author: Alexander Kanevskiy <alexander.kanevskiy@intel.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License, version 2,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+
+"""
+Common code to run hooks from plugins
+"""
+import os
+import sys
+import ConfigParser
+
+from gerrithooks.misc import find_config, configure_logging, daemonize
+
+from pkg_resources import iter_entry_points
+
+def run_plugin_hooks(hook, params, logger):
+    """
+    Common code for every hook. Must provide parsed params and logger
+    """
+
+    if not 'debug' in params or not params.debug:
+        logger.debug("Daemonizing...")
+        daemonize()
+
+    logger.debug("looking for config")
+    confname = "gerrit-hooks.conf"
+    conf = find_config(confname, subdir="gerrit-hooks")
+    if not conf:
+        logger.error("Log configuration file %s not found or not readable",
+                        confname)
+        return 1
+
+    # read rules from configuration file
+    config = ConfigParser.RawConfigParser()
+    #config.optionxform = str
+
+    logger.debug("runner: reading config from %s", conf)
+    config.read(conf)
+
+    entry_point_group = "gerrit-hooks"
+    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",
+            entry_point.name, entry_point.module)
+        plugin = entry_point.load()
+        logger.debug("Executing plugin %s", entry_point.name)
+        plugin(hook=hook, params=params, config=config)
+
+    logger.info("done")
+    return 0
+
+if __name__ == "__main__":
+    raise SystemExit("This module does not supposed to be run directly!")