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