ez_setup.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2',
  41. 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e',
  42. 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372',
  43. }
  44. import sys, os
  45. def _validate_md5(egg_name, data):
  46. if egg_name in md5_data:
  47. from md5 import md5
  48. digest = md5(data).hexdigest()
  49. if digest != md5_data[egg_name]:
  50. print >>sys.stderr, (
  51. "md5 validation of %s failed! (Possible download problem?)"
  52. % egg_name
  53. )
  54. sys.exit(2)
  55. return data
  56. def use_setuptools(
  57. version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
  58. download_delay=15
  59. ):
  60. """Automatically find/download setuptools and make it available on sys.path
  61. `version` should be a valid setuptools version number that is available
  62. as an egg for download under the `download_base` URL (which should end with
  63. a '/'). `to_dir` is the directory where setuptools will be downloaded, if
  64. it is not already available. If `download_delay` is specified, it should
  65. be the number of seconds that will be paused before initiating a download,
  66. should one be required. If an older version of setuptools is installed,
  67. this routine will print a message to ``sys.stderr`` and raise SystemExit in
  68. an attempt to abort the calling script.
  69. """
  70. try:
  71. import setuptools
  72. if setuptools.__version__ == '0.0.1':
  73. print >>sys.stderr, (
  74. "You have an obsolete version of setuptools installed. Please\n"
  75. "remove it from your system entirely before rerunning this script."
  76. )
  77. sys.exit(2)
  78. except ImportError:
  79. egg = download_setuptools(version, download_base, to_dir, download_delay)
  80. sys.path.insert(0, egg)
  81. import setuptools; setuptools.bootstrap_install_from = egg
  82. import pkg_resources
  83. try:
  84. pkg_resources.require("setuptools>="+version)
  85. except pkg_resources.VersionConflict, e:
  86. # XXX could we install in a subprocess here?
  87. print >>sys.stderr, (
  88. "The required version of setuptools (>=%s) is not available, and\n"
  89. "can't be installed while this script is running. Please install\n"
  90. " a more recent version first.\n\n(Currently using %r)"
  91. ) % (version, e.args[0])
  92. sys.exit(2)
  93. def download_setuptools(
  94. version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
  95. delay = 15
  96. ):
  97. """Download setuptools from a specified location and return its filename
  98. `version` should be a valid setuptools version number that is available
  99. as an egg for download under the `download_base` URL (which should end
  100. with a '/'). `to_dir` is the directory where the egg will be downloaded.
  101. `delay` is the number of seconds to pause before an actual download attempt.
  102. """
  103. import urllib2, shutil
  104. egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
  105. url = download_base + egg_name
  106. saveto = os.path.join(to_dir, egg_name)
  107. src = dst = None
  108. if not os.path.exists(saveto): # Avoid repeated downloads
  109. try:
  110. from distutils import log
  111. if delay:
  112. log.warn("""
  113. ---------------------------------------------------------------------------
  114. This script requires setuptools version %s to run (even to display
  115. help). I will attempt to download it for you (from
  116. %s), but
  117. you may need to enable firewall access for this script first.
  118. I will start the download in %d seconds.
  119. (Note: if this machine does not have network access, please obtain the file
  120. %s
  121. and place it in this directory before rerunning this script.)
  122. ---------------------------------------------------------------------------""",
  123. version, download_base, delay, url
  124. ); from time import sleep; sleep(delay)
  125. log.warn("Downloading %s", url)
  126. src = urllib2.urlopen(url)
  127. # Read/write all in one block, so we don't create a corrupt file
  128. # if the download is interrupted.
  129. data = _validate_md5(egg_name, src.read())
  130. dst = open(saveto,"wb"); dst.write(data)
  131. finally:
  132. if src: src.close()
  133. if dst: dst.close()
  134. return os.path.realpath(saveto)
  135. def main(argv, version=DEFAULT_VERSION):
  136. """Install or upgrade setuptools and EasyInstall"""
  137. try:
  138. import setuptools
  139. except ImportError:
  140. egg = None
  141. try:
  142. egg = download_setuptools(version, delay=0)
  143. sys.path.insert(0,egg)
  144. from setuptools.command.easy_install import main
  145. return main(list(argv)+[egg]) # we're done here
  146. finally:
  147. if egg and os.path.exists(egg):
  148. os.unlink(egg)
  149. else:
  150. if setuptools.__version__ == '0.0.1':
  151. # tell the user to uninstall obsolete version
  152. use_setuptools(version)
  153. req = "setuptools>="+version
  154. import pkg_resources
  155. try:
  156. pkg_resources.require(req)
  157. except pkg_resources.VersionConflict:
  158. try:
  159. from setuptools.command.easy_install import main
  160. except ImportError:
  161. from easy_install import main
  162. main(list(argv)+[download_setuptools(delay=0)])
  163. sys.exit(0) # try to force an exit
  164. else:
  165. if argv:
  166. from setuptools.command.easy_install import main
  167. main(argv)
  168. else:
  169. print "Setuptools version",version,"or greater has been installed."
  170. print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
  171. def update_md5(filenames):
  172. """Update our built-in md5 registry"""
  173. import re
  174. from md5 import md5
  175. for name in filenames:
  176. base = os.path.basename(name)
  177. f = open(name,'rb')
  178. md5_data[base] = md5(f.read()).hexdigest()
  179. f.close()
  180. data = [" %r: %r,\n" % it for it in md5_data.items()]
  181. data.sort()
  182. repl = "".join(data)
  183. import inspect
  184. srcfile = inspect.getsourcefile(sys.modules[__name__])
  185. f = open(srcfile, 'rb'); src = f.read(); f.close()
  186. match = re.search("\nmd5_data = {\n([^}]+)}", src)
  187. if not match:
  188. print >>sys.stderr, "Internal error!"
  189. sys.exit(2)
  190. src = src[:match.start(1)] + repl + src[match.end(1):]
  191. f = open(srcfile,'w')
  192. f.write(src)
  193. f.close()
  194. if __name__=='__main__':
  195. if len(sys.argv)>2 and sys.argv[1]=='--md5update':
  196. update_md5(sys.argv[2:])
  197. else:
  198. main(sys.argv[1:])