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