]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/bin/pwclient
pwclient: exit on first patch which fails to apply
[patchwork] / apps / patchwork / bin / pwclient
index 4714ffc1a1953771174d7d930c371e680a040323..38d604e6173d0d36eec082d357a1b10d10080437 100755 (executable)
@@ -29,6 +29,7 @@ import subprocess
 import base64
 import ConfigParser
 import shutil
+import re
 
 # Default Patchwork remote XML-RPC server URL
 # This script will check the PW_XMLRPC_URL environment variable
@@ -68,7 +69,7 @@ class Filter:
             else:
                 self.d['state_id'] = id
 
-        if self.project != "":
+        if self.project != None:
             id = project_id_by_name(rpc, self.project)
             if id == 0:
                 sys.stderr.write("Note: No Project found matching %s, " \
@@ -134,17 +135,34 @@ def person_ids_by_name(rpc, name):
     people = rpc.person_list(name, 0)
     return map(lambda x: x['id'], people)
 
-def list_patches(patches):
+def list_patches(patches, format_str=None):
     """Dump a list of patches to stdout."""
-    print("%-7s %-12s %s" % ("ID", "State", "Name"))
-    print("%-7s %-12s %s" % ("--", "-----", "----"))
-    for patch in patches:
-        print("%-7d %-12s %s" % (patch['id'], patch['state'], patch['name']))
+    if format_str:
+        format_field_re = re.compile("%{([a-z0-9_]+)}")
 
-def action_list(rpc, filter, submitter_str, delegate_str):
+        def patch_field(matchobj):
+            fieldname = matchobj.group(1)
+
+            if fieldname == "_msgid_":
+                # naive way to strip < and > from message-id
+                val = string.strip(str(patch["msgid"]), "<>")
+            else:
+                val = str(patch[fieldname])
+
+            return val
+
+        for patch in patches:
+            print(format_field_re.sub(patch_field, format_str))
+    else:
+        print("%-7s %-12s %s" % ("ID", "State", "Name"))
+        print("%-7s %-12s %s" % ("--", "-----", "----"))
+        for patch in patches:
+            print("%-7d %-12s %s" % (patch['id'], patch['state'], patch['name']))
+
+def action_list(rpc, filter, submitter_str, delegate_str, format_str=None):
     filter.resolve_ids(rpc)
 
-    if submitter_str != "":
+    if submitter_str != None:
         ids = person_ids_by_name(rpc, submitter_str)
         if len(ids) == 0:
             sys.stderr.write("Note: Nobody found matching *%s*\n" % \
@@ -158,10 +176,10 @@ def action_list(rpc, filter, submitter_str, delegate_str):
                 f = filter
                 f.add("submitter_id", id)
                 patches = rpc.patch_list(f.d)
-                list_patches(patches)
+                list_patches(patches, format_str)
         return
 
-    if delegate_str != "":
+    if delegate_str != None:
         ids = person_ids_by_name(rpc, delegate_str)
         if len(ids) == 0:
             sys.stderr.write("Note: Nobody found matching *%s*\n" % \
@@ -174,11 +192,11 @@ def action_list(rpc, filter, submitter_str, delegate_str):
                 f = filter
                 f.add("delegate_id", id)
                 patches = rpc.patch_list(f.d)
-                list_patches(patches)
+                list_patches(patches, format_str)
         return
 
     patches = rpc.patch_list(filter.d)
-    list_patches(patches)
+    list_patches(patches, format_str)
 
 def action_projects(rpc):
     projects = rpc.project_list("", 0)
@@ -251,6 +269,7 @@ def action_apply(rpc, patch_id, apply_cmd=None):
     if len(s) > 0:
         proc = subprocess.Popen(apply_cmd, stdin = subprocess.PIPE)
         proc.communicate(unicode(s).encode('utf-8'))
+        return proc.returncode
     else:
         sys.stderr.write("Error: No patch content found\n")
         sys.exit(1)
@@ -341,6 +360,10 @@ def main():
         'id', metavar='ID', nargs='*', action='store', type=int,
         help='Patch ID',
     )
+    hash_parser.add_argument(
+        '-p', metavar='PROJECT',
+        help='''Lookup patch in project'''
+    )
 
     filter_parser = argparse.ArgumentParser(add_help=False, version=False)
     filter_parser.add_argument(
@@ -368,6 +391,11 @@ def main():
         '-m', metavar='MESSAGEID',
         help='''Filter by Message-Id'''
     )
+    filter_parser.add_argument(
+        '-f', metavar='FORMAT',
+        help='''Print output in the given format. You can use tags matching '''
+            '''fields, e.g. %%{id}, %%{state}, or %%{msgid}.'''
+    )
     filter_parser.add_argument(
         'patch_name', metavar='STR', nargs='?',
         help='substring to search for patches by name',
@@ -482,7 +510,7 @@ def main():
         sys.exit(0)
 
     args = action_parser.parse_args()
-    args=dict(vars(args))
+    args = dict(vars(args))
     action = args.get('subcmd')
 
     if args.get('hash') and len(args.get('id')):
@@ -494,28 +522,17 @@ def main():
 
     # set defaults
     filt = Filter()
-    submitter_str = ""
-    delegate_str = ""
-    project_str = ""
-    commit_str = ""
-    state_str = ""
-    hash_str = None
-    msgid_str = ""
-    patch_ids = None
+    commit_str = None
     url = DEFAULT_URL
 
-    if args.get('s'):
-        state_str = args.get('s')
-    if args.get('p'):
-        project_str = args.get('p')
-    if args.get('w'):
-        submitter_str = args.get('w')
-    if args.get('d'):
-        delegate_str = args.get('d')
-    if args.get('hash'):
-        hash_str = args.get('hash')
-    if args.get('id'):
-        patch_ids = args.get('id')
+    state_str = args.get('s')
+    project_str = args.get('p')
+    submitter_str = args.get('w')
+    delegate_str = args.get('d')
+    format_str = args.get('f')
+    hash_str = args.get('hash')
+    patch_ids = args.get('id')
+    msgid_str = args.get('m')
     if args.get('c'):
         # update multiple IDs with a single commit-hash does not make sense
         if action == 'update' and patch_ids and len(patch_ids) > 1:
@@ -525,8 +542,7 @@ def main():
             update_parser.print_help()
             sys.exit(1)
         commit_str = args.get('c')
-    if args.get('m'):
-        msgid_str = args.get('m')
+
     if args.get('n') != None:
         try:
             filt.add("max_count", args.get('n'))
@@ -581,7 +597,6 @@ def main():
     if not config.has_section(project_str):
         sys.stderr.write("No section for project %s\n" % project_str)
         sys.exit(1)
-
     if not config.has_option(project_str, 'url'):
         sys.stderr.write("No URL for project %s\n" % project_str)
         sys.exit(1)
@@ -646,7 +661,7 @@ def main():
     if action == 'list' or action == 'search':
         if args.get('patch_name') != None:
             filt.add("name__icontains", args.get('patch_name'))
-        action_list(rpc, filt, submitter_str, delegate_str)
+        action_list(rpc, filt, submitter_str, delegate_str, format_str)
 
     elif action.startswith('project'):
         action_projects(rpc)
@@ -660,26 +675,36 @@ def main():
             if len(s) > 0:
                 print unicode(s).encode("utf-8")
 
-    elif action in ('get', 'save', 'info'):
-        if action == 'info':
-            [action_info(rpc, patch_id) for patch_id in non_empty(h, patch_ids)]
-        else:
-            [action_get(rpc, patch_id) for patch_id in non_empty(h, patch_ids)]
+    elif action == 'info':
+        for patch_id in non_empty(h, patch_ids):
+            action_info(rpc, patch_id)
+
+    elif action == 'get':
+        for patch_id in non_empty(h, patch_ids):
+            action_get(rpc, patch_id)
 
     elif action == 'apply':
-        [action_apply(rpc, patch_id) for patch_id in non_empty(h, patch_ids)]
+        for patch_id in non_empty(h, patch_ids):
+            ret = action_apply(rpc, patch_id)
+            if ret:
+                sys.stderr.write("Apply failed with exit status %d\n" % ret)
+                sys.exit(1)
 
     elif action == 'git_am':
         cmd = ['git', 'am']
         if do_signoff:
             cmd.append('-s')
-        [action_apply(rpc, patch_id, cmd) for patch_id in
-         non_empty(h, patch_ids)]
+        for patch_id in non_empty(h, patch_ids):
+            ret = action_apply(rpc, patch_id, cmd)
+            if ret:
+                sys.stderr.write("'git am' failed with exit status %d\n" % ret)
+                sys.exit(1)
 
     elif action == 'update':
-        [action_update_patch(rpc, patch_id, state = state_str,
+        for patch_id in non_empty(h, patch_ids):
+            action_update_patch(rpc, patch_id, state = state_str,
                 commit = commit_str
-         ) for patch_id in non_empty(h, patch_ids)]
+            )
 
     else:
         sys.stderr.write("Unknown action '%s'\n" % action)