]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/bin/pwclient
pwclient: allow multiple IDs
[patchwork] / apps / patchwork / bin / pwclient
index a31099da099e6671297659c36cd638302d212ccb..bd79b3a601de9d5834fea1bf424a0228bb428333 100755 (executable)
@@ -106,36 +106,6 @@ class BasicHTTPAuthTransport(xmlrpclib.SafeTransport):
             fn = xmlrpclib.Transport.make_connection
         return fn(self, host)
 
-def usage():
-    sys.stderr.write("Usage: %s <action> [options]\n\n" % \
-                        (os.path.basename(sys.argv[0])))
-    sys.stderr.write("Where <action> is one of:\n")
-    sys.stderr.write(
-"""        apply <ID>    : Apply a patch (in the current dir, using -p1)
-        git-am <ID>   : Apply a patch to current git branch using "git am"
-        get <ID>      : Download a patch and save it locally
-        info <ID>     : Display patchwork info about a given patch ID
-        projects      : List all projects
-        states        : Show list of potential patch states
-        list [str]    : List patches, using the optional filters specified
-                        below and an optional substring to search for patches
-                        by name
-        search [str]  : Same as 'list'
-        view <ID>     : View a patch
-        update [-s state] [-c commit-ref] <ID>
-                      : Update patch\n""")
-    sys.stderr.write("""\nFilter options for 'list' and 'search':
-        -s <state>    : Filter by patch state (e.g., 'New', 'Accepted', etc.)
-        -p <project>  : Filter by project name (see 'projects' for list)
-        -w <who>      : Filter by submitter (name, e-mail substring search)
-        -d <who>      : Filter by delegate (name, e-mail substring search)
-        -n <max #>    : Restrict number of results
-        -m <messageid>: Filter by Message-Id\n""")
-    sys.stderr.write("""\nActions that take an ID argument can also be \
-invoked with:
-        -h <hash>     : Lookup by patch hash\n""")
-    sys.exit(1)
-
 def project_id_by_name(rpc, linkname):
     """Given a project short name, look up the Project ID."""
     if len(linkname) == 0:
@@ -361,13 +331,12 @@ class _RecursiveHelpAction(argparse._HelpAction):
 
 def main():
     hash_parser = argparse.ArgumentParser(add_help=False, version=False)
