source: main/kofacustom.sampleuni/trunk/bootstrap.py @ 13275

Last change on this file since 13275 was 11858, checked in by Henrik Bettermann, 10 years ago

Replace bootstrap.py.

  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
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
16Simply run this script in a directory containing a buildout.cfg.
17The script accepts buildout command-line options, so you can
18use the -c option to specify an alternate configuration file.
19"""
20
21import os
22import shutil
23import sys
24import tempfile
25
26from optparse import OptionParser
27
28tmpeggs = tempfile.mkdtemp()
29
30usage = '''\
31[DESIRED PYTHON FOR BUILDOUT] bootstrap.py [options]
32
33Bootstraps a buildout-based project.
34
35Simply run this script in a directory containing a buildout.cfg, using the
36Python that you want bin/buildout to use.
37
38Note that by using --find-links to point to local resources, you can keep
39this script from going over the network.
40'''
41
42parser = OptionParser(usage=usage)
43parser.add_option("-v", "--version", help="use a specific zc.buildout version")
44
45parser.add_option("-t", "--accept-buildout-test-releases",
46                  dest='accept_buildout_test_releases',
47                  action="store_true", default=False,
48                  help=("Normally, if you do not specify a --version, the "
49                        "bootstrap script and buildout gets the newest "
50                        "*final* versions of zc.buildout and its recipes and "
51                        "extensions for you.  If you use this flag, "
52                        "bootstrap and buildout will get the newest releases "
53                        "even if they are alphas or betas."))
54parser.add_option("-c", "--config-file",
55                  help=("Specify the path to the buildout configuration "
56                        "file to be used."))
57parser.add_option("-f", "--find-links",
58                  help=("Specify a URL to search for buildout releases"))
59
60
61options, args = parser.parse_args()
62
63######################################################################
64# load/install distribute
65
66to_reload = False
67try:
68    import pkg_resources
69    import setuptools
70    if not hasattr(pkg_resources, '_distribute'):
71        to_reload = True
72        raise ImportError
73except ImportError:
74    ez = {}
75
76    try:
77        from urllib.request import urlopen
78    except ImportError:
79        from urllib2 import urlopen
80
81    exec(urlopen(
82        'http://downloads.buildout.org/2.1/distribute_setup.py').read(), ez)
83    setup_args = dict(to_dir=tmpeggs, download_delay=0, no_fake=True)
84    ez['use_setuptools'](**setup_args)
85
86    if to_reload:
87        reload(pkg_resources)
88    import pkg_resources
89    # This does not (always?) update the default working set.  We will
90    # do it.
91    for path in sys.path:
92        if path not in pkg_resources.working_set.entries:
93            pkg_resources.working_set.add_entry(path)
94
95######################################################################
96# Install buildout
97
98ws = pkg_resources.working_set
99
100cmd = [sys.executable, '-c',
101       'from setuptools.command.easy_install import main; main()',
102       '-mZqNxd', tmpeggs]
103
104find_links = os.environ.get(
105    'bootstrap-testing-find-links',
106    options.find_links or
107    ('http://downloads.buildout.org/'
108     if options.accept_buildout_test_releases else None)
109    )
110if find_links:
111    cmd.extend(['-f', find_links])
112
113distribute_path = ws.find(
114    pkg_resources.Requirement.parse('distribute')).location
115
116requirement = 'zc.buildout'
117version = options.version
118if version is None and not options.accept_buildout_test_releases:
119    # Figure out the most recent final version of zc.buildout.
120    import setuptools.package_index
121    _final_parts = '*final-', '*final'
122
123    def _final_version(parsed_version):
124        for part in parsed_version:
125            if (part[:1] == '*') and (part not in _final_parts):
126                return False
127        return True
128    index = setuptools.package_index.PackageIndex(
129        search_path=[distribute_path])
130    if find_links:
131        index.add_find_links((find_links,))
132    req = pkg_resources.Requirement.parse(requirement)
133    if index.obtain(req) is not None:
134        best = []
135        bestv = None
136        for dist in index[req.project_name]:
137            distv = dist.parsed_version
138            if _final_version(distv):
139                if bestv is None or distv > bestv:
140                    best = [dist]
141                    bestv = distv
142                elif distv == bestv:
143                    best.append(dist)
144        if best:
145            best.sort()
146            version = best[-1].version
147if version:
148    requirement = '=='.join((requirement, version))
149cmd.append(requirement)
150
151import subprocess
152if subprocess.call(cmd, env=dict(os.environ, PYTHONPATH=distribute_path)) != 0:
153    raise Exception(
154        "Failed to execute command:\n%s",
155        repr(cmd)[1:-1])
156
157######################################################################
158# Import and run buildout
159
160ws.add_entry(tmpeggs)
161ws.require(requirement)
162import zc.buildout.buildout
163
164if not [a for a in args if '=' not in a]:
165    args.append('bootstrap')
166
167# if -c was provided, we push it back into args for buildout' main function
168if options.config_file is not None:
169    args[0:0] = ['-c', options.config_file]
170
171zc.buildout.buildout.main(args)
172shutil.rmtree(tmpeggs)
Note: See TracBrowser for help on using the repository browser.