Statistics
| Branch: | Tag: | Revision:

root / devtools / fabfile.py @ 4319c408

History | View | Annotate | Download (9 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

    
46
env.packages = ['snf-common', 'snf-cyclades-app', 'snf-cyclades-gtools',
47
                'snf-webproject', 'snf-pithos-backend', 'snf-pithos-app',
48
                'snf-pithos-tools', 'snf-astakos-app']
49

    
50
env.capture = False
51
env.colors = True
52
env.pypi_root = 'pypi'
53
env.roledefs = {
54
    'docs': ['docs.dev.grnet.gr'],
55
    'pypi': ['docs.dev.grnet.gr']
56
}
57

    
58

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

    
64

    
65
def dev():
66
    env.develop = True
67

    
68

    
69
# wrap local to respect global capturing setting from env.capture
70
oldlocal = local
71

    
72

    
73
def local(cmd, capture="default"):
74
    if capture != "default":
75
        capture = capture
76
    else:
77
        capture = env.capture
78
    return oldlocal(cmd, capture=capture)
79

    
80

    
81
def package_root(p):
82
    return os.path.join(env.project_root, p)
83

    
84

    
85
def remove_pkg(p):
86
    notice("uninstalling package: %s" % p)
87
    with lcd(package_root(p)):
88
        with settings(warn_only=True):
89
            local("pip uninstall %s -y" % p, env.capture)
90

    
91

    
92
def build_pkg(p):
93
    info("building package: %s" % p)
94
    with lcd(package_root(p)):
95
        local("if [ -d dist ]; then rm -r dist; fi;")
96
        local("if [ -d build ]; then rm -r build; fi;")
97
        local("python setup.py egg_info -d sdist")
98

    
99

    
100
def install_pkg(p):
101
    info("installing package: %s" % p)
102
    with lcd(package_root(p)):
103
        if env.develop:
104
            local("python setup.py develop")
105
        else:
106
            local("python setup.py install")
107

    
108

    
109
def install(*packages):
110
    for p in packages:
111
        install_pkg("snf-%s" % p)
112

    
113

    
114
def buildall():
115
    for p in env.packages:
116
        build_pkg(p)
117
    collectdists()
118

    
119

    
120
def installall():
121
    for p in env.packages:
122
        install_pkg(p)
123

    
124

    
125
def collectdists():
126
    if os.path.exists("./packages"):
127
        notice("removing 'packages' directory")
128
        local("rm -r packages")
129

    
130
    local("mkdir packages")
131
    for p in env.packages:
132
        local("cp %s/dist/*.tar.gz ./packages/" % package_root(p))
133

    
134

    
135
def removeall():
136
    for p in env.packages:
137
        remove_pkg(p)
138

    
139

    
140
def remove(*packages):
141
    for p in packages:
142
        remove_pkg("snf-%s" % p)
143

    
144

    
145
#
146
# GIT helpers
147
#
148

    
149

    
150
def git(params, locl=True):
151
    cmd = local if locl else run
152
    return cmd("git %s" % params, capture=True)
153

    
154

    
155
def branch():
156
    return git("symbolic-ref HEAD").split("/")[-1]
157

    
158

    
159
@contextmanager
160
def co(c):
161
    current_branch = branch()
162
    git("checkout %s" % c)
163
    # Use a try block to make sure we checkout the original branch.
164
    try:
165
        yield
166
    finally:
167
        try:
168
            git("checkout %s" % current_branch)
169
        except Exception:
170
            error("Could not checkout %s, you're still left at %s" % c)
171

    
172
#
173
# Debian packaging helpers
174
#
175

    
176
env.debian_branch = 'debian'
177
env.deb_packages = ['snf-common', 'snf-cyclades-app', 'snf-cyclades-gtools',
178
                    'snf-webproject', 'snf-pithos-backend', 'snf-pithos-tools',
179
                    'snf-pithos-app', 'snf-astakos-app']
180
env.signdebs = True
181
env.debrelease = False  # Increase release number in Debian changelogs
182

    
183
def _last_commit(f):
184
    return local("git rev-list --all --date-order --max-count=1 %s" % f,
185
                 capture=True).strip()
186

    
187

    
188
def _diff_from_master(c,f):
189
    return local("git log --oneline %s..master %s" \
190
                 " | wc -l" % (c, f), capture=True)
191

    
192

    
193
def dch(p):
194
    with co(env.debian_branch):
195
        local("git merge master")
196
        local("git merge %s" % env.upstream)
197
        with lcd(package_root(p)):
198
            local("if [ ! -d .git ]; then mkdir .git; fi")
199

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

    
224
            local("rmdir .git")
225

    
226

    
227
def dchall():
228
    for p in env.deb_packages:
229
        info("updating debian changelog for package: %s" % p)
230
        dch(p)
231

    
232

    
233
def debrelease():
234
    env.debrelease = True
235

    
236

    
237
def signdebs():
238
    env.signdebs = True
239

    
240

    
241
# Commands which automatically add and reset the version files which are not tracked by
242
# git. Those version files are created from each setup.py using the synnefo-common
243
# update_version, so we execute `python setup.py clean` to ensure that file gets
244
# created and git add will not fail. The reset of those files after each build
245
# certifies that succeded git checkouts will not fail due to existing local
246
# changes.
247
add_versions_cmd = "find . -regextype posix-egrep -regex \".*version.py$|.*\/synnefo\/versions\/.*py$\" -exec git add -f {} \;"
248
reset_versions_cmd = "find . -regextype posix-egrep -regex \".*version.py$|.*\/synnefo\/versions\/.*py$\" -exec git reset {} \;"
249

    
250

    
251
def builddeb(p, master="master", branch="debian-0.8"):
252
    with co(branch):
253
        info("Building debian package for %s" % p)
254
        with lcd(package_root(p)):
255
            local("git merge master")
256
            local("if [ ! -d .git ]; then mkdir .git; fi")
257
            local("python setup.py clean")
258
            local(add_versions_cmd)
259
            local(("git-buildpackage --git-upstream-branch=%s --git-debian-branch=%s"
260
                   " --git-export=INDEX --git-ignore-new %s") %
261
                  (master, branch, "" if env.signdebs else "-us -uc"))
262
            local("rm -rf .git")
263
            local(reset_versions_cmd)
264
        info("Done building debian package for %s" % p)
265

    
266

    
267
def builddeball(b="debian-0.8"):
268
    for p in env.deb_packages:
269
        builddeb(p=p, branch=b)
270

    
271

    
272
@roles('pypi')
273
def uploadtars():
274
    put("packages/*.tar.gz", 'www/pypi/')
275

    
276

    
277
def cleandocs():
278
    """
279
    Remove _build directories for each doc project
280
    """
281

    
282
    # docs contains conf.py in root directory
283
    if os.path.exists("docs/docs/_build"):
284
        local("rm -r docs/docs/_build")
285

    
286
    for p in env.packages:
287
        buildpth = os.path.join(package_root(p), 'docs', '_build')
288
        if os.path.exists(buildpth):
289
            local('rm -r %s' % buildpth)
290

    
291

    
292
def builddocs():
293
    """
294
    Run sphinx builder for each project separately
295
    """
296
    builddocs_cmd = "sphinx-build -b html -d _build/doctrees   . _build/html"
297

    
298
    # docs contains conf.py in root directory
299
    with lcd("docs"):
300
        local(builddocs_cmd)
301

    
302
    for p in env.packages:
303
        info("Building %s docs" % p)
304
        docspth = os.path.join(package_root(p), 'docs')
305
        if os.path.exists(docspth):
306
            with lcd(docspth):
307
                local(builddocs_cmd)