1717import tarfile
1818import textwrap
1919import urllib .request
20+ from urllib .request import Request
2021import versioneer
2122import warnings
2223
23- if sys .version_info < (3 , ):
24- from urllib2 import urlopen , Request
25-
26- class FileExistsError (OSError ):
27- pass
28-
29- def makedirs (path , exist_ok = True ):
30- if not exist_ok :
31- raise ValueError ("this backport only supports exist_ok is True" )
32- if not path or os .path .exists (path ):
33- return
34- head , tail = os .path .split (path )
35-
36- makedirs (head , exist_ok = True )
37- os .makedirs (path )
38-
39- else :
40- from urllib .request import urlopen , Request
41- from os import makedirs
42-
4324_log = logging .getLogger (__name__ )
4425
4526
@@ -95,30 +76,20 @@ def download_or_cache(url, sha):
9576 def get_from_cache (local_fn ):
9677 if cache_dir is None :
9778 raise Exception ("no cache dir" )
98- cache_filename = os .path .join (cache_dir , local_fn )
99- with open (cache_filename , 'rb' ) as fin :
100- buf = BytesIO (fin .read ())
101- file_sha = get_fd_hash (buf )
102- if file_sha != sha :
79+ buf = BytesIO (pathlib .Path (cache_dir , local_fn ).read_bytes ())
80+ if get_fd_hash (buf ) != sha :
10381 return None
10482 buf .seek (0 )
10583 return buf
10684
10785 def write_cache (local_fn , data ):
10886 if cache_dir is None :
10987 raise Exception ("no cache dir" )
110-
11188 cache_filename = os .path .join (cache_dir , local_fn )
112- makedirs (cache_dir , exist_ok = True )
113- if sys .version_info < (3 , ):
114- if os .path .exists (cache_filename ):
115- raise FileExistsError
116- mode = 'wb'
117- else :
118- mode = 'xb'
89+ os .makedirs (cache_dir , exist_ok = True )
11990 old_pos = data .tell ()
12091 data .seek (0 )
121- with open (cache_filename , mode = mode ) as fout :
92+ with open (cache_filename , "xb" ) as fout :
12293 fout .write (data .read ())
12394 data .seek (old_pos )
12495
@@ -130,7 +101,7 @@ def write_cache(local_fn, data):
130101 # jQueryUI's website blocks direct downloads from urllib.request's
131102 # default User-Agent, but not (for example) wget; so I don't feel too
132103 # bad passing in an empty User-Agent.
133- with urlopen (
104+ with urllib . request . urlopen (
134105 Request (url , headers = {"User-Agent" : "" })) as req :
135106 file_contents = BytesIO (req .read ())
136107 file_contents .seek (0 )
@@ -899,8 +870,7 @@ def do_custom_build(self):
899870
900871 # do we need to download / load the source from cache?
901872 if not os .path .exists (src_path ):
902- if not os .path .exists ('build' ):
903- os .makedirs ('build' )
873+ os .makedirs ('build' , exist_ok = True )
904874
905875 url_fmts = [
906876 ('https://downloads.sourceforge.net/project/freetype'
0 commit comments