-
Notifications
You must be signed in to change notification settings - Fork 188
/
postreview.py
executable file
·1357 lines (1138 loc) · 50.5 KB
/
postreview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import base64
import cookielib
import getpass
import logging
import mimetools
import os
import platform
import re
import sys
import urllib2
from optparse import OptionParser
from pkg_resources import parse_version
from urlparse import urljoin, urlparse
from rbtools import get_package_version, get_version_string
from rbtools.api.capabilities import Capabilities
from rbtools.api.errors import APIError
from rbtools.clients import scan_usable_client
from rbtools.clients.perforce import PerforceClient
from rbtools.clients.plastic import PlasticClient
from rbtools.utils.filesystem import get_config_value, load_config_files
from rbtools.utils.process import die
try:
# Specifically import json_loads, to work around some issues with
# installations containing incompatible modules named "json".
from json import loads as json_loads
except ImportError:
from simplejson import loads as json_loads
options = None
configs = []
ADD_REPOSITORY_DOCS_URL = \
'http://www.reviewboard.org/docs/manual/dev/admin/configuration/repositories/'
class HTTPRequest(urllib2.Request):
def __init__(self, url, body='', headers={}, method="PUT"):
urllib2.Request.__init__(self, url, body, headers)
self.method = method
def get_method(self):
return self.method
class PresetHTTPAuthHandler(urllib2.BaseHandler):
"""urllib2 handler that conditionally presets the use of HTTP Basic Auth.
This is used when specifying --username= on the command line. It will
force an HTTP_AUTHORIZATION header with the user info, asking the user
for any missing info beforehand. It will then try this header for that
first request.
It will only do this once.
"""
handler_order = 480 # After Basic auth
def __init__(self, url, password_mgr):
self.url = url
self.password_mgr = password_mgr
self.used = False
def reset(self):
self.password_mgr.rb_user = options.http_username
self.password_mgr.rb_pass = options.http_password
self.used = False
def http_request(self, request):
if options.username and not self.used:
# Note that we call password_mgr.find_user_password to get the
# username and password we're working with. This allows us to
# prompt if, say, --username was specified but --password was not.
username, password = \
self.password_mgr.find_user_password('Web API', self.url)
raw = '%s:%s' % (username, password)
request.add_header(
urllib2.HTTPBasicAuthHandler.auth_header,
'Basic %s' % base64.b64encode(raw).strip())
self.used = True
return request
https_request = http_request
class ReviewBoardHTTPErrorProcessor(urllib2.HTTPErrorProcessor):
"""Processes HTTP error codes.
Python 2.6 gets HTTP error code processing right, but 2.4 and 2.5 only
accepts HTTP 200 and 206 as success codes. This handler ensures that
anything in the 200 range is a success.
"""
def http_response(self, request, response):
if not (200 <= response.code < 300):
response = self.parent.error('http', request, response,
response.code, response.msg,
response.info())
return response
https_response = http_response
class ReviewBoardHTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
"""Custom Basic Auth handler that doesn't retry excessively.
urllib2's HTTPBasicAuthHandler retries over and over, which is useless.
This subclass only retries once to make sure we've attempted with a
valid username and password. It will then fail so we can use
tempt_fate's retry handler.
"""
def __init__(self, *args, **kwargs):
urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
self._retried = False
self._lasturl = ""
def retry_http_basic_auth(self, *args, **kwargs):
if self._lasturl != args[0]:
self._retried = False
self._lasturl = args[0]
if not self._retried:
self._retried = True
self.retried = 0
response = urllib2.HTTPBasicAuthHandler.retry_http_basic_auth(
self, *args, **kwargs)
if response.code != 401:
self._retried = False
return response
else:
return None
class ReviewBoardHTTPPasswordMgr(urllib2.HTTPPasswordMgr):
"""
Adds HTTP authentication support for URLs.
Python 2.4's password manager has a bug in http authentication when the
target server uses a non-standard port. This works around that bug on
Python 2.4 installs. This also allows post-review to prompt for passwords
in a consistent way.
See: http://bugs.python.org/issue974757
"""
def __init__(self, reviewboard_url, rb_user=None, rb_pass=None):
self.passwd = {}
self.rb_url = reviewboard_url
self.rb_user = rb_user
self.rb_pass = rb_pass
def find_user_password(self, realm, uri):
if realm == 'Web API':
if self.rb_user is None or self.rb_pass is None:
if options.diff_filename == '-':
die('HTTP authentication is required, but cannot be '
'used with --diff-filename=-')
print "==> HTTP Authentication Required"
print 'Enter authorization information for "%s" at %s' % \
(realm, urlparse(uri)[1])
if not self.rb_user:
# getpass will write its prompt to stderr but raw_input
# writes to stdout. See bug 2831.
sys.stderr.write('Username: ')
self.rb_user = raw_input()
if not self.rb_pass:
self.rb_pass = getpass.getpass('Password: ')
return self.rb_user, self.rb_pass
else:
# If this is an auth request for some other domain (since HTTP
# handlers are global), fall back to standard password management.
return urllib2.HTTPPasswordMgr.find_user_password(self, realm, uri)
class ReviewBoardServer(object):
"""
An instance of a Review Board server.
"""
def __init__(self, url, info, cookie_file):
self.url = url
if self.url[-1] != '/':
self.url += '/'
self._info = info
self._server_info = None
self.capabilities = None
self.root_resource = None
self.deprecated_api = False
self.rb_version = "0.0.0.0"
self.cookie_file = cookie_file
self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file)
self.deprecated_api = False
self.root_resource = None
if self.cookie_file:
try:
self.cookie_jar.load(self.cookie_file, ignore_expires=True)
except IOError:
pass
# Set up the HTTP libraries to support all of the features we need.
password_mgr = ReviewBoardHTTPPasswordMgr(self.url,
options.username,
options.password)
self.preset_auth_handler = PresetHTTPAuthHandler(self.url, password_mgr)
handlers = []
if options.disable_proxy:
debug('Disabling HTTP(s) proxy support')
handlers.append(urllib2.ProxyHandler({}))
handlers += [
urllib2.HTTPCookieProcessor(self.cookie_jar),
ReviewBoardHTTPBasicAuthHandler(password_mgr),
urllib2.HTTPDigestAuthHandler(password_mgr),
self.preset_auth_handler,
ReviewBoardHTTPErrorProcessor(),
]
opener = urllib2.build_opener(*handlers)
opener.addheaders = [('User-agent', 'RBTools/' + get_package_version())]
urllib2.install_opener(opener)
def check_api_version(self):
"""Checks the API version on the server to determine which to use."""
try:
root_resource = self.api_get('api/')
rsp = self.api_get(root_resource['links']['info']['href'])
self.rb_version = rsp['info']['product']['package_version']
if parse_version(self.rb_version) >= parse_version('1.5.2'):
self.deprecated_api = False
self.root_resource = root_resource
debug('Using the new web API')
return True
except APIError, e:
if e.http_status not in (401, 404):
# We shouldn't reach this. If there's a permission denied
# from lack of logging in, then the basic auth handler
# should have hit it.
#
# However in some versions it wants you to be logged in
# and returns a 401 from the application after you've
# done your http basic auth
die("Unable to access the root /api/ URL on the server.")
return False
# This is an older Review Board server with the old API.
self.deprecated_api = True
debug('Using the deprecated Review Board 1.0 web API')
return True
def load_capabilities(self):
"""Loads the server capabilities."""
info = self.api_get('api/info/')
caps = None
try:
caps = info['info']['capabilities']
except KeyError:
# The capabilities list is left empty.
pass
self.capabilities = Capabilities(caps)
def login(self, force=False):
"""
Logs in to a Review Board server, prompting the user for login
information if needed.
"""
if (options.diff_filename == '-' and
not (self.has_valid_cookie() or
(options.username and options.password))):
die('Authentication information needs to be provided on '
'the command line when using --diff-filename=-')
if self.deprecated_api:
print "==> Review Board Login Required"
print "Enter username and password for Review Board at %s" % \
self.url
if options.username:
username = options.username
elif options.submit_as:
username = options.submit_as
elif not force and self.has_valid_cookie():
# We delay the check for a valid cookie until after looking
# at args, so that it doesn't override the command line.
return
else:
# getpass will write its prompt to stderr but raw_input
# writes to stdout. See bug 2831.
sys.stderr.write('Username: ')
username = raw_input()
if not options.password:
password = getpass.getpass('Password: ')
else:
password = options.password
debug('Logging in with username "%s"' % username)
try:
self.api_post('api/json/accounts/login/', {
'username': username,
'password': password,
})
except APIError, e:
die("Unable to log in: %s" % e)
debug("Logged in.")
elif force:
self.preset_auth_handler.reset()
def has_valid_cookie(self):
"""
Load the user's cookie file and see if they have a valid
'rbsessionid' cookie for the current Review Board server. Returns
true if so and false otherwise.
"""
try:
parsed_url = urlparse(self.url)
host = parsed_url[1]
path = parsed_url[2] or '/'
# Cookie files don't store port numbers, unfortunately, so
# get rid of the port number if it's present.
host = host.split(":")[0]
# Cookie files also append .local to bare hostnames
if '.' not in host:
host += '.local'
debug("Looking for '%s %s' cookie in %s" % \
(host, path, self.cookie_file))
try:
cookie = self.cookie_jar._cookies[host][path]['rbsessionid']
if not cookie.is_expired():
debug("Loaded valid cookie -- no login required")
return True
debug("Cookie file loaded, but cookie has expired")
except KeyError:
debug("Cookie file loaded, but no cookie for this server")
except IOError, error:
debug("Couldn't load cookie file: %s" % error)
return False
def new_review_request(self, changenum, submit_as=None):
"""
Creates a review request on a Review Board server, updating an
existing one if the changeset number already exists.
If submit_as is provided, the specified user name will be recorded as
the submitter of the review request (given that the logged in user has
the appropriate permissions).
"""
# If repository_path is a list, find a name in the list that's
# registered on the server.
if isinstance(self.info.path, list):
repositories = self.get_repositories()
debug("Repositories on Server: %s" % repositories)
debug("Server Aliases: %s" % self.info.path)
for repository in repositories:
if repository['path'] in self.info.path:
self.info.path = repository['path']
break
if isinstance(self.info.path, list):
sys.stderr.write('\n')
sys.stderr.write('There was an error creating this review '
'request.\n')
sys.stderr.write('\n')
sys.stderr.write('There was no matching repository path'
'found on the server.\n')
sys.stderr.write('List of configured repositories:\n')
for repository in repositories:
sys.stderr.write('\t%s\n' % repository['path'])
sys.stderr.write('Unknown repository paths found:\n')
for foundpath in self.info.path:
sys.stderr.write('\t%s\n' % foundpath)
sys.stderr.write('Ask the administrator to add one of '
'these repositories\n')
sys.stderr.write('to the Review Board server.\n')
sys.stderr.write('For information on adding repositories, '
'please read\n')
sys.stderr.write(ADD_REPOSITORY_DOCS_URL + '\n')
die()
repository = options.repository_url or self.info.path
try:
debug("Attempting to create review request on %s for %s" %
(repository, changenum))
data = {}
if changenum:
data['changenum'] = changenum
if submit_as:
debug("Submitting the review request as %s" % submit_as)
data['submit_as'] = submit_as
if self.deprecated_api:
data['repository_path'] = repository
rsp = self.api_post('api/json/reviewrequests/new/', data)
else:
data['repository'] = repository
links = self.root_resource['links']
assert 'review_requests' in links
review_request_href = links['review_requests']['href']
rsp = self.api_post(review_request_href, data)
except APIError, e:
if e.error_code == 204: # Change number in use
rsp = e.rsp
if options.diff_only:
# In this case, fall through and return to tempt_fate.
debug("Review request already exists.")
else:
debug("Review request already exists. Updating it...")
self.update_review_request_from_changenum(
changenum, rsp['review_request'])
elif e.error_code == 206: # Invalid repository
sys.stderr.write('\n')
sys.stderr.write('There was an error creating this review '
'request.\n')
sys.stderr.write('\n')
sys.stderr.write('The repository path "%s" is not in the\n' %
self.info.path)
sys.stderr.write('list of known repositories on the server.\n')
sys.stderr.write('\n')
sys.stderr.write('Ask the administrator to add this '
'repository to the Review Board server.\n')
sys.stderr.write('For information on adding repositories, '
'please read\n')
sys.stderr.write(ADD_REPOSITORY_DOCS_URL + '\n')
die()
else:
raise e
else:
debug("Review request created")
return rsp['review_request']
def update_review_request_from_changenum(self, changenum, review_request):
if self.deprecated_api:
self.api_post(
'api/json/reviewrequests/%s/update_from_changenum/'
% review_request['id'])
else:
self.api_put(review_request['links']['self']['href'], {
'changenum': review_request['changenum'],
})
def set_review_request_field(self, review_request, field, value):
"""
Sets a field in a review request to the specified value.
"""
rid = review_request['id']
debug("Attempting to set field '%s' to '%s' for review request '%s'" %
(field, value, rid))
if self.deprecated_api:
self.api_post('api/json/reviewrequests/%s/draft/set/' % rid, {
field: value,
})
else:
self.api_put(review_request['links']['draft']['href'], {
field: value,
})
def get_review_request(self, rid):
"""
Returns the review request with the specified ID.
"""
if self.deprecated_api:
url = 'api/json/reviewrequests/%s/' % rid
else:
url = '%s%s/' % (
self.root_resource['links']['review_requests']['href'], rid)
rsp = self.api_get(url)
return rsp['review_request']
def get_repositories(self):
"""
Returns the list of repositories on this server.
"""
if self.deprecated_api:
rsp = self.api_get('api/json/repositories/')
repositories = rsp['repositories']
else:
rsp = self.api_get(
self.root_resource['links']['repositories']['href'])
repositories = rsp['repositories']
while 'next' in rsp['links']:
rsp = self.api_get(rsp['links']['next']['href'])
repositories.extend(rsp['repositories'])
return repositories
def get_repository_info(self, rid):
"""
Returns detailed information about a specific repository.
"""
if self.deprecated_api:
url = 'api/json/repositories/%s/info/' % rid
else:
rsp = self.api_get(
'%s%s/' % (self.root_resource['links']['repositories']['href'],
rid))
url = rsp['repository']['links']['info']['href']
rsp = self.api_get(url)
return rsp['info']
def save_draft(self, review_request):
"""
Saves a draft of a review request.
"""
if self.deprecated_api:
self.api_post('api/json/reviewrequests/%s/draft/save/' % \
review_request['id'])
else:
self.api_put(review_request['links']['draft']['href'], {
'public': 1,
})
debug("Review request draft saved")
def upload_diff(self, review_request, diff_content, parent_diff_content):
"""
Uploads a diff to a Review Board server.
"""
debug("Uploading diff, size: %d" % len(diff_content))
if parent_diff_content:
debug("Uploading parent diff, size: %d" % len(parent_diff_content))
fields = {}
files = {}
if self.info.base_path:
fields['basedir'] = self.info.base_path
if options.basedir:
fields['basedir'] = options.basedir
files['path'] = {
'filename': 'diff',
'content': diff_content
}
if parent_diff_content:
files['parent_diff_path'] = {
'filename': 'parent_diff',
'content': parent_diff_content
}
if self.deprecated_api:
self.api_post('api/json/reviewrequests/%s/diff/new/' %
review_request['id'], fields, files)
else:
self.api_post(review_request['links']['diffs']['href'],
fields, files)
def reopen(self, review_request):
"""
Reopen discarded review request.
"""
debug("Reopening")
if self.deprecated_api:
self.api_post('api/json/reviewrequests/%s/reopen/' %
review_request['id'])
else:
self.api_put(review_request['links']['self']['href'], {
'status': 'pending',
})
def publish(self, review_request):
"""
Publishes a review request.
"""
debug("Publishing")
if self.deprecated_api:
self.api_post('api/json/reviewrequests/%s/publish/' %
review_request['id'])
else:
self.api_put(review_request['links']['draft']['href'], {
'public': 1,
})
def _get_server_info(self):
if not self._server_info:
self._server_info = self._info.find_server_repository_info(self)
return self._server_info
info = property(_get_server_info)
def process_json(self, data):
"""
Loads in a JSON file and returns the data if successful. On failure,
APIError is raised.
"""
rsp = json_loads(data)
if rsp['stat'] == 'fail':
# With the new API, we should get something other than HTTP
# 200 for errors, in which case we wouldn't get this far.
assert self.deprecated_api
self.process_error(200, data)
return rsp
def process_error(self, http_status, data):
"""Processes an error, raising an APIError with the information."""
try:
rsp = json_loads(data)
assert rsp['stat'] == 'fail'
debug("Got API Error %d (HTTP code %d): %s" %
(rsp['err']['code'], http_status, rsp['err']['msg']))
debug("Error data: %r" % rsp)
raise APIError(http_status, rsp['err']['code'], rsp,
rsp['err']['msg'])
except ValueError:
debug("Got HTTP error: %s: %s" % (http_status, data))
raise APIError(http_status, None, None, data)
def http_get(self, path):
"""
Performs an HTTP GET on the specified path, storing any cookies that
were set.
"""
debug('HTTP GETting %s' % path)
url = self._make_url(path)
rsp = urllib2.urlopen(url).read()
try:
self.cookie_jar.save(self.cookie_file)
except IOError, e:
debug('Failed to write cookie file: %s' % e)
return rsp
def _make_url(self, path):
"""Given a path on the server returns a full http:// style url"""
if path.startswith('http'):
# This is already a full path.
return path
app = urlparse(self.url)[2]
if path[0] == '/':
url = urljoin(self.url, app[:-1] + path)
else:
url = urljoin(self.url, app + path)
if not url.startswith('http'):
url = 'http://%s' % url
return url
def api_get(self, path):
"""
Performs an API call using HTTP GET at the specified path.
"""
try:
return self.process_json(self.http_get(path))
except urllib2.HTTPError, e:
self.process_error(e.code, e.read())
def http_post(self, path, fields, files=None):
"""
Performs an HTTP POST on the specified path, storing any cookies that
were set.
"""
if fields:
debug_fields = fields.copy()
else:
debug_fields = {}
if 'password' in debug_fields:
debug_fields["password"] = "**************"
url = self._make_url(path)
debug('HTTP POSTing to %s: %s' % (url, debug_fields))
content_type, body = self._encode_multipart_formdata(fields, files)
headers = {
'Content-Type': content_type,
'Content-Length': str(len(body))
}
try:
if type(url) == unicode:
url = url.encode('utf8')
r = urllib2.Request(url, body, headers)
data = urllib2.urlopen(r).read()
try:
self.cookie_jar.save(self.cookie_file)
except IOError, e:
debug('Failed to write cookie file: %s' % e)
return data
except urllib2.HTTPError, e:
# Re-raise so callers can interpret it.
raise e
except urllib2.URLError, e:
try:
debug(e.read())
except AttributeError:
pass
die("Unable to access %s. The host path may be invalid\n%s" % \
(url, e))
def http_put(self, path, fields):
"""
Performs an HTTP PUT on the specified path, storing any cookies that
were set.
"""
url = self._make_url(path)
debug('HTTP PUTting to %s: %s' % (url, fields))
content_type, body = self._encode_multipart_formdata(fields, None)
headers = {
'Content-Type': content_type,
'Content-Length': str(len(body))
}
try:
if type(url) == unicode:
url = url.encode('utf8')
r = HTTPRequest(url, body, headers, method='PUT')
data = urllib2.urlopen(r).read()
try:
self.cookie_jar.save(self.cookie_file)
except IOError, e:
debug('Failed to write cookie file: %s' % e)
return data
except urllib2.HTTPError, e:
# Re-raise so callers can interpret it.
raise e
except urllib2.URLError, e:
try:
debug(e.read())
except AttributeError:
pass
die("Unable to access %s. The host path may be invalid\n%s" % \
(url, e))
def http_delete(self, path):
"""
Performs an HTTP DELETE on the specified path, storing any cookies that
were set.
"""
url = self._make_url(path)
debug('HTTP DELETing %s' % url)
try:
r = HTTPRequest(url, method='DELETE')
data = urllib2.urlopen(r).read()
try:
self.cookie_jar.save(self.cookie_file)
except IOError, e:
debug('Failed to write cookie file: %s' % e)
return data
except urllib2.HTTPError, e:
# Re-raise so callers can interpret it.
raise e
except urllib2.URLError, e:
try:
debug(e.read())
except AttributeError:
pass
die("Unable to access %s. The host path may be invalid\n%s" % \
(url, e))
def api_post(self, path, fields=None, files=None):
"""
Performs an API call using HTTP POST at the specified path.
"""
try:
return self.process_json(self.http_post(path, fields, files))
except urllib2.HTTPError, e:
self.process_error(e.code, e.read())
def api_put(self, path, fields=None):
"""
Performs an API call using HTTP PUT at the specified path.
"""
try:
return self.process_json(self.http_put(path, fields))
except urllib2.HTTPError, e:
self.process_error(e.code, e.read())
def api_delete(self, path):
"""
Performs an API call using HTTP DELETE at the specified path.
"""
try:
return self.process_json(self.http_delete(path))
except urllib2.HTTPError, e:
self.process_error(e.code, e.read())
def _encode_multipart_formdata(self, fields, files):
"""
Encodes data for use in an HTTP POST.
"""
BOUNDARY = mimetools.choose_boundary()
content = ""
fields = fields or {}
files = files or {}
for key in fields:
content += "--" + BOUNDARY + "\r\n"
content += "Content-Disposition: form-data; name=\"%s\"\r\n" % key
content += "\r\n"
content += str(fields[key]) + "\r\n"
for key in files:
filename = files[key]['filename']
value = files[key]['content']
content += "--" + BOUNDARY + "\r\n"
content += "Content-Disposition: form-data; name=\"%s\"; " % key
content += "filename=\"%s\"\r\n" % filename
content += "\r\n"
content += value + "\r\n"
content += "--" + BOUNDARY + "--\r\n"
content += "\r\n"
content_type = "multipart/form-data; boundary=%s" % BOUNDARY
return content_type, content
def debug(s):
"""
Prints debugging information if post-review was run with --debug
"""
if options and options.debug:
print ">>> %s" % s
def tempt_fate(server, tool, changenum, diff_content=None,
parent_diff_content=None, submit_as=None, retries=3):
"""
Attempts to create a review request on a Review Board server and upload
a diff. On success, the review request path is displayed.
"""
try:
if options.rid:
review_request = server.get_review_request(options.rid)
status = review_request['status']
if status == 'submitted':
die("Review request %s is marked as %s. In order to "
"update it, please reopen the request using the web "
"interface and try again." % (options.rid, status))
else:
review_request = server.new_review_request(changenum, submit_as)
if options.target_groups:
server.set_review_request_field(review_request, 'target_groups',
options.target_groups)
if options.target_people:
server.set_review_request_field(review_request, 'target_people',
options.target_people)
if options.summary:
server.set_review_request_field(review_request, 'summary',
options.summary)
if options.branch:
server.set_review_request_field(review_request, 'branch',
options.branch)
if options.bugs_closed: # append to existing list
options.bugs_closed = options.bugs_closed.strip(", ")
bug_set = set(re.split("[, ]+", options.bugs_closed)) | \
set(review_request['bugs_closed'])
options.bugs_closed = ",".join(bug_set)
server.set_review_request_field(review_request, 'bugs_closed',
options.bugs_closed)
if options.description:
server.set_review_request_field(review_request, 'description',
options.description)
if options.testing_done:
server.set_review_request_field(review_request, 'testing_done',
options.testing_done)
if options.change_description:
server.set_review_request_field(review_request, 'changedescription',
options.change_description)
except APIError, e:
if e.error_code == 103: # Not logged in
retries = retries - 1
# We had an odd issue where the server ended up a couple of
# years in the future. Login succeeds but the cookie date was
# "odd" so use of the cookie appeared to fail and eventually
# ended up at max recursion depth :-(. Check for a maximum
# number of retries.
if retries >= 0:
server.login(force=True)
return tempt_fate(server, tool, changenum, diff_content,
parent_diff_content, submit_as,
retries=retries)
if options.rid:
die("Error getting review request %s: %s" % (options.rid, e))
else:
die("Error creating review request: %s" % e)
if not server.info.supports_changesets or not options.change_only:
try:
server.upload_diff(review_request, diff_content,
parent_diff_content)
except APIError, e:
sys.stderr.write('\n')
sys.stderr.write('Error uploading diff\n')
sys.stderr.write('\n')
if e.error_code == 101 and e.http_status == 403:
die('You do not have permissions to modify this review request\n')
elif e.error_code == 105:
sys.stderr.write('The generated diff file was empty. This '
'usually means no files were\n')
sys.stderr.write('modified in this change.\n')
sys.stderr.write('\n')
sys.stderr.write('Try running with --output-diff and --debug '
'for more information.\n')
sys.stderr.write('\n')
die("Your review request still exists, but the diff is not " +
"attached.")
if options.reopen:
server.reopen(review_request)
if options.publish:
server.publish(review_request)
request_url = 'r/' + str(review_request['id']) + '/'
review_url = urljoin(server.url, request_url)
if not review_url.startswith('http'):
review_url = 'http://%s' % review_url
print "Review request #%s posted." % (review_request['id'],)
print
print review_url
return review_url
def parse_options(args):
parser = OptionParser(usage="%prog [-pond] [-r review_id] [changenum]",
version="RBTools " + get_version_string())
parser.add_option("-p", "--publish",
dest="publish", action="store_true",
default=get_config_value(configs, 'PUBLISH', False),
help="publish the review request immediately after "