Statistics
| Branch: | Tag: | Revision:

root / fabfile.py @ 46e05679

History | View | Annotate | Download (8.7 kB)

1 5832c79d Kostas Papadimitriou
# Copyright 2011 GRNET S.A. All rights reserved.
2 5832c79d Kostas Papadimitriou
#
3 5832c79d Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or
4 5832c79d Kostas Papadimitriou
# without modification, are permitted provided that the following
5 5832c79d Kostas Papadimitriou
# conditions are met:
6 5832c79d Kostas Papadimitriou
#
7 5832c79d Kostas Papadimitriou
#   1. Redistributions of source code must retain the above
8 5832c79d Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
9 5832c79d Kostas Papadimitriou
#      disclaimer.
10 5832c79d Kostas Papadimitriou
#
11 5832c79d Kostas Papadimitriou
#   2. Redistributions in binary form must reproduce the above
12 5832c79d Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
13 5832c79d Kostas Papadimitriou
#      disclaimer in the documentation and/or other materials
14 5832c79d Kostas Papadimitriou
#      provided with the distribution.
15 5832c79d Kostas Papadimitriou
#
16 5832c79d Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 5832c79d Kostas Papadimitriou
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 5832c79d Kostas Papadimitriou
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 5832c79d Kostas Papadimitriou
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 5832c79d Kostas Papadimitriou
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 5832c79d Kostas Papadimitriou
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 5832c79d Kostas Papadimitriou
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 5832c79d Kostas Papadimitriou
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 5832c79d Kostas Papadimitriou
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 5832c79d Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 5832c79d Kostas Papadimitriou
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 5832c79d Kostas Papadimitriou
# POSSIBILITY OF SUCH DAMAGE.
28 5832c79d Kostas Papadimitriou
#
29 5832c79d Kostas Papadimitriou
# The views and conclusions contained in the software and
30 5832c79d Kostas Papadimitriou
# documentation are those of the authors and should not be
31 5832c79d Kostas Papadimitriou
# interpreted as representing official policies, either expressed
32 5832c79d Kostas Papadimitriou
# or implied, of GRNET S.A.
33 5832c79d Kostas Papadimitriou
#
34 5832c79d Kostas Papadimitriou
35 5832c79d Kostas Papadimitriou
import os
36 5832c79d Kostas Papadimitriou
import sys
37 5832c79d Kostas Papadimitriou
38 f3d68c6f Kostas Papadimitriou
from contextlib import contextmanager
39 5832c79d Kostas Papadimitriou
from fabric.api import *
40 5832c79d Kostas Papadimitriou
from fabric.colors import *
41 5832c79d Kostas Papadimitriou
42 5832c79d Kostas Papadimitriou
env.project_root = "./"
43 5832c79d Kostas Papadimitriou
env.develop = False
44 5832c79d Kostas Papadimitriou
env.autoremove = True
45 8bf93821 Kostas Papadimitriou
env.packages = ['snf-common', 'snf-cyclades-app', 'snf-cyclades-gtools',
46 8bf93821 Kostas Papadimitriou
                'snf-webproject']
47 5832c79d Kostas Papadimitriou
env.capture = False
48 5832c79d Kostas Papadimitriou
env.colors = True
49 fec363b3 Kostas Papadimitriou
env.pypi_root = 'pypi'
50 fec363b3 Kostas Papadimitriou
env.roledefs = {
51 fec363b3 Kostas Papadimitriou
    'docs': ['docs.dev.grnet.gr'],
52 fec363b3 Kostas Papadimitriou
    'pypi': ['docs.dev.grnet.gr']
53 fec363b3 Kostas Papadimitriou
}
54 5832c79d Kostas Papadimitriou
55 c1129823 Kostas Papadimitriou
56 77bdcdd0 Vangelis Koukis
# colored logging
57 5832c79d Kostas Papadimitriou
notice = lambda x: sys.stdout.write(yellow(x) + "\n")
58 5832c79d Kostas Papadimitriou
info = lambda x: sys.stdout.write(green(x) + "\n")
59 5832c79d Kostas Papadimitriou
error = lambda x: sys.stdout.write(red(x) + "\n")
60 5832c79d Kostas Papadimitriou
61 096f0cc3 Vangelis Koukis
62 5832c79d Kostas Papadimitriou
def dev():
63 5832c79d Kostas Papadimitriou
    env.develop = True
