17
17
import tarfile
18
18
import textwrap
19
19
import urllib .request
20
+ from urllib .request import Request
20
21
import versioneer
21
22
import warnings
22
23
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
-
43
24
_log = logging .getLogger (__name__ )
44
25
45
26
@@ -95,30 +76,20 @@ def download_or_cache(url, sha):
95
76
def get_from_cache (local_fn ):
96
77
if cache_dir is None :
97
78
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 :
103
81
return None
104
82
buf .seek (0 )
105
83
return buf
106
84
107
85
def write_cache (local_fn , data ):
108
86
if cache_dir is None :
109
87
raise Exception ("no cache dir" )
110
-
111
88
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 )
119
90
old_pos = data .tell ()
120
91
data .seek (0 )
121
- with open (cache_filename , mode = mode ) as fout :
92
+ with open (cache_filename , "xb" ) as fout :
122
93
fout .write (data .read ())
123
94
data .seek (old_pos )
124
95
@@ -130,7 +101,7 @@ def write_cache(local_fn, data):
130
101
# jQueryUI's website blocks direct downloads from urllib.request's
131
102
# default User-Agent, but not (for example) wget; so I don't feel too
132
103
# bad passing in an empty User-Agent.
133
- with urlopen (
104
+ with urllib . request . urlopen (
134
105
Request (url , headers = {"User-Agent" : "" })) as req :
135
106
file_contents = BytesIO (req .read ())
136
107
file_contents .seek (0 )
@@ -899,8 +870,7 @@ def do_custom_build(self):
899
870
900
871
# do we need to download / load the source from cache?
901
872
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 )
904
874
905
875
url_fmts = [
906
876
('https://downloads.sourceforge.net/project/freetype'
0 commit comments