/ez_setup.py

https://bitbucket.org/bbangert/kai/ · Python · 272 lines · 256 code · 8 blank · 8 comment · 17 complexity · 94ce3ba3f5933e3915e999c26da9563b MD5 · raw file

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