Implement --edit command line option
authorEd Bartosh <eduard.bartosh@intel.com>
Sat, 14 Feb 2015 22:59:41 +0000 (00:59 +0200)
committerEd Bartosh <eduard.bartosh@intel.com>
Thu, 19 Feb 2015 13:07:49 +0000 (15:07 +0200)
-e or --edit option is used in accept, reject, rebuild, lock, unlock and
remove commands to edit a comment. Default comment is taken from
accept_comment, reject_comment, rebuild_comment, lock_comment,
unlock_comment or remove_comment configuration options.

Fixes: #2420
Change-Id: I5979ebd246541b5c4ba56629ba97fd268b178d8d
Signed-off-by: Ed Bartosh <eduard.bartosh@intel.com>
repa.1
repa/accept.py
repa/common.py
repa/lock.py
repa/rebuild.py
repa/reject.py
repa/remove.py
repa/unlock.py

diff --git a/repa.1 b/repa.1
index 0ce6399d07f9e998a3bb30fcd22ac1b9201b075f..bbed6eb87aec3e8595c3f61e347b0eaea04d4bf7 100644 (file)
--- a/repa.1
+++ b/repa.1
@@ -255,6 +255,12 @@ Add acceptance comment for created SR.
 Trigger Jenkins job to accept submission.
 .RE
 
+.PP
+\-e \-\-edit
+.RS 2
+Run editor to edit comment. Editor is taken from EDITOR environment variable.
+.RE
+
 .\"
 .\" The "reject" command description
 .\"
@@ -287,6 +293,12 @@ Add rejection comment for created SR.
 Trigger Jenkins job to reject submission.
 .RE
 
+.PP
+\-e \-\-edit
+.RS 2
+Run editor to edit comment. Editor is taken from EDITOR environment variable.
+.RE
+
 .\"
 .\" The "group" command description
 .\"
@@ -422,6 +434,12 @@ Provide a comment with the explanation of a reason for rebuild. Mandatory option
 Rebuild only one package.
 .RE
 
+.PP
+\-e \-\-edit
+.RS 2
+Run editor to edit comment. Editor is taken from EDITOR environment variable.
+.RE
+
 .\"
 .\" The "lock" command description
 .\"
@@ -447,6 +465,12 @@ Print short help text about the "lock" command and exit.
 Provide a comment with the explanation of a reason for lock. Mandatory option.
 .RE
 
+.PP
+\-e \-\-edit
+.RS 2
+Run editor to edit comment. Editor is taken from EDITOR environment variable.
+.RE
+
 .\"
 .\" The "unlock" command description
 .\"
@@ -472,6 +496,11 @@ Print short help text about the "unlock" command and exit.
 Provide a comment with the explanation of a reason for unlock.
 .RE
 
+.PP
+\-e \-\-edit
+.RS 2
+Run editor to edit comment. Editor is taken from EDITOR environment variable.
+.RE
 
 .\"
 .\" The "remove" command description
@@ -498,6 +527,12 @@ Print short help text about the "remove" command and exit.
 Provide a comment with the explanation of a reason for remove. This is mandatory option.
 .RE
 
+.PP
+\-e \-\-edit
+.RS 2
+Run editor to edit comment. Editor is taken from EDITOR environment variable.
+.RE
+
 
 .SH CONFIGURATION FILE
 
@@ -554,8 +589,36 @@ noaggregate = mic-bootstrap-x86-arm.rpm|mic-bootstrap.rpm|mic-bootstrap-debugsou
 .RS 2
 showtime = off
 .RE
-
-
+.RS 2
+accept_comment = default comment
+ for accept
+ command
+.RE
+.RS 2
+reject_comment = default comment
+ for reject
+ command
+.RE
+.RS 2
+rebuild_comment = default comment
+ for rebuild
+ command
+.RE
+.RS 2
+lock_comment = default comment
+ for lock
+ command
+.RE
+.RS 2
+unlock_comment = default comment
+ for unlock
+ command
+.RE
+.RS 2
+remove_comment = default comment
+ for remove
+ command
+.RE
 
 .RS 2
 Mandatory options: apiurl, apiuser, apipasswd, jenkins_url, jenkins_user, jenkins_passwd and project
index 410952ddf90bdd85793eb844a6697685c6fd1929..45be50a8dff8cbb4a343b5965d3d3266d7d22a9b 100644 (file)
@@ -34,7 +34,7 @@ from collections import namedtuple
 
 from repa.obs import OBS
 from repa.main import sub_main