64 5832c79d Kostas Papadimitriou
65 e4740989 Vangelis Koukis
66 5832c79d Kostas Papadimitriou
# wrap local to respect global capturing setting from env.capture
67 5832c79d Kostas Papadimitriou
oldlocal = local
68 f3d68c6f Kostas Papadimitriou
def local(cmd, capture="default"):
69 f3d68c6f Kostas Papadimitriou
    if capture != "default":
70 f3d68c6f Kostas Papadimitriou
        capture = capture
71 f3d68c6f Kostas Papadimitriou
    else:
72 f3d68c6f Kostas Papadimitriou
        capture = env.capture
73 f3d68c6f Kostas Papadimitriou
    return oldlocal(cmd, capture=capture)
74 5832c79d Kostas Papadimitriou
75 5832c79d Kostas Papadimitriou
76 5832c79d Kostas Papadimitriou
def package_root(p):
77 5832c79d Kostas Papadimitriou
    return os.path.join(env.project_root, p)
78 5832c79d Kostas Papadimitriou
79 5832c79d Kostas Papadimitriou
80 5832c79d Kostas Papadimitriou
def remove_pkg(p):
81 5832c79d Kostas Papadimitriou
    notice("uninstalling package: %s" % p)
82 5832c79d Kostas Papadimitriou
    with lcd(package_root(p)):
83 5832c79d Kostas Papadimitriou
        with settings(warn_only=True):
84 5832c79d Kostas Papadimitriou
            local("pip uninstall %s -y" % p, env.capture)
85 5832c79d Kostas Papadimitriou
86 5832c79d Kostas Papadimitriou
87 5832c79d Kostas Papadimitriou
def build_pkg(p):
88 5832c79d Kostas Papadimitriou
    info ("building package: %s" % p)
89 5832c79d Kostas Papadimitriou
    with lcd(package_root(p)):
90 aec8f58c Kostas Papadimitriou
        local("if [ -d dist ]; then rm -r dist; fi;")
91 aec8f58c Kostas Papadimitriou
        local("if [ -d build ]; then rm -r build; fi;")
92 fec363b3 Kostas Papadimitriou
        local("python setup.py egg_info -d sdist")
93 5832c79d Kostas Papadimitriou
94 5832c79d Kostas Papadimitriou
95 5832c79d Kostas Papadimitriou
def install_pkg(p):
96 5832c79d Kostas Papadimitriou
    info("installing package: %s" % p)
97 5832c79d Kostas Papadimitriou
    with lcd(package_root(p)):
98 5832c79d Kostas Papadimitriou
        if env.develop:
99 5832c79d Kostas Papadimitriou
            local("python setup.py develop")
100 5832c79d Kostas Papadimitriou
        else:
101 5832c79d Kostas Papadimitriou
            local("python setup.py install")
102 5832c79d Kostas Papadimitriou
103 5832c79d Kostas Papadimitriou
104 a0757a1c Kostas Papadimitriou
def install(*packages):
105 a0757a1c Kostas Papadimitriou
    for p in packages:
106 a0757a1c Kostas Papadimitriou
        install_pkg("snf-%s" % p)
107 a0757a1c Kostas Papadimitriou
108 a0757a1c Kostas Papadimitriou
109 5832c79d Kostas Papadimitriou
def buildall():
110 5832c79d Kostas Papadimitriou
    for p in env.packages:
111 5832c79d Kostas Papadimitriou
        build_pkg(p)
112 5832c79d Kostas Papadimitriou
    collectdists()
113 5832c79d Kostas Papadimitriou
114 5832c79d Kostas Papadimitriou
115 a0757a1c Kostas Papadimitriou
def installall():
116 a0757a1c Kostas Papadimitriou
    for p in env.packages:
117 a0757a1c Kostas Papadimitriou
        install_pkg(p)
118 a0757a1c Kostas Papadimitriou
119 77bdcdd0 Vangelis Koukis
120 5832c79d Kostas Papadimitriou
def collectdists():
121 5832c79d Kostas Papadimitriou
    if os.path.exists("./packages"):
122 5832c79d Kostas Papadimitriou
        notice("removing 'packages' directory")
123 5832c79d Kostas Papadimitriou
        local("rm -r packages");
124 5832c79d Kostas Papadimitriou
125 5832c79d Kostas Papadimitriou
    local("mkdir packages");
126 5832c79d Kostas Papadimitriou
    for p in env.packages:
127 5832c79d Kostas Papadimitriou
        local("cp %s/dist/*.tar.gz ./packages/" % package_root(p));
