From fe311da5d18817733ad6b858028a0eeb231c6381 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 27 Sep 2017 09:48:37 -0700 Subject: [PATCH] Fix download of freetype 2.6.1. by checking file hash before exiting the download loop. --- setupext.py | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/setupext.py b/setupext.py index 1b03b4d93bf2..997b3ea5ba5b 100644 --- a/setupext.py +++ b/setupext.py @@ -1166,40 +1166,39 @@ def do_custom_build(self): if not os.path.exists('build'): os.makedirs('build') - sourceforge_url = ( + url_fmts = [ 'https://downloads.sourceforge.net/project/freetype' - '/freetype2/{0}/'.format(LOCAL_FREETYPE_VERSION) - ) - url_fmts = ( - sourceforge_url + '{0}', - 'https://download.savannah.gnu.org/releases/freetype/{0}' - ) + '/freetype2/{version}/{tarball}', + 'https://download.savannah.gnu.org/releases/freetype' + '/{tarball}' + ] for url_fmt in url_fmts: - tarball_url = url_fmt.format(tarball) + tarball_url = url_fmt.format( + version=LOCAL_FREETYPE_VERSION, tarball=tarball) print("Downloading {0}".format(tarball_url)) try: urlretrieve(tarball_url, tarball_path) - except: + except IOError: # URLError (a subclass) on Py3. print("Failed to download {0}".format(tarball_url)) else: - break - if not os.path.isfile(tarball_path): + if get_file_hash(tarball_path) != LOCAL_FREETYPE_HASH: + print("Invalid hash.") + else: + break + else: raise IOError("Failed to download freetype") - if get_file_hash(tarball_path) == LOCAL_FREETYPE_HASH: - try: - os.makedirs(tarball_cache_dir) - except OSError: - # Don't care if it exists. - pass - try: - shutil.copy(tarball_path, tarball_cache_path) - print('Cached tarball at: {}' - .format(tarball_cache_path)) - except OSError: - # again, we do not care if this fails, can - # always re download - pass + try: + os.makedirs(tarball_cache_dir) + except OSError: + # Don't care if it exists. + pass + try: + shutil.copy(tarball_path, tarball_cache_path) + print('Cached tarball at: {}'.format(tarball_cache_path)) + except OSError: + # If this fails, we can always re-download. + pass if get_file_hash(tarball_path) != LOCAL_FREETYPE_HASH: raise IOError(