Workaround SSL cert issues, enable python 3
authorBoris Zanin <boris.zanin@mobica.com>
Mon, 13 Feb 2017 09:47:28 +0000 (10:47 +0100)
committerPyry Haulos <phaulos@google.com>
Fri, 17 Feb 2017 16:36:42 +0000 (11:36 -0500)
 * The script command line switch --insecure added to bypass SSL
   certificate validity checks. Though bypassing is not recommended,
   sometimes the issue can be resolved on the remote side only,
   which we have no control over. For example please see
   https://sourceforge.net/p/forge/site-support/14336/
   The parameter is supported for pythons 2.7.9 and 3.4.3 and newer,
   due to older versions of urlopen do not support context parameter

 * Minor changes allow to run fetch_sources.py under both v2 and v3
   pythons. Changes required due to python 3 prohibits statement-style
   usage for print and forces print statements to be converted into
   functions. Also urlopen() is in library urllib.request in python 3

Components: Framework

VK-GL-CTS Issue #64

Change-Id: If4354332ea19878f8f72352e2df5cac9ee254ac6

external/fetch_sources.py
scripts/build/common.py

index c6b2be7..fa603cc 100644 (file)
@@ -24,10 +24,10 @@ import os
 import sys
 import shutil
 import tarfile
-import urllib2
 import hashlib
 import argparse
 import subprocess
+import ssl
 
 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "scripts"))
 
@@ -94,12 +94,30 @@ class SourcePackage (Source):
                        return None
 
        def storeExtractedChecksum (self, checksum):
-               writeFile(self.getExtractedChecksumFilePath(), checksum)
+               checksum_bytes = checksum.encode("utf-8")
+               writeFile(self.getExtractedChecksumFilePath(), checksum_bytes)
+
+       def connectToUrl (self, url):
+               result = None
+
+               if sys.version_info < (3, 0):
+                       from urllib2 import urlopen
+               else:
+                       from urllib.request import urlopen
+
+               if args.insecure:
+                       print("Ignoring certificate checks")
+                       ssl_context = ssl._create_unverified_context()
+                       result = urlopen(url, context=ssl_context)
+               else:
+                       result = urlopen(url)
+
+               return result
 
        def fetchAndVerifyArchive (self):
-               print "Fetching %s" % self.url
+               print("Fetching %s" % self.url)
 
-               req                     = urllib2.urlopen(self.url)
+               req                     = self.connectToUrl(self.url)
                data            = req.read()
                checksum        = computeChecksum(data)
                dstPath         = os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
@@ -113,7 +131,7 @@ class SourcePackage (Source):
                writeFile(dstPath, data)
 
        def extract (self):
-               print "Extracting %s to %s/%s" % (self.filename, self.baseDir, self.extractDir)
+               print("Extracting %s to %s/%s" % (self.filename, self.baseDir, self.extractDir))
 
                srcPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
                tmpPath = os.path.join(EXTERNAL_DIR, ".extract-tmp-%s" % self.baseDir)
@@ -240,12 +258,29 @@ PACKAGES = [
 ]
 
 def parseArgs ():
+       versionsForInsecure = ((2,7,9), (3,4,3))
+       versionsForInsecureStr = ' or '.join(('.'.join(str(x) for x in v)) for v in versionsForInsecure)
+
        parser = argparse.ArgumentParser(description = "Fetch external sources")
        parser.add_argument('--clean', dest='clean', action='store_true', default=False,
                                                help='Remove sources instead of fetching')
+       parser.add_argument('--insecure', dest='insecure', action='store_true', default=False,
+                                               help="Disable certificate check for external sources."
+                                               " Minimum python version required " + versionsForInsecureStr)
        parser.add_argument('--protocol', dest='protocol', default=None, choices=['ssh', 'https'],
                                                help="Select protocol to checkout git repositories.")
-       return parser.parse_args()
+
+       args = parser.parse_args()
+
+       if args.insecure:
+               for versionItem in versionsForInsecure:
+                       if (sys.version_info.major == versionItem[0]):
+                               if sys.version_info < versionItem:
+                                       parser.error("For --insecure minimum required python version is " +
+                                                               versionsForInsecureStr)
+                               break;
+
+       return args
 
 if __name__ == "__main__":
        args = parseArgs()
index 04ca49c..069905e 100644 (file)
@@ -27,7 +27,7 @@ import subprocess
 DEQP_DIR = os.path.realpath(os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..")))
 
 def die (msg):
-       print msg
+       print(msg)
        exit(-1)
 
 def shellquote(s):