128 5832c79d Kostas Papadimitriou
129 77bdcdd0 Vangelis Koukis
130 5832c79d Kostas Papadimitriou
def removeall():
131 5832c79d Kostas Papadimitriou
    for p in env.packages:
132 5832c79d Kostas Papadimitriou
        remove_pkg(p)
133 5832c79d Kostas Papadimitriou
134 5832c79d Kostas Papadimitriou
135 5832c79d Kostas Papadimitriou
def remove(*packages):
136 5832c79d Kostas Papadimitriou
    for p in packages:
137 5832c79d Kostas Papadimitriou
        remove_pkg("snf-%s" % p)
138 5832c79d Kostas Papadimitriou
139 5832c79d Kostas Papadimitriou
140 f3d68c6f Kostas Papadimitriou
#
141 f3d68c6f Kostas Papadimitriou
# GIT helpers
142 f3d68c6f Kostas Papadimitriou
#
143 f3d68c6f Kostas Papadimitriou
144 f3d68c6f Kostas Papadimitriou
145 f3d68c6f Kostas Papadimitriou
def git(params, locl=True):
146 f3d68c6f Kostas Papadimitriou
    cmd = local if locl else run
147 f3d68c6f Kostas Papadimitriou
    return cmd("git %s" % params, capture=True)
148 f3d68c6f Kostas Papadimitriou
149 f3d68c6f Kostas Papadimitriou
150 f3d68c6f Kostas Papadimitriou
def branch():
151 f3d68c6f Kostas Papadimitriou
    return git("symbolic-ref HEAD").split("/")[-1]
152 f3d68c6f Kostas Papadimitriou
153 f3d68c6f Kostas Papadimitriou
154 f3d68c6f Kostas Papadimitriou
@contextmanager
155 f3d68c6f Kostas Papadimitriou
def co(c):
156 f3d68c6f Kostas Papadimitriou
    current_branch = branch();
157 f3d68c6f Kostas Papadimitriou
    git("checkout %s" % c)
158 98df1105 Vangelis Koukis
    # Use a try block to make sure we checkout the original branch.
159 98df1105 Vangelis Koukis
    try:
160 98df1105 Vangelis Koukis
        yield
161 98df1105 Vangelis Koukis
    finally:
162 096f0cc3 Vangelis Koukis
        try:
163 096f0cc3 Vangelis Koukis
            git("checkout %s" % current_branch)
164 096f0cc3 Vangelis Koukis
        except Exception:
165 096f0cc3 Vangelis Koukis
            error("Could not checkout %s, you're still left at %s" % c)
166 f3d68c6f Kostas Papadimitriou
167 f3d68c6f Kostas Papadimitriou
#
168 f3d68c6f Kostas Papadimitriou
# Debian packaging helpers
169 f3d68c6f Kostas Papadimitriou
#
170 f3d68c6f Kostas Papadimitriou
171 c1129823 Kostas Papadimitriou
env.debian_branch = 'debian-0.8'
172 8bf93821 Kostas Papadimitriou
env.deb_packages = ['snf-common', 'snf-cyclades-app', 'snf-cyclades-gtools', 'snf-webproject']
173 8e7dc2ce Vangelis Koukis
env.signdebs = False
174 096f0cc3 Vangelis Koukis
env.debrelease = False  # Increase release number in Debian changelogs
175 e4740989 Vangelis Koukis
176 c1129823 Kostas Papadimitriou
177 77bdcdd0 Vangelis Koukis
178 c1129823 Kostas Papadimitriou
def _last_commit(f):
179 c1129823 Kostas Papadimitriou
    return local("git rev-list --all --date-order --max-count=1 %s" % f,
180 c1129823 Kostas Papadimitriou
            capture=True).strip()
181 c1129823 Kostas Papadimitriou
182 77bdcdd0 Vangelis Koukis
183 c1129823 Kostas Papadimitriou
def _diff_from_master(c,f):
184 c1129823 Kostas Papadimitriou
    return local("git log --oneline %s..master %s" \
185 c1129823 Kostas Papadimitriou
                 " | wc -l" % (c, f), capture=True)
186 c1129823 Kostas Papadimitriou
187 77bdcdd0 Vangelis Koukis
188 c1129823 Kostas Papadimitriou
def dch(p):
189 c1129823 Kostas Papadimitriou
    with co(env.debian_branch):
190 c1129823 Kostas Papadimitriou
        local("git merge master")
191 c1129823 Kostas Papadimitriou
        with lcd(package_root(p)):
192 096f0cc3 Vangelis Koukis
            local("if [ ! -d .git ]; then mkdir .git; fi")
