]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/bin/pwclient
pwclient: accept more than one project in ~/.pwclientrc
[patchwork] / apps / patchwork / bin / pwclient
1 #!/usr/bin/env python
2 #
3 # Patchwork command line client
4 # Copyright (C) 2008 Nate Case <ncase@xes-inc.com>
5 #
6 # This file is part of the Patchwork package.
7 #
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.
12 #
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.
17 #
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
21
22 import os
23 import sys
24 import xmlrpclib
25 import getopt
26 import string
27 import tempfile
28 import subprocess
29 import base64
30 import ConfigParser
31 import shutil
32
33 # Default Patchwork remote XML-RPC server URL
34 # This script will check the PW_XMLRPC_URL environment variable
35 # for the URL to access.  If that is unspecified, it will fallback to
36 # the hardcoded default value specified here.
37 DEFAULT_URL = "http://patchwork/xmlrpc/"
38 CONFIG_FILE = os.path.expanduser('~/.pwclientrc')
39
40 class Filter:
41     """Filter for selecting patches."""
42     def __init__(self):
43         # These fields refer to specific objects, so they are special
44         # because we have to resolve them to IDs before passing the
45         # filter to the server
46         self.state = ""
47         self.project = ""
48
49         # The dictionary that gets passed to via XML-RPC
50         self.d = {}
51
52     def add(self, field, value):
53         if field == 'state':
54             self.state = value
55         elif field == 'project':
56             self.project = value
57         else:
58             # OK to add directly
59             self.d[field] = value
60
61     def resolve_ids(self, rpc):
62         """Resolve State, Project, and Person IDs based on filter strings."""
63         if self.state != "":
64             id = state_id_by_name(rpc, self.state)
65             if id == 0:
66                 sys.stderr.write("Note: No State found matching %s*, " \
67                                  "ignoring filter\n" % self.state)
68             else:
69                 self.d['state_id'] = id
70
71         if self.project != "":
72             id = project_id_by_name(rpc, self.project)
73             if id == 0:
74                 sys.stderr.write("Note: No Project found matching %s, " \
75                                  "ignoring filter\n" % self.project)
76             else:
77                 self.d['project_id'] = id
78
79     def __str__(self):
80         """Return human-readable description of the filter."""
81         return str(self.d)
82
83 class BasicHTTPAuthTransport(xmlrpclib.SafeTransport):
84
85     def __init__(self, username = None, password = None, use_https = False):
86         self.username = username
87         self.password = password
88         self.use_https = use_https
89         xmlrpclib.SafeTransport.__init__(self)
90
91     def authenticated(self):
92         return self.username != None and self.password != None
93
94     def send_host(self, connection, host):
95         xmlrpclib.Transport.send_host(self, connection, host)
96         if not self.authenticated():
97             return
98         credentials = '%s:%s' % (self.username, self.password)
99         auth = 'Basic ' + base64.encodestring(credentials).strip()
100         connection.putheader('Authorization', auth)
101
102     def make_connection(self, host):
103         if self.use_https:
104             fn = xmlrpclib.SafeTransport.make_connection
105         else:
106             fn = xmlrpclib.Transport.make_connection
107         return fn(self, host)
108
109 def usage():
110     sys.stderr.write("Usage: %s <action> [options]\n\n" % \
111                         (os.path.basename(sys.argv[0])))
112     sys.stderr.write("Where <action> is one of:\n")
113     sys.stderr.write(
114 """        apply <ID>    : Apply a patch (in the current dir, using -p1)
115         git-am <ID>   : Apply a patch to current git branch using "git am"
116         get <ID>      : Download a patch and save it locally
117         info <ID>     : Display patchwork info about a given patch ID
118         projects      : List all projects
119         states        : Show list of potential patch states
120         list [str]    : List patches, using the optional filters specified
121                         below and an optional substring to search for patches
122                         by name
123         search [str]  : Same as 'list'
124         view <ID>     : View a patch
125         update [-s state] [-c commit-ref] <ID>
126                       : Update patch\n""")
127     sys.stderr.write("""\nFilter options for 'list' and 'search':
128         -s <state>    : Filter by patch state (e.g., 'New', 'Accepted', etc.)
129         -p <project>  : Filter by project name (see 'projects' for list)
130         -w <who>      : Filter by submitter (name, e-mail substring search)
131         -d <who>      : Filter by delegate (name, e-mail substring search)
132         -n <max #>    : Restrict number of results
133         -m <messageid>: Filter by Message-Id\n""")
134     sys.stderr.write("""\nActions that take an ID argument can also be \
135 invoked with:
136         -h <hash>     : Lookup by patch hash\n""")
137     sys.exit(1)
138
139 def project_id_by_name(rpc, linkname):
140     """Given a project short name, look up the Project ID."""
141     if len(linkname) == 0:
142         return 0
143     projects = rpc.project_list(linkname, 0)
144     for project in projects:
145         if project['linkname'] == linkname:
146             return project['id']
147     return 0
148
149 def state_id_by_name(rpc, name):
150     """Given a partial state name, look up the state ID."""
151     if len(name) == 0:
152         return 0
153     states = rpc.state_list(name, 0)
154     for state in states:
155         if state['name'].lower().startswith(name.lower()):
156             return state['id']
157     return 0
158
159 def person_ids_by_name(rpc, name):
160     """Given a partial name or email address, return a list of the
161     person IDs that match."""
162     if len(name) == 0:
163         return []
164     people = rpc.person_list(name, 0)
165     return map(lambda x: x['id'], people)
166
167 def list_patches(patches):
168     """Dump a list of patches to stdout."""
169     print("%-7s %-12s %s" % ("ID", "State", "Name"))
170     print("%-7s %-12s %s" % ("--", "-----", "----"))
171     for patch in patches:
172         print("%-7d %-12s %s" % (patch['id'], patch['state'], patch['name']))
173
174 def action_list(rpc, filter, submitter_str, delegate_str):
175     filter.resolve_ids(rpc)
176
177     if submitter_str != "":
178         ids = person_ids_by_name(rpc, submitter_str)
179         if len(ids) == 0:
180             sys.stderr.write("Note: Nobody found matching *%s*\n" % \
181                              submitter_str)
182         else:
183             for id in ids:
184                 person = rpc.person_get(id)
185                 print "Patches submitted by %s <%s>:" % \
186                         (unicode(person['name']).encode("utf-8"), \
187                          unicode(person['email']).encode("utf-8"))
188                 f = filter
189                 f.add("submitter_id", id)
190                 patches = rpc.patch_list(f.d)
191                 list_patches(patches)
192         return
193
194     if delegate_str != "":
195         ids = person_ids_by_name(rpc, delegate_str)
196         if len(ids) == 0:
197             sys.stderr.write("Note: Nobody found matching *%s*\n" % \
198                              delegate_str)
199         else:
200             for id in ids:
201                 person = rpc.person_get(id)
202                 print "Patches delegated to %s <%s>:" % \
203                         (person['name'], person['email'])
204                 f = filter
205                 f.add("delegate_id", id)
206                 patches = rpc.patch_list(f.d)
207                 list_patches(patches)
208         return
209
210     patches = rpc.patch_list(filter.d)
211     list_patches(patches)
212
213 def action_projects(rpc):
214     projects = rpc.project_list("", 0)
215     print("%-5s %-24s %s" % ("ID", "Name", "Description"))
216     print("%-5s %-24s %s" % ("--", "----", "-----------"))
217     for project in projects:
218         print("%-5d %-24s %s" % (project['id'], \
219                 project['linkname'], \
220                 project['name']))
221
222 def action_states(rpc):
223     states = rpc.state_list("", 0)
224     print("%-5s %s" % ("ID", "Name"))
225     print("%-5s %s" % ("--", "----"))
226     for state in states:
227         print("%-5d %s" % (state['id'], state['name']))
228
229 def action_info(rpc, patch_id):
230     patch = rpc.patch_get(patch_id)
231     s = "Information for patch id %d" % (patch_id)
232     print(s)
233     print('-' * len(s))
234     for key, value in sorted(patch.iteritems()):
235         print("- %- 14s: %s" % (key, unicode(value).encode("utf-8")))
236
237 def action_get(rpc, patch_id):
238     patch = rpc.patch_get(patch_id)
239     s = rpc.patch_get_mbox(patch_id)
240
241     if patch == {} or len(s) == 0:
242         sys.stderr.write("Unable to get patch %d\n" % patch_id)
243         sys.exit(1)
244
245     base_fname = fname = os.path.basename(patch['filename'])
246     i = 0
247     while os.path.exists(fname):
248         fname = "%s.%d" % (base_fname, i)
249         i += 1
250
251     try:
252         f = open(fname, "w")
253     except:
254         sys.stderr.write("Unable to open %s for writing\n" % fname)
255         sys.exit(1)
256
257     try:
258         f.write(unicode(s).encode("utf-8"))
259         f.close()
260         print "Saved patch to %s" % fname
261     except:
262         sys.stderr.write("Failed to write to %s\n" % fname)
263         sys.exit(1)
264
265 def action_apply(rpc, patch_id, apply_cmd=None):
266     patch = rpc.patch_get(patch_id)
267     if patch == {}:
268         sys.stderr.write("Error getting information on patch ID %d\n" % \
269                          patch_id)
270         sys.exit(1)
271
272     if apply_cmd is None:
273       print "Applying patch #%d to current directory" % patch_id
274       apply_cmd = ['patch', '-p1']
275     else:
276       print "Applying patch #%d using %s" % (
277           patch_id, repr(' '.join(apply_cmd)))
278
279     print "Description: %s" % patch['name']
280     s = rpc.patch_get_mbox(patch_id)
281     if len(s) > 0:
282         proc = subprocess.Popen(apply_cmd, stdin = subprocess.PIPE)
283         proc.communicate(unicode(s).encode('utf-8'))
284     else:
285         sys.stderr.write("Error: No patch content found\n")
286         sys.exit(1)
287
288 def action_update_patch(rpc, patch_id, state = None, commit = None):
289     patch = rpc.patch_get(patch_id)
290     if patch == {}:
291         sys.stderr.write("Error getting information on patch ID %d\n" % \
292                          patch_id)
293         sys.exit(1)
294
295     params = {}
296
297     if state:
298         state_id = state_id_by_name(rpc, state)
299         if state_id == 0:
300             sys.stderr.write("Error: No State found matching %s*\n" % state)
301             sys.exit(1)
302         params['state'] = state_id
303
304     if commit:
305         params['commit_ref'] = commit
306
307     success = False
308     try:
309         success = rpc.patch_set(patch_id, params)
310     except xmlrpclib.Fault, f:
311         sys.stderr.write("Error updating patch: %s\n" % f.faultString)
312
313     if not success:
314         sys.stderr.write("Patch not updated\n")
315
316 def patch_id_from_hash(rpc, project, hash):
317     try:
318         patch = rpc.patch_get_by_project_hash(project, hash)
319     except xmlrpclib.Fault:
320         # the server may not have the newer patch_get_by_project_hash function,
321         # so fall back to hash-only.
322         patch = rpc.patch_get_by_hash(hash)
323
324     if patch == {}:
325         return None
326
327     return patch['id']
328
329 auth_actions = ['update']
330
331 def main():
332     try:
333         opts, args = getopt.getopt(sys.argv[2:], 's:p:w:d:n:c:h:m:')
334     except getopt.GetoptError, err:
335         print str(err)
336         usage()
337
338     if len(sys.argv) < 2:
339         usage()
340
341     action = sys.argv[1].lower()
342
343     # set defaults
344     filt = Filter()
345     submitter_str = ""
346     delegate_str = ""
347     project_str = ""
348     commit_str = ""
349     state_str = ""
350     hash_str = ""
351     msgid_str = ""
352     url = DEFAULT_URL
353
354     for name, value in opts:
355         if name == '-s':
356             state_str = value
357         elif name == '-p':
358             project_str = value
359         elif name == '-w':
360             submitter_str = value
361         elif name == '-d':
362             delegate_str = value
363         elif name == '-c':
364             commit_str = value
365         elif name == '-h':
366             hash_str = value
367         elif name == '-m':
368             msgid_str = value
369         elif name == '-n':
370             try:
371                 filt.add("max_count", int(value))
372             except:
373                 sys.stderr.write("Invalid maximum count '%s'\n" % value)
374                 usage()
375         else:
376             sys.stderr.write("Unknown option '%s'\n" % name)
377             usage()
378
379     if len(args) > 1:
380         sys.stderr.write("Too many arguments specified\n")
381         usage()
382
383     # grab settings from config files
384     config = ConfigParser.ConfigParser()
385     config.read([CONFIG_FILE])
386
387     if not config.has_section('options'):
388         sys.stderr.write('~/.pwclientrc is in the old format. Migrating it...')
389
390         old_project = config.get('base','project')
391
392         new_config = ConfigParser.ConfigParser()
393         new_config.add_section('options')
394
395         new_config.set('options','default',old_project)
396         new_config.add_section(old_project)
397
398         new_config.set(old_project,'url',config.get('base','url'))
399         if config.has_option('auth', 'username'):
400             new_config.set(old_project,'username',config.get('auth','username'))
401         if config.has_option('auth', 'password'):
402             new_config.set(old_project,'password',config.get('auth','password'))
403
404         old_config_file = CONFIG_FILE + '.orig'
405         shutil.copy2(CONFIG_FILE,old_config_file)
406
407         with open(CONFIG_FILE, 'wb') as fd:
408             new_config.write(fd)
409
410         sys.stderr.write(' Done.\n')
411         sys.stderr.write('Your old ~/.pwclientrc was saved to %s\n' % old_config_file)
412         sys.stderr.write('and was converted to the new format. You may want to\n')
413         sys.stderr.write('inspect it before continuing.\n')
414         sys.exit(1)
415
416     if not project_str:
417         try:
418             project_str = config.get('options', 'default')
419         except:
420             sys.stderr.write("No default project configured in ~/.pwclientrc\n")
421             usage()
422
423     if not config.has_section(project_str):
424         sys.stderr.write("No section for project %s\n" % project_str)
425         sys.exit(1)
426
427     if not config.has_option(project_str, 'url'):
428         sys.stderr.write("No URL for project %s\n" % project_str)
429         sys.exit(1)
430
431     url = config.get(project_str, 'url')
432
433     (username, password) = (None, None)
434     transport = None
435     if action in auth_actions:
436         if config.has_option(project_str, 'username') and \
437                 config.has_option(project_str, 'password'):
438
439             use_https = url.startswith('https')
440
441             transport = BasicHTTPAuthTransport( \
442                     config.get(project_str, 'username'),
443                     config.get(project_str, 'password'),
444                     use_https)
445
446         else:
447             sys.stderr.write(("The %s action requires authentication, "
448                     "but no username or password\nis configured\n") % action)
449             sys.exit(1)
450
451     if project_str:
452         filt.add("project", project_str)
453
454     if state_str:
455         filt.add("state", state_str)
456
457     if msgid_str:
458         filt.add("msgid", msgid_str)
459
460     try:
461         rpc = xmlrpclib.Server(url, transport = transport)
462     except:
463         sys.stderr.write("Unable to connect to %s\n" % url)
464         sys.exit(1)
465
466     patch_id = None
467     if hash_str:
468         patch_id = patch_id_from_hash(rpc, project_str, hash_str)
469         if patch_id is None:
470             sys.stderr.write("No patch has the hash provided\n")
471             sys.exit(1)
472
473
474     if action == 'list' or action == 'search':
475         if len(args) > 0:
476             filt.add("name__icontains", args[0])
477         action_list(rpc, filt, submitter_str, delegate_str)
478
479     elif action.startswith('project'):
480         action_projects(rpc)
481
482     elif action.startswith('state'):
483         action_states(rpc)
484
485     elif action == 'view':
486         try:
487             patch_id = patch_id or int(args[0])
488         except:
489             sys.stderr.write("Invalid patch ID given\n")
490             sys.exit(1)
491
492         s = rpc.patch_get_mbox(patch_id)
493         if len(s) > 0:
494             print unicode(s).encode("utf-8")
495
496     elif action in ('get', 'save', 'info'):
497         try:
498             patch_id = patch_id or int(args[0])
499         except:
500             sys.stderr.write("Invalid patch ID given\n")
501             sys.exit(1)
502
503         if action == 'info':
504             action_info(rpc, patch_id)
505         else:
506             action_get(rpc, patch_id)
507
508     elif action == 'apply':
509         try:
510             patch_id = patch_id or int(args[0])
511         except:
512             sys.stderr.write("Invalid patch ID given\n")
513             sys.exit(1)
514
515         action_apply(rpc, patch_id)
516
517     elif action == 'git-am':
518         try:
519             patch_id = patch_id or int(args[0])
520         except:
521             sys.stderr.write("Invalid patch ID given\n")
522             sys.exit(1)
523
524         action_apply(rpc, patch_id, ['git', 'am'])
525
526     elif action == 'update':
527         try:
528             patch_id = patch_id or int(args[0])
529         except:
530             sys.stderr.write("Invalid patch ID given\n")
531             sys.exit(1)
532
533         action_update_patch(rpc, patch_id, state = state_str,
534                 commit = commit_str)
535
536     else:
537         sys.stderr.write("Unknown action '%s'\n" % action)
538         usage()
539
540 if __name__ == "__main__":
541     main()