ez_setup.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!python
  2. """Bootstrap setuptools installation
  3. If you want to use setuptools in your package's setup.py, just include this
  4. file in the same directory with it, and add this to the top of your setup.py::
  5. from ez_setup import use_setuptools
  6. use_setuptools()
  7. If you want to require a specific version of setuptools, set a download
  8. mirror, or use an alternate download directory, you can do so by supplying
  9. the appropriate options to ``use_setuptools()``.
  10. This file can also be run as a script to install or upgrade setuptools.
  11. """
  12. import sys
  13. DEFAULT_VERSION = "0.6c7"
  14. DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]
  15. md5_data = {
  16. 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
  17. 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
  18. 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
  19. 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a',
  20. 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618',
  21. 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac',
  22. 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5',
  23. 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4',
  24. 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c',
  25. 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b',
  26. 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27',
  27. 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277',
  28. 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa',
  29. 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e',
  30. 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e',
  31. 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f',
  32. 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2',
  33. 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc',
  34. 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167',
  35. 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64',
  36. 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d',
  37. 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20',
  38. 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab',
  39. 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53',
  40. }
  41. import sys, os
  42. def _validate_md5(egg_name, data):
  43. if egg_name in md5_data:
  44. from md5 import md5
  45. digest = md5(data).hexdigest()
  46. if digest != md5_data[egg_name]:
  47. print >>sys.stderr, (
  48. "md5 validation of %s failed! (Possible download problem?)"
  49. % egg_name
  50. )
  51. sys.exit(2)
  52. return data
  53. def use_setuptools(
  54. version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, min_version=None,
  55. download_delay=15
  56. ):
  57. """Automatically find/download setuptools and make it available on sys.path
  58. `version` should be a valid setuptools version number that is available
  59. as an egg for download under the `download_base` URL (which should end with
  60. a '/'). `to_dir` is the directory where setuptools will be downloaded, if
  61. it is not already available. If `download_delay` is specified, it should
  62. be the number of seconds that will be paused before initiating a download,
  63. should one be required. If an older version of setuptools is installed,
  64. this routine will print a message to ``sys.stderr`` and raise SystemExit in
  65. an attempt to abort the calling script.
  66. """
  67. try:
  68. import setuptools
  69. if setuptools.__version__ == '0.0.1':
  70. print >>sys.stderr, (
  71. "You have an obsolete version of setuptools installed. Please\n"
  72. "remove it from your system entirely before rerunning this script."
  73. )
  74. sys.exit(2)
  75. except ImportError:
  76. egg = download_setuptools(version, download_base, to_dir, download_delay)
  77. sys.path.insert(0, egg)
  78. import setuptools; setuptools.bootstrap_install_from = egg
  79. import pkg_resources
  80. try:
  81. if not min_version:
  82. min_version = version
  83. pkg_resources.require("setuptools>="+min_version)
  84. except pkg_resources.VersionConflict, e:
  85. # XXX could we install in a subprocess here?
  86. print >>sys.stderr, (
  87. "The required version of setuptools (>=%s) is not available, and\n"
  88. "can't be installed while this script is running. Please install\n"
  89. " a more recent version first.\n\n(Currently using %r)"
  90. ) % (min_version, e.args[0])
  91. sys.exit(2)
  92. def download_setuptools(
  93. version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
  94. delay = 15
  95. ):
  96. """Download setuptools from a specified location and return its filename
  97. `version` should be a valid setuptools version number that is available
  98. as an egg for download under the `download_base` URL (which should end
  99. with a '/'). `to_dir` is the directory where the egg will be downloaded.
  100. `delay` is the number of seconds to pause before an actual download attempt.
  101. """
  102. import urllib2, shutil
  103. egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
  104. url = download_base + egg_name
  105. saveto = os.path.join(to_dir, egg_name)
  106. src = dst = None
  107. if not os.path.exists(saveto): # Avoid repeated downloads
  108. try:
  109. from distutils import log
  110. if delay:
  111. log.warn("""
  112. ---------------------------------------------------------------------------
  113. This script requires setuptools version %s to run (even to display
  114. help). I will attempt to download it for you (from
  115. %s), but
  116. you may need to enable firewall access for this script first.
  117. I will start the download in %d seconds.
  118. (Note: if this machine does not have network access, please obtain the file
  119. %s
  120. and place it in this directory before rerunning this script.)
  121. ---------------------------------------------------------------------------""",
  122. version, download_base, delay, url
  123. ); from time import sleep; sleep(delay)
  124. log.warn("Downloading %s", url)
  125. src = urllib2.urlopen(url)
  126. # Read/write all in one block, so we don't create a corrupt file
  127. # if the download is interrupted.
  128. data = _validate_md5(egg_name, src.read())
  129. dst = open(saveto,"wb"); dst.write(data)
  130. finally:
  131. if src: src.close()
  132. if dst: dst.close()
  133. return os.path.realpath(saveto)
  134. def main(argv, version=DEFAULT_VERSION):
  135. """Install or upgrade setuptools and EasyInstall"""
  136. try:
  137. import setuptools
  138. except ImportError:
  139. egg = None
  140. try:
  141. egg = download_setuptools(version, delay=0)
  142. sys.path.insert(0,egg)
  143. from setuptools.command.easy_install import main
  144. return main(list(argv)+[egg]) # we're done here
  145. finally:
  146. if egg and os.path.exists(egg):
  147. os.unlink(egg)
  148. else:
  149. if setuptools.__version__ == '0.0.1':
  150. # tell the user to uninstall obsolete version
  151. use_setuptools(version)
  152. req = "setuptools>="+version
  153. import pkg_resources
  154. try:
  155. pkg_resources.require(req)
  156. except pkg_resources.VersionConflict:
  157. try:
  158. from setuptools.command.easy_install import main
  159. except ImportError:
  160. from easy_install import main
  161. main(list(argv)+[download_setuptools(delay=0)])
  162. sys.exit(0) # try to force an exit
  163. else:
  164. if argv:
  165. from setuptools.command.easy_install import main
  166. main(argv)
  167. else:
  168. print "Setuptools version",version,"or greater has been installed."
  169. print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
  170. def update_md5(filenames):
  171. """Update our built-in md5 registry"""
  172. import re
  173. from md5 import md5
  174. for name in filenames:
  175. base = os.path.basename(name)
  176. f = open(name,'rb')
  177. md5_data[base] = md5(f.read()).hexdigest()
  178. f.close()
  179. data = [" %r: %r,\n" % it for it in md5_data.items()]
  180. data.sort()
  181. repl = "".join(data)
  182. import inspect
  183. srcfile = inspect.getsourcefile(sys.modules[__name__])
  184. f = open(srcfile, 'rb'); src = f.read(); f.close()
  185. match = re.search("\nmd5_data = {\n([^}]+)}", src)
  186. if not match:
  187. print >>sys.stderr, "Internal error!"
  188. sys.exit(2)
  189. src = src[:match.start(1)] + repl + src[match.end(1):]
  190. f = open(srcfile,'w')
  191. f.write(src)
  192. f.close()
  193. if __name__=='__main__':
  194. if len(sys.argv)>2 and sys.argv[1]=='--md5update':
  195. update_md5(sys.argv[2:])
  196. else:
  197. main(sys.argv[1:])