-    hash_parser_x = hash_parser.add_mutually_exclusive_group(required=True)
-    hash_parser_x.add_argument(
+    hash_parser.add_argument(
         '-h', metavar='HASH', dest='hash', action='store', required=False,
         help='''Lookup by patch hash'''
     )
-    hash_parser_x.add_argument(
-        'id', metavar='ID', nargs='?', action='store', type=int,
+    hash_parser.add_argument(
+        'id', metavar='ID', nargs='*', action='store', type=int,
         help='Patch ID',
     )
 
@@ -407,7 +376,7 @@ def main():
         add_help=False,
         version=False,
         formatter_class=argparse.RawDescriptionHelpFormatter,
-        epilog='''(apply | get | info | view | update) (-h HASH | ID)''',
+        epilog='''(apply | get | info | view | update) (-h HASH | ID [ID ...])''',
     )
     action_parser.add_argument(
         '--help',
@@ -470,7 +439,8 @@ def main():
     update_parser = subparsers.add_parser(
         'update', parents=[hash_parser],
         add_help=False,
-        help='''Update patch'''
+        help='''Update patch''',
+        epilog='''Using a COMMIT-REF allows for only one ID to be specified''',
     )
     update_parser.set_defaults(subcmd='update')
     update_parser.add_argument(
@@ -505,6 +475,12 @@ def main():
     args = action_parser.parse_args()
     args=dict(vars(args))
 
+    if args.get('hash') and len(args.get('id')):
+        # mimic mutual exclusive group
+        sys.stderr.write("[-h HASH] and [ID [ID ...]] are mutually exlusive!\n")
+        action_parser.print_help()
+        sys.exit(1)
+
     # set defaults
     filt = Filter()
     submitter_str = ""
@@ -514,7 +490,7 @@ def main():
     state_str = ""
     hash_str = None
     msgid_str = ""
-    id_str = None
+    patch_ids = None
     url = DEFAULT_URL
 
     action = args.get('subcmd')
@@ -527,12 +503,17 @@ def main():
         submitter_str = args.get('w')
     if args.get('d'):
         delegate_str = args.get('d')
-    if args.get('c'):
-        commit_str = args.get('c')
     if args.get('hash'):
         hash_str = args.get('hash')
     if args.get('id'):
-        id_str = args.get('id')
+        patch_ids = frozenset(args.get('id'))
+    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:
+            sys.stderr.write("Declining update with COMMIT-REF on multiple IDs\n")
+            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:
@@ -543,6 +524,8 @@ def main():
             action_parser.print_help()
             sys.exit(1)
 
+    do_signoff = args.get('signoff')
+
     # grab settings from config files
     config = ConfigParser.ConfigParser()
     config.read([CONFIG_FILE])
@@ -581,7 +564,8 @@ def main():
             project_str = config.get('options', 'default')
         except:
             sys.stderr.write("No default project configured in ~/.pwclientrc\n")
-            usage()
+            action_parser.print_help()
+            sys.exit(1)
 
     if not config.has_section(project_str):
         sys.stderr.write("No section for project %s\n" % project_str)
@@ -590,6 +574,10 @@ def main():
     if not config.has_option(project_str, 'url'):
         sys.stderr.write("No URL for project %s\n" % project_str)
         sys.exit(1)
+    if not do_signoff and config.has_option('options', 'signoff'):
+        do_signoff = config.getboolean('options', 'signoff')
+    if not do_signoff and config.has_option(project_str, 'signoff'):
+        do_signoff = config.getboolean(project_str, 'signoff')
 
     url = config.get(project_str, 'url')
 
@@ -626,13 +614,9 @@ def main():
         sys.stderr.write("Unable to connect to %s\n" % url)
         sys.exit(1)
 
-    patch_id = None
-    # hash_str and id_str are mutually exclusive
-    if hash_str:
-        patch_id = patch_id_from_hash(rpc, project_str, hash_str)
-    else:
-        # id_str from argparse is an int
-        patch_id = id_str
+    # It should be safe to assume hash_str is not zero, but who knows..
+    if hash_str != None:
+        patch_ids = [patch_id_from_hash(rpc, project_str, hash_str)]
 
     if action == 'list' or action == 'search':
         if args.get('patch_name') != None:
@@ -646,32 +630,34 @@ def main():
         action_states(rpc)
 
     elif action == 'view':
-        s = rpc.patch_get_mbox(patch_id)
-        if len(s) > 0:
-            print unicode(s).encode("utf-8")
+        for patch_id in patch_ids:
+            s = rpc.patch_get_mbox(patch_id)
+            if len(s) > 0:
+                print unicode(s).encode("utf-8")
 
     elif action in ('get', 'save', 'info'):
         if action == 'info':
-            action_info(rpc, patch_id)
+            [action_info(rpc, patch_id) for patch_id in patch_ids]
         else:
-            action_get(rpc, patch_id)
+            [action_get(rpc, patch_id) for patch_id in patch_ids]
 
     elif action == 'apply':
-        action_apply(rpc, patch_id)
+        [action_apply(rpc, patch_id) for patch_id in patch_ids]
 
     elif action == 'git-am':
         cmd = ['git', 'am']
-        if args.get('signoff'):
+        if do_signoff:
             cmd.append('-s')
-        action_apply(rpc, patch_id, cmd)
+        [action_apply(rpc, patch_id, cmd) for patch_id in patch_ids]
 
     elif action == 'update':
-        action_update_patch(rpc, patch_id, state = state_str,
-                commit = commit_str)
+        [action_update_patch(rpc, patch_id, state = state_str,
+                commit = commit_str) for patch_id in patch_ids]
 
     else:
         sys.stderr.write("Unknown action '%s'\n" % action)
-        usage()
+        action_parser.print_help()
+        sys.exit(1)
 
 if __name__ == "__main__":
     main()