-from repa.common import accept_or_reject
+from repa.common import accept_or_reject, edit
 
 
 class Accept(object):
@@ -45,16 +45,23 @@ class Accept(object):
     help = description
 
     @staticmethod
-    def add_arguments(parser, _):
+    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('-c', '--comment', help='comment', default='')
+        parser.add_argument('-c', '--comment', help='comment',
+                            default=config.get('accept_comment', ''))
         parser.add_argument('-j', '--jenkins', action='store_true',
                             help='trigger Jenkins job')
+        parser.add_argument('-e', '--edit', action='store_true',
+                            help='run editor to edit comment')
 
     @staticmethod
     def run(argv):
         """Command line entry point. Called from [sub_]main."""
+        if argv.edit:
+            argv.comment = edit(argv.comment)
+
         obs = OBS(argv.apiurl, argv.apiuser, argv.apipasswd)
 
         cred = None
index 7829f52230352b1807d0f941bce63b362c9a4b2e..e63f07312652dc1512411c2d2e35277faf34b174 100644 (file)
@@ -33,7 +33,10 @@ import sys
 import os
 import time
 import json
+import tempfile
+import subprocess
 from functools import wraps, partial
+from distutils.spawn import find_executable
 
 from repa.jenkins import trigger_build
 
@@ -219,3 +222,33 @@ def get_obs_url(meta, buildurl='https://build.tizen.org'):
     return os.path.join(buildurl, 'project/show?project=home:prerelease:%s:%s'
                         % (meta['obs_target_prj'], name.replace('/', ':')))
 
+
+def edit(content):
+    """
+    Launch an editor to get input from user.
+    Returns: content of user input.
+    """
+    editor = os.getenv('EDITOR') or 'vi'
+
+    if not find_executable(editor):
+        raise RepaException("editor %s not found. Please set EDITOR "
+                            "environment variable or install vi" % editor)
+
+    fds, path = tempfile.mkstemp('.tmp', 'repa-', text=True)
+    try:
+        if content:
+            os.write(fds, content)
+        os.close(fds)
+
+        try:
+            subprocess.call([editor, path])
+        except OSError as error:
+            raise RepaException("Can't run %s %s: %s" % (editor, path, error))
+
+        with open(path) as fobj:
+            result = fobj.read()
+    finally:
+        os.unlink(path)
+
+    return result
+
index 9edd966136fb521c37752459712c20b2b7443cf8..a9d9c04d93acc3d8fc7a8d04955417b1ba776cf3 100644 (file)
@@ -35,6 +35,7 @@ from collections import namedtuple
 
 from repa.main import sub_main
 from repa.jenkins import trigger_build
+from repa.common import edit
 
 class Lock(object):
     """Subcommand: lock submissions."""
@@ -44,14 +45,19 @@ class Lock(object):
     help = description
 
     @staticmethod
-    def add_arguments(parser, _):
+    def add_arguments(parser, config):
         """Adds arguments to the parser. Called from [sub_]main."""
         parser.add_argument('submission', help='submission')
-        parser.add_argument('-c', '--comment', help='comment', required=True)
+        parser.add_argument('-c', '--comment', help='comment',
+                            default=config.get('lock_comment', ''))
+        parser.add_argument('-e', '--edit', action='store_true',
+                            help='run editor to edit comment')
 
     @staticmethod
     def run(argv):
         """Command line entry point. Called from [sub_]main."""
+        if argv.edit:
+            argv.comment = edit(argv.comment)
         job = 're'
         cred = namedtuple('cred', ['url', 'username', 'password'])(\
                    argv.jenkins_url, argv.jenkins_user, argv.jenkins_passwd)
index d79b82350b6ae3caa3335b023edb61eacd015a46..898658f16536255c6f642de803b2bcf96df60a18 100644 (file)
@@ -35,6 +35,7 @@ from collections import namedtuple
 
 from repa.main import sub_main
 from repa.jenkins import trigger_build
+from repa.common import edit
 
 class Rebuild(object):
     """Subcommand: rebuild submissions."""
@@ -44,15 +45,20 @@ class Rebuild(object):
     help = description
 
     @staticmethod
-    def add_arguments(parser, _):
+    def add_arguments(parser, config):
         """Adds arguments to the parser. Called from [sub_]main."""
         parser.add_argument('submission', help='submission')
         parser.add_argument('-p', '--package', help='package')
