~johnpaulett/djangorecipe/relative-paths

« back to all changes in this revision

Viewing changes to src/djangorecipe/recipe.py

  • Committer: Roland van Laar
  • Date: 2011-06-06 16:29:05 UTC
  • Revision ID: roland@micite.net-20110606162905-xovbmbp4yvu392vp
Removed download and svn code. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from random import choice
2
2
import os
3
 
import subprocess
4
 
import urllib2
5
 
import shutil
6
3
import logging
7
4
import re
8
5
 
9
 
from zc.buildout import UserError
10
6
import zc.recipe.egg
11
 
import setuptools
12
7
 
13
8
from boilerplate import script_template, versions
14
9
 
15
10
 
16
 
 
17
11
class Recipe(object):
18
12
    def __init__(self, buildout, name, options):
19
13
        self.log = logging.getLogger(name)
39
33
        else:
40
34
            options.setdefault('extra-paths', options.get('pythonpath', ''))
41
35
 
42
 
        # Usefull when using archived versions
43
 
        buildout['buildout'].setdefault(
44
 
            'download-cache',
45
 
            os.path.join(buildout['buildout']['directory'],
46
 
                         'downloads'))
47
 
 
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', '')
53
41
 
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'
57
 
 
58
42
    def install(self):
59
43
        location = self.options['location']
60
44
        base_dir = self.buildout['buildout']['directory']
61
45
 
62
46
        project_dir = os.path.join(base_dir, self.options['project'])
63
47
 
64
 
        download_dir = self.buildout['buildout']['download-cache']
65
 
        if not os.path.exists(download_dir):
66
 
            os.mkdir(download_dir)
67
 
 
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)
72
 
 
73
 
        if self.is_svn_url(version):
74
 
            self.install_svn_version(version, download_dir, location,
75
 
                                     self.install_from_cache)
76
 
        else:
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)
80
 
 
81
 
        self.options['setup'] = location
82
 
        development = zc.recipe.egg.Develop(self.buildout,
83
 
                                            self.options['recipe'],
84
 
                                            self.options)
85
 
        development.install()
86
 
        del self.options['setup']
87
 
 
88
48
        extra_paths = self.get_extra_paths()
89
49
        requirements, ws = self.egg.working_set(['djangorecipe'])
90
50
 
111
71
 
112
72
        return script_paths + [location]
113
73
 
114
 
    def install_svn_version(self, version, download_dir, location,
115
 
                            install_from_cache):
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):
123
 
                    raise UserError(
124
 
                        "Failed to update Django; %s. "
125
 
                        "Please check your internet connection." % (
126
 
                            download_location))
127
 
            else:
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'):
131
 
                    cmd += ' -q'
132
 
                if self.command(cmd):
133
 
                    raise UserError("Failed to checkout Django. "
134
 
                                    "Please check your internet connection.")
135
 
        else:
136
 
            self.log.info("Installing Django from cache: " + download_location)
137
 
 
138
 
        shutil.copytree(download_location, location)
139
 
 
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)
149
 
 
150
 
    def get_release(self, version, download_dir):
151
 
        tarball = os.path.join(download_dir, 'django-%s.tar.gz' % version)
152
 
 
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))
158
 
 
159
 
            tarball_f = open(tarball, 'wb')
160
 
            f = urllib2.urlopen(download_url % version)
161
 
            tarball_f.write(f.read())
162
 
            tarball_f.close()
163
 
            f.close()
164
 
        return tarball
165
 
 
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
258
166
        return scripts
259
167
 
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
266
 
 
267
 
    def version_to_svn(self, version):
268
 
        if version == 'trunk':
269
 
            return 'http://code.djangoproject.com/svn/django/trunk/'
270
 
        else:
271
 
            return version
272
 
 
273
 
    def version_to_download_suffix(self, version):
274
 
        if version == 'trunk':
275
 
            return 'svn'
276
 
        return [p for p in version.split('/') if p][-1]
277
 
 
278
 
    def svn_update(self, path, version):
279
 
        command = 'svn up'
280
 
        revision_search = re.compile(r'@([0-9]*)$').search(
281
 
            self.options['version'])
282
 
 
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'):
287
 
            command += ' -q'
288
 
        return self.command(command, cwd=path)
289
 
 
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
313
191
 
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'])
319
 
 
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)
330
203
 
331
 
    def command(self, cmd, **kwargs):
332
 
        output = subprocess.PIPE
333
 
        if self.buildout['buildout'].get('verbosity'):
334
 
            output = None
335
 
        command = subprocess.Popen(
336
 
            cmd, shell=True, stdout=output, **kwargs)
337
 
        return command.wait()
338
 
 
339
204
    def create_file(self, file, template, options):
340
205
        if os.path.exists(file):
341
206
            return