@@ -35,19 +35,10 @@ def _get_xdg_cache_dir():
3535 return pathlib .Path (cache_dir , 'matplotlib' )
3636
3737
38- def get_fd_hash (fd ):
39- """
40- Compute the sha256 hash of the bytes in a file-like
41- """
42- BLOCKSIZE = 1 << 16
38+ def _get_hash (data ):
39+ """Compute the sha256 hash of *data*."""
4340 hasher = hashlib .sha256 ()
44- old_pos = fd .tell ()
45- fd .seek (0 )
46- buf = fd .read (BLOCKSIZE )
47- while buf :
48- hasher .update (buf )
49- buf = fd .read (BLOCKSIZE )
50- fd .seek (old_pos )
41+ hasher .update (data )
5142 return hasher .hexdigest ()
5243
5344
@@ -58,10 +49,9 @@ def download_or_cache(url, sha):
5849 Parameters
5950 ----------
6051 url : str
61- The url to download
62-
52+ The url to download.
6353 sha : str
64- The sha256 of the file
54+ The sha256 of the file.
6555
6656 Returns
6757 -------
@@ -70,52 +60,37 @@ def download_or_cache(url, sha):
7060 """
7161 cache_dir = _get_xdg_cache_dir ()
7262
73- def get_from_cache (local_fn ):
74- if cache_dir is None :
75- raise Exception ("no cache dir" )
76- buf = BytesIO ((cache_dir / local_fn ).read_bytes ())
77- if get_fd_hash (buf ) != sha :
78- return None
79- buf .seek (0 )
80- return buf
81-
82- def write_cache (local_fn , data ):
83- if cache_dir is None :
84- raise Exception ("no cache dir" )
85- cache_dir .mkdir (parents = True , exist_ok = True )
86- old_pos = data .tell ()
87- data .seek (0 )
88- with open (cache_dir / local_fn , "xb" ) as fout :
89- fout .write (data .read ())
90- data .seek (old_pos )
91-
92- try :
93- return get_from_cache (sha )
94- except Exception :
95- pass
63+ if cache_dir is not None : # Try to read from cache.
64+ try :
65+ data = (cache_dir / sha ).read_bytes ()
66+ except IOError :
67+ pass
68+ else :
69+ if _get_hash (data ) == sha :
70+ return BytesIO (data )
9671
9772 # jQueryUI's website blocks direct downloads from urllib.request's
9873 # default User-Agent, but not (for example) wget; so I don't feel too
9974 # bad passing in an empty User-Agent.
10075 with urllib .request .urlopen (
10176 urllib .request .Request (url , headers = {"User-Agent" : "" })) as req :
102- file_contents = BytesIO (req .read ())
103- file_contents .seek (0 )
104-
105- file_sha = get_fd_hash (file_contents )
77+ data = req .read ()
10678
79+ file_sha = _get_hash (data )
10780 if file_sha != sha :
10881 raise Exception (
10982 f"The download file does not match the expected sha. { url } was "
11083 f"expected to have { sha } but it had { file_sha } " )
11184
112- try :
113- write_cache (sha , file_contents )
114- except Exception :
115- pass
85+ if cache_dir is not None : # Try to cache the downloaded file.
86+ try :
87+ cache_dir .mkdir (parents = True , exist_ok = True )
88+ with open (cache_dir / sha , "xb" ) as fout :
89+ fout .write (data )
90+ except IOError :
91+ pass
11692
117- file_contents .seek (0 )
118- return file_contents
93+ return BytesIO (data )
11994
12095
12196# SHA256 hashes of the FreeType tarballs
@@ -183,16 +158,6 @@ def print_status(package, status):
183158 subsequent_indent = indent ))
184159
185160
186- def get_buffer_hash (fd ):
187- BLOCKSIZE = 1 << 16
188- hasher = hashlib .sha256 ()
189- buf = fd .read (BLOCKSIZE )
190- while buf :
191- hasher .update (buf )
192- buf = fd .read (BLOCKSIZE )
193- return hasher .hexdigest ()
194-
195-
196161@functools .lru_cache (1 ) # We only need to compute this once.
197162def get_pkg_config ():
198163 """
@@ -506,18 +471,13 @@ def do_custom_build(self):
506471 if not src_path .exists ():
507472 os .makedirs ('build' , exist_ok = True )
508473
509- url_fmts = [
510- ('https://downloads.sourceforge.net/project/freetype'
511- '/freetype2/{version}/{tarball}' ),
512- ('https://download.savannah.gnu.org/releases/freetype'
513- '/{tarball}' )
514- ]
515474 tarball = f'freetype-{ LOCAL_FREETYPE_VERSION } .tar.gz'
516-
517475 target_urls = [
518- url_fmt .format (version = LOCAL_FREETYPE_VERSION ,
519- tarball = tarball )
520- for url_fmt in url_fmts ]
476+ (f'https://downloads.sourceforge.net/project/freetype'
477+ f'/freetype2/{ LOCAL_FREETYPE_VERSION } /{ tarball } ' ),
478+ (f'https://download.savannah.gnu.org/releases/freetype'
479+ f'/{ tarball } ' )
480+ ]
521481
522482 for tarball_url in target_urls :
523483 try :
@@ -533,10 +493,7 @@ def do_custom_build(self):
533493 f"top-level of the source repository." )
534494
535495 print (f"Extracting { tarball } " )
536- # just to be sure
537- tar_contents .seek (0 )
538- with tarfile .open (tarball , mode = "r:gz" ,
539- fileobj = tar_contents ) as tgz :
496+ with tarfile .open (fileobj = tar_contents , mode = "r:gz" ) as tgz :
540497 tgz .extractall ("build" )
541498
542499 print (f"Building freetype in { src_path } " )
0 commit comments