public
Description: An easy-to-use Fabric script for deploying Python projects
Homepage: http://lincolnloop.com/blog/2008/dec/07/simple-easy-deployment-fabric-and-virtualenv/
Clone URL: git://github.com/lincolnloop/lincoln-loop-deploy.git
lincoln-loop-deploy / fabfile.py
100644 106 lines (90 sloc) 3.883 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import sys
        
class VersionControl(object):
    """Generates command strings for VCS tasks"""
    def __init__(self, *args, **kwargs):
        for k,v in kwargs.items():
            setattr(self, k, v)
        self.cmd = '%s ' % self.dist
        
class Subversion(VersionControl):
    def checkout(self):
        cmd = self.cmd
        if hasattr(self, 'rev'):
            cmd += '-r %s ' % self.rev
        cmd += 'co %s ./src/%s' % (self.url, self.name)
        return cmd
 
class Git(VersionControl):
    def clone(self):
        cmd = '%s clone %s ./src/%s' % (self.cmd, self.url, self.name)
        if hasattr(self, 'branch'):
            cmd += '\\\n&& (cd ./src/%s; git checkout --track -b %s origin/%s)' % (self.name, self.branch, self.branch)
        return cmd
 
class Mercurial(VersionControl):
     def clone(self):
        cmd = '%s clone %s ./src/%s' % (self.cmd, self.url, self.name)
        if hasattr(self, 'branch'):
            cmd += '\\\n&& (cd ./src/%s; hg update -C %s )' % (self.name, self.branch)
        return cmd
 
class Bazaar(VersionControl):
    def branch(self):
        cmd = '%s branch %s ./src/%s' % (self.cmd, self.url, self.name)
        return cmd
 
 
def install_module(src_dir, module_name='', dist_utils=False):
    """
Installs a Python module from the ./src directory either using
distutils or by symlinking the package to site-packages
 
"""
 
    #setup using distutils
    if dist_utils:
        cmd = '(cd src/%s;\\\n../../ve/bin/python setup.py install)' % src_dir
    #symlink to site-packages
    else:
        src_path = os.path.join(src_dir,module_name).rstrip('/')
        cmd = '(cd ve/lib/python2.5/site-packages;\\\nln -s ../../../../src/%s .)' % (src_path)
    return cmd
        
 
def bootstrap():
    """
1. Creates a new virtualenv
2. Downloads all sources from fabreqs.py, adding them
to the PYTHONPATH along the way
"""
    #put the cwd on the python path so we can use fabreqs.py
    sys.path.append('.')
    from fabreqs import requirements
    local('virtualenv ve')
    #hack activate so it uses project directory instead of ve in prompt
    local('sed \'s/(`basename \\\\"\\$VIRTUAL_ENV\\\\\"`)/(`basename \\\\`dirname \\\\"$VIRTUAL_ENV\\\\"\\\\``)/g\' ve/bin/activate > ve/bin/activate.tmp')
    #sed 's/(`basename \\"\$VIRTUAL_ENV\\\"`)/(`basename \\`dirname \\"$VIRTUAL_ENV\\"\\``)/g'
    local('mv ve/bin/activate.tmp ve/bin/activate')
    local('mkdir src')
    for pkg in requirements:
        #easy_install package from PyPi
        if pkg['dist'] == 'pypi':
            cmd = './ve/bin/easy_install -a %s' % pkg['name']
            if pkg.has_key('rev'):
                cmd += '==%s' % pkg['rev']
            local(cmd)
            
        #download single file
        elif pkg['dist'] == 'wget':
            local('cd src && wget %s' % pkg['url'])
            local(install_module(pkg['name']))
        
        else: #it's a vcs
            if pkg['dist'] == 'svn':
                local(Subversion(**pkg).checkout())
            elif pkg['dist'] == 'git':
                local(Git(**pkg).clone())
            elif pkg['dist'] == 'hg':
                local(Mercurial(**pkg).clone())
            elif pkg['dist'] == 'bzr':
                local(Bazaar(**pkg).branch())
            else:
                raise Exception, '%s is not a recognized distribution method' % pkg['dist']
            #if a package name isn't specified, assume dist_utils
            if pkg.has_key('package'):
                if isinstance(pkg['package'], list):
                    for package in pkg['package']:
                        local(install_module(pkg['name'], package))
                else:
                    local(install_module(pkg['name'], pkg['package']))
            else:
                local(install_module(pkg['name'], dist_utils=True))