Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 532a06e

Browse files
committed
Verify hash of freetype tarball
1 parent cd12a12 commit 532a06e

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

setupext.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# of freetype. It must match the value in
2424
# lib/matplotlib.__init__.py:validate_test_dependencies
2525
LOCAL_FREETYPE_VERSION = '2.5.2'
26-
26+
# md5 hash of the freetype tarball
27+
LOCAL_FREETYPE_HASH = '004320381043d275c4e28bbacf05a1b7'
2728

2829
try:
2930
from subprocess import check_output
@@ -247,6 +248,21 @@ def make_extension(name, files, *args, **kwargs):
247248
return ext
248249

249250

251+
def get_file_hash(filename):
252+
"""
253+
Get the MD5 hash of a given filename.
254+
"""
255+
import hashlib
256+
BLOCKSIZE = 1 << 16
257+
hasher = hashlib.md5()
258+
with open(filename, 'rb') as fd:
259+
buf = fd.read(BLOCKSIZE)
260+
while len(buf) > 0:
261+
hasher.update(buf)
262+
buf = fd.read(BLOCKSIZE)
263+
return hasher.hexdigest()
264+
265+
250266
class PkgConfig(object):
251267
"""
252268
This is a class for communicating with pkg-config.
@@ -991,15 +1007,18 @@ def do_custom_build(self):
9911007
tarball = 'freetype-{0}.tar.gz'.format(LOCAL_FREETYPE_VERSION)
9921008
tarball_path = os.path.join('build', tarball)
9931009
if not os.path.isfile(tarball_path):
994-
print("Downloading {0}".format(tarball))
1010+
tarball_url = 'http://download.savannah.gnu.org/releases/freetype/{0}'.format(tarball)
1011+
1012+
print("Downloading {0}".format(tarball_url))
9951013
if sys.version_info[0] == 2:
9961014
from urllib import urlretrieve
9971015
else:
9981016
from urllib.request import urlretrieve
9991017

1000-
urlretrieve(
1001-
'http://download.savannah.gnu.org/releases/freetype/{0}'.format(tarball),
1002-
tarball_path)
1018+
urlretrieve(tarball_url, tarball_path)
1019+
1020+
if get_file_hash(tarball_path) != LOCAL_FREETYPE_HASH:
1021+
raise IOError("{0} does not match expected hash.".format(tarball))
10031022

10041023
print("Building {0}".format(tarball))
10051024
subprocess.check_call(

0 commit comments

Comments
 (0)