OLD | NEW |
(Empty) | |
| 1 |
| 2 """Bootstrap distribute installation |
| 3 |
| 4 If you want to use setuptools in your package's setup.py, just include this |
| 5 file in the same directory with it, and add this to the top of your setup.py:: |
| 6 |
| 7 from distribute_setup import use_setuptools |
| 8 use_setuptools() |
| 9 |
| 10 If you want to require a specific version of setuptools, set a download |
| 11 mirror, or use an alternate download directory, you can do so by supplying |
| 12 the appropriate options to ``use_setuptools()``. |
| 13 |
| 14 This file can also be run as a script to install or upgrade setuptools. |
| 15 """ |
| 16 import os |
| 17 import shutil |
| 18 import sys |
| 19 import time |
| 20 import fnmatch |
| 21 import tempfile |
| 22 import tarfile |
| 23 import optparse |
| 24 |
| 25 from distutils import log |
| 26 |
| 27 try: |
| 28 from site import USER_SITE |
| 29 except ImportError: |
| 30 USER_SITE = None |
| 31 |
| 32 try: |
| 33 import subprocess |
| 34 |
| 35 def _python_cmd(*args): |
| 36 args = (sys.executable,) + args |
| 37 return subprocess.call(args) == 0 |
| 38 |
| 39 except ImportError: |
| 40 def _python_cmd(*args): |
| 41 args = (sys.executable,) + args |
| 42 if sys.platform == 'win32': |
| 43 def quote(arg): |
| 44 if ' ' in arg: |
| 45 return '"%s"' % arg |
| 46 return arg |
| 47 args = [quote(arg) for arg in args] |
| 48 return os.spawnl(os.P_WAIT, sys.executable, *args) == 0 |
| 49 |
| 50 DEFAULT_VERSION = "0.6.49" |
| 51 DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/" |
| 52 SETUPTOOLS_FAKED_VERSION = "0.6c11" |
| 53 |
| 54 SETUPTOOLS_PKG_INFO = """\ |
| 55 Metadata-Version: 1.0 |
| 56 Name: setuptools |
| 57 Version: %s |
| 58 Summary: xxxx |
| 59 Home-page: xxx |
| 60 Author: xxx |
| 61 Author-email: xxx |
| 62 License: xxx |
| 63 Description: xxx |
| 64 """ % SETUPTOOLS_FAKED_VERSION |
| 65 |
| 66 |
| 67 def _install(tarball, install_args=()): |
| 68 tmpdir = tempfile.mkdtemp() |
| 69 log.warn('Extracting in %s', tmpdir) |
| 70 old_wd = os.getcwd() |
| 71 try: |
| 72 os.chdir(tmpdir) |
| 73 tar = tarfile.open(tarball) |
| 74 _extractall(tar) |
| 75 tar.close() |
| 76 |
| 77 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) |
| 78 os.chdir(subdir) |
| 79 log.warn('Now working in %s', subdir) |
| 80 |
| 81 log.warn('Installing Distribute') |
| 82 if not _python_cmd('setup.py', 'install', *install_args): |
| 83 log.warn('Something went wrong during the installation.') |
| 84 log.warn('See the error message above.') |
| 85 return 2 |
| 86 finally: |
| 87 os.chdir(old_wd) |
| 88 shutil.rmtree(tmpdir) |
| 89 |
| 90 |
| 91 def _build_egg(egg, tarball, to_dir): |
| 92 tmpdir = tempfile.mkdtemp() |
| 93 log.warn('Extracting in %s', tmpdir) |
| 94 old_wd = os.getcwd() |
| 95 try: |
| 96 os.chdir(tmpdir) |
| 97 tar = tarfile.open(tarball) |
| 98 _extractall(tar) |
| 99 tar.close() |
| 100 |
| 101 subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) |
| 102 os.chdir(subdir) |
| 103 log.warn('Now working in %s', subdir) |
| 104 |
| 105 log.warn('Building a Distribute egg in %s', to_dir) |
| 106 _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir) |
| 107 |
| 108 finally: |
| 109 os.chdir(old_wd) |
| 110 shutil.rmtree(tmpdir) |
| 111 log.warn(egg) |
| 112 if not os.path.exists(egg): |
| 113 raise IOError('Could not build the egg.') |
| 114 |
| 115 |
| 116 def _do_download(version, download_base, to_dir, download_delay): |
| 117 egg = os.path.join(to_dir, 'distribute-%s-py%d.%d.egg' |
| 118 % (version, sys.version_info[0], sys.version_info[1])) |
| 119 if not os.path.exists(egg): |
| 120 tarball = download_setuptools(version, download_base, |
| 121 to_dir, download_delay) |
| 122 _build_egg(egg, tarball, to_dir) |
| 123 sys.path.insert(0, egg) |
| 124 import setuptools |
| 125 setuptools.bootstrap_install_from = egg |
| 126 |
| 127 |
| 128 def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, |
| 129 to_dir=os.curdir, download_delay=15, no_fake=True): |
| 130 to_dir = os.path.abspath(to_dir) |
| 131 was_imported = 'pkg_resources' in sys.modules or \ |
| 132 'setuptools' in sys.modules |
| 133 try: |
| 134 try: |
| 135 import pkg_resources |
| 136 |
| 137 try: |
| 138 pkg_resources.require("setuptools>=0.7b") |
| 139 return |
| 140 except (pkg_resources.DistributionNotFound, |
| 141 pkg_resources.VersionConflict): |
| 142 pass |
| 143 |
| 144 if not hasattr(pkg_resources, '_distribute'): |
| 145 if not no_fake: |
| 146 _fake_setuptools() |
| 147 raise ImportError |
| 148 except ImportError: |
| 149 return _do_download(version, download_base, to_dir, download_delay) |
| 150 try: |
| 151 pkg_resources.require("distribute>=" + version) |
| 152 return |
| 153 except pkg_resources.VersionConflict: |
| 154 e = sys.exc_info()[1] |
| 155 if was_imported: |
| 156 sys.stderr.write( |
| 157 "The required version of distribute (>=%s) is not available,\n" |
| 158 "and can't be installed while this script is running. Please\n" |
| 159 "install a more recent version first, using\n" |
| 160 "'easy_install -U distribute'." |
| 161 "\n\n(Currently using %r)\n" % (version, e.args[0])) |
| 162 sys.exit(2) |
| 163 else: |
| 164 del pkg_resources, sys.modules['pkg_resources'] |
| 165 return _do_download(version, download_base, to_dir, |
| 166 download_delay) |
| 167 except pkg_resources.DistributionNotFound: |
| 168 return _do_download(version, download_base, to_dir, |
| 169 download_delay) |
| 170 finally: |
| 171 if not no_fake: |
| 172 _create_fake_setuptools_pkg_info(to_dir) |
| 173 |
| 174 |
| 175 def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL, |
| 176 to_dir=os.curdir, delay=15): |
| 177 """Download distribute from a specified location and return its filename |
| 178 |
| 179 `version` should be a valid distribute version number that is available |
| 180 as an egg for download under the `download_base` URL (which should end |
| 181 with a '/'). `to_dir` is the directory where the egg will be downloaded. |
| 182 `delay` is the number of seconds to pause before an actual download |
| 183 attempt. |
| 184 """ |
| 185 to_dir = os.path.abspath(to_dir) |
| 186 try: |
| 187 from urllib.request import urlopen |
| 188 except ImportError: |
| 189 from urllib2 import urlopen |
| 190 tgz_name = "distribute-%s.tar.gz" % version |
| 191 url = download_base + tgz_name |
| 192 saveto = os.path.join(to_dir, tgz_name) |
| 193 src = dst = None |
| 194 if not os.path.exists(saveto): |
| 195 try: |
| 196 log.warn("Downloading %s", url) |
| 197 src = urlopen(url) |
| 198 data = src.read() |
| 199 dst = open(saveto, "wb") |
| 200 dst.write(data) |
| 201 finally: |
| 202 if src: |
| 203 src.close() |
| 204 if dst: |
| 205 dst.close() |
| 206 return os.path.realpath(saveto) |
| 207 |
| 208 |
| 209 def _no_sandbox(function): |
| 210 def __no_sandbox(*args, **kw): |
| 211 try: |
| 212 from setuptools.sandbox import DirectorySandbox |
| 213 if not hasattr(DirectorySandbox, '_old'): |
| 214 def violation(*args): |
| 215 pass |
| 216 DirectorySandbox._old = DirectorySandbox._violation |
| 217 DirectorySandbox._violation = violation |
| 218 patched = True |
| 219 else: |
| 220 patched = False |
| 221 except ImportError: |
| 222 patched = False |
| 223 |
| 224 try: |
| 225 return function(*args, **kw) |
| 226 finally: |
| 227 if patched: |
| 228 DirectorySandbox._violation = DirectorySandbox._old |
| 229 del DirectorySandbox._old |
| 230 |
| 231 return __no_sandbox |
| 232 |
| 233 |
| 234 def _patch_file(path, content): |
| 235 """Will backup the file then patch it""" |
| 236 f = open(path) |
| 237 existing_content = f.read() |
| 238 f.close() |
| 239 if existing_content == content: |
| 240 log.warn('Already patched.') |
| 241 return False |
| 242 log.warn('Patching...') |
| 243 _rename_path(path) |
| 244 f = open(path, 'w') |
| 245 try: |
| 246 f.write(content) |
| 247 finally: |
| 248 f.close() |
| 249 return True |
| 250 |
| 251 _patch_file = _no_sandbox(_patch_file) |
| 252 |
| 253 |
| 254 def _same_content(path, content): |
| 255 f = open(path) |
| 256 existing_content = f.read() |
| 257 f.close() |
| 258 return existing_content == content |
| 259 |
| 260 |
| 261 def _rename_path(path): |
| 262 new_name = path + '.OLD.%s' % time.time() |
| 263 log.warn('Renaming %s to %s', path, new_name) |
| 264 os.rename(path, new_name) |
| 265 return new_name |
| 266 |
| 267 |
| 268 def _remove_flat_installation(placeholder): |
| 269 if not os.path.isdir(placeholder): |
| 270 log.warn('Unkown installation at %s', placeholder) |
| 271 return False |
| 272 found = False |
| 273 for file in os.listdir(placeholder): |
| 274 if fnmatch.fnmatch(file, 'setuptools*.egg-info'): |
| 275 found = True |
| 276 break |
| 277 if not found: |
| 278 log.warn('Could not locate setuptools*.egg-info') |
| 279 return |
| 280 |
| 281 log.warn('Moving elements out of the way...') |
| 282 pkg_info = os.path.join(placeholder, file) |
| 283 if os.path.isdir(pkg_info): |
| 284 patched = _patch_egg_dir(pkg_info) |
| 285 else: |
| 286 patched = _patch_file(pkg_info, SETUPTOOLS_PKG_INFO) |
| 287 |
| 288 if not patched: |
| 289 log.warn('%s already patched.', pkg_info) |
| 290 return False |
| 291 for element in ('setuptools', 'pkg_resources.py', 'site.py'): |
| 292 element = os.path.join(placeholder, element) |
| 293 if os.path.exists(element): |
| 294 _rename_path(element) |
| 295 else: |
| 296 log.warn('Could not find the %s element of the ' |
| 297 'Setuptools distribution', element) |
| 298 return True |
| 299 |
| 300 _remove_flat_installation = _no_sandbox(_remove_flat_installation) |
| 301 |
| 302 |
| 303 def _after_install(dist): |
| 304 log.warn('After install bootstrap.') |
| 305 placeholder = dist.get_command_obj('install').install_purelib |
| 306 _create_fake_setuptools_pkg_info(placeholder) |
| 307 |
| 308 |
| 309 def _create_fake_setuptools_pkg_info(placeholder): |
| 310 if not placeholder or not os.path.exists(placeholder): |
| 311 log.warn('Could not find the install location') |
| 312 return |
| 313 pyver = '%s.%s' % (sys.version_info[0], sys.version_info[1]) |
| 314 setuptools_file = 'setuptools-%s-py%s.egg-info' % \ |
| 315 (SETUPTOOLS_FAKED_VERSION, pyver) |
| 316 pkg_info = os.path.join(placeholder, setuptools_file) |
| 317 if os.path.exists(pkg_info): |
| 318 log.warn('%s already exists', pkg_info) |
| 319 return |
| 320 |
| 321 log.warn('Creating %s', pkg_info) |
| 322 try: |
| 323 f = open(pkg_info, 'w') |
| 324 except EnvironmentError: |
| 325 log.warn("Don't have permissions to write %s, skipping", pkg_info) |
| 326 return |
| 327 try: |
| 328 f.write(SETUPTOOLS_PKG_INFO) |
| 329 finally: |
| 330 f.close() |
| 331 |
| 332 pth_file = os.path.join(placeholder, 'setuptools.pth') |
| 333 log.warn('Creating %s', pth_file) |
| 334 f = open(pth_file, 'w') |
| 335 try: |
| 336 f.write(os.path.join(os.curdir, setuptools_file)) |
| 337 finally: |
| 338 f.close() |
| 339 |
| 340 _create_fake_setuptools_pkg_info = _no_sandbox( |
| 341 _create_fake_setuptools_pkg_info |
| 342 ) |
| 343 |
| 344 |
| 345 def _patch_egg_dir(path): |
| 346 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO') |
| 347 if os.path.exists(pkg_info): |
| 348 if _same_content(pkg_info, SETUPTOOLS_PKG_INFO): |
| 349 log.warn('%s already patched.', pkg_info) |
| 350 return False |
| 351 _rename_path(path) |
| 352 os.mkdir(path) |
| 353 os.mkdir(os.path.join(path, 'EGG-INFO')) |
| 354 pkg_info = os.path.join(path, 'EGG-INFO', 'PKG-INFO') |
| 355 f = open(pkg_info, 'w') |
| 356 try: |
| 357 f.write(SETUPTOOLS_PKG_INFO) |
| 358 finally: |
| 359 f.close() |
| 360 return True |
| 361 |
| 362 _patch_egg_dir = _no_sandbox(_patch_egg_dir) |
| 363 |
| 364 |
| 365 def _before_install(): |
| 366 log.warn('Before install bootstrap.') |
| 367 _fake_setuptools() |
| 368 |
| 369 |
| 370 def _under_prefix(location): |
| 371 if 'install' not in sys.argv: |
| 372 return True |
| 373 args = sys.argv[sys.argv.index('install') + 1:] |
| 374 for index, arg in enumerate(args): |
| 375 for option in ('--root', '--prefix'): |
| 376 if arg.startswith('%s=' % option): |
| 377 top_dir = arg.split('root=')[-1] |
| 378 return location.startswith(top_dir) |
| 379 elif arg == option: |
| 380 if len(args) > index: |
| 381 top_dir = args[index + 1] |
| 382 return location.startswith(top_dir) |
| 383 if arg == '--user' and USER_SITE is not None: |
| 384 return location.startswith(USER_SITE) |
| 385 return True |
| 386 |
| 387 |
| 388 def _fake_setuptools(): |
| 389 log.warn('Scanning installed packages') |
| 390 try: |
| 391 import pkg_resources |
| 392 except ImportError: |
| 393 log.warn('Setuptools or Distribute does not seem to be installed.') |
| 394 return |
| 395 ws = pkg_resources.working_set |
| 396 try: |
| 397 setuptools_dist = ws.find( |
| 398 pkg_resources.Requirement.parse('setuptools', replacement=False) |
| 399 ) |
| 400 except TypeError: |
| 401 setuptools_dist = ws.find( |
| 402 pkg_resources.Requirement.parse('setuptools') |
| 403 ) |
| 404 |
| 405 if setuptools_dist is None: |
| 406 log.warn('No setuptools distribution found') |
| 407 return |
| 408 setuptools_location = setuptools_dist.location |
| 409 log.warn('Setuptools installation detected at %s', setuptools_location) |
| 410 |
| 411 if not _under_prefix(setuptools_location): |
| 412 log.warn('Not patching, --root or --prefix is installing Distribute' |
| 413 ' in another location') |
| 414 return |
| 415 |
| 416 if not setuptools_location.endswith('.egg'): |
| 417 log.warn('Non-egg installation') |
| 418 res = _remove_flat_installation(setuptools_location) |
| 419 if not res: |
| 420 return |
| 421 else: |
| 422 log.warn('Egg installation') |
| 423 pkg_info = os.path.join(setuptools_location, 'EGG-INFO', 'PKG-INFO') |
| 424 if (os.path.exists(pkg_info) and |
| 425 _same_content(pkg_info, SETUPTOOLS_PKG_INFO)): |
| 426 log.warn('Already patched.') |
| 427 return |
| 428 log.warn('Patching...') |
| 429 res = _patch_egg_dir(setuptools_location) |
| 430 if not res: |
| 431 return |
| 432 log.warn('Patching complete.') |
| 433 _relaunch() |
| 434 |
| 435 |
| 436 def _relaunch(): |
| 437 log.warn('Relaunching...') |
| 438 _cmd1 = ['-c', 'install', '--single-version-externally-managed'] |
| 439 _cmd2 = ['-c', 'install', '--record'] |
| 440 if sys.argv[:3] == _cmd1 or sys.argv[:3] == _cmd2: |
| 441 sys.argv[0] = 'setup.py' |
| 442 args = [sys.executable] + sys.argv |
| 443 sys.exit(subprocess.call(args)) |
| 444 |
| 445 |
| 446 def _extractall(self, path=".", members=None): |
| 447 """Extract all members from the archive to the current working |
| 448 directory and set owner, modification time and permissions on |
| 449 directories afterwards. `path' specifies a different directory |
| 450 to extract to. `members' is optional and must be a subset of the |
| 451 list returned by getmembers(). |
| 452 """ |
| 453 import copy |
| 454 import operator |
| 455 from tarfile import ExtractError |
| 456 directories = [] |
| 457 |
| 458 if members is None: |
| 459 members = self |
| 460 |
| 461 for tarinfo in members: |
| 462 if tarinfo.isdir(): |
| 463 directories.append(tarinfo) |
| 464 tarinfo = copy.copy(tarinfo) |
| 465 tarinfo.mode = 448 |
| 466 self.extract(tarinfo, path) |
| 467 |
| 468 if sys.version_info < (2, 4): |
| 469 def sorter(dir1, dir2): |
| 470 return cmp(dir1.name, dir2.name) |
| 471 directories.sort(sorter) |
| 472 directories.reverse() |
| 473 else: |
| 474 directories.sort(key=operator.attrgetter('name'), reverse=True) |
| 475 |
| 476 for tarinfo in directories: |
| 477 dirpath = os.path.join(path, tarinfo.name) |
| 478 try: |
| 479 self.chown(tarinfo, dirpath) |
| 480 self.utime(tarinfo, dirpath) |
| 481 self.chmod(tarinfo, dirpath) |
| 482 except ExtractError: |
| 483 e = sys.exc_info()[1] |
| 484 if self.errorlevel > 1: |
| 485 raise |
| 486 else: |
| 487 self._dbg(1, "tarfile: %s" % e) |
| 488 |
| 489 |
| 490 def _build_install_args(options): |
| 491 """ |
| 492 Build the arguments to 'python setup.py install' on the distribute package |
| 493 """ |
| 494 install_args = [] |
| 495 if options.user_install: |
| 496 if sys.version_info < (2, 6): |
| 497 log.warn("--user requires Python 2.6 or later") |
| 498 raise SystemExit(1) |
| 499 install_args.append('--user') |
| 500 return install_args |
| 501 |
| 502 def _parse_args(): |
| 503 """ |
| 504 Parse the command line for options |
| 505 """ |
| 506 parser = optparse.OptionParser() |
| 507 parser.add_option( |
| 508 '--user', dest='user_install', action='store_true', default=False, |
| 509 help='install in user site package (requires Python 2.6 or later)') |
| 510 parser.add_option( |
| 511 '--download-base', dest='download_base', metavar="URL", |
| 512 default=DEFAULT_URL, |
| 513 help='alternative URL from where to download the distribute package') |
| 514 options, args = parser.parse_args() |
| 515 return options |
| 516 |
| 517 def main(version=DEFAULT_VERSION): |
| 518 """Install or upgrade setuptools and EasyInstall""" |
| 519 options = _parse_args() |
| 520 tarball = download_setuptools(download_base=options.download_base) |
| 521 return _install(tarball, _build_install_args(options)) |
| 522 |
| 523 if __name__ == '__main__': |
| 524 sys.exit(main()) |
OLD | NEW |