| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 21 |
import os |
| 22 |
import shutil |
| 23 |
import sys |
| 24 |
import tempfile |
| 25 |
|
| 26 |
from optparse import OptionParser |
| 27 |
|
| 28 |
tmpeggs = tempfile.mkdtemp() |
| 29 |
|
| 30 |
usage = '''\ |
| 31 |
[DESIRED PYTHON FOR BUILDOUT] bootstrap.py [options] |
| 32 |
|
| 33 |
Bootstraps a buildout-based project. |
| 34 |
|
| 35 |
Simply run this script in a directory containing a buildout.cfg, using the |
| 36 |
Python that you want bin/buildout to use. |
| 37 |
|
| 38 |
Note that by using --find-links to point to local resources, you can keep |
| 39 |
this script from going over the network. |
| 40 |
''' |
| 41 |
|
| 42 |
parser = OptionParser(usage=usage) |
| 43 |
parser.add_option("-v", "--version", help="use a specific zc.buildout version") |
| 44 |
|
| 45 |
parser.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.")) |
| 54 |
parser.add_option("-c", "--config-file", |
| 55 |
help=("Specify the path to the buildout configuration " |
| 56 |
"file to be used.")) |
| 57 |
parser.add_option("-f", "--find-links", |
| 58 |
help=("Specify a URL to search for buildout releases")) |
| 59 |
parser.add_option("--allow-site-packages", |
| 60 |
action="store_true", default=False, |
| 61 |
help=("Let bootstrap.py use existing site packages")) |
| 62 |
parser.add_option("--setuptools-version", help="use a specific setuptools version") |
| 63 |
|
| 64 |
|
| 65 |
options, args = parser.parse_args() |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
if options.allow_site_packages: |
| 71 |
import setuptools |
| 72 |
import pkg_resources |
| 73 |
from urllib.request import urlopen |
| 74 |
|
| 75 |
|
| 76 |
ez = {} |
| 77 |
exec(urlopen('https://bootstrap.pypa.io/ez_setup.py').read(), ez) |
| 78 |
|
| 79 |
if not options.allow_site_packages: |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
import site |
| 84 |
|
| 85 |
|
| 86 |
if hasattr(site, 'getsitepackages'): |
| 87 |
for sitepackage_path in site.getsitepackages(): |
| 88 |
sys.path[:] = [x for x in sys.path if sitepackage_path not in x] |
| 89 |
|
| 90 |
setup_args = dict(to_dir=tmpeggs, download_delay=0) |
| 91 |
|
| 92 |
if options.setuptools_version is not None: |
| 93 |
setup_args['version'] = options.setuptools_version |
| 94 |
|
| 95 |
ez['use_setuptools'](**setup_args) |
| 96 |
import setuptools |
| 97 |
import pkg_resources |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
for path in sys.path: |
| 102 |
if path not in pkg_resources.working_set.entries: |
| 103 |
pkg_resources.working_set.add_entry(path) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
ws = pkg_resources.working_set |
| 109 |
|
| 110 |
cmd = [sys.executable, '-c', |
| 111 |
'from setuptools.command.easy_install import main; main()', |
| 112 |
'-mZqNxd', tmpeggs] |
| 113 |
|
| 114 |
find_links = os.environ.get( |
| 115 |
'bootstrap-testing-find-links', |
| 116 |
options.find_links or |
| 117 |
('http://downloads.buildout.org/' |
| 118 |
if options.accept_buildout_test_releases else None) |
| 119 |
) |
| 120 |
if find_links: |
| 121 |
cmd.extend(['-f', find_links]) |
| 122 |
|
| 123 |
setuptools_path = ws.find( |
| 124 |
pkg_resources.Requirement.parse('setuptools')).location |
| 125 |
|
| 126 |
requirement = 'zc.buildout' |
| 127 |
version = options.version |
| 128 |
if version is None and not options.accept_buildout_test_releases: |
| 129 |
|
| 130 |
import setuptools.package_index |
| 131 |
_final_parts = '*final-', '*final' |
| 132 |
|
| 133 |
def _final_version(parsed_version): |
| 134 |
for part in parsed_version: |
| 135 |
if (part[:1] == '*') and (part not in _final_parts): |
| 136 |
return False |
| 137 |
return True |
| 138 |
index = setuptools.package_index.PackageIndex( |
| 139 |
search_path=[setuptools_path]) |
| 140 |
if find_links: |
| 141 |
index.add_find_links((find_links,)) |
| 142 |
req = pkg_resources.Requirement.parse(requirement) |
| 143 |
if index.obtain(req) is not None: |
| 144 |
best = [] |
| 145 |
bestv = None |
| 146 |
for dist in index[req.project_name]: |
| 147 |
distv = dist.parsed_version |
| 148 |
if _final_version(distv): |
| 149 |
if bestv is None or distv > bestv: |
| 150 |
best = [dist] |
| 151 |
bestv = distv |
| 152 |
elif distv == bestv: |
| 153 |
best.append(dist) |
| 154 |
if best: |
| 155 |
best.sort() |
| 156 |
version = best[-1].version |
| 157 |
if version: |
| 158 |
requirement = '=='.join((requirement, version)) |
| 159 |
cmd.append(requirement) |
| 160 |
|
| 161 |
import subprocess |
| 162 |
if subprocess.call(cmd, env=dict(os.environ, PYTHONPATH=setuptools_path)) != 0: |
| 163 |
raise Exception( |
| 164 |
"Failed to execute command:\n%s" % repr(cmd)[1:-1]) |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
ws.add_entry(tmpeggs) |
| 170 |
ws.require(requirement) |
| 171 |
import zc.buildout.buildout |
| 172 |
|
| 173 |
if not [a for a in args if '=' not in a]: |
| 174 |
args.append('bootstrap') |
| 175 |
|
| 176 |
|
| 177 |
if options.config_file is not None: |
| 178 |
args[0:0] = ['-c', options.config_file] |
| 179 |
|
| 180 |
zc.buildout.buildout.main(args) |
| 181 |
shutil.rmtree(tmpeggs) |