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
36 from email
.mime
.text
import MIMEText
38 notify_on_state_change
= {
39 'accepted': 'emacs-orgmode@gnu.org',
40 'rejected': 'emacs-orgmode@gnu.org',
41 'changes requested': 'emacs-orgmode@gnu.org',
42 'rfc': 'emacs-orgmode@gnu.org'
45 # Default Patchwork remote XML-RPC server URL
46 # This script will check the PW_XMLRPC_URL environment variable
47 # for the URL to access. If that is unspecified, it will fallback to
48 # the hardcoded default value specified here.
49 DEFAULT_URL
= "http://patchwork.newartisans.com/xmlrpc/"
50 CONFIG_FILES
= [os
.path
.expanduser('~/.pwclientrc')]
53 """Filter for selecting patches."""
55 # These fields refer to specific objects, so they are special
56 # because we have to resolve them to IDs before passing the
57 # filter to the server
61 # The dictionary that gets passed to via XML-RPC
64 def add(self
, field
, value
):
67 elif field
== 'project':
73 def resolve_ids(self
, rpc
):
74 """Resolve State, Project, and Person IDs based on filter strings."""
76 id = state_id_by_name(rpc
, self
.state
)
78 sys
.stderr
.write("Note: No State found matching %s*, " \
79 "ignoring filter\n" % self
.state
)
81 self
.d
['state_id'] = id
83 if self
.project
!= "":
84 id = project_id_by_name(rpc
, self
.project
)
86 sys
.stderr
.write("Note: No Project found matching %s, " \
87 "ignoring filter\n" % self
.project
)
89 self
.d
['project_id'] = id
92 """Return human-readable description of the filter."""
95 class BasicHTTPAuthTransport(xmlrpclib
.SafeTransport
):
97 def __init__(self
, username
= None, password
= None, use_https
= False):
98 self
.username
= username
99 self
.password
= password
100 self
.use_https
= use_https
101 xmlrpclib
.SafeTransport
.__init
__(self
)
103 def authenticated(self
):
104 return self
.username
!= None and self
.password
!= None
106 def send_host(self
, connection
, host
):
107 xmlrpclib
.Transport
.send_host(self
, connection
, host
)
108 if not self
.authenticated():
110 credentials
= '%s:%s' % (self
.username
, self
.password
)
111 auth
= 'Basic ' + base64
.encodestring(credentials
).strip()
112 connection
.putheader('Authorization', auth
)
114 def make_connection(self
, host
):
116 fn
= xmlrpclib
.SafeTransport
.make_connection
118 fn
= xmlrpclib
.Transport
.make_connection
119 return fn(self
, host
)
122 sys
.stderr
.write("Usage: %s <action> [options]\n\n" % \
123 (os
.path
.basename(sys
.argv
[0])))
124 sys
.stderr
.write("Where <action> is one of:\n")
126 """ apply <ID> : Apply a patch (in the current dir, using -p1)
127 get <ID> : Download a patch and save it locally
128 projects : List all projects
129 states : Show list of potential patch states
130 list [str] : List patches, using the optional filters specified
131 below and an optional substring to search for patches
133 search [str] : Same as 'list'
134 view <ID> : View a patch
135 show <ID> : Same as view
136 update [-s state] [-c commit-ref] [-m email-comment] <ID>
137 : Update patch, send mail to mailing list if appropriate
138 branch <ID> : Create a topic branch t/patchNNN for this patch
139 : and update it to the current master
140 merge [-m email-comment] <ID>
141 : Merge the t/patchNNN topic branch into master
143 sys
.stderr
.write("""\nFilter options for 'list' and 'search':
144 -s <state> : Filter by patch state (e.g., 'New', 'Accepted', etc.)
145 -p <project> : Filter by project name (see 'projects' for list)
146 -w <who> : Filter by submitter (name, e-mail substring search)
147 -d <who> : Filter by delegate (name, e-mail substring search)
148 -n <max #> : Restrict number of results\n""")
149 sys
.stderr
.write("""\nActions that take an ID argument can also be \
151 -h <hash> : Lookup by patch hash\n""")
154 def project_id_by_name(rpc
, linkname
):
155 """Given a project short name, look up the Project ID."""
156 if len(linkname
) == 0:
158 projects
= rpc
.project_list(linkname
, 0)
159 for project
in projects
:
160 if project
['linkname'] == linkname
:
164 def state_id_by_name(rpc
, name
):
165 """Given a partial state name, look up the state ID."""
168 states
= rpc
.state_list(name
, 0)
170 if state
['name'].lower().startswith(name
.lower()):
174 def person_ids_by_name(rpc
, name
):
175 """Given a partial name or email address, return a list of the
176 person IDs that match."""
179 people
= rpc
.person_list(name
, 0)
180 return map(lambda x
: x
['id'], people
)
182 def list_patches(patches
):
183 """Dump a list of patches to stdout."""
184 print("%-5s %-12s %s" % ("ID", "State", "Name"))
185 print("%-5s %-12s %s" % ("--", "-----", "----"))
186 for patch
in patches
:
187 print("%-5d %-12s %s" % (patch
['id'], patch
['state'], patch
['name']))
189 def action_list(rpc
, filter, submitter_str
, delegate_str
):
190 filter.resolve_ids(rpc
)
192 if submitter_str
!= "":
193 ids
= person_ids_by_name(rpc
, submitter_str
)
195 sys
.stderr
.write("Note: Nobody found matching *%s*\n", \
199 person
= rpc
.person_get(id)
200 print "Patches submitted by %s <%s>:" % \
201 (person
['name'], person
['email'])
203 f
.add("submitter_id", id)
204 patches
= rpc
.patch_list(f
.d
)
205 list_patches(patches
)
208 if delegate_str
!= "":
209 ids
= person_ids_by_name(rpc
, delegate_str
)
211 sys
.stderr
.write("Note: Nobody found matching *%s*\n", \
215 person
= rpc
.person_get(id)
216 print "Patches delegated to %s <%s>:" % \
217 (person
['name'], person
['email'])
219 f
.add("delegate_id", id)
220 patches
= rpc
.patch_list(f
.d
)
221 list_patches(patches
)
224 patches
= rpc
.patch_list(filter.d
)
225 list_patches(patches
)
227 def action_projects(rpc
):
228 projects
= rpc
.project_list("", 0)
229 print("%-5s %-24s %s" % ("ID", "Name", "Description"))
230 print("%-5s %-24s %s" % ("--", "----", "-----------"))
231 for project
in projects
:
232 print("%-5d %-24s %s" % (project
['id'], \
233 project
['linkname'], \
236 def action_states(rpc
):
237 states
= rpc
.state_list("", 0)
238 print("%-5s %s" % ("ID", "Name"))
239 print("%-5s %s" % ("--", "----"))
241 print("%-5d %s" % (state
['id'], state
['name']))
243 def action_get(rpc
, patch_id
):
244 patch
= rpc
.patch_get(patch_id
)
245 s
= rpc
.patch_get_mbox(patch_id
)
247 if patch
== {} or len(s
) == 0:
248 sys
.stderr
.write("Unable to get patch %d\n" % patch_id
)
251 base_fname
= fname
= os
.path
.basename(patch
['filename'])
253 while os
.path
.exists(fname
):
254 fname
= "%s.%d" % (base_fname
, i
)
260 sys
.stderr
.write("Unable to open %s for writing\n" % fname
)
264 f
.write(unicode(s
).encode("utf-8"))
266 print "Saved patch to %s" % fname
268 sys
.stderr
.write("Failed to write to %s\n" % fname
)
271 def action_apply(rpc
, patch_id
):
272 patch
= rpc
.patch_get(patch_id
)
274 sys
.stderr
.write("Error getting information on patch ID %d\n" % \
277 print "Applying patch #%d to current directory" % patch_id
278 print "Description: %s" % patch
['name']
279 s
= rpc
.patch_get_mbox(patch_id
)
281 proc
= subprocess
.Popen(['patch', '-p1'], stdin
= subprocess
.PIPE
)
284 sys
.stderr
.write("Error: No patch content found\n")
287 def action_update_patch(rpc
, patch_id
, state
= None, commit
= None,
288 delegate_str
= "", comment_str
= "No comment", archived
= False):
289 patch
= rpc
.patch_get(patch_id
)
291 sys
.stderr
.write("Error getting information on patch ID %d\n" % \
298 if delegate_str
!= "":
299 params
['delegate'] = delegate_str
300 ids
= person_ids_by_name(rpc
, delegate_str
)
302 sys
.stderr
.write("Note: Nobody found matching *%s*\n"% \
308 state_id
= state_id_by_name(rpc
, state
)
310 sys
.stderr
.write("Error: No State found matching %s*\n" % state
)
312 params
['state'] = state_id
314 if state
.lower() in notify_on_state_change
:
316 sys
.stderr
.write("Error: Delete (-d) required for this update\n")
319 person
= rpc
.person_get(delegate_id
)
320 submitter
= rpc
.person_get(patch
['submitter_id'])
322 from_addr
= '%s <%s>' % (person
['name'], person
['email'])
323 cc_addr
= '%s <%s>' % (submitter
['name'], submitter
['email'])
324 to_addr
= notify_on_state_change
[state
.lower()]
326 orig_mail
= rpc
.patch_get_mbox(patch_id
)
327 orig_mail
= '> ' + orig_mail
.replace('\n','\n> ')
329 longdesc
= '''Patch %s (http://patchwork.newartisans.com/patch/%s/) is now "%s".
331 Maintaner comment: %s
333 This relates to the following submission:
335 http://mid.gmane.org/%s
337 Here is the original message containing the patch:
340 (patch
['id'], patch
['id'], state
, comment_str
, urllib
.quote(patch
['msgid']), orig_mail
)
341 shortdesc
= '[%s] %s' % (state
, patch
['name'])
343 msg
= MIMEText(longdesc
)
345 msg
['Subject'] = shortdesc
346 msg
['From'] = from_addr
349 msg
['References'] = patch
['msgid']
351 # Send the message via our own SMTP server, but don't include
352 # the envelope header.
354 s
= smtplib
.SMTP('localhost')
355 print "Sending e-mail to: %s, %s" % (to_addr
, cc_addr
)
356 s
.sendmail(from_addr
, [to_addr
, cc_addr
], msg
.as_string())
359 sys
.stderr
.write("Warning: Failed to send e-mail " +
360 "(no SMTP server listening at localhost?)\n")
363 params
['commit_ref'] = commit
366 params
['archived'] = archived
370 print "Updating patch %d to state '%s', delegate %s" % \
371 (patch_id
, state
, delegate_str
)
372 success
= rpc
.patch_set(patch_id
, params
)
373 except xmlrpclib
.Fault
, f
:
374 sys
.stderr
.write("Error updating patch: %s\n" % f
.faultString
)
377 sys
.stderr
.write("Patch not updated\n")
379 def patch_id_from_hash(rpc
, project
, hash):
381 patch
= rpc
.patch_get_by_project_hash(project
, hash)
382 except xmlrpclib
.Fault
:
383 # the server may not have the newer patch_get_by_project_hash function,
384 # so fall back to hash-only.
385 patch
= rpc
.patch_get_by_hash(hash)
392 def branch_with(patch_id
, rpc
, delegate_str
):
393 s
= rpc
.patch_get_mbox(patch_id
)
395 print unicode(s
).encode("utf-8")
397 patch
= rpc
.patch_get(patch_id
)
399 # Find the latest commit from the day before the patch
400 proc
= subprocess
.Popen(['git', 'log', '--until=' + patch
['date'],
401 '-1', '--format=%H', 'master'],
402 stdout
= subprocess
.PIPE
)
403 sha
= proc
.stdout
.read()[:-1]
405 # Create a topic branch named after this commit
406 proc
= subprocess
.Popen(['git', 'checkout', '-b', 't/patch%s' %
408 sts
= os
.waitpid(proc
.pid
, 0)
410 sys
.stderr
.write("Could not create branch for patch\n")
413 # Apply the patch to the branch
418 sys
.stderr
.write("Unable to open %s for writing\n" % fname
)
422 f
.write(unicode(s
).encode("utf-8"))
424 print "Saved patch to %s" % fname
426 sys
.stderr
.write("Failed to write to %s\n" % fname
)
429 proc
= subprocess
.Popen(['git', 'am', '/tmp/patch'])
430 sts
= os
.waitpid(proc
.pid
, 0)
432 sys
.stderr
.write("Failed to apply patch to branch\n")
433 proc
= subprocess
.Popen(['git', 'checkout', 'master'])
434 os
.waitpid(proc
.pid
, 0)
435 proc
= subprocess
.Popen(['git', 'branch', '-D', 't/patch%s' %
437 os
.waitpid(proc
.pid
, 0)
440 # If it succeeded this far, mark the patch as "Under Review" by the
442 action_update_patch(rpc
, patch_id
, state
= 'Under Review',
443 delegate_str
= delegate_str
)
445 proc
= subprocess
.Popen(['git', 'rebase', 'master'])
446 sts
= os
.waitpid(proc
.pid
, 0)
450 def merge_with(patch_id
, rpc
, delegate_str
, comment_str
):
451 s
= rpc
.patch_get_mbox(patch_id
)
453 print unicode(s
).encode("utf-8")
455 proc
= subprocess
.Popen(['git', 'checkout', 'master'])
456 sts
= os
.waitpid(proc
.pid
, 0)
458 sys
.stderr
.write("Failed to checkout master branch\n")
461 proc
= subprocess
.Popen(['git', 'merge', '--ff', 't/patch%s' % patch_id
])
462 sts
= os
.waitpid(proc
.pid
, 0)
464 sys
.stderr
.write("Failed to merge t/patch%s into master\n" % patch_id
)
467 proc
= subprocess
.Popen(['git', 'rev-parse', 'master'],
468 stdout
= subprocess
.PIPE
)
469 sha
= proc
.stdout
.read()[:-1]
471 # If it succeeded this far, mark the patch as "Accepted" by the invoking
473 action_update_patch(rpc
, patch_id
, state
= 'Accepted', commit
= sha
,
474 delegate_str
= delegate_str
, archived
= True)
478 auth_actions
= ['update', 'branch', 'merge']
482 opts
, args
= getopt
.getopt(sys
.argv
[2:], 's:p:w:d:n:c:h:m:')
483 except getopt
.GetoptError
, err
:
487 if len(sys
.argv
) < 2:
490 action
= sys
.argv
[1].lower()
502 config
= ConfigParser
.ConfigParser()
503 config
.read(CONFIG_FILES
)
505 # grab settings from config files
506 if config
.has_option('base', 'url'):
507 url
= config
.get('base', 'url')
509 if config
.has_option('base', 'project'):
510 project_str
= config
.get('base', 'project')
512 for name
, value
in opts
:
518 submitter_str
= value
529 filt
.add("max_count", int(value
))
531 sys
.stderr
.write("Invalid maximum count '%s'\n" % value
)
534 sys
.stderr
.write("Unknown option '%s'\n" % name
)
538 sys
.stderr
.write("Too many arguments specified\n")
541 (username
, password
) = (None, None)
543 if action
in auth_actions
:
544 if config
.has_option('auth', 'username') and \
545 config
.has_option('auth', 'password'):
547 use_https
= url
.startswith('https')
549 transport
= BasicHTTPAuthTransport( \
550 config
.get('auth', 'username'),
551 config
.get('auth', 'password'),
555 sys
.stderr
.write(("The %s action requires authentication, "
556 "but no username or password\nis configured\n") % action
)
560 filt
.add("project", project_str
)
563 filt
.add("state", state_str
)
566 rpc
= xmlrpclib
.Server(url
, transport
= transport
)
568 sys
.stderr
.write("Unable to connect to %s\n" % url
)
573 patch_id
= patch_id_from_hash(rpc
, project_str
, hash_str
)
575 sys
.stderr
.write("No patch has the hash provided\n")
579 if action
== 'list' or action
== 'search':
581 filt
.add("name__icontains", args
[0])
582 action_list(rpc
, filt
, submitter_str
, delegate_str
)
584 elif action
.startswith('project'):
587 elif action
.startswith('state'):
590 elif action
== 'branch':
592 patch_id
= patch_id
or int(args
[0])
594 sys
.stderr
.write("Invalid patch ID given\n")
597 branch_with(patch_id
, rpc
, config
.get('auth', 'username'))
599 elif action
== 'merge':
601 patch_id
= patch_id
or int(args
[0])
603 sys
.stderr
.write("Invalid patch ID given\n")
606 merge_with(patch_id
, rpc
, config
.get('auth', 'username'), comment_str
)
608 elif action
== 'test':
610 patch_id
= patch_id
or int(args
[0])
611 patch
= rpc
.patch_get(patch_id
)
612 s
= rpc
.patch_get_mbox(patch_id
)
614 print "xxx %s xxx" % patch
['name']
615 print "xxx %s xxx" % rpc
.patch_get_mbox(patch_id
)
617 elif action
== 'view' or action
== 'show':
619 patch_id
= patch_id
or int(args
[0])
621 sys
.stderr
.write("Invalid patch ID given\n")
624 s
= rpc
.patch_get_mbox(patch_id
)
626 print unicode(s
).encode("utf-8")
628 elif action
== 'get' or action
== 'save':
630 patch_id
= patch_id
or int(args
[0])
632 sys
.stderr
.write("Invalid patch ID given\n")
635 action_get(rpc
, patch_id
)
637 elif action
== 'apply':
639 patch_id
= patch_id
or int(args
[0])
641 sys
.stderr
.write("Invalid patch ID given\n")
644 action_apply(rpc
, patch_id
)
646 elif action
== 'update':
648 patch_id
= patch_id
or int(args
[0])
650 sys
.stderr
.write("Invalid patch ID given\n")
653 action_update_patch(rpc
, patch_id
, state
= state_str
,
654 commit
= commit_str
, delegate_str
= delegate_str
, comment_str
= comment_str
)
657 sys
.stderr
.write("Unknown action '%s'\n" % action
)
660 if __name__
== "__main__":