ez_setup.py 8.6 KB

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