2626
2727from distutils import log
2828
29+ try :
30+ from urllib .request import urlopen
31+ except ImportError :
32+ from urllib2 import urlopen
33+
2934try :
3035 from site import USER_SITE
3136except ImportError :
3237 USER_SITE = None
3338
34- DEFAULT_VERSION = "3.6 "
39+ DEFAULT_VERSION = "5.7 "
3540DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
3641
3742def _python_cmd (* args ):
@@ -64,17 +69,24 @@ def _build_egg(egg, archive_filename, to_dir):
6469 raise IOError ('Could not build the egg.' )
6570
6671
67- def get_zip_class ( ):
72+ class ContextualZipFile ( zipfile . ZipFile ):
6873 """
6974 Supplement ZipFile class to support context manager for Python 2.6
7075 """
71- class ContextualZipFile (zipfile .ZipFile ):
72- def __enter__ (self ):
73- return self
74- def __exit__ (self , type , value , traceback ):
75- self .close
76- return zipfile .ZipFile if hasattr (zipfile .ZipFile , '__exit__' ) else \
77- ContextualZipFile
76+
77+ def __enter__ (self ):
78+ return self
79+
80+ def __exit__ (self , type , value , traceback ):
81+ self .close ()
82+
83+ def __new__ (cls , * args , ** kwargs ):
84+ """
85+ Construct a ZipFile or ContextualZipFile as appropriate
86+ """
87+ if hasattr (zipfile .ZipFile , '__exit__' ):
88+ return zipfile .ZipFile (* args , ** kwargs )
89+ return super (ContextualZipFile , cls ).__new__ (cls )
7890
7991
8092@contextlib .contextmanager
@@ -85,7 +97,7 @@ def archive_context(filename):
8597 old_wd = os .getcwd ()
8698 try :
8799 os .chdir (tmpdir )
88- with get_zip_class () (filename ) as archive :
100+ with ContextualZipFile (filename ) as archive :
89101 archive .extractall ()
90102
91103 # going in the directory
@@ -183,14 +195,11 @@ def has_powershell():
183195 if platform .system () != 'Windows' :
184196 return False
185197 cmd = ['powershell' , '-Command' , 'echo test' ]
186- devnull = open (os .path .devnull , 'wb' )
187- try :
198+ with open (os .path .devnull , 'wb' ) as devnull :
188199 try :
189200 subprocess .check_call (cmd , stdout = devnull , stderr = devnull )
190201 except Exception :
191202 return False
192- finally :
193- devnull .close ()
194203 return True
195204
196205download_file_powershell .viable = has_powershell
@@ -201,14 +210,11 @@ def download_file_curl(url, target):
201210
202211def has_curl ():
203212 cmd = ['curl' , '--version' ]
204- devnull = open (os .path .devnull , 'wb' )
205- try :
213+ with open (os .path .devnull , 'wb' ) as devnull :
206214 try :
207215 subprocess .check_call (cmd , stdout = devnull , stderr = devnull )
208216 except Exception :
209217 return False
210- finally :
211- devnull .close ()
212218 return True
213219
214220download_file_curl .viable = has_curl
@@ -219,14 +225,11 @@ def download_file_wget(url, target):
219225
220226def has_wget ():
221227 cmd = ['wget' , '--version' ]
222- devnull = open (os .path .devnull , 'wb' )
223- try :
228+ with open (os .path .devnull , 'wb' ) as devnull :
224229 try :
225230 subprocess .check_call (cmd , stdout = devnull , stderr = devnull )
226231 except Exception :
227232 return False
228- finally :
229- devnull .close ()
230233 return True
231234
232235download_file_wget .viable = has_wget
@@ -236,45 +239,36 @@ def download_file_insecure(url, target):
236239 Use Python to download the file, even though it cannot authenticate the
237240 connection.
238241 """
242+ src = urlopen (url )
239243 try :
240- from urllib .request import urlopen
241- except ImportError :
242- from urllib2 import urlopen
243- src = dst = None
244- try :
245- src = urlopen (url )
246- # Read/write all in one block, so we don't create a corrupt file
247- # if the download is interrupted.
244+ # Read all the data in one block.
248245 data = src .read ()
249- dst = open (target , "wb" )
250- dst .write (data )
251246 finally :
252- if src :
253- src .close ()
254- if dst :
255- dst .close ()
247+ src .close ()
248+
249+ # Write all the data in one block to avoid creating a partial file.
250+ with open (target , "wb" ) as dst :
251+ dst .write (data )
256252
257253download_file_insecure .viable = lambda : True
258254
259255def get_best_downloader ():
260- downloaders = [
256+ downloaders = (
261257 download_file_powershell ,
262258 download_file_curl ,
263259 download_file_wget ,
264260 download_file_insecure ,
265- ]
266-
267- for dl in downloaders :
268- if dl .viable ():
269- return dl
261+ )
262+ viable_downloaders = (dl for dl in downloaders if dl .viable ())
263+ return next (viable_downloaders , None )
270264
271265def download_setuptools (version = DEFAULT_VERSION , download_base = DEFAULT_URL ,
272266 to_dir = os .curdir , delay = 15 , downloader_factory = get_best_downloader ):
273267 """
274268 Download setuptools from a specified location and return its filename
275269
276270 `version` should be a valid setuptools version number that is available
277- as an egg for download under the `download_base` URL (which should end
271+ as an sdist for download under the `download_base` URL (which should end
278272 with a '/'). `to_dir` is the directory where the egg will be downloaded.
279273 `delay` is the number of seconds to pause before an actual download
280274 attempt.
0 commit comments