3 # Patchwork command line client
4 # Copyright (C) 2008 Nate Case <ncase@xes-inc.com>
6 # This file is part of the Patchwork package.
8 # Patchwork is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # Patchwork is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Patchwork; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 # Default Patchwork remote XML-RPC server URL
35 # This script will check the PW_XMLRPC_URL environment variable
36 # for the URL to access. If that is unspecified, it will fallback to
37 # the hardcoded default value specified here.
38 DEFAULT_URL = "http://patchwork/xmlrpc/"
39 CONFIG_FILE = os.path.expanduser('~/.pwclientrc')
42 """Filter for selecting patches."""
44 # These fields refer to specific objects, so they are special
45 # because we have to resolve them to IDs before passing the
46 # filter to the server
50 # The dictionary that gets passed to via XML-RPC
53 def add(self, field, value):
56 elif field == 'project':
62 def resolve_ids(self, rpc):
63 """Resolve State, Project, and Person IDs based on filter strings."""
65 id = state_id_by_name(rpc, self.state)
67 sys.stderr.write("Note: No State found matching %s*, " \
68 "ignoring filter\n" % self.state)
70 self.d['state_id'] = id
72 if self.project != None:
73 id = project_id_by_name(rpc, self.project)
75 sys.stderr.write("Note: No Project found matching %s, " \
76 "ignoring filter\n" % self.project)
78 self.d['project_id'] = id
81 """Return human-readable description of the filter."""
84 class BasicHTTPAuthTransport(xmlrpclib.SafeTransport):
86 def __init__(self, username = None, password = None, use_https = False):
87 self.username = username
88 self.password = password
89 self.use_https = use_https
90 xmlrpclib.SafeTransport.__init__(self)
92 def authenticated(self):
93 return self.username != None and self.password != None
95 def send_host(self, connection, host):
96 xmlrpclib.Transport.send_host(self, connection, host)
97 if not self.authenticated():
99 credentials = '%s:%s' % (self.username, self.password)
100 auth = 'Basic ' + base64.encodestring(credentials).strip()
101 connection.putheader('Authorization', auth)
103 def make_connection(self, host):
105 fn = xmlrpclib.SafeTransport.make_connection
107 fn = xmlrpclib.Transport.make_connection
108 return fn(self, host)
110 def project_id_by_name(rpc, linkname):
111 """Given a project short name, look up the Project ID."""
112 if len(linkname) == 0:
114 projects = rpc.project_list(linkname, 0)
115 for project in projects:
116 if project['linkname'] == linkname:
120 def state_id_by_name(rpc, name):
121 """Given a partial state name, look up the state ID."""
124 states = rpc.state_list(name, 0)
126 if state['name'].lower().startswith(name.lower()):
130 def person_ids_by_name(rpc, name):
131 """Given a partial name or email address, return a list of the
132 person IDs that match."""
135 people = rpc.person_list(name, 0)
136 return map(lambda x: x['id'], people)
138 def list_patches(patches, format_str=None):
139 """Dump a list of patches to stdout."""
141 format_field_re = re.compile("%{([a-z0-9_]+)}")
143 def patch_field(matchobj):
144 fieldname = matchobj.group(1)
146 if fieldname == "_msgid_":
147 # naive way to strip < and > from message-id
148 val = string.strip(str(patch["msgid"]), "<>")
150 val = str(patch[fieldname])
154 for patch in patches:
155 print(format_field_re.sub(patch_field, format_str))
157 print("%-7s %-12s %s" % ("ID", "State", "Name"))
158 print("%-7s %-12s %s" % ("--", "-----", "----"))
159 for patch in patches:
160 print("%-7d %-12s %s" % (patch['id'], patch['state'], patch['name']))
162 def action_list(rpc, filter, submitter_str, delegate_str, format_str=None):
163 filter.resolve_ids(rpc)
165 if submitter_str != None:
166 ids = person_ids_by_name(rpc, submitter_str)
168 sys.stderr.write("Note: Nobody found matching *%s*\n" % \
172 person = rpc.person_get(id)
173 print "Patches submitted by %s <%s>:" % \
174 (unicode(person['name']).encode("utf-8"), \
175 unicode(person['email']).encode("utf-8"))
177 f.add("submitter_id", id)
178 patches = rpc.patch_list(f.d)
179 list_patches(patches, format_str)
182 if delegate_str != None:
183 ids = person_ids_by_name(rpc, delegate_str)
185 sys.stderr.write("Note: Nobody found matching *%s*\n" % \
189 person = rpc.person_get(id)
190 print "Patches delegated to %s <%s>:" % \
191 (person['name'], person['email'])
193 f.add("delegate_id", id)
194 patches = rpc.patch_list(f.d)
195 list_patches(patches, format_str)
198 patches = rpc.patch_list(filter.d)
199 list_patches(patches, format_str)
201 def action_projects(rpc):
202 projects = rpc.project_list("", 0)
203 print("%-5s %-24s %s" % ("ID", "Name", "Description"))
204 print("%-5s %-24s %s" % ("--", "----", "-----------"))
205 for project in projects:
206 print("%-5d %-24s %s" % (project['id'], \
207 project['linkname'], \
210 def action_states(rpc):
211 states = rpc.state_list("", 0)
212 print("%-5s %s" % ("ID", "Name"))
213 print("%-5s %s" % ("--", "----"))
215 print("%-5d %s" % (state['id'], state['name']))
217 def action_info(rpc, patch_id):
218 patch = rpc.patch_get(patch_id)
219 s = "Information for patch id %d" % (patch_id)
222 for key, value in sorted(patch.iteritems()):
223 print("- %- 14s: %s" % (key, unicode(value).encode("utf-8")))
225 def action_get(rpc, patch_id):
226 patch = rpc.patch_get(patch_id)
227 s = rpc.patch_get_mbox(patch_id)
229 if patch == {} or len(s) == 0:
230 sys.stderr.write("Unable to get patch %d\n" % patch_id)
233 base_fname = fname = os.path.basename(patch['filename'])
235 while os.path.exists(fname):
236 fname = "%s.%d" % (base_fname, i)
242 sys.stderr.write("Unable to open %s for writing\n" % fname)
246 f.write(unicode(s).encode("utf-8"))
248 print "Saved patch to %s" % fname
250 sys.stderr.write("Failed to write to %s\n" % fname)
253 def action_apply(rpc, patch_id, apply_cmd=None):
254 patch = rpc.patch_get(patch_id)
256 sys.stderr.write("Error getting information on patch ID %d\n" % \
260 if apply_cmd is None:
261 print "Applying patch #%d to current directory" % patch_id
262 apply_cmd = ['patch', '-p1']
264 print "Applying patch #%d using %s" % (
265 patch_id, repr(' '.join(apply_cmd)))
267 print "Description: %s" % patch['name']
268 s = rpc.patch_get_mbox(patch_id)
270 proc = subprocess.Popen(apply_cmd, stdin = subprocess.PIPE)
271 proc.communicate(unicode(s).encode('utf-8'))
272 return proc.returncode
274 sys.stderr.write("Error: No patch content found\n")
277 def action_update_patch(rpc, patch_id, state = None, commit = None):
278 patch = rpc.patch_get(patch_id)
280 sys.stderr.write("Error getting information on patch ID %d\n" % \
287 state_id = state_id_by_name(rpc, state)
289 sys.stderr.write("Error: No State found matching %s*\n" % state)
291 params['state'] = state_id
294 params['commit_ref'] = commit
298 success = rpc.patch_set(patch_id, params)
299 except xmlrpclib.Fault, f:
300 sys.stderr.write("Error updating patch: %s\n" % f.faultString)
303 sys.stderr.write("Patch not updated\n")
305 def patch_id_from_hash(rpc, project, hash):
307 patch = rpc.patch_get_by_project_hash(project, hash)
308 except xmlrpclib.Fault:
309 # the server may not have the newer patch_get_by_project_hash function,
310 # so fall back to hash-only.
311 patch = rpc.patch_get_by_hash(hash)
314 sys.stderr.write("No patch has the hash provided\n")
317 patch_id = patch['id']
320 patch_id = int(patch_id)
322 sys.stderr.write("Invalid patch ID obtained from server\n")
326 auth_actions = ['update']
328 # unfortunately we currently have to revert to this ugly hack..
329 class _RecursiveHelpAction(argparse._HelpAction):
331 def __call__(self, parser, namespace, values, option_string=None):
335 subparsers_actions = [
336 action for action in parser._actions
337 if isinstance(action, argparse._SubParsersAction)
339 hash_n_id_actions = set(['hash', 'id', 'help'])
340 for subparsers_action in subparsers_actions:
341 for choice, subparser in subparsers_action.choices.items():
342 # gross but the whole thing is..
343 if (len(subparser._actions) == 3 \
344 and set([a.dest for a in subparser._actions]) \
345 == hash_n_id_actions) \
346 or len(subparser._actions) == 0:
348 print("command '{}'".format(choice))
349 print(subparser.format_help())
354 hash_parser = argparse.ArgumentParser(add_help=False, version=False)
355 hash_parser.add_argument(
356 '-h', metavar='HASH', dest='hash', action='store',
357 help='''Lookup by patch hash'''
359 hash_parser.add_argument(
360 'id', metavar='ID', nargs='*', action='store', type=int,
363 hash_parser.add_argument(
364 '-p', metavar='PROJECT',
365 help='''Lookup patch in project'''
368 filter_parser = argparse.ArgumentParser(add_help=False, version=False)
369 filter_parser.add_argument(
370 '-s', metavar='STATE',
371 help='''Filter by patch state (e.g., 'New', 'Accepted', etc.)'''
373 filter_parser.add_argument(
374 '-p', metavar='PROJECT',
375 help='''Filter by project name (see 'projects' for list)'''
377 filter_parser.add_argument(
379 help='''Filter by submitter (name, e-mail substring search)'''
381 filter_parser.add_argument(
383 help='''Filter by delegate (name, e-mail substring search)'''
385 filter_parser.add_argument(
386 '-n', metavar='MAX#',
388 help='''Restrict number of results'''
390 filter_parser.add_argument(
391 '-m', metavar='MESSAGEID',
392 help='''Filter by Message-Id'''
394 filter_parser.add_argument(
395 '-f', metavar='FORMAT',
396 help='''Print output in the given format. You can use tags matching '''
397 '''fields, e.g. %%{id}, %%{state}, or %%{msgid}.'''
399 filter_parser.add_argument(
400 'patch_name', metavar='STR', nargs='?',
401 help='substring to search for patches by name',
403 help_parser = argparse.ArgumentParser(add_help=False, version=False)
404 help_parser.add_argument(
405 '--help', action='help', help=argparse.SUPPRESS,
406 #help='''show this help message and exit'''
409 action_parser = argparse.ArgumentParser(
413 formatter_class=argparse.RawDescriptionHelpFormatter,
414 epilog='''(apply | get | info | view | update) (-h HASH | ID [ID ...])''',
416 action_parser.add_argument(
419 action=_RecursiveHelpAction,
420 help='''Print this help text'''
423 subparsers = action_parser.add_subparsers(
427 apply_parser = subparsers.add_parser(
428 'apply', parents=[hash_parser, help_parser],
430 help='''Apply a patch (in the current dir, using -p1)'''
432 apply_parser.set_defaults(subcmd='apply')
433 git_am_parser = subparsers.add_parser(
434 'git-am', parents=[hash_parser, help_parser],
436 help='''Apply a patch to current git branch using "git am".'''
438 git_am_parser.set_defaults(subcmd='git_am')
439 git_am_parser.add_argument(
442 help='''pass --signoff to git-am'''
444 get_parser = subparsers.add_parser(
445 'get', parents=[hash_parser, help_parser],
447 help='''Download a patch and save it locally'''
449 get_parser.set_defaults(subcmd='get')
450 info_parser = subparsers.add_parser(
451 'info', parents=[hash_parser, help_parser],
453 help='''Display patchwork info about a given patch ID'''
455 info_parser.set_defaults(subcmd='info')
456 projects_parser = subparsers.add_parser(
459 help='''List all projects'''
461 projects_parser.set_defaults(subcmd='projects')
462 states_parser = subparsers.add_parser(
465 help='''Show list of potential patch states'''
467 states_parser.set_defaults(subcmd='states')
468 view_parser = subparsers.add_parser(
469 'view', parents=[hash_parser, help_parser],
471 help='''View a patch'''
473 view_parser.set_defaults(subcmd='view')
474 update_parser = subparsers.add_parser(
475 'update', parents=[hash_parser, help_parser],
477 help='''Update patch''',
478 epilog='''Using a COMMIT-REF allows for only one ID to be specified''',
480 update_parser.add_argument(
481 '-c', metavar='COMMIT-REF',
482 help='''commit reference hash'''
484 update_parser.add_argument(
485 '-s', metavar='STATE',
487 help='''Set patch state (e.g., 'Accepted', 'Superseded' etc.)'''
489 update_parser.set_defaults(subcmd='update')
490 list_parser = subparsers.add_parser("list",
493 parents=[filter_parser, help_parser],
494 help='''List patches, using the optional filters specified
495 below and an optional substring to search for patches
498 list_parser.set_defaults(subcmd='list')
499 search_parser = subparsers.add_parser("search",
501 parents=[filter_parser, help_parser],
502 help='''Alias for "list"'''
504 # Poor man's argparse aliases:
505 # We register the "search" parser but effectively use "list" for the
507 search_parser.set_defaults(subcmd='list')
508 if len(sys.argv) < 2:
509 action_parser.print_help()
512 args = action_parser.parse_args()
513 args = dict(vars(args))
514 action = args.get('subcmd')
516 if args.get('hash') and len(args.get('id')):
517 # mimic mutual exclusive group
518 sys.stderr.write("Error: [-h HASH] and [ID [ID ...]] " +
519 "are mutually exlusive\n")
520 locals()[action + '_parser'].print_help()
528 state_str = args.get('s')
529 project_str = args.get('p')
530 submitter_str = args.get('w')
531 delegate_str = args.get('d')
532 format_str = args.get('f')
533 hash_str = args.get('hash')
534 patch_ids = args.get('id')
535 msgid_str = args.get('m')
537 # update multiple IDs with a single commit-hash does not make sense
538 if action == 'update' and patch_ids and len(patch_ids) > 1:
540 "Declining update with COMMIT-REF on multiple IDs\n"
542 update_parser.print_help()
544 commit_str = args.get('c')
546 if args.get('n') != None:
548 filt.add("max_count", args.get('n'))
550 sys.stderr.write("Invalid maximum count '%s'\n" % args.get('n'))
551 action_parser.print_help()
554 do_signoff = args.get('signoff')
556 # grab settings from config files
557 config = ConfigParser.ConfigParser()
558 config.read([CONFIG_FILE])
560 if not config.has_section('options'):
561 sys.stderr.write('~/.pwclientrc is in the old format. Migrating it...')
563 old_project = config.get('base','project')
565 new_config = ConfigParser.ConfigParser()
566 new_config.add_section('options')
568 new_config.set('options','default',old_project)
569 new_config.add_section(old_project)
571 new_config.set(old_project,'url',config.get('base','url'))
572 if config.has_option('auth', 'username'):
573 new_config.set(old_project,'username',config.get('auth','username'))
574 if config.has_option('auth', 'password'):
575 new_config.set(old_project,'password',config.get('auth','password'))
577 old_config_file = CONFIG_FILE + '.orig'
578 shutil.copy2(CONFIG_FILE,old_config_file)
580 with open(CONFIG_FILE, 'wb') as fd:
583 sys.stderr.write(' Done.\n')
584 sys.stderr.write('Your old ~/.pwclientrc was saved to %s\n' % old_config_file)
585 sys.stderr.write('and was converted to the new format. You may want to\n')
586 sys.stderr.write('inspect it before continuing.\n')
591 project_str = config.get('options', 'default')
593 sys.stderr.write("No default project configured in ~/.pwclientrc\n")
594 action_parser.print_help()
597 if not config.has_section(project_str):
598 sys.stderr.write("No section for project %s\n" % project_str)
600 if not config.has_option(project_str, 'url'):
601 sys.stderr.write("No URL for project %s\n" % project_str)
603 if not do_signoff and config.has_option('options', 'signoff'):
604 do_signoff = config.getboolean('options', 'signoff')
605 if not do_signoff and config.has_option(project_str, 'signoff'):
606 do_signoff = config.getboolean(project_str, 'signoff')
608 url = config.get(project_str, 'url')
610 (username, password) = (None, None)
612 if action in auth_actions:
613 if config.has_option(project_str, 'username') and \
614 config.has_option(project_str, 'password'):
616 use_https = url.startswith('https')
618 transport = BasicHTTPAuthTransport( \
619 config.get(project_str, 'username'),
620 config.get(project_str, 'password'),
624 sys.stderr.write(("The %s action requires authentication, "
625 "but no username or password\nis configured\n") % action)
629 filt.add("project", project_str)
632 filt.add("state", state_str)
635 filt.add("msgid", msgid_str)
638 rpc = xmlrpclib.Server(url, transport = transport)
640 sys.stderr.write("Unable to connect to %s\n" % url)
643 # It should be safe to assume hash_str is not zero, but who knows..
645 patch_ids = [patch_id_from_hash(rpc, project_str, hash_str)]
647 # helper for non_empty() to print correct helptext
648 h = locals()[action + '_parser']
650 # Require either hash_str or IDs for
651 def non_empty(h, patch_ids):
652 """Error out if no patch IDs were specified"""
653 if patch_ids == None or len(patch_ids) < 1:
654 sys.stderr.write("Error: Missing Argument! " +
655 "Either [-h HASH] or [ID [ID ...]] are required\n")
661 if action == 'list' or action == 'search':
662 if args.get('patch_name') != None:
663 filt.add("name__icontains", args.get('patch_name'))
664 action_list(rpc, filt, submitter_str, delegate_str, format_str)
666 elif action.startswith('project'):
669 elif action.startswith('state'):
672 elif action == 'view':
673 for patch_id in non_empty(h, patch_ids):
674 s = rpc.patch_get_mbox(patch_id)
676 print unicode(s).encode("utf-8")
678 elif action == 'info':
679 for patch_id in non_empty(h, patch_ids):
680 action_info(rpc, patch_id)
682 elif action == 'get':
683 for patch_id in non_empty(h, patch_ids):
684 action_get(rpc, patch_id)
686 elif action == 'apply':
687 for patch_id in non_empty(h, patch_ids):
688 ret = action_apply(rpc, patch_id)
690 sys.stderr.write("Apply failed with exit status %d\n" % ret)
693 elif action == 'git_am':
697 for patch_id in non_empty(h, patch_ids):
698 ret = action_apply(rpc, patch_id, cmd)
700 sys.stderr.write("'git am' failed with exit status %d\n" % ret)
703 elif action == 'update':
704 for patch_id in non_empty(h, patch_ids):
705 action_update_patch(rpc, patch_id, state = state_str,
710 sys.stderr.write("Unknown action '%s'\n" % action)
711 action_parser.print_help()
714 if __name__ == "__main__":