Draft implementation of repa accept
authorEd Bartosh <eduard.bartosh@intel.com>
Sat, 17 Aug 2013 09:06:10 +0000 (12:06 +0300)
committerEd Bartosh <eduard.bartosh@intel.com>
Sat, 17 Aug 2013 09:10:37 +0000 (12:10 +0300)
Acceptance is done by creating SRs for all packages and immediately
changing their state to 'accepted'.

This change also introduces common APIs, which will be used for further
implementation of repa reject command.

Change-Id: I35660dd266941531dd8421b7c141c904eb350b9b
Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
repa/accept.py [new file with mode: 0644]
repa/common.py
setup.py

diff --git a/repa/accept.py b/repa/accept.py
new file mode 100644 (file)
index 0000000..178b3c1
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+"""
+REPA: Release Engineering Process Assistant.
+
+Copyright (C) Intel Corporation 2013
+Licence: GPL version 2
+Author: Ed Bartosh <eduard.bartosh@intel.com>
+
+Accept module.
+Accept submissions.
+"""
+
+import sys
+
+from repa.obs import OBS
+from repa.main import sub_main
+from repa.common import accept_or_reject
+
+
+class Accept(object):
+    """Subcommand: accept submissions."""
+
+    name = 'accept'
+    description = 'Accept submission or group'
+    help = description
+
+    @staticmethod
+    def add_arguments(parser, _config):
+        """Adds arguments to the parser. Called from [sub_]main."""
+        parser.add_argument('submission', help='submission or group')
+        parser.add_argument('-comment', help='comment', default='')
+
+    @staticmethod
+    def run(argv):
+        """Command line entry point. Called from [sub_]main."""
+        obs = OBS(argv.apiurl, argv.apiuser, argv.apipasswd)
+        return accept_or_reject(obs, argv.submission, 'accepted',
+                                argv.comment)
+
+if __name__ == '__main__':
+    sys.exit(sub_main(sys.argv[1:], Accept()))
index 2fd61d5..9384839 100644 (file)
@@ -11,7 +11,67 @@ Common module.
 Common functions, classes, exceptions.
 """
 
+import json
+
+from repa.obs import OBS_PREFIX
+
 class RepaException(Exception):
     """Custom repa exception. All repa modules should use it."""
     pass
 
+def get_project_by_name(obs, name):
+    """Lookup for a project in OBS by submission or group name."""
+    if name.startswith("submitgroup"):
+        mask = '^%s%s$'
+    else:
+        mask = '^%s.*:%s$'
+
+    projects = list(obs.get_projects(mask % (OBS_PREFIX,
+                                             name.replace('/', ':'))))
+    if not projects:
+        raise RepaException('OBS project not found for %s' % name)
+    if len(projects) > 1:
+        raise RepaException('%s resolves into multiple projects' % name)
+
+    return projects[0][0], json.loads(projects[0][1])
+
+
+
+def _resolve_submissions(obs, name):
+    """Get list of submissions with meta. Resolves submitgroups."""
+    project, meta = get_project_by_name(obs, name)
+    if name.startswith('submitgroup'):
+        for subm in meta['submissions']:
+            sprj, smeta = get_project_by_name(obs, subm)
+            yield subm, sprj, smeta
+    else:
+        yield name, project, meta
+
+
+def delete_project(obs, name):
+    """Delete OBS project related to submission or submit group."""
+    project = get_project_by_name(obs, name)[0]
+    obs.delete_project(project)
+
+
+def accept_or_reject(obs, submission, state, comment=''):
+    """Create SRs and set their state for one submission or for a group."""
+    for name, project, meta in _resolve_submissions(obs, submission):
+        # osc submitreq [OPTIONS] SOURCEPRJ SOURCEPKG DESTPRJ [DESTPKG]
+        # osc request accept [-m TEXT] ID
+        message = "submission %s" % str(name)
+        print message
+        if comment:
+            message += ' comment: %s' % comment
+        for pkg in obs.get_source_packages(project):
+            # Create SR
+            reqid = obs.create_sr(project, pkg, str(meta['obs_target_prj']),
+                                  message=message)
+            print 'package %s: created SR %s' % (pkg, reqid)
+            # and immediately set its state
+            obs.set_sr_state(reqid, state=state, message=message, force=True)
+            print 'set SR state to', state
+    # delete submit group
+    if submission.startswith('submitgroup'):
+        delete_project(obs, submission)
+
index a1f5e0f..7930697 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -24,6 +24,7 @@ setup(name = "repa",
       entry_points = {
           'console_scripts': ['repa = repa.main:main'],
           'repa_commands':   ['list = repa.list:List',
-                              'group = repa.group:Group']
+                              'group = repa.group:Group',
+                              'accept = repa.accept:Accept']
       }
 )