1414This file can also be run as a script to install or upgrade setuptools.
1515"""
1616import os
17+ import shutil
1718import sys
1819import time
1920import fnmatch
2021import tempfile
2122import tarfile
23+ import optparse
24+
2225from distutils import log
2326
2427try :
@@ -46,7 +49,7 @@ def quote(arg):
4649 args = [quote (arg ) for arg in args ]
4750 return os .spawnl (os .P_WAIT , sys .executable , * args ) == 0
4851
49- DEFAULT_VERSION = "0.6.28 "
52+ DEFAULT_VERSION = "0.6.45 "
5053DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/"
5154SETUPTOOLS_FAKED_VERSION = "0.6c11"
5255
@@ -84,8 +87,11 @@ def _install(tarball, install_args=()):
8487 if not _python_cmd ('setup.py' , 'install' , * install_args ):
8588 log .warn ('Something went wrong during the installation.' )
8689 log .warn ('See the error message above.' )
90+ # exitcode will be 2
91+ return 2
8792 finally :
8893 os .chdir (old_wd )
94+ shutil .rmtree (tmpdir )
8995
9096
9197def _build_egg (egg , tarball , to_dir ):
@@ -110,6 +116,7 @@ def _build_egg(egg, tarball, to_dir):
110116
111117 finally :
112118 os .chdir (old_wd )
119+ shutil .rmtree (tmpdir )
113120 # returning the result
114121 log .warn (egg )
115122 if not os .path .exists (egg ):
@@ -137,6 +144,16 @@ def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
137144 try :
138145 try :
139146 import pkg_resources
147+
148+ # Setuptools 0.7b and later is a suitable (and preferable)
149+ # substitute for any Distribute version.
150+ try :
151+ pkg_resources .require ("setuptools>=0.7b" )
152+ return
153+ except (pkg_resources .DistributionNotFound ,
154+ pkg_resources .VersionConflict ):
155+ pass
156+
140157 if not hasattr (pkg_resources , '_distribute' ):
141158 if not no_fake :
142159 _fake_setuptools ()
@@ -232,7 +249,9 @@ def violation(*args):
232249
233250def _patch_file (path , content ):
234251 """Will backup the file then patch it"""
235- existing_content = open (path ).read ()
252+ f = open (path )
253+ existing_content = f .read ()
254+ f .close ()
236255 if existing_content == content :
237256 # already patched
238257 log .warn ('Already patched.' )
@@ -250,12 +269,15 @@ def _patch_file(path, content):
250269
251270
252271def _same_content (path , content ):
253- return open (path ).read () == content
272+ f = open (path )
273+ existing_content = f .read ()
274+ f .close ()
275+ return existing_content == content
254276
255277
256278def _rename_path (path ):
257279 new_name = path + '.OLD.%s' % time .time ()
258- log .warn ('Renaming %s into %s' , path , new_name )
280+ log .warn ('Renaming %s to %s' , path , new_name )
259281 os .rename (path , new_name )
260282 return new_name
261283
@@ -273,7 +295,7 @@ def _remove_flat_installation(placeholder):
273295 log .warn ('Could not locate setuptools*.egg-info' )
274296 return
275297
276- log .warn ('Removing elements out of the way...' )
298+ log .warn ('Moving elements out of the way...' )
277299 pkg_info = os .path .join (placeholder , file )
278300 if os .path .isdir (pkg_info ):
279301 patched = _patch_egg_dir (pkg_info )
@@ -314,11 +336,12 @@ def _create_fake_setuptools_pkg_info(placeholder):
314336 log .warn ('%s already exists' , pkg_info )
315337 return
316338
317- if not os .access (pkg_info , os .W_OK ):
318- log .warn ("Don't have permissions to write %s, skipping" , pkg_info )
319-
320339 log .warn ('Creating %s' , pkg_info )
321- f = open (pkg_info , 'w' )
340+ try :
341+ f = open (pkg_info , 'w' )
342+ except EnvironmentError :
343+ log .warn ("Don't have permissions to write %s, skipping" , pkg_info )
344+ return
322345 try :
323346 f .write (SETUPTOOLS_PKG_INFO )
324347 finally :
@@ -432,16 +455,17 @@ def _fake_setuptools():
432455 res = _patch_egg_dir (setuptools_location )
433456 if not res :
434457 return
435- log .warn ('Patched done .' )
458+ log .warn ('Patching complete .' )
436459 _relaunch ()
437460
438461
439462def _relaunch ():
440463 log .warn ('Relaunching...' )
441464 # we have to relaunch the process
442465 # pip marker to avoid a relaunch bug
443- _cmd = ['-c' , 'install' , '--single-version-externally-managed' ]
444- if sys .argv [:3 ] == _cmd :
466+ _cmd1 = ['-c' , 'install' , '--single-version-externally-managed' ]
467+ _cmd2 = ['-c' , 'install' , '--record' ]
468+ if sys .argv [:3 ] == _cmd1 or sys .argv [:3 ] == _cmd2 :
445469 sys .argv [0 ] = 'setup.py'
446470 args = [sys .executable ] + sys .argv
447471 sys .exit (subprocess .call (args ))
@@ -494,22 +518,39 @@ def sorter(dir1, dir2):
494518 self ._dbg (1 , "tarfile: %s" % e )
495519
496520
497- def _build_install_args (argv ):
521+ def _build_install_args (options ):
522+ """
523+ Build the arguments to 'python setup.py install' on the distribute package
524+ """
498525 install_args = []
499- user_install = '--user' in argv
500- if user_install and sys .version_info < (2 , 6 ):
501- log .warn ("--user requires Python 2.6 or later" )
502- raise SystemExit (1 )
503- if user_install :
526+ if options .user_install :
527+ if sys .version_info < (2 , 6 ):
528+ log .warn ("--user requires Python 2.6 or later" )
529+ raise SystemExit (1 )
504530 install_args .append ('--user' )
505531 return install_args
506532
507-
508- def main (argv , version = DEFAULT_VERSION ):
533+ def _parse_args ():
534+ """
535+ Parse the command line for options
536+ """
537+ parser = optparse .OptionParser ()
538+ parser .add_option (
539+ '--user' , dest = 'user_install' , action = 'store_true' , default = False ,
540+ help = 'install in user site package (requires Python 2.6 or later)' )
541+ parser .add_option (
542+ '--download-base' , dest = 'download_base' , metavar = "URL" ,
543+ default = DEFAULT_URL ,
544+ help = 'alternative URL from where to download the distribute package' )
545+ options , args = parser .parse_args ()
546+ # positional arguments are ignored
547+ return options
548+
549+ def main (version = DEFAULT_VERSION ):
509550 """Install or upgrade setuptools and EasyInstall"""
510- tarball = download_setuptools ()
511- _install ( tarball , _build_install_args ( argv ) )
512-
551+ options = _parse_args ()
552+ tarball = download_setuptools ( download_base = options . download_base )
553+ return _install ( tarball , _build_install_args ( options ))
513554
514555if __name__ == '__main__' :
515- main ( sys .argv [ 1 :] )
556+ sys .exit ( main () )
0 commit comments