OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 """Defines class Rietveld to easily access a rietveld instance. | 4 """Defines class Rietveld to easily access a rietveld instance. |
5 | 5 |
6 Security implications: | 6 Security implications: |
7 | 7 |
8 The following hypothesis are made: | 8 The following hypothesis are made: |
9 - Rietveld enforces: | 9 - Rietveld enforces: |
10 - Nobody else than issue owner can upload a patch set | 10 - Nobody else than issue owner can upload a patch set |
(...skipping 24 matching lines...) Expand all Loading... |
35 # Hack out upload logging.info() | 35 # Hack out upload logging.info() |
36 upload.logging = logging.getLogger('upload') | 36 upload.logging = logging.getLogger('upload') |
37 # Mac pylint choke on this line. | 37 # Mac pylint choke on this line. |
38 upload.logging.setLevel(logging.WARNING) # pylint: disable=E1103 | 38 upload.logging.setLevel(logging.WARNING) # pylint: disable=E1103 |
39 | 39 |
40 | 40 |
41 class Rietveld(object): | 41 class Rietveld(object): |
42 """Accesses rietveld.""" | 42 """Accesses rietveld.""" |
43 def __init__(self, url, email, password): | 43 def __init__(self, url, email, password): |
44 self.issue = None | 44 self.issue = None |
45 self.user = email | |
46 self.url = url | 45 self.url = url |
47 self._get_creds = lambda: (email, password) | 46 if email and password: |
| 47 get_creds = lambda: (email, password) |
| 48 self.rpc_server = upload.HttpRpcServer( |
| 49 self.url, |
| 50 get_creds) |
| 51 else: |
| 52 self.rpc_server = upload.GetRpcServer(url, email) |
48 self._xsrf_token = None | 53 self._xsrf_token = None |
49 self._xsrf_token_time = None | 54 self._xsrf_token_time = None |
50 self.rpc_server = upload.HttpRpcServer( | |
51 self.url, | |
52 self._get_creds, | |
53 save_cookies=False) | |
54 | 55 |
55 def xsrf_token(self): | 56 def xsrf_token(self): |
56 if (not self._xsrf_token_time or | 57 if (not self._xsrf_token_time or |
57 (time.time() - self._xsrf_token_time) > 30*60): | 58 (time.time() - self._xsrf_token_time) > 30*60): |
58 self._xsrf_token_time = time.time() | 59 self._xsrf_token_time = time.time() |
59 self._xsrf_token = self.get( | 60 self._xsrf_token = self.get( |
60 '/xsrf_token', | 61 '/xsrf_token', |
61 extra_headers={'X-Requesting-XSRF-Token': '1'}) | 62 extra_headers={'X-Requesting-XSRF-Token': '1'}) |
62 return self._xsrf_token | 63 return self._xsrf_token |
63 | 64 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 ('send_mail', 'True'), | 169 ('send_mail', 'True'), |
169 ('no_redirect', 'True')]) | 170 ('no_redirect', 'True')]) |
170 | 171 |
171 def set_flag(self, issue, patchset, flag, value): | 172 def set_flag(self, issue, patchset, flag, value): |
172 return self.post('/%s/edit_flags' % issue, [ | 173 return self.post('/%s/edit_flags' % issue, [ |
173 ('last_patchset', str(patchset)), | 174 ('last_patchset', str(patchset)), |
174 ('xsrf_token', self.xsrf_token()), | 175 ('xsrf_token', self.xsrf_token()), |
175 (flag, value)]) | 176 (flag, value)]) |
176 | 177 |
177 def get(self, request_path, **kwargs): | 178 def get(self, request_path, **kwargs): |
178 return self._send(request_path, payload=None, **kwargs) | 179 kwargs.setdefault('payload', None) |
| 180 return self._send(request_path, **kwargs) |
179 | 181 |
180 def post(self, request_path, data, **kwargs): | 182 def post(self, request_path, data, **kwargs): |
181 ctype, body = upload.EncodeMultipartFormData(data, []) | 183 ctype, body = upload.EncodeMultipartFormData(data, []) |
182 return self._send(request_path, payload=body, content_type=ctype, **kwargs) | 184 return self._send(request_path, payload=body, content_type=ctype, **kwargs) |
183 | 185 |
184 def _send(self, request_path, **kwargs): | 186 def _send(self, request_path, **kwargs): |
185 """Sends a POST/GET to Rietveld. Returns the response body.""" | 187 """Sends a POST/GET to Rietveld. Returns the response body.""" |
186 maxtries = 5 | 188 maxtries = 5 |
187 for retry in xrange(maxtries): | 189 for retry in xrange(maxtries): |
188 try: | 190 try: |
189 result = self.rpc_server.Send(request_path, **kwargs) | 191 result = self.rpc_server.Send(request_path, **kwargs) |
190 # Sometimes GAE returns a HTTP 200 but with HTTP 500 as the content. How | 192 # Sometimes GAE returns a HTTP 200 but with HTTP 500 as the content. How |
191 # nice. | 193 # nice. |
192 return result | 194 return result |
193 except urllib2.HTTPError, e: | 195 except urllib2.HTTPError, e: |
194 if retry >= (maxtries - 1): | 196 if retry >= (maxtries - 1): |
195 raise | 197 raise |
196 if e.code not in (500, 502, 503): | 198 if e.code not in (500, 502, 503): |
197 raise | 199 raise |
198 except urllib2.URLError, e: | 200 except urllib2.URLError, e: |
199 if retry >= (maxtries - 1): | 201 if retry >= (maxtries - 1): |
200 raise | 202 raise |
201 if not 'Name or service not known' in e.reason: | 203 if not 'Name or service not known' in e.reason: |
202 # Usually internal GAE flakiness. | 204 # Usually internal GAE flakiness. |
203 raise | 205 raise |
204 # If reaching this line, loop again. Uses a small backoff. | 206 # If reaching this line, loop again. Uses a small backoff. |
205 time.sleep(1+maxtries*2) | 207 time.sleep(1+maxtries*2) |
| 208 |
| 209 # DEPRECATED. |
| 210 Send = get |
OLD | NEW |