Statistics
| Branch: | Tag: | Revision:

root / fabfile.py @ 8dc7096e

History | View | Annotate | Download (7.8 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33
#
34

    
35
import os
36
import sys
37

    
38
from contextlib import contextmanager
39
from fabric.api import *
40
from fabric.colors import *
41

    
42
env.project_root = "./"
43
env.develop = False
44
env.autoremove = True
45
env.packages = ['snf-common', 'snf-app', 'snf-ganeti-tools', 'snf-webproject',
46
                'snf-okeanos-site']
47
env.capture = False
48
env.colors = True
49
env.pypi_root = 'pypi'
50
env.roledefs = {
51
    'docs': ['docs.dev.grnet.gr'],
52
    'pypi': ['docs.dev.grnet.gr']
53
}
54

    
55

    
56
# colored logging
57
notice = lambda x: sys.stdout.write(yellow(x) + "\n")
58
info = lambda x: sys.stdout.write(green(x) + "\n")
59
error = lambda x: sys.stdout.write(red(x) + "\n")
60

    
61

    
62
def dev():
63
    env.develop = True
64

    
65

    
66
# wrap local to respect global capturing setting from env.capture
67
oldlocal = local
68
def local(cmd, capture="default"):
69
    if capture != "default":
70
        capture = capture
71
    else:
72
        capture = env.capture
73
    return oldlocal(cmd, capture=capture)
74

    
75

    
76
def package_root(p):
77
    return os.path.join(env.project_root, p)
78

    
79

    
80
def remove_pkg(p):
81
    notice("uninstalling package: %s" % p)
82
    with lcd(package_root(p)):
83
        with settings(warn_only=True):
84
            local("pip uninstall %s -y" % p, env.capture)
85

    
86

    
87
def build_pkg(p):
88
    info ("building package: %s" % p)
89
    with lcd(package_root(p)):
90
        local("rm -r dist build")
91
        local("python setup.py egg_info -d sdist")
92

    
93

    
94
def install_pkg(p):
95
    info("installing package: %s" % p)
96
    with lcd(package_root(p)):
97
        if env.develop:
98
            local("python setup.py develop")
99
        else:
100
            local("python setup.py install")
101

    
102

    
103
def install(*packages):
104
    for p in packages:
105
        install_pkg("snf-%s" % p)
106

    
107

    
108
def buildall():
109
    for p in env.packages:
110
        build_pkg(p)
111
    collectdists()
112

    
113

    
114
def installall():
115
    for p in env.packages:
116
        install_pkg(p)
117

    
118

    
119
def collectdists():
120
    if os.path.exists("./packages"):
121
        notice("removing 'packages' directory")
122
        local("rm -r packages");
123

    
124
    local("mkdir packages");
125
    for p in env.packages:
126
        local("cp %s/dist/*.tar.gz ./packages/" % package_root(p));
127

    
128

    
129
def removeall():
130
    for p in env.packages:
131
        remove_pkg(p)
132

    
133

    
134
def remove(*packages):
135
    for p in packages:
136
        remove_pkg("snf-%s" % p)
137

    
138

    
139
#
140
# GIT helpers
141
#
142

    
143

    
144
def git(params, locl=True):
145
    cmd = local if locl else run
146
    return cmd("git %s" % params, capture=True)
147

    
148

    
149
def branch():
150
    return git("symbolic-ref HEAD").split("/")[-1]
151

    
152

    
153
@contextmanager
154
def co(c):
155
    current_branch = branch();
156
    git("checkout %s" % c)
157
    # Use a try block to make sure we checkout the original branch.
158
    try:
159
        yield
160
    finally:
161
        try:
162
            git("checkout %s" % current_branch)
163
        except Exception:
164
            error("Could not checkout %s, you're still left at %s" % c)
165

    
166
#
167
# Debian packaging helpers
168
#
169

    
170
env.debian_branch = 'debian-0.8'
171
env.deb_packages = ['snf-common', 'snf-app', 'snf-ganeti-tools', 'snf-webproject', 'snf-okeanos-site']
172
env.signdebs = True
173
env.debrelease = False  # Increase release number in Debian changelogs
174

    
175

    
176

    
177
def _last_commit(f):
178
    return local("git rev-list --all --date-order --max-count=1 %s" % f,
179
            capture=True).strip()
180

    
181

    
182
def _diff_from_master(c,f):
183
    return local("git log --oneline %s..master %s" \
184
                 " | wc -l" % (c, f), capture=True)
185

    
186

    
187
def dch(p):
188
    with co(env.debian_branch):
189
        local("git merge master")
190
        with lcd(package_root(p)):
191
            local("if [ ! -d .git ]; then mkdir .git; fi")
192

    
193
            # FIXME:
194
            # Checking for new changes in packages
195
            # has been removed temporarily.
196
            # Always create a new Debian changelog entry.
197
            ## Check for new changes in package dir
198
            #diff = _diff_from_master(_last_commit("debian/changelog"), ".")
199
            #vercmd  = "git describe --tags --abbrev=0"\
200
            #          " | sed -rn '\''s/^v(.*)/\\1/p'\''"
201
            #version = local(vercmd, capture=True)
202
            #if int(diff) > 0:
203
            if True:
204
                # Run git-dch in snapshot mode.
205
                # TODO: Support a --release mode in fabfile
206
                local(("git-dch --debian-branch=%s --auto %s" %
207
                       (env.debian_branch,
208
                        "--release" if env.debrelease else "--snapshot")))
209
                local(("git commit debian/changelog"
210
                       " -m 'Updated %s changelog'" % p))
211
                notice(("Make sure to tag Debian release in %s" %
212
                        env.debian_branch))
213

    
214
            local("rmdir .git")
215

    
216

    
217
def debrelease():
218
    env.debrelease = True
219

    
220

    
221
def nosigndebs():
222
    env.signdebs = False
223

    
224

    
225

    
226
def builddeb(p, master="master", branch="debian-0.8"):
227
    with co(branch):
228
        info("Building debian package for %s" % p)
229
        with lcd(package_root(p)):
230
            local("git merge master")
231
            local("if [ ! -d .git ]; then mkdir .git; fi")
232
            local("python setup.py clean")
233
            local("git add synnefo/versions/*.py -f")
234
            local(("git-buildpackage --git-upstream-branch=%s --git-debian-branch=%s"
235
                   " --git-export=INDEX --git-ignore-new %s") %
236
                   (master, branch, "" if env.signdebs else "-us -uc"))
237
            local("rm -rf .git")
238
            local("git reset synnefo/versions/*.py")
239
        info("Done building debian package for %s" % p)
240

    
241

    
242
def builddeball(b="debian-0.8"):
243
    for p in env.deb_packages:
244
        builddeb(p, b)
245

    
246

    
247

    
248
@roles('pypi')
249
def uploadtars():
250
    put("packages/*.tar.gz", 'www/pypi/')
251

    
252

    
253
def cleandocs():
254
    """
255
    Remove _build directories for each doc project
256
    """
257

    
258
    # snf-docs contains conf.py in root directory
259
    if os.path.exists("snf-docs/docs/_build"):
260
        local("rm -r snf-docs/docs/_build")
261

    
262
    for p in env.packages:
263
        buildpth = os.path.join(package_root(p), 'docs', '_build')
264
        if os.path.exists(buildpth):
265
            local('rm -r %s' % buildpth)
266

    
267

    
268
def builddocs():
269
    """
270
    Run sphinx builder for each project separately
271
    """
272
    builddocs_cmd = "sphinx-build -b html -d _build/doctrees   . _build/html"
273

    
274
    # snf-docs contains conf.py in root directory
275
    with lcd("snf-docs"):
276
        local(builddocs_cmd)
277

    
278
    for p in env.packages:
279
        info("Building %s docs" % p)
280
        docspth = os.path.join(package_root(p), 'docs')
281
        if os.path.exists(docspth):
282
            with lcd(docspth):
283
                local(builddocs_cmd)