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
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, " \
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" % \
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" % \
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)
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)
-def action_update_patch(rpc, patch_id, state = None, commit = None):
+def action_update_patch(rpc, patch_id, state = None, archived = None, commit = None):
patch = rpc.patch_get(patch_id)
if patch == {}:
sys.stderr.write("Error getting information on patch ID %d\n" % \
if commit:
params['commit_ref'] = commit
+ if archived:
+ params['archived'] = archived == 'yes'
+
success = False
try:
success = rpc.patch_set(patch_id, params)
'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(
'-s', metavar='STATE',
help='''Filter by patch state (e.g., 'New', 'Accepted', etc.)'''
)
+ filter_parser.add_argument(
+ '-a', choices=['yes','no'],
+ help='''Filter by patch archived state'''
+ )
filter_parser.add_argument(
'-p', metavar='PROJECT',
help='''Filter by project name (see 'projects' for list)'''
'-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',
required=True,
help='''Set patch state (e.g., 'Accepted', 'Superseded' etc.)'''
)
+ update_parser.add_argument(
+ '-a', choices=['yes', 'no'],
+ help='''Set patch archived state'''
+ )
update_parser.set_defaults(subcmd='update')
list_parser = subparsers.add_parser("list",
add_help=False,
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')):
# 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')
+ archived_str = args.get('a')
+ 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:
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'))
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)
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 \
if state_str:
filt.add("state", state_str)
+ if archived_str:
+ filt.add("archived", archived_str == 'yes')
+
if msgid_str:
filt.add("msgid", msgid_str)
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)
action_states(rpc)
elif action == 'view':
+ pager = os.environ.get('PAGER')
+ if pager:
+ pager = subprocess.Popen(
+ pager.split(), stdin=subprocess.PIPE
+ )
+ if pager:
+ i = list()
+ for patch_id in non_empty(h, patch_ids):
+ s = rpc.patch_get_mbox(patch_id)
+ if len(s) > 0:
+ i.append(unicode(s).encode("utf-8"))
+ if len(i) > 0:
+ pager.communicate(input="\n".join(i))
+ pager.stdin.close()
+ else:
+ for patch_id in non_empty(h, patch_ids):
+ s = rpc.patch_get_mbox(patch_id)
+ if len(s) > 0:
+ print unicode(s).encode("utf-8")
+
+ elif action == 'info':
for patch_id in non_empty(h, patch_ids):
- s = rpc.patch_get_mbox(patch_id)
- if len(s) > 0:
- print unicode(s).encode("utf-8")
+ action_info(rpc, patch_id)
- 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 == '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,
- commit = commit_str
- ) for patch_id in non_empty(h, patch_ids)]
+ for patch_id in non_empty(h, patch_ids):
+ action_update_patch(rpc, patch_id, state = state_str,
+ archived = archived_str, commit = commit_str
+ )
else:
sys.stderr.write("Unknown action '%s'\n" % action)