]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/bin/pwclient
pwclient: drop unused variables
[patchwork] / apps / patchwork / bin / pwclient
index b86949c264b94246d30fff84ad39e48b343bb6b7..70d4f82932728dbf43a00e14ea0f14e7ebdd923b 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
@@ -134,14 +135,31 @@ 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 != None:
@@ -158,7 +176,7 @@ 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 != None:
@@ -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',
@@ -501,6 +529,7 @@ def main():
     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')
@@ -578,7 +607,6 @@ def main():
 
     url = config.get(project_str, 'url')
 
-    (username, password) = (None, None)
     transport = None
     if action in auth_actions:
         if config.has_option(project_str, 'username') and \
@@ -632,7 +660,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)
@@ -650,20 +678,26 @@ def main():
         for patch_id in non_empty(h, patch_ids):
             action_info(rpc, patch_id)
 
-    elif action in ('get', 'save'):
+    elif action == 'get':
         for patch_id in non_empty(h, patch_ids):
             action_get(rpc, patch_id)
 
     elif action == 'apply':
         for patch_id in non_empty(h, patch_ids):
-            action_apply(rpc, patch_id)
+            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')
         for patch_id in non_empty(h, patch_ids):
-            action_apply(rpc, patch_id, cmd)
+            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':
         for patch_id in non_empty(h, patch_ids):