1
1
from random import choice
9
from zc.buildout import UserError
10
6
import zc.recipe.egg
13
8
from boilerplate import script_template, versions
17
11
class Recipe(object):
18
12
def __init__(self, buildout, name, options):
19
13
self.log = logging.getLogger(name)
40
34
options.setdefault('extra-paths', options.get('pythonpath', ''))
42
# Usefull when using archived versions
43
buildout['buildout'].setdefault(
45
os.path.join(buildout['buildout']['directory'],
48
36
# mod_wsgi support script
49
37
options.setdefault('wsgi', 'false')
50
38
options.setdefault('fcgi', 'false')
51
39
options.setdefault('wsgilog', '')
52
40
options.setdefault('logfile', '')
54
# only try to download stuff if we aren't asked to install from cache
55
self.install_from_cache = self.buildout['buildout'].get(
56
'install-from-cache', '').strip() == 'true'
59
43
location = self.options['location']
60
44
base_dir = self.buildout['buildout']['directory']
62
46
project_dir = os.path.join(base_dir, self.options['project'])
64
download_dir = self.buildout['buildout']['download-cache']
65
if not os.path.exists(download_dir):
66
os.mkdir(download_dir)
68
version = self.options['version']
69
# Remove a pre-existing installation if it is there
70
if os.path.exists(location):
71
shutil.rmtree(location)
73
if self.is_svn_url(version):
74
self.install_svn_version(version, download_dir, location,
75
self.install_from_cache)
77
tarball = self.get_release(version, download_dir)
78
# Extract and put the dir in its proper place
79
self.install_release(version, download_dir, tarball, location)
81
self.options['setup'] = location
82
development = zc.recipe.egg.Develop(self.buildout,
83
self.options['recipe'],
86
del self.options['setup']
88
48
extra_paths = self.get_extra_paths()
89
49
requirements, ws = self.egg.working_set(['djangorecipe'])
112
72
return script_paths + [location]
114
def install_svn_version(self, version, download_dir, location,
116
svn_url = self.version_to_svn(version)
117
download_location = os.path.join(
118
download_dir, 'django-' +
119
self.version_to_download_suffix(version))
120
if not install_from_cache:
121
if os.path.exists(download_location):
122
if self.svn_update(download_location, version):
124
"Failed to update Django; %s. "
125
"Please check your internet connection." % (
128
self.log.info("Checking out Django from svn: %s" % svn_url)
129
cmd = 'svn co \'%s\' \'%s\'' % (svn_url, download_location)
130
if not self.buildout['buildout'].get('verbosity'):
132
if self.command(cmd):
133
raise UserError("Failed to checkout Django. "
134
"Please check your internet connection.")
136
self.log.info("Installing Django from cache: " + download_location)
138
shutil.copytree(download_location, location)
140
def install_release(self, version, download_dir, tarball, destination):
141
extraction_dir = os.path.join(download_dir, 'django-archive')
142
setuptools.archive_util.unpack_archive(tarball, extraction_dir)
143
# Lookup the resulting extraction dir instead of guessing it
144
# (Django releases have a tendency not to be consistend here)
145
untarred_dir = os.path.join(extraction_dir,
146
os.listdir(extraction_dir)[0])
147
shutil.move(untarred_dir, destination)
148
shutil.rmtree(extraction_dir)
150
def get_release(self, version, download_dir):
151
tarball = os.path.join(download_dir, 'django-%s.tar.gz' % version)
153
# Only download when we don't yet have an archive
154
if not os.path.exists(tarball):
155
download_url = 'http://www.djangoproject.com/download/%s/tarball/'
156
self.log.info("Downloading Django from: %s" % (
157
download_url % version))
159
tarball_f = open(tarball, 'wb')
160
f = urllib2.urlopen(download_url % version)
161
tarball_f.write(f.read())
166
74
def create_manage_script(self, extra_paths, ws):
167
75
project = self.options.get('projectegg', self.options['project'])
168
76
return zc.buildout.easy_install.scripts(
257
165
zc.buildout.easy_install.script_template = _script_template
260
def is_svn_url(self, version):
261
# Search if there is http/https/svn or svn+[a tunnel identifier] in the
262
# url or if the trunk marker is used, all indicating the use of svn
263
svn_version_search = re.compile(
264
r'^(http|https|svn|svn\+[a-zA-Z-_]+)://|^(trunk)$').search(version)
265
return svn_version_search is not None
267
def version_to_svn(self, version):
268
if version == 'trunk':
269
return 'http://code.djangoproject.com/svn/django/trunk/'
273
def version_to_download_suffix(self, version):
274
if version == 'trunk':
276
return [p for p in version.split('/') if p][-1]
278
def svn_update(self, path, version):
280
revision_search = re.compile(r'@([0-9]*)$').search(
281
self.options['version'])
283
if revision_search is not None:
284
command += ' -r ' + revision_search.group(1)
285
self.log.info("Updating Django from svn")
286
if not self.buildout['buildout'].get('verbosity'):
288
return self.command(command, cwd=path)
290
168
def get_extra_paths(self):
291
169
extra_paths = [self.options['location'],
292
170
self.buildout['buildout']['directory'],
312
190
return extra_paths
314
192
def update(self):
315
newest = self.buildout['buildout'].get('newest') != 'false'
316
if newest and not self.install_from_cache and \
317
self.is_svn_url(self.options['version']):
318
self.svn_update(self.options['location'], self.options['version'])
320
193
extra_paths = self.get_extra_paths()
321
194
requirements, ws = self.egg.working_set(['djangorecipe'])
322
195
# Create the Django management script
328
201
# Make the wsgi and fastcgi scripts if enabled
329
202
self.make_scripts(extra_paths, ws)
331
def command(self, cmd, **kwargs):
332
output = subprocess.PIPE
333
if self.buildout['buildout'].get('verbosity'):
335
command = subprocess.Popen(
336
cmd, shell=True, stdout=output, **kwargs)
337
return command.wait()
339
204
def create_file(self, file, template, options):
340
205
if os.path.exists(file):