add new job to store repos.yaml to redis
authorLin Yang <lin.a.yang@intel.com>
Fri, 28 Jun 2013 08:45:32 +0000 (16:45 +0800)
committerEd Bartosh <eduard.bartosh@intel.com>
Fri, 28 Jun 2013 10:11:39 +0000 (13:11 +0300)
This job will monitor repos.yaml project maintained in gerrit, if anything
change, it will store this change to redis.

Change-Id: I0ed3bc7f4a2a83a9ef7a4ce53fbb8971de4a6e1e
Signed-off-by: Lin Yang <lin.a.yang@intel.com>
job_load_repos.yaml.py [new file with mode: 0755]

diff --git a/job_load_repos.yaml.py b/job_load_repos.yaml.py
new file mode 100755 (executable)
index 0000000..728356a
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+
+"""
+This code is called by gerrit-plugin to load the backend config to redis.
+"""
+
+import os
+import sys
+import tempfile
+
+from common.backenddb import BackendDB
+from common.gerritevent import get_gerrit_event
+from common.git import Git, clone_gitproject
+
+def load_repos(config_file):
+    """
+    store the repos conf in repos.yaml to redis
+
+    Args:
+         config_file (str): the repos.yaml file location
+    """
+
+    bdb = BackendDB(os.getenv('REDIS_HOSTNAME'),
+                    int(os.getenv('REDIS_PORT')))
+    return bdb.read_repos(file(config_file, 'r').read())
+
+def main():
+    """Script entry point.
+    """
+
+    print '---[JOB STARTED: %s ]-------------------------'
+    event = get_gerrit_event()
+
+    if event['project'] == os.getenv('REPOSYAML_PRJ'):
+        # prepare separate temp directory for each build
+        workspace = os.getenv('WORKSPACE')
+        tmpdir = tempfile.mkdtemp(prefix=workspace+'/')
+        prjdir = os.path.join(tmpdir, event['project'])
+
+        # clone gerrit project to local dir
+        clone_gitproject(event['project'], prjdir)
+        mygit = Git(prjdir)
+        mygit.checkout(event['newrev'])
+        return load_repos(os.path.join(prjdir, 'repos.yaml'))
+
+if __name__ == '__main__':
+    sys.exit(main())