yetus-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ajayyad...@apache.org
Subject yetus git commit: YETUS-460, new exception handler for urllib2 network errors
Date Thu, 23 Feb 2017 21:43:16 GMT
Repository: yetus
Updated Branches:
  refs/heads/master ab902e27b -> 9cbfe9c04


YETUS-460, new exception handler for urllib2 network errors

Signed-off-by: Ajay Yadava <ajayyadava@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/yetus/repo
Commit: http://git-wip-us.apache.org/repos/asf/yetus/commit/9cbfe9c0
Tree: http://git-wip-us.apache.org/repos/asf/yetus/tree/9cbfe9c0
Diff: http://git-wip-us.apache.org/repos/asf/yetus/diff/9cbfe9c0

Branch: refs/heads/master
Commit: 9cbfe9c0442661ea4a97da6cd855f04c6f2607ce
Parents: ab902e2
Author: Adam Faris <adam.m.faris+github@gmail.com>
Authored: Thu Feb 23 08:54:33 2017 -0800
Committer: Ajay Yadava <ajayyadava@apache.org>
Committed: Thu Feb 23 16:41:55 2017 -0500

----------------------------------------------------------------------
 release-doc-maker/releasedocmaker.py | 51 +++++++++----------------------
 release-doc-maker/utils.py           | 33 ++++++++++++++++++++
 2 files changed, 47 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/yetus/blob/9cbfe9c0/release-doc-maker/releasedocmaker.py
----------------------------------------------------------------------
diff --git a/release-doc-maker/releasedocmaker.py b/release-doc-maker/releasedocmaker.py
index 75b3904..abe99b3 100755
--- a/release-doc-maker/releasedocmaker.py
+++ b/release-doc-maker/releasedocmaker.py
@@ -29,8 +29,7 @@ import urllib
 import urllib2
 import httplib
 import json
-from utils import to_unicode, sanitize_text, processrelnote, Outputs
-
+from utils import get_jira, to_unicode, sanitize_text, processrelnote, Outputs
 
 try:
     import dateutil.parser
@@ -69,7 +68,6 @@ ASF_LICENSE = '''
 -->
 '''
 
-
 def buildindex(title, asf_license):
     """Write an index file for later conversion using mvn site"""
     versions = glob("[0-9]*.[0-9]*")
@@ -114,19 +112,10 @@ class GetVersions(object):
             url = JIRA_BASE_URL + \
               "/rest/api/2/project/%s/versions" % project.upper()
             try:
-                resp = urllib2.urlopen(url)
-            except urllib2.HTTPError as err:
-                code = err.code
-                print "JIRA returns HTTP error %d: %s. Aborting." % (code, err.msg)
-                error_response = err.read()
-                try:
-                    error_response = json.loads(error_response)
-                    print "- Please ensure that specified projects are correct."
-                    for message in error_response['errorMessages']:
-                        print "-", message
-                except Exception:
-                    print "Couldn't parse server response."
+                resp = get_jira(url)
+            except (urllib2.HTTPError, urllib2.URLError, httplib.BadStatusLine):
                 sys.exit(1)
+
             datum = json.loads(resp.read())
             for data in datum:
                 newversions.add(data['name'])
@@ -156,7 +145,7 @@ class Version(object):
             self.parts = [int(p) for p in found.group(1).split('.')]
         else:
             self.parts = []
-        # backfill version with zeroes if missing parts
+        # backfill version with zeros if missing parts
         self.parts.extend((0,) * (3 - len(self.parts)))
 
     def __str__(self):
@@ -305,8 +294,11 @@ class JiraIter(object):
     @staticmethod
     def collect_fields():
         """send a query to JIRA and collect field-id map"""
-        resp = urllib2.urlopen(JIRA_BASE_URL + "/rest/api/2/field")
-        data = json.loads(resp.read())
+        try:
+            resp = get_jira(JIRA_BASE_URL + "/rest/api/2/field")
+            data = json.loads(resp.read())
+        except (urllib2.HTTPError, urllib2.URLError, httplib.BadStatusLine, ValueError):
+            sys.exit(1)
         field_id_map = {}
         for part in data:
             field_id_map[part['name']] = part['id']
@@ -330,22 +322,10 @@ class JiraIter(object):
     def load_jira(params, fail_count):
         """send query to JIRA and collect with retries"""
         try:
-            resp = urllib2.urlopen(JIRA_BASE_URL + "/rest/api/2/search?%s" %
-                                   params)
-        except urllib2.HTTPError, err:
-            code = err.code
-            print "JIRA returns HTTP error %d: %s. Aborting." % (code, err.msg)
-            error_response = err.read()
-            try:
-                error_response = json.loads(error_response)
-                print "- Please ensure that specified projects, fixVersions etc. are correct."
-                for message in error_response['errorMessages']:
-                    print "-", message
-            except Exception:
-                print "Couldn't parse server response."
-            sys.exit(1)
-        except httplib.BadStatusLine as err:
-            return JiraIter.retry_load(err, params, fail_count)
+            resp = get_jira(JIRA_BASE_URL + "/rest/api/2/search?%s" % params)
+        except (urllib2.HTTPError, urllib2.URLError, httplib.BadStatusLine):
+            JiraIter.retry_load(resp, params, fail_count)
+
         try:
             data = json.loads(resp.read())
         except httplib.IncompleteRead as err:
@@ -716,9 +696,6 @@ def main():
         global BACKWARD_INCOMPATIBLE_LABEL
         BACKWARD_INCOMPATIBLE_LABEL = options.incompatible_label
 
-    proxy = urllib2.ProxyHandler()
-    opener = urllib2.build_opener(proxy)
-    urllib2.install_opener(opener)
 
     projects = options.projects
 

http://git-wip-us.apache.org/repos/asf/yetus/blob/9cbfe9c0/release-doc-maker/utils.py
----------------------------------------------------------------------
diff --git a/release-doc-maker/utils.py b/release-doc-maker/utils.py
index 6d89f59..ba42143 100644
--- a/release-doc-maker/utils.py
+++ b/release-doc-maker/utils.py
@@ -17,6 +17,10 @@
 # limitations under the License.
 
 import re
+import urllib2
+import sys
+import json
+import httplib
 
 NAME_PATTERN = re.compile(r' \([0-9]+\)')
 BASE_URL = "https://issues.apache.org/jira"
@@ -26,6 +30,35 @@ def clean(input_string):
     return sanitize_markdown(re.sub(NAME_PATTERN, "", input_string))
 
 
+def get_jira(jira_url):
+    """ Provide standard method for fetching content from apache jira and
+        handling of potential errors. Returns urllib2 response or
+        raises one of several exceptions."""
+    try:
+        response = urllib2.urlopen(jira_url)
+    except urllib2.HTTPError as http_err:
+        code = http_err.code
+        print "JIRA returns HTTP error %d: %s. Aborting." % \
+              (code, http_err.msg)
+        error_response = http_err.read()
+        try:
+            error_response = json.loads(error_response)
+            print "- Please ensure that specified projects, fixVersions etc."\
+                  " are correct."
+            for message in error_response['errorMessages']:
+                print "-", message
+        except ValueError:
+            print "FATAL: Could not parse json response from server."
+        sys.exit(1)
+    except urllib2.URLError as url_err:
+        print "Error contacting JIRA: %s\n" % jira_url
+        print "Reason: %s" % url_err.reason
+        raise url_err
+    except httplib.BadStatusLine as err:
+        raise err
+    return response
+
+
 def format_components(input_string):
     input_string = re.sub(NAME_PATTERN, '', input_string).replace("'", "")
     if input_string != "":


Mime
View raw message