Added OBS API module
authorEd Bartosh <eduard.bartosh@intel.com>
Sat, 10 Aug 2013 19:13:08 +0000 (22:13 +0300)
committerEduard Bartosh <eduard.bartosh@intel.com>
Thu, 15 Aug 2013 12:14:28 +0000 (05:14 -0700)
This module will be used by repa subcommands to access OBS. It's
inherited from oscapi and extends it. As soon as new APIs are mature and
generic they'll be contributed to oscapi.

This change adds 2 new API calls: get_packages and get_build_results.

Change-Id: I284f9d62c634254f54b8545a0f7258ad5c7f704f
Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
Reviewed-on: https://otctools.jf.intel.com/review/5838
Tested-by: OTC Tools Tester <ed.bartosh@linux.intel.com>
packaging/repa.spec
repa/obs.py [new file with mode: 0644]

index 176d0b0..69a5d3e 100644 (file)
@@ -16,6 +16,8 @@ BuildRequires:  python-setuptools
 
 Requires:       python >= 2.6
 Requires:       python-setuptools
+Requires:       osc
+Requires:       gbs-api
 
 %description
 This tool is to assist release engineers to operate with submissions
diff --git a/repa/obs.py b/repa/obs.py
new file mode 100644 (file)
index 0000000..d672f86
--- /dev/null
@@ -0,0 +1,85 @@
+"""
+REPA: Release Engineering Process Assistant.
+
+Copyright (C) Intel Corporation 2013
+Licence: GPL version 2
+Author: Ed Bartosh <eduard.bartosh@intel.com>
+
+OBS module. 
+Interface module to access Open Build System.
+
+This module is a intermediate step for new APIs
+to oscapi. As soon as APIs are mature enough they
+should be contributed to oscapi.
+"""
+
+import re
+
+from base64 import b64encode
+from xml.etree import cElementTree as ET
+
+from osc import core
+
+from gitbuildsys.oscapi import OSC
+from gitbuildsys.utils import Temp
+
+
+OBS_PREFIX = "home:prerelease:"
+
+OSCRC_TEMPLATE = """[general]
+apiurl = %(apiurl)s
+plaintext_passwd=0
+use_keyring=0
+http_debug = %(http_debug)s
+debug = %(debug)s
+gnome_keyring=0
+[%(apiurl)s]
+user=%(user)s
+passx=%(passwdx)s
+"""
+
+class OBS(OSC):
+    """Interface to OBS API."""
+
+    def __init__(self, apiurl, apiuser, apipasswd):
+        oscrc = OSCRC_TEMPLATE % {
+            'http_debug': 0,
+            "debug": 0,
+            "apiurl": apiurl,
+            "user": apiuser,
+            "passwdx": b64encode(apipasswd.encode('bz2'))}
+
+        self.apiurl = apiurl
+        tmpf = Temp(prefix='.oscrc', content=oscrc)
+        self.oscrcpath = tmpf.path
+        OSC.__init__(self, apiurl, self.oscrcpath)
+
+
+    def get_projects(self, regexp=''):
+        """List projects with attributes."""
+        for project in core.meta_get_project_list(self.apiurl):
+            if regexp and not re.match(regexp, project):
+                continue
+            yield project, self.get_description(project)
+
+
+    def get_build_results(self, prj):
+        """Get project build results."""
+        meta = core.show_prj_results_meta(self.apiurl, prj)
+        root = ET.fromstring(''.join(meta))
+        buildres = {}
+        if not root.find('result'):
+            return buildres
+        for results in root.findall('result'):
+            key = (results.get('repository'), results.get('arch'))
+            buildres[key] = dict([(field, results.get(field)) \
+                                      for field in ('code', 'state')])
+            pkgresults = []
+            for node in results:
+                code = node.get('code')
+                if node.get('code') != 'excluded':
+                    pkgresults.append((node.get('package'), code))
+            buildres[key]['packages'] = pkgresults
+            
+        return buildres
+