/bootstrap.py

https://github.com/uhuracreativemedia/emencia-django-newsletter · Python · 121 lines · 80 code · 18 blank · 23 comment · 18 complexity · 64dc5b52739a76e2f88421b868c2d72f MD5 · raw file

  1. ##############################################################################
  2. #
  3. # Copyright (c) 2006 Zope Corporation 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. $Id$
  19. """
  20. import os, shutil, sys, tempfile, urllib2
  21. from optparse import OptionParser
  22. tmpeggs = tempfile.mkdtemp()
  23. is_jython = sys.platform.startswith('java')
  24. # parsing arguments
  25. parser = OptionParser()
  26. parser.add_option("-v", "--version", dest="version",
  27. help="use a specific zc.buildout version")
  28. parser.add_option("-d", "--distribute",
  29. action="store_true", dest="distribute", default=False,
  30. help="Use Distribute rather than Setuptools.")
  31. parser.add_option("-c", None, action="store", dest="config_file",
  32. help=("Specify the path to the buildout configuration "
  33. "file to be used."))
  34. options, args = parser.parse_args()
  35. # if -c was provided, we push it back into args for buildout' main function
  36. if options.config_file is not None:
  37. args += ['-c', options.config_file]
  38. if options.version is not None:
  39. VERSION = '==%s' % options.version
  40. else:
  41. VERSION = ''
  42. USE_DISTRIBUTE = options.distribute
  43. args = args + ['bootstrap']
  44. to_reload = False
  45. try:
  46. import pkg_resources
  47. if not hasattr(pkg_resources, '_distribute'):
  48. to_reload = True
  49. raise ImportError
  50. except ImportError:
  51. ez = {}
  52. if USE_DISTRIBUTE:
  53. exec urllib2.urlopen('http://python-distribute.org/distribute_setup.py'
  54. ).read() in ez
  55. ez['use_setuptools'](to_dir=tmpeggs, download_delay=0, no_fake=True)
  56. else:
  57. exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
  58. ).read() in ez
  59. ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
  60. if to_reload:
  61. reload(pkg_resources)
  62. else:
  63. import pkg_resources
  64. if sys.platform == 'win32':
  65. def quote(c):
  66. if ' ' in c:
  67. return '"%s"' % c # work around spawn lamosity on windows
  68. else:
  69. return c
  70. else:
  71. def quote (c):
  72. return c
  73. cmd = 'from setuptools.command.easy_install import main; main()'
  74. ws = pkg_resources.working_set
  75. if USE_DISTRIBUTE:
  76. requirement = 'distribute'
  77. else:
  78. requirement = 'setuptools'
  79. if is_jython:
  80. import subprocess
  81. assert subprocess.Popen([sys.executable] + ['-c', quote(cmd), '-mqNxd',
  82. quote(tmpeggs), 'zc.buildout' + VERSION],
  83. env=dict(os.environ,
  84. PYTHONPATH=
  85. ws.find(pkg_resources.Requirement.parse(requirement)).location
  86. ),
  87. ).wait() == 0
  88. else:
  89. assert os.spawnle(
  90. os.P_WAIT, sys.executable, quote (sys.executable),
  91. '-c', quote (cmd), '-mqNxd', quote (tmpeggs), 'zc.buildout' + VERSION,
  92. dict(os.environ,
  93. PYTHONPATH=
  94. ws.find(pkg_resources.Requirement.parse(requirement)).location
  95. ),
  96. ) == 0
  97. ws.add_entry(tmpeggs)
  98. ws.require('zc.buildout' + VERSION)
  99. import zc.buildout.buildout
  100. zc.buildout.buildout.main(args)
  101. shutil.rmtree(tmpeggs)