46
49
args = [quote(arg) for arg in args]
47
50
return os.spawnl(os.P_WAIT, sys.executable, *args) == 0
49
DEFAULT_VERSION = "0.6.8"
52
DEFAULT_VERSION = "0.6.32"
50
53
DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/"
54
SETUPTOOLS_FAKED_VERSION = "0.6c11"
51
56
SETUPTOOLS_PKG_INFO = """\
52
57
Metadata-Version: 1.0
64
def _install(tarball):
66
""" % SETUPTOOLS_FAKED_VERSION
69
def _install(tarball, install_args=()):
65
70
# extracting the tarball
66
71
tmpdir = tempfile.mkdtemp()
67
72
log.warn('Extracting in %s', tmpdir)
81
86
log.warn('Installing Distribute')
82
assert _python_cmd('setup.py', 'install')
87
if not _python_cmd('setup.py', 'install', *install_args):
88
log.warn('Something went wrong during the installation.')
89
log.warn('See the error message above.')
87
97
def _build_egg(egg, tarball, to_dir):
140
151
except ImportError:
141
152
return _do_download(version, download_base, to_dir, download_delay)
143
pkg_resources.require("distribute>="+version)
154
pkg_resources.require("distribute>=" + version)
145
156
except pkg_resources.VersionConflict:
146
157
e = sys.exc_info()[1]
164
175
_create_fake_setuptools_pkg_info(to_dir)
166
178
def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
167
179
to_dir=os.curdir, delay=15):
168
180
"""Download distribute from a specified location and return its filename
200
212
return os.path.realpath(saveto)
215
def _no_sandbox(function):
216
def __no_sandbox(*args, **kw):
218
from setuptools.sandbox import DirectorySandbox
219
if not hasattr(DirectorySandbox, '_old'):
220
def violation(*args):
222
DirectorySandbox._old = DirectorySandbox._violation
223
DirectorySandbox._violation = violation
231
return function(*args, **kw)
234
DirectorySandbox._violation = DirectorySandbox._old
235
del DirectorySandbox._old
203
240
def _patch_file(path, content):
204
241
"""Will backup the file then patch it"""
205
242
existing_content = open(path).read()
224
263
def _rename_path(path):
225
264
new_name = path + '.OLD.%s' % time.time()
226
log.warn('Renaming %s into %s', path, new_name)
228
from setuptools.sandbox import DirectorySandbox
229
def _violation(*args):
231
DirectorySandbox._violation = _violation
265
log.warn('Renaming %s to %s', path, new_name)
235
266
os.rename(path, new_name)
249
280
log.warn('Could not locate setuptools*.egg-info')
252
log.warn('Removing elements out of the way...')
283
log.warn('Moving elements out of the way...')
253
284
pkg_info = os.path.join(placeholder, file)
254
285
if os.path.isdir(pkg_info):
255
286
patched = _patch_egg_dir(pkg_info)
269
300
'Setuptools distribution', element)
303
_remove_flat_installation = _no_sandbox(_remove_flat_installation)
273
306
def _after_install(dist):
274
307
log.warn('After install bootstrap.')
275
308
placeholder = dist.get_command_obj('install').install_purelib
276
309
_create_fake_setuptools_pkg_info(placeholder)
278
312
def _create_fake_setuptools_pkg_info(placeholder):
279
313
if not placeholder or not os.path.exists(placeholder):
280
314
log.warn('Could not find the install location')
282
316
pyver = '%s.%s' % (sys.version_info[0], sys.version_info[1])
283
setuptools_file = 'setuptools-0.6c9-py%s.egg-info' % pyver
317
setuptools_file = 'setuptools-%s-py%s.egg-info' % \
318
(SETUPTOOLS_FAKED_VERSION, pyver)
284
319
pkg_info = os.path.join(placeholder, setuptools_file)
285
320
if os.path.exists(pkg_info):
286
321
log.warn('%s already exists', pkg_info)
288
324
log.warn('Creating %s', pkg_info)
289
f = open(pkg_info, 'w')
326
f = open(pkg_info, 'w')
327
except EnvironmentError:
328
log.warn("Don't have permissions to write %s, skipping", pkg_info)
291
331
f.write(SETUPTOOLS_PKG_INFO)
294
335
pth_file = os.path.join(placeholder, 'setuptools.pth')
295
336
log.warn('Creating %s', pth_file)
296
337
f = open(pth_file, 'w')
327
374
def _under_prefix(location):
328
375
if 'install' not in sys.argv:
330
args = sys.argv[sys.argv.index('install')+1:]
377
args = sys.argv[sys.argv.index('install') + 1:]
331
378
for index, arg in enumerate(args):
332
379
for option in ('--root', '--prefix'):
333
380
if arg.startswith('%s=' % option):
335
382
return location.startswith(top_dir)
336
383
elif arg == option:
337
384
if len(args) > index:
338
top_dir = args[index+1]
385
top_dir = args[index + 1]
339
386
return location.startswith(top_dir)
340
elif option == '--user' and USER_SITE is not None:
341
return location.startswith(USER_SITE)
387
if arg == '--user' and USER_SITE is not None:
388
return location.startswith(USER_SITE)
353
400
ws = pkg_resources.working_set
355
setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools',
402
setuptools_dist = ws.find(
403
pkg_resources.Requirement.parse('setuptools', replacement=False)
357
405
except TypeError:
358
406
# old distribute API
359
setuptools_dist = ws.find(pkg_resources.Requirement.parse('setuptools'))
407
setuptools_dist = ws.find(
408
pkg_resources.Requirement.parse('setuptools')
361
411
if setuptools_dist is None:
362
412
log.warn('No setuptools distribution found')
390
440
res = _patch_egg_dir(setuptools_location)
393
log.warn('Patched done.')
443
log.warn('Patching complete.')
398
448
log.warn('Relaunching...')
399
449
# we have to relaunch the process
450
# pip marker to avoid a relaunch bug
451
_cmd1 = ['-c', 'install', '--single-version-externally-managed']
452
_cmd2 = ['-c', 'install', '--record']
453
if sys.argv[:3] == _cmd1 or sys.argv[:3] == _cmd2:
454
sys.argv[0] = 'setup.py'
400
455
args = [sys.executable] + sys.argv
401
456
sys.exit(subprocess.call(args))
421
476
# Extract directories with a safe mode.
422
477
directories.append(tarinfo)
423
478
tarinfo = copy.copy(tarinfo)
424
tarinfo.mode = 448 # decimal for oct 0700
479
tarinfo.mode = 448 # decimal for oct 0700
425
480
self.extract(tarinfo, path)
427
482
# Reverse sort directories.
448
503
self._dbg(1, "tarfile: %s" % e)
451
def main(argv, version=DEFAULT_VERSION):
506
def _build_install_args(options):
508
Build the arguments to 'python setup.py install' on the distribute package
511
if options.user_install:
512
if sys.version_info < (2, 6):
513
log.warn("--user requires Python 2.6 or later")
515
install_args.append('--user')
520
Parse the command line for options
522
parser = optparse.OptionParser()
524
'--user', dest='user_install', action='store_true', default=False,
525
help='install in user site package (requires Python 2.6 or later)')
527
'--download-base', dest='download_base', metavar="URL",
529
help='alternative URL from where to download the distribute package')
530
options, args = parser.parse_args()
531
# positional arguments are ignored
534
def main(version=DEFAULT_VERSION):
452
535
"""Install or upgrade setuptools and EasyInstall"""
453
tarball = download_setuptools()
536
options = _parse_args()
537
tarball = download_setuptools(download_base=options.download_base)
538
return _install(tarball, _build_install_args(options))
457
540
if __name__ == '__main__':