8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
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 |
||
8926.1.1
by Gary Poster
handle a couple more site-packages issues; one if pkg_resources is around but setuptools is not; and the other to make developing an egg have the same sys.path as bin/buildout |
20 |
$Id: bootstrap.py 101930 2009-07-15 18:34:35Z gary $
|
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
21 |
"""
|
22 |
||
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
23 |
import os, re, shutil, sys, tempfile, textwrap, urllib, urllib2 |
24 |
||
25 |
# We have to manually parse our options rather than using one of the stdlib
|
|
26 |
# tools because we want to pass the ones we don't recognize along to
|
|
27 |
# zc.buildout.buildout.main.
|
|
28 |
||
29 |
configuration = { |
|
30 |
'--ez_setup-source': 'http://peak.telecommunity.com/dist/ez_setup.py', |
|
31 |
'--version': '', |
|
32 |
'--download-base': None, |
|
33 |
'--eggs': None} |
|
34 |
||
35 |
helpstring = __doc__ + textwrap.dedent(''' |
|
36 |
This script recognizes the following options itself. The first option it
|
|
37 |
encounters that is not one of these will cause the script to stop parsing
|
|
38 |
options and pass the rest on to buildout. Therefore, if you want to use
|
|
39 |
any of the following options *and* buildout command-line options like
|
|
40 |
-c, first use the following options, and then use the buildout options.
|
|
41 |
||
42 |
Options:
|
|
43 |
--version=ZC_BUILDOUT_VERSION
|
|
44 |
Specify a version number of the zc.buildout to use
|
|
45 |
--ez_setup-source=URL_OR_FILE
|
|
46 |
Specify a URL or file location for the ez_setup file.
|
|
47 |
Defaults to
|
|
48 |
%(--ez_setup-source)s
|
|
49 |
--download-base=URL_OR_DIRECTORY
|
|
50 |
Specify a URL or directory for downloading setuptools and
|
|
51 |
zc.buildout. Defaults to PyPI.
|
|
52 |
--eggs=DIRECTORY
|
|
53 |
Specify a directory for storing eggs. Defaults to a temporary
|
|
54 |
directory that is deleted when the bootstrap script completes.
|
|
55 |
||
56 |
By using --ez_setup-source and --download-base to point to local resources,
|
|
57 |
you can keep this script from going over the network.
|
|
58 |
''' % configuration) |
|
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
59 |
match_equals = re.compile(r'(%s)=(.*)' % ('|'.join(configuration),)).match |
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
60 |
args = sys.argv[1:] |
61 |
if args == ['--help']: |
|
62 |
print helpstring |
|
63 |
sys.exit(0) |
|
64 |
||
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
65 |
# If we end up using a temporary directory for storing our eggs, this will
|
66 |
# hold the path of that directory. On the other hand, if an explicit directory
|
|
67 |
# is specified in the argv, this will remain None.
|
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
68 |
tmpeggs = None |
69 |
||
70 |
while args: |
|
71 |
val = args[0] |
|
72 |
if val in configuration: |
|
73 |
del args[0] |
|
74 |
if not args or args[0].startswith('-'): |
|
75 |
print "ERROR: %s requires an argument." |
|
76 |
print helpstring |
|
77 |
sys.exit(1) |
|
78 |
configuration[val] = args[0] |
|
79 |
else: |
|
80 |
match = match_equals(val) |
|
81 |
if match and match.group(1) in configuration: |
|
82 |
configuration[match.group(1)] = match.group(2) |
|
83 |
else: |
|
84 |
break
|
|
85 |
del args[0] |
|
86 |
||
87 |
for name in ('--ez_setup-source', '--download-base'): |
|
88 |
val = configuration[name] |
|
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
89 |
if val is not None and '://' not in val: # We're being lazy. |
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
90 |
configuration[name] = 'file://%s' % ( |
91 |
urllib.pathname2url(os.path.abspath(os.path.expanduser(val))),) |
|
92 |
||
93 |
if (configuration['--download-base'] and |
|
94 |
not configuration['--download-base'].endswith('/')): |
|
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
95 |
# Download base needs a trailing slash to make the world happy.
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
96 |
configuration['--download-base'] += '/' |
97 |
||
98 |
if not configuration['--eggs']: |
|
99 |
configuration['--eggs'] = tmpeggs = tempfile.mkdtemp() |
|
100 |
else: |
|
101 |
configuration['--eggs'] = os.path.abspath( |
|
102 |
os.path.expanduser(configuration['--eggs'])) |
|
103 |
||
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
104 |
# The requirement is what we will pass to setuptools to specify zc.buildout.
|
105 |
requirement = 'zc.buildout' |
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
106 |
if configuration['--version']: |
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
107 |
requirement += '==' + configuration['--version'] |
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
108 |
|
109 |
try: |
|
8926.1.1
by Gary Poster
handle a couple more site-packages issues; one if pkg_resources is around but setuptools is not; and the other to make developing an egg have the same sys.path as bin/buildout |
110 |
import setuptools # A flag. Sometimes pkg_resources is installed alone. |
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
111 |
import pkg_resources |
112 |
except ImportError: |
|
113 |
ez = {} |
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
114 |
exec urllib2.urlopen(configuration['--ez_setup-source']).read() in ez |
115 |
setuptools_args = dict(to_dir=configuration['--eggs'], download_delay=0) |
|
116 |
if configuration['--download-base']: |
|
117 |
setuptools_args['download_base'] = configuration['--download-base'] |
|
118 |
ez['use_setuptools'](**setuptools_args) |
|
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
119 |
import pkg_resources |
8926.1.1
by Gary Poster
handle a couple more site-packages issues; one if pkg_resources is around but setuptools is not; and the other to make developing an egg have the same sys.path as bin/buildout |
120 |
# This does not (always?) update the default working set. We will
|
121 |
# do it.
|
|
122 |
for path in sys.path: |
|
123 |
if path not in pkg_resources.working_set.entries: |
|
124 |
pkg_resources.working_set.add_entry(path) |
|
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
125 |
|
126 |
if sys.platform == 'win32': |
|
127 |
def quote(c): |
|
128 |
if ' ' in c: |
|
129 |
return '"%s"' % c # work around spawn lamosity on windows |
|
130 |
else: |
|
131 |
return c |
|
132 |
else: |
|
133 |
def quote (c): |
|
134 |
return c |
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
135 |
cmd = [quote(sys.executable), |
136 |
'-c', |
|
137 |
quote('from setuptools.command.easy_install import main; main()'), |
|
138 |
'-mqNxd', |
|
139 |
quote(configuration['--eggs'])] |
|
140 |
||
141 |
if configuration['--download-base']: |
|
142 |
cmd.extend(['-f', quote(configuration['--download-base'])]) |
|
143 |
||
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
144 |
cmd.append(requirement) |
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
145 |
|
146 |
ws = pkg_resources.working_set |
|
147 |
env = dict( |
|
148 |
os.environ, |
|
149 |
PYTHONPATH=ws.find(pkg_resources.Requirement.parse('setuptools')).location) |
|
150 |
||
8827.2.2
by Gary Poster
use temporary, local eggs for buildout work. |
151 |
is_jython = sys.platform.startswith('java') |
152 |
if is_jython: |
|
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
153 |
import subprocess |
8827.2.2
by Gary Poster
use temporary, local eggs for buildout work. |
154 |
exitcode = subprocess.Popen(cmd, env=env).wait() |
155 |
else: # Windows needs this, apparently; otherwise we would prefer subprocess |
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
156 |
exitcode = os.spawnle(*([os.P_WAIT, sys.executable] + cmd + [env])) |
157 |
if exitcode != 0: |
|
8827.2.4
by Gary Poster
try to make ec2test happy, and fix up a bootstrap bug. |
158 |
sys.stdout.flush() |
8827.2.2
by Gary Poster
use temporary, local eggs for buildout work. |
159 |
print ("An error occured when trying to install zc.buildout. " |
160 |
"Look above this message for any errors that "
|
|
161 |
"were output by easy_install.") |
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
162 |
sys.exit(exitcode) |
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
163 |
|
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
164 |
ws.add_entry(configuration['--eggs']) |
8827.2.3
by Gary Poster
put in new zc.buildout eggs |
165 |
ws.require(requirement) |
8234.1.2
by Gary Poster
checkpoint: this initial buildout variant has several parts working, including run, start, stop, and harness. |
166 |
import zc.buildout.buildout |
8827.2.1
by Gary Poster
use experimental eggs for buildout and friends |
167 |
args.append('bootstrap') |
168 |
zc.buildout.buildout.main(args) |
|
169 |
if tmpeggs is not None: |
|
170 |
shutil.rmtree(tmpeggs) |