2
0

ez_setup.py 7.9 KB

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