| OLD | NEW |
| 1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2013 The Chromium OS 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 | 4 |
| 5 """ | 5 """ |
| 6 Utilities for requesting information for a gerrit server via https. | 6 Utilities for requesting information for a gerrit server via https. |
| 7 | 7 |
| 8 https://gerrit-review.googlesource.com/Documentation/rest-api.html | 8 https://gerrit-review.googlesource.com/Documentation/rest-api.html |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import base64 | 11 import base64 |
| 12 import httplib | 12 import httplib |
| 13 import json | 13 import json |
| 14 import logging | 14 import logging |
| 15 import netrc | 15 import netrc |
| 16 import os | 16 import os |
| 17 import re | 17 import re |
| 18 import stat |
| 19 import sys |
| 18 import time | 20 import time |
| 19 import urllib | 21 import urllib |
| 20 from cStringIO import StringIO | 22 from cStringIO import StringIO |
| 21 | 23 |
| 24 _netrc_file = '_netrc' if sys.platform.startswith('win') else '.netrc' |
| 25 _netrc_file = os.path.join(os.environ['HOME'], _netrc_file) |
| 22 try: | 26 try: |
| 23 NETRC = netrc.netrc() | 27 NETRC = netrc.netrc(_netrc_file) |
| 24 except (IOError, netrc.NetrcParseError): | 28 except IOError: |
| 29 print >> sys.stderr, 'WARNING: Could not read netrc file %s' % _netrc_file |
| 25 NETRC = netrc.netrc(os.devnull) | 30 NETRC = netrc.netrc(os.devnull) |
| 31 except netrc.NetrcParseError as e: |
| 32 _netrc_stat = os.stat(e.filename) |
| 33 if _netrc_stat.st_mode & (stat.S_IRWXG | stat.S_IRWXO): |
| 34 print >> sys.stderr, ( |
| 35 'WARNING: netrc file %s cannot be used because its file permissions ' |
| 36 'are insecure. netrc file permissions should be 600.' % _netrc_file) |
| 37 else: |
| 38 print >> sys.stderr, ('ERROR: Cannot use netrc file %s due to a parsing ' |
| 39 'error.' % _netrc_file) |
| 40 raise |
| 41 del _netrc_stat |
| 42 NETRC = netrc.netrc(os.devnull) |
| 43 del _netrc_file |
| 44 |
| 26 LOGGER = logging.getLogger() | 45 LOGGER = logging.getLogger() |
| 27 TRY_LIMIT = 5 | 46 TRY_LIMIT = 5 |
| 28 | 47 |
| 29 # Controls the transport protocol used to communicate with gerrit. | 48 # Controls the transport protocol used to communicate with gerrit. |
| 30 # This is parameterized primarily to enable GerritTestCase. | 49 # This is parameterized primarily to enable GerritTestCase. |
| 31 GERRIT_PROTOCOL = 'https' | 50 GERRIT_PROTOCOL = 'https' |
| 32 | 51 |
| 33 | 52 |
| 34 class GerritError(Exception): | 53 class GerritError(Exception): |
| 35 """Exception class for errors commuicating with the gerrit-on-borg service.""" | 54 """Exception class for errors commuicating with the gerrit-on-borg service.""" |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 username = review.get('email', jmsg.get('name', '')) | 465 username = review.get('email', jmsg.get('name', '')) |
| 447 raise GerritError(200, 'Unable to set %s label for user "%s"' | 466 raise GerritError(200, 'Unable to set %s label for user "%s"' |
| 448 ' on change %s.' % (label, username, change)) | 467 ' on change %s.' % (label, username, change)) |
| 449 jmsg = GetChangeCurrentRevision(host, change) | 468 jmsg = GetChangeCurrentRevision(host, change) |
| 450 if not jmsg: | 469 if not jmsg: |
| 451 raise GerritError( | 470 raise GerritError( |
| 452 200, 'Could not get review information for change "%s"' % change) | 471 200, 'Could not get review information for change "%s"' % change) |
| 453 elif jmsg[0]['current_revision'] != revision: | 472 elif jmsg[0]['current_revision'] != revision: |
| 454 raise GerritError(200, 'While resetting labels on change "%s", ' | 473 raise GerritError(200, 'While resetting labels on change "%s", ' |
| 455 'a new patchset was uploaded.' % change) | 474 'a new patchset was uploaded.' % change) |
| OLD | NEW |