Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

332 wiersze
10 KiB

  1. #!/usr/bin/env python
  2. """Bootstrap setuptools installation
  3. To use setuptools in your package's setup.py, include this
  4. file in the same directory and add this to the top of your setup.py::
  5. from ez_setup import use_setuptools
  6. use_setuptools()
  7. To require a specific version of setuptools, set a download
  8. mirror, or use an alternate download directory, simply supply
  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 os
  13. import shutil
  14. import sys
  15. import tempfile
  16. import zipfile
  17. import optparse
  18. import subprocess
  19. import platform
  20. import textwrap
  21. import contextlib
  22. from distutils import log
  23. try:
  24. from site import USER_SITE
  25. except ImportError:
  26. USER_SITE = None
  27. DEFAULT_VERSION = "3.5.1"
  28. DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
  29. def _python_cmd(*args):
  30. """
  31. Return True if the command succeeded.
  32. """
  33. args = (sys.executable,) + args
  34. return subprocess.call(args) == 0
  35. def _install(archive_filename, install_args=()):
  36. with archive_context(archive_filename):
  37. # installing
  38. log.warn('Installing Setuptools')
  39. if not _python_cmd('setup.py', 'install', *install_args):
  40. log.warn('Something went wrong during the installation.')
  41. log.warn('See the error message above.')
  42. # exitcode will be 2
  43. return 2
  44. def _build_egg(egg, archive_filename, to_dir):
  45. with archive_context(archive_filename):
  46. # building an egg
  47. log.warn('Building a Setuptools egg in %s', to_dir)
  48. _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
  49. # returning the result
  50. log.warn(egg)
  51. if not os.path.exists(egg):
  52. raise IOError('Could not build the egg.')
  53. def get_zip_class():
  54. """
  55. Supplement ZipFile class to support context manager for Python 2.6
  56. """
  57. class ContextualZipFile(zipfile.ZipFile):
  58. def __enter__(self):
  59. return self
  60. def __exit__(self, type, value, traceback):
  61. self.close
  62. return zipfile.ZipFile if hasattr(zipfile.ZipFile, '__exit__') else \
  63. ContextualZipFile
  64. @contextlib.contextmanager
  65. def archive_context(filename):
  66. # extracting the archive
  67. tmpdir = tempfile.mkdtemp()
  68. log.warn('Extracting in %s', tmpdir)
  69. old_wd = os.getcwd()
  70. try:
  71. os.chdir(tmpdir)
  72. with get_zip_class()(filename) as archive:
  73. archive.extractall()
  74. # going in the directory
  75. subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
  76. os.chdir(subdir)
  77. log.warn('Now working in %s', subdir)
  78. yield
  79. finally:
  80. os.chdir(old_wd)
  81. shutil.rmtree(tmpdir)
  82. def _do_download(version, download_base, to_dir, download_delay):
  83. egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
  84. % (version, sys.version_info[0], sys.version_info[1]))
  85. if not os.path.exists(egg):
  86. archive = download_setuptools(version, download_base,
  87. to_dir, download_delay)
  88. _build_egg(egg, archive, to_dir)
  89. sys.path.insert(0, egg)
  90. # Remove previously-imported pkg_resources if present (see
  91. # https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
  92. if 'pkg_resources' in sys.modules:
  93. del sys.modules['pkg_resources']
  94. import setuptools
  95. setuptools.bootstrap_install_from = egg
  96. def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
  97. to_dir=os.curdir, download_delay=15):
  98. to_dir = os.path.abspath(to_dir)
  99. rep_modules = 'pkg_resources', 'setuptools'
  100. imported = set(sys.modules).intersection(rep_modules)
  101. try:
  102. import pkg_resources
  103. except ImportError:
  104. return _do_download(version, download_base, to_dir, download_delay)
  105. try:
  106. pkg_resources.require("setuptools>=" + version)
  107. return
  108. except pkg_resources.DistributionNotFound:
  109. return _do_download(version, download_base, to_dir, download_delay)
  110. except pkg_resources.VersionConflict as VC_err:
  111. if imported:
  112. msg = textwrap.dedent("""
  113. The required version of setuptools (>={version}) is not available,
  114. and can't be installed while this script is running. Please
  115. install a more recent version first, using
  116. 'easy_install -U setuptools'.
  117. (Currently using {VC_err.args[0]!r})
  118. """).format(VC_err=VC_err, version=version)
  119. sys.stderr.write(msg)
  120. sys.exit(2)
  121. # otherwise, reload ok
  122. del pkg_resources, sys.modules['pkg_resources']
  123. return _do_download(version, download_base, to_dir, download_delay)
  124. def _clean_check(cmd, target):
  125. """
  126. Run the command to download target. If the command fails, clean up before
  127. re-raising the error.
  128. """
  129. try:
  130. subprocess.check_call(cmd)
  131. except subprocess.CalledProcessError:
  132. if os.access(target, os.F_OK):
  133. os.unlink(target)
  134. raise
  135. def download_file_powershell(url, target):
  136. """
  137. Download the file at url to target using Powershell (which will validate
  138. trust). Raise an exception if the command cannot complete.
  139. """
  140. target = os.path.abspath(target)
  141. cmd = [
  142. 'powershell',
  143. '-Command',
  144. "(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(),
  145. ]
  146. _clean_check(cmd, target)
  147. def has_powershell():
  148. if platform.system() != 'Windows':
  149. return False
  150. cmd = ['powershell', '-Command', 'echo test']
  151. devnull = open(os.path.devnull, 'wb')
  152. try:
  153. try:
  154. subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
  155. except Exception:
  156. return False
  157. finally:
  158. devnull.close()
  159. return True
  160. download_file_powershell.viable = has_powershell
  161. def download_file_curl(url, target):
  162. cmd = ['curl', url, '--silent', '--output', target]
  163. _clean_check(cmd, target)
  164. def has_curl():
  165. cmd = ['curl', '--version']
  166. devnull = open(os.path.devnull, 'wb')
  167. try:
  168. try:
  169. subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
  170. except Exception:
  171. return False
  172. finally:
  173. devnull.close()
  174. return True
  175. download_file_curl.viable = has_curl
  176. def download_file_wget(url, target):
  177. cmd = ['wget', url, '--quiet', '--output-document', target]
  178. _clean_check(cmd, target)
  179. def has_wget():
  180. cmd = ['wget', '--version']
  181. devnull = open(os.path.devnull, 'wb')
  182. try:
  183. try:
  184. subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
  185. except Exception:
  186. return False
  187. finally:
  188. devnull.close()
  189. return True
  190. download_file_wget.viable = has_wget
  191. def download_file_insecure(url, target):
  192. """
  193. Use Python to download the file, even though it cannot authenticate the
  194. connection.
  195. """
  196. try:
  197. from urllib.request import urlopen
  198. except ImportError:
  199. from urllib2 import urlopen
  200. src = dst = None
  201. try:
  202. src = urlopen(url)
  203. # Read/write all in one block, so we don't create a corrupt file
  204. # if the download is interrupted.
  205. data = src.read()
  206. dst = open(target, "wb")
  207. dst.write(data)
  208. finally:
  209. if src:
  210. src.close()
  211. if dst:
  212. dst.close()
  213. download_file_insecure.viable = lambda: True
  214. def get_best_downloader():
  215. downloaders = [
  216. download_file_powershell,
  217. download_file_curl,
  218. download_file_wget,
  219. download_file_insecure,
  220. ]
  221. for dl in downloaders:
  222. if dl.viable():
  223. return dl
  224. def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
  225. to_dir=os.curdir, delay=15, downloader_factory=get_best_downloader):
  226. """
  227. Download setuptools from a specified location and return its filename
  228. `version` should be a valid setuptools version number that is available
  229. as an egg for download under the `download_base` URL (which should end
  230. with a '/'). `to_dir` is the directory where the egg will be downloaded.
  231. `delay` is the number of seconds to pause before an actual download
  232. attempt.
  233. ``downloader_factory`` should be a function taking no arguments and
  234. returning a function for downloading a URL to a target.
  235. """
  236. # making sure we use the absolute path
  237. to_dir = os.path.abspath(to_dir)
  238. zip_name = "setuptools-%s.zip" % version
  239. url = download_base + zip_name
  240. saveto = os.path.join(to_dir, zip_name)
  241. if not os.path.exists(saveto): # Avoid repeated downloads
  242. log.warn("Downloading %s", url)
  243. downloader = downloader_factory()
  244. downloader(url, saveto)
  245. return os.path.realpath(saveto)
  246. def _build_install_args(options):
  247. """
  248. Build the arguments to 'python setup.py install' on the setuptools package
  249. """
  250. return ['--user'] if options.user_install else []
  251. def _parse_args():
  252. """
  253. Parse the command line for options
  254. """
  255. parser = optparse.OptionParser()
  256. parser.add_option(
  257. '--user', dest='user_install', action='store_true', default=False,
  258. help='install in user site package (requires Python 2.6 or later)')
  259. parser.add_option(
  260. '--download-base', dest='download_base', metavar="URL",
  261. default=DEFAULT_URL,
  262. help='alternative URL from where to download the setuptools package')
  263. parser.add_option(
  264. '--insecure', dest='downloader_factory', action='store_const',
  265. const=lambda: download_file_insecure, default=get_best_downloader,
  266. help='Use internal, non-validating downloader'
  267. )
  268. parser.add_option(
  269. '--version', help="Specify which version to download",
  270. default=DEFAULT_VERSION,
  271. )
  272. options, args = parser.parse_args()
  273. # positional arguments are ignored
  274. return options
  275. def main():
  276. """Install or upgrade setuptools and EasyInstall"""
  277. options = _parse_args()
  278. archive = download_setuptools(
  279. version=options.version,
  280. download_base=options.download_base,
  281. downloader_factory=options.downloader_factory,
  282. )
  283. return _install(archive, _build_install_args(options))
  284. if __name__ == '__main__':
  285. sys.exit(main())