OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Reads a .isolated, creates a tree of hardlinks and runs the test. | 6 """Reads a .isolated, creates a tree of hardlinks and runs the test. |
7 | 7 |
8 Keeps a local cache. | 8 Keeps a local cache. |
9 """ | 9 """ |
10 | 10 |
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 new_url = urlparse.urlunparse(url_parts) | 529 new_url = urlparse.urlunparse(url_parts) |
530 request = urllib2.Request(new_url, data=payload) | 530 request = urllib2.Request(new_url, data=payload) |
531 if payload is not None: | 531 if payload is not None: |
532 if content_type: | 532 if content_type: |
533 request.add_header('Content-Type', content_type) | 533 request.add_header('Content-Type', content_type) |
534 request.add_header('Content-Length', len(payload)) | 534 request.add_header('Content-Length', len(payload)) |
535 return request | 535 return request |
536 | 536 |
537 return self._retry_loop(make_request, **kwargs) | 537 return self._retry_loop(make_request, **kwargs) |
538 | 538 |
539 def _retry_loop(self, make_request, retry_404=False): | 539 def _retry_loop(self, make_request, retry_404=False, retry_50x=True): |
540 """Runs internal request-retry loop.""" | 540 """Runs internal request-retry loop.""" |
541 authenticated = False | 541 authenticated = False |
542 last_error = None | 542 last_error = None |
543 for attempt in range(MAX_URL_OPEN_ATTEMPTS): | 543 for attempt in range(MAX_URL_OPEN_ATTEMPTS): |
544 extra = {COUNT_KEY: attempt} if attempt else {} | 544 extra = {COUNT_KEY: attempt} if attempt else {} |
545 request = make_request(extra) | 545 request = make_request(extra) |
546 try: | 546 try: |
547 url_response = self._url_open(request) | 547 url_response = self._url_open(request) |
548 logging.debug('url_open(%s) succeeded', request.get_full_url()) | 548 logging.debug('url_open(%s) succeeded', request.get_full_url()) |
549 return url_response | 549 return url_response |
550 except urllib2.HTTPError as e: | 550 except urllib2.HTTPError as e: |
551 # Unauthorized. Ask to authenticate and then try again. | 551 # Unauthorized. Ask to authenticate and then try again. |
552 if e.code in (401, 403): | 552 if e.code in (401, 403): |
553 # Try to authenticate only once. If it doesn't help, then server does | 553 # Try to authenticate only once. If it doesn't help, then server does |
554 # not support app engine authentication. | 554 # not support app engine authentication. |
555 logging.error( | 555 logging.error( |
556 'Authentication is required for %s on attempt %d.\n%s', | 556 'Authentication is required for %s on attempt %d.\n%s', |
557 request.get_full_url(), attempt, | 557 request.get_full_url(), attempt, |
558 self._format_exception(e, verbose=True)) | 558 self._format_exception(e, verbose=True)) |
559 if not authenticated and self.authenticate(): | 559 if not authenticated and self.authenticate(): |
560 authenticated = True | 560 authenticated = True |
561 continue | 561 continue |
562 logging.error( | 562 logging.error( |
563 'Unable to authenticate to %s.\n%s', | 563 'Unable to authenticate to %s.\n%s', |
564 request.get_full_url(), self._format_exception(e, verbose=True)) | 564 request.get_full_url(), self._format_exception(e, verbose=True)) |
565 return None | 565 return None |
566 | 566 |
567 if e.code < 500 and not (retry_404 and e.code == 404): | 567 if ((e.code < 500 and not (retry_404 and e.code == 404)) or |
| 568 (e.code >= 500 and not retry_50x)): |
568 # This HTTPError means we reached the server and there was a problem | 569 # This HTTPError means we reached the server and there was a problem |
569 # with the request, so don't retry. | 570 # with the request, so don't retry. |
570 logging.error( | 571 logging.error( |
571 'Able to connect to %s but an exception was thrown.\n%s', | 572 'Able to connect to %s but an exception was thrown.\n%s', |
572 request.get_full_url(), self._format_exception(e, verbose=True)) | 573 request.get_full_url(), self._format_exception(e, verbose=True)) |
573 return None | 574 return None |
574 | 575 |
575 # The HTTPError was due to a server error, so retry the attempt. | 576 # The HTTPError was due to a server error, so retry the attempt. |
576 logging.warning('Able to connect to %s on attempt %d.\n%s', | 577 logging.warning('Able to connect to %s on attempt %d.\n%s', |
577 request.get_full_url(), attempt, | 578 request.get_full_url(), attempt, |
(...skipping 1196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1774 except Exception, e: | 1775 except Exception, e: |
1775 # Make sure any exception is logged. | 1776 # Make sure any exception is logged. |
1776 logging.exception(e) | 1777 logging.exception(e) |
1777 return 1 | 1778 return 1 |
1778 | 1779 |
1779 | 1780 |
1780 if __name__ == '__main__': | 1781 if __name__ == '__main__': |
1781 # Ensure that we are always running with the correct encoding. | 1782 # Ensure that we are always running with the correct encoding. |
1782 fix_default_encoding() | 1783 fix_default_encoding() |
1783 sys.exit(main()) | 1784 sys.exit(main()) |
OLD | NEW |