@@ -35,19 +35,10 @@ def _get_xdg_cache_dir():
35
35
return pathlib .Path (cache_dir , 'matplotlib' )
36
36
37
37
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*."""
43
40
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 )
51
42
return hasher .hexdigest ()
52
43
53
44
@@ -58,10 +49,9 @@ def download_or_cache(url, sha):
58
49
Parameters
59
50
----------
60
51
url : str
61
- The url to download
62
-
52
+ The url to download.
63
53
sha : str
64
- The sha256 of the file
54
+ The sha256 of the file.
65
55
66
56
Returns
67
57
-------
@@ -70,52 +60,37 @@ def download_or_cache(url, sha):
70
60
"""
71
61
cache_dir = _get_xdg_cache_dir ()
72
62
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 )
96
71
97
72
# jQueryUI's website blocks direct downloads from urllib.request's
98
73
# default User-Agent, but not (for example) wget; so I don't feel too
99
74
# bad passing in an empty User-Agent.
100
75
with urllib .request .urlopen (
101
76
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 ()
106
78
79
+ file_sha = _get_hash (data )
107
80
if file_sha != sha :
108
81
raise Exception (
109
82
f"The download file does not match the expected sha. { url } was "
110
83
f"expected to have { sha } but it had { file_sha } " )
111
84
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
116
92
117
- file_contents .seek (0 )
118
- return file_contents
93
+ return BytesIO (data )
119
94
120
95
121
96
# SHA256 hashes of the FreeType tarballs
@@ -183,16 +158,6 @@ def print_status(package, status):
183
158
subsequent_indent = indent ))
184
159
185
160
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
-
196
161
@functools .lru_cache (1 ) # We only need to compute this once.
197
162
def get_pkg_config ():
198
163
"""
@@ -506,18 +471,13 @@ def do_custom_build(self):
506
471
if not src_path .exists ():
507
472
os .makedirs ('build' , exist_ok = True )
508
473
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
- ]
515
474
tarball = f'freetype-{ LOCAL_FREETYPE_VERSION } .tar.gz'
516
-
517
475
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
+ ]
521
481
522
482
for tarball_url in target_urls :
523
483
try :
@@ -533,10 +493,7 @@ def do_custom_build(self):
533
493
f"top-level of the source repository." )
534
494
535
495
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 :
540
497
tgz .extractall ("build" )
541
498
542
499
print (f"Building freetype in { src_path } " )
0 commit comments