PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/bootstrap.py

https://bitbucket.org/tkhyn/django-idmap
Python | 170 lines | 146 code | 5 blank | 19 comment | 1 complexity | b1e63a7a6b56bcd490e8413a1f59c102 MD5 | raw file
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2006 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Bootstrap a buildout-based project
  15. Simply run this script in a directory containing a buildout.cfg.
  16. The script accepts buildout command-line options, so you can
  17. use the -c option to specify an alternate configuration file.
  18. """
  19. import os
  20. import shutil
  21. import sys
  22. import tempfile
  23. from optparse import OptionParser
  24. tmpeggs = tempfile.mkdtemp()
  25. usage = '''\
  26. [DESIRED PYTHON FOR BUILDOUT] bootstrap.py [options]
  27. Bootstraps a buildout-based project.
  28. Simply run this script in a directory containing a buildout.cfg, using the
  29. Python that you want bin/buildout to use.
  30. Note that by using --find-links to point to local resources, you can keep
  31. this script from going over the network.
  32. '''
  33. parser = OptionParser(usage=usage)
  34. parser.add_option("-v", "--version", help="use a specific zc.buildout version")
  35. parser.add_option("-t", "--accept-buildout-test-releases",
  36. dest='accept_buildout_test_releases',
  37. action="store_true", default=False,
  38. help=("Normally, if you do not specify a --version, the "
  39. "bootstrap script and buildout gets the newest "
  40. "*final* versions of zc.buildout and its recipes and "
  41. "extensions for you. If you use this flag, "
  42. "bootstrap and buildout will get the newest releases "
  43. "even if they are alphas or betas."))
  44. parser.add_option("-c", "--config-file",
  45. help=("Specify the path to the buildout configuration "
  46. "file to be used."))
  47. parser.add_option("-f", "--find-links",
  48. help=("Specify a URL to search for buildout releases"))
  49. options, args = parser.parse_args()
  50. ######################################################################
  51. # load/install setuptools
  52. to_reload = False
  53. try:
  54. import pkg_resources
  55. import setuptools
  56. except ImportError:
  57. ez = {}
  58. try:
  59. from urllib.request import urlopen
  60. except ImportError:
  61. from urllib2 import urlopen
  62. # XXX use a more permanent ez_setup.py URL when available.
  63. exec(urlopen('https://bitbucket.org/pypa/setuptools/raw/0.7.2/ez_setup.py'
  64. ).read(), ez)
  65. setup_args = dict(to_dir=tmpeggs, download_delay=0)
  66. ez['use_setuptools'](**setup_args)
  67. if to_reload:
  68. reload(pkg_resources)
  69. import pkg_resources
  70. # This does not (always?) update the default working set. We will
  71. # do it.
  72. for path in sys.path:
  73. if path not in pkg_resources.working_set.entries:
  74. pkg_resources.working_set.add_entry(path)
  75. ######################################################################
  76. # Install buildout
  77. ws = pkg_resources.working_set
  78. cmd = [sys.executable, '-c',
  79. 'from setuptools.command.easy_install import main; main()',
  80. '-mZqNxd', tmpeggs]
  81. find_links = os.environ.get(
  82. 'bootstrap-testing-find-links',
  83. options.find_links or
  84. ('http://downloads.buildout.org/'
  85. if options.accept_buildout_test_releases else None)
  86. )
  87. if find_links:
  88. cmd.extend(['-f', find_links])
  89. setuptools_path = ws.find(
  90. pkg_resources.Requirement.parse('setuptools')).location
  91. requirement = 'zc.buildout'
  92. version = options.version
  93. if version is None and not options.accept_buildout_test_releases:
  94. # Figure out the most recent final version of zc.buildout.
  95. import setuptools.package_index
  96. _final_parts = '*final-', '*final'
  97. def _final_version(parsed_version):
  98. for part in parsed_version:
  99. if (part[:1] == '*') and (part not in _final_parts):
  100. return False
  101. return True
  102. index = setuptools.package_index.PackageIndex(
  103. search_path=[setuptools_path])
  104. if find_links:
  105. index.add_find_links((find_links,))
  106. req = pkg_resources.Requirement.parse(requirement)
  107. if index.obtain(req) is not None:
  108. best = []
  109. bestv = None
  110. for dist in index[req.project_name]:
  111. distv = dist.parsed_version
  112. if _final_version(distv):
  113. if bestv is None or distv > bestv:
  114. best = [dist]
  115. bestv = distv
  116. elif distv == bestv:
  117. best.append(dist)
  118. if best:
  119. best.sort()
  120. version = best[-1].version
  121. if version:
  122. requirement = '=='.join((requirement, version))
  123. cmd.append(requirement)
  124. import subprocess
  125. if subprocess.call(cmd, env=dict(os.environ, PYTHONPATH=setuptools_path)) != 0:
  126. raise Exception(
  127. "Failed to execute command:\n%s",
  128. repr(cmd)[1:-1])
  129. ######################################################################
  130. # Import and run buildout
  131. ws.add_entry(tmpeggs)
  132. ws.require(requirement)
  133. import zc.buildout.buildout
  134. if not [a for a in args if '=' not in a]:
  135. args.append('bootstrap')
  136. # if -c was provided, we push it back into args for buildout' main function
  137. if options.config_file is not None:
  138. args[0:0] = ['-c', options.config_file]
  139. zc.buildout.buildout.main(args)
  140. shutil.rmtree(tmpeggs)