193 096f0cc3 Vangelis Koukis
194 096f0cc3 Vangelis Koukis
            # FIXME:
195 096f0cc3 Vangelis Koukis
            # Checking for new changes in packages
196 096f0cc3 Vangelis Koukis
            # has been removed temporarily.
197 096f0cc3 Vangelis Koukis
            # Always create a new Debian changelog entry.
198 096f0cc3 Vangelis Koukis
            ## Check for new changes in package dir
199 096f0cc3 Vangelis Koukis
            #diff = _diff_from_master(_last_commit("debian/changelog"), ".")
200 096f0cc3 Vangelis Koukis
            #vercmd  = "git describe --tags --abbrev=0"\
201 096f0cc3 Vangelis Koukis
            #          " | sed -rn '\''s/^v(.*)/\\1/p'\''"
202 096f0cc3 Vangelis Koukis
            #version = local(vercmd, capture=True)
203 096f0cc3 Vangelis Koukis
            #if int(diff) > 0:
204 096f0cc3 Vangelis Koukis
            if True:
205 096f0cc3 Vangelis Koukis
                # Run git-dch in snapshot mode.
206 096f0cc3 Vangelis Koukis
                # TODO: Support a --release mode in fabfile
207 7e2a283b Vangelis Koukis
                if not env.debrelease:
208 7e2a283b Vangelis Koukis
                    notice(("Producing snapshot changelog entry, "
209 7e2a283b Vangelis Koukis
                            "use 'debrelease' to produce release entries."))
210 096f0cc3 Vangelis Koukis
                local(("git-dch --debian-branch=%s --auto %s" %
211 096f0cc3 Vangelis Koukis
                       (env.debian_branch,
212 096f0cc3 Vangelis Koukis
                        "--release" if env.debrelease else "--snapshot")))
213 096f0cc3 Vangelis Koukis
                local(("git commit debian/changelog"
214 096f0cc3 Vangelis Koukis
                       " -m 'Updated %s changelog'" % p))
215 096f0cc3 Vangelis Koukis
                notice(("Make sure to tag Debian release in %s" %
216 096f0cc3 Vangelis Koukis
                        env.debian_branch))
217 8dc7096e Kostas Papadimitriou
218 096f0cc3 Vangelis Koukis
            local("rmdir .git")
219 096f0cc3 Vangelis Koukis
220 096f0cc3 Vangelis Koukis
221 7e2a283b Vangelis Koukis
def dchall():
222 7e2a283b Vangelis Koukis
    for p in env.deb_packages:
223 7e2a283b Vangelis Koukis
        info("updating debian changelog for package: %s" % p)
224 7e2a283b Vangelis Koukis
        dch(p)
225 7e2a283b Vangelis Koukis
226 7e2a283b Vangelis Koukis
227 096f0cc3 Vangelis Koukis
def debrelease():
228 096f0cc3 Vangelis Koukis
    env.debrelease = True
229 c1129823 Kostas Papadimitriou
230 c1129823 Kostas Papadimitriou
231 8e7dc2ce Vangelis Koukis
def signdebs():
232 8e7dc2ce Vangelis Koukis
    env.signdebs = True
233 e4740989 Vangelis Koukis
234 f3d68c6f Kostas Papadimitriou
235 a6efa23d Kostas Papadimitriou
# Commands which automatically add and reset the version files which are not tracked by
236 a6efa23d Kostas Papadimitriou
# git. Those version files are created from each setup.py using the synnefo-common
237 a6efa23d Kostas Papadimitriou
# update_version, so we execute `python setup.py clean` to ensure that file gets
238 a6efa23d Kostas Papadimitriou
# created and git add will not fail. The reset of those files after each build
239 a6efa23d Kostas Papadimitriou
# certifies that succeded git checkouts will not fail due to existing local
240 a6efa23d Kostas Papadimitriou
# changes.
241 a6efa23d Kostas Papadimitriou
add_versions_cmd = "find . -regextype posix-egrep -regex \".*version.py$|.*\/synnefo\/versions\/.*py$\" -exec git add -f {} \;"
242 a6efa23d Kostas Papadimitriou
reset_versions_cmd = "find . -regextype posix-egrep -regex \".*version.py$|.*\/synnefo\/versions\/.*py$\" -exec git reset {} \;"
243 a6efa23d Kostas Papadimitriou
244 77bdcdd0 Vangelis Koukis
245 f3d68c6f Kostas Papadimitriou
def builddeb(p, master="master", branch="debian-0.8"):
246 e4740989 Vangelis Koukis
    with co(branch):