-        parser.add_argument('-c', '--comment', help='comment', required=True)
+        parser.add_argument('-c', '--comment', help='comment',
+                            default=config.get('rebuild_comment', ''))
+        parser.add_argument('-e', '--edit', action='store_true',
+                            help='run editor to edit comment')
 
     @staticmethod
     def run(argv):
         """Command line entry point. Called from [sub_]main."""
+        if argv.edit:
+            argv.comment = edit(argv.comment)
         job = 're'
         cred = namedtuple('cred', ['url', 'username', 'password'])(\
                    argv.jenkins_url, argv.jenkins_user, argv.jenkins_passwd)
index 4f8290aaff0f434b4638e754abe599a82ac8c5d9..8866e3f9c5bf4c2ef9811171aff7e8aa3b4fbf1a 100644 (file)
@@ -34,7 +34,7 @@ from collections import namedtuple
 
 from repa.obs import OBS
 from repa.main import sub_main
-from repa.common import accept_or_reject
+from repa.common import accept_or_reject, edit
 
 
 class Reject(object):
@@ -45,16 +45,22 @@ class Reject(object):
     help = description
 
     @staticmethod
-    def add_arguments(parser, _):
+    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('-c', '--comment', help='comment', default='')
+        parser.add_argument('-c', '--comment', help='comment',
+                            default=config.get('reject_comment', ''))
         parser.add_argument('-j', '--jenkins', action='store_true',
                             help='trigger Jenkins job')
+        parser.add_argument('-e', '--edit', action='store_true',
+                            help='run editor to edit comment')
 
     @staticmethod
     def run(argv):
         """Command line entry point. Called from [sub_]main."""
+        if argv.edit:
+            argv.comment = edit(argv.comment)
+
         obs = OBS(argv.apiurl, argv.apiuser, argv.apipasswd)
 
         cred = None
index f06cbbf07692aec3b4aee38070715ce318e6b9ba..5bfe05b00c27cf98f708cacdf74b2a7af4c278fa 100644 (file)
@@ -35,6 +35,7 @@ from collections import namedtuple
 
 from repa.main import sub_main
 from repa.jenkins import trigger_build
+from repa.common import edit
 
 class Remove(object):
     """Subcommand: remove submissions."""
@@ -44,14 +45,19 @@ class Remove(object):
     help = description
 
     @staticmethod
-    def add_arguments(parser, _):
+    def add_arguments(parser, config):
         """Adds arguments to the parser. Called from [sub_]main."""
         parser.add_argument('submission', help='submission')
-        parser.add_argument('-c', '--comment', help='comment', required=True)
+        parser.add_argument('-c', '--comment', help='comment',
+                            default=config.get('remove_comment', ''))
+        parser.add_argument('-e', '--edit', action='store_true',
+                            help='run editor to edit comment')
 
     @staticmethod
     def run(argv):
         """Command line entry point. Called from [sub_]main."""
+        if argv.edit:
+            argv.comment = edit(argv.comment)
         job = 're'
         cred = namedtuple('cred', ['url', 'username', 'password'])(\
                    argv.jenkins_url, argv.jenkins_user, argv.jenkins_passwd)
index 47e0fec5e270c3ed9f46dab17b8bfd12d228614e..7e9033070f01e9118787a6637b41724d4c59cf56 100644 (file)
@@ -35,6 +35,7 @@ from collections import namedtuple
 
 from repa.main import sub_main
 from repa.jenkins import trigger_build
+from repa.common import edit
 
 class Unlock(object):
     """Subcommand: unlock submissions."""
@@ -44,14 +45,19 @@ class Unlock(object):
     help = description
 
     @staticmethod
-    def add_arguments(parser, _):
+    def add_arguments(parser, config):
         """Adds arguments to the parser. Called from [sub_]main."""
         parser.add_argument('submission', help='submission')
-        parser.add_argument('-c', '--comment', help='comment')
+        parser.add_argument('-c', '--comment', help='comment',
+                            default=config.get('unlock_comment', ''))
+        parser.add_argument('-e', '--edit', action='store_true',
+                            help='run editor to edit comment')
 
     @staticmethod
     def run(argv):
         """Command line entry point. Called from [sub_]main."""
+        if argv.edit:
+            argv.comment = edit(argv.comment)
         job = 're'
         cred = namedtuple('cred', ['url', 'username', 'password'])(\
                    argv.jenkins_url, argv.jenkins_user, argv.jenkins_passwd)