Bug 791924 - Avoid repository manifest to pass invalid repository information. r=rail FENNEC_31_3_0esr_BUILD1 FENNEC_31_3_0esr_RELEASE
authorArmen Zambrano Gasparnian <armenzg@mozilla.com>
Sun, 30 Nov 2014 11:53:12 -0500
changeset 5231 c554d1982e431cdc62e951782b5e471ffc9b8d1d
parent 5230 c543fab7a52087086c687ad0354c28c392ae5841
child 5232 06738b743643d0a30c5fe159c6ba6466e11d027a
push id1
push usersledru@mozilla.com
push dateThu, 04 Dec 2014 17:23:55 +0000
reviewersrail
bugs791924
Bug 791924 - Avoid repository manifest to pass invalid repository information. r=rail
buildfarm/utils/repository_manifest.py
--- a/buildfarm/utils/repository_manifest.py
+++ b/buildfarm/utils/repository_manifest.py
@@ -9,59 +9,85 @@
 # Target:        Python 2.7.x
 #
 """
    Reads a repository manifest and outputs the repo and
    revision/branch in a format digestable for buildbot
    properties ("key: value").
 """
 import json
+import logging
 import urllib2
 
 from optparse import OptionParser
 
+SUCCESS_CODE = 0
+# This is not an infra error and we can't recover from
+FAILURE_CODE = 1
 # When an infra error happens we want to turn purple and
 # let sheriffs determine if re-triggering is needed
 INFRA_CODE = 3
 
 def main():
     '''
     Determine which repository and revision mozharness.json indicates.
     If none is found we fall back to the default repository
     '''
     parser = OptionParser(__doc__)
     parser.add_option("--manifest-url", dest="manifest_url")
     parser.add_option("--default-repo", dest="default_repo")
     parser.add_option("--default-revision", dest="default_revision")
+    parser.add_option("--timeout", dest="timeout", type="float", default=10)
     options, args = parser.parse_args()
 
     if not options.manifest_url or \
        not options.default_repo or \
        not options.default_revision:
         parser.error("You have to call the script with all options")
 
-    exit_code = 0
+    exit_code = FAILURE_CODE
     try:
-        url_opener = urllib2.urlopen(options.manifest_url, timeout=10)
+        url = options.manifest_url
+        url_opener = urllib2.urlopen(url, timeout=options.timeout)
         http_code = url_opener.getcode()
         if http_code == 200:
-            manifest = json.load(url_opener)
-            print "script_repo_url: %s" % manifest["repo"]
-            print "script_repo_revision: %s" % manifest["revision"]
-            exit_code = 0
+            try:
+                manifest = json.load(url_opener)
+                repo = manifest["repo"]
+                revision = manifest["revision"]
+                # Let's determine if the repo and revision exist
+                url = '%s/rev/%s' % (repo, revision)
+                urllib2.urlopen(url, timeout=options.timeout)
+                print "script_repo_url: %s" % repo
+                print "script_repo_revision: %s" % revision
+                exit_code = SUCCESS_CODE
+            except urllib2.HTTPError:
+                logging.exception(url)
+                exit_code = FAILURE_CODE
+            except ValueError:
+                logging.exception("We have a non-valid json manifest.")
+                exit_code = FAILURE_CODE
         else:
             print "We have failed to retrieve the manifest (http code: %s)" % \
                     http_code
             exit_code = INFRA_CODE
+
     except urllib2.HTTPError, e:
-        # Fallback to default values for branches where the manifest
-        # is not defined
-        print "script_repo_url: %s" % options.default_repo
-        print "script_repo_revision: %s" % options.default_revision
-        exit_code = 0
-    except Exception, e:
-        print str(e)
+        if e.getcode() == 404:
+            # Fallback to default values for branches where the manifest
+            # is not defined
+            print "script_repo_url: %s" % options.default_repo
+            print "script_repo_revision: %s" % options.default_revision
+            exit_code = SUCCESS_CODE
+        else:
+            logging.exception("We got HTTPError code: %s." % e.getcode())
+            exit_code = FAILURE_CODE
+    except urllib2.URLError, e:
+        logging.exception("URLError for %s" % url)
         exit_code = INFRA_CODE
+    except Exception:
+        logging.exception("Unknown case")
+        exit_code = FAILURE_CODE
 
     exit(exit_code)
 
 if __name__ == '__main__':
     main()