247 3b61a062 Vangelis Koukis
        info("Building debian package for %s" % p)
248 e4740989 Vangelis Koukis
        with lcd(package_root(p)):
249 e4740989 Vangelis Koukis
            local("git merge master")
250 e4740989 Vangelis Koukis
            local("if [ ! -d .git ]; then mkdir .git; fi")
251 c6b41340 Kostas Papadimitriou
            local("python setup.py clean")
252 a6efa23d Kostas Papadimitriou
            local(add_versions_cmd)
253 e4740989 Vangelis Koukis
            local(("git-buildpackage --git-upstream-branch=%s --git-debian-branch=%s"
254 e4740989 Vangelis Koukis
                   " --git-export=INDEX --git-ignore-new %s") %
255 e4740989 Vangelis Koukis
                   (master, branch, "" if env.signdebs else "-us -uc"))
256 f3d68c6f Kostas Papadimitriou
            local("rm -rf .git")
257 a6efa23d Kostas Papadimitriou
            local(reset_versions_cmd)
258 3b61a062 Vangelis Koukis
        info("Done building debian package for %s" % p)
259 f3d68c6f Kostas Papadimitriou
260 f3d68c6f Kostas Papadimitriou
261 f3d68c6f Kostas Papadimitriou
def builddeball(b="debian-0.8"):
262 f3d68c6f Kostas Papadimitriou
    for p in env.deb_packages:
263 555db578 Vangelis Koukis
        builddeb(p=p, branch=b)
264 f3d68c6f Kostas Papadimitriou
265 5832c79d Kostas Papadimitriou
266 fec363b3 Kostas Papadimitriou
@roles('pypi')
267 fec363b3 Kostas Papadimitriou
def uploadtars():
268 fec363b3 Kostas Papadimitriou
    put("packages/*.tar.gz", 'www/pypi/')
269 fec363b3 Kostas Papadimitriou
270 dc0671c0 Kostas Papadimitriou
271 dc0671c0 Kostas Papadimitriou
def cleandocs():
272 dc0671c0 Kostas Papadimitriou
    """
273 dc0671c0 Kostas Papadimitriou
    Remove _build directories for each doc project
274 dc0671c0 Kostas Papadimitriou
    """
275 dc0671c0 Kostas Papadimitriou
276 70f13b0f Vangelis Koukis
    # docs contains conf.py in root directory
277 70f13b0f Vangelis Koukis
    if os.path.exists("docs/docs/_build"):
278 70f13b0f Vangelis Koukis
        local("rm -r docs/docs/_build")
279 dc0671c0 Kostas Papadimitriou
280 dc0671c0 Kostas Papadimitriou
    for p in env.packages:
281 dc0671c0 Kostas Papadimitriou
        buildpth = os.path.join(package_root(p), 'docs', '_build')
282 dc0671c0 Kostas Papadimitriou
        if os.path.exists(buildpth):
283 dc0671c0 Kostas Papadimitriou
            local('rm -r %s' % buildpth)
284 dc0671c0 Kostas Papadimitriou
285 dc0671c0 Kostas Papadimitriou
286 dc0671c0 Kostas Papadimitriou
def builddocs():
287 dc0671c0 Kostas Papadimitriou
    """
288 dc0671c0 Kostas Papadimitriou
    Run sphinx builder for each project separately
289 dc0671c0 Kostas Papadimitriou
    """
290 dc0671c0 Kostas Papadimitriou
    builddocs_cmd = "sphinx-build -b html -d _build/doctrees   . _build/html"
291 dc0671c0 Kostas Papadimitriou
292 70f13b0f Vangelis Koukis
    # docs contains conf.py in root directory
293 70f13b0f Vangelis Koukis
    with lcd("docs"):
294 dc0671c0 Kostas Papadimitriou
        local(builddocs_cmd)
295 dc0671c0 Kostas Papadimitriou
296 dc0671c0 Kostas Papadimitriou
    for p in env.packages:
297 dc0671c0 Kostas Papadimitriou
        info("Building %s docs" % p)
298 dc0671c0 Kostas Papadimitriou
        docspth = os.path.join(package_root(p), 'docs')
299 dc0671c0 Kostas Papadimitriou
        if os.path.exists(docspth):
300 dc0671c0 Kostas Papadimitriou
            with lcd(docspth):
301 dc0671c0 Kostas Papadimitriou
                local(builddocs_cmd)