fabfile.py 6.93 KB
Newer Older
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
# Copyright 2011 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
#

import os
import sys

from contextlib import contextmanager
from fabric.api import *
from fabric.colors import *

env.project_root = "./"
env.develop = False
env.autoremove = True
Antony Chazapis's avatar
Antony Chazapis committed
45
env.packages = ['snf-pithos-backend', 'snf-pithos-app', 'snf-pithos-tools']
46 47 48 49 50 51 52 53
env.capture = False
env.colors = True
env.pypi_root = 'pypi'
env.roledefs = {
    'docs': ['docs.dev.grnet.gr'],
    'pypi': ['docs.dev.grnet.gr']
}

54 55

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


def dev():
    env.develop = True

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
# wrap local to respect global capturing setting from env.capture
oldlocal = local
def local(cmd, capture="default"):
    if capture != "default":
        capture = capture
    else:
        capture = env.capture
    return oldlocal(cmd, capture=capture)


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


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


def build_pkg(p):
    info ("building package: %s" % p)
    with lcd(package_root(p)):
Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
89 90 91 92
        try:
            local("rm -r dist build")
        except:
            pass
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        local("python setup.py egg_info -d sdist")


def install_pkg(p):
    info("installing package: %s" % p)
    with lcd(package_root(p)):
        if env.develop:
            local("python setup.py develop")
        else:
            local("python setup.py install")


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


def buildall():
    for p in env.packages:
        build_pkg(p)
    collectdists()


def installall():
    for p in env.packages:
        install_pkg(p)

120

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

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

130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
def removeall():
    for p in env.packages:
        remove_pkg(p)


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


#
# GIT helpers
#


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


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


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

#
# Debian packaging helpers
#

Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
172
env.debian_branch = 'debian-0.9'
Antony Chazapis's avatar
Antony Chazapis committed
173
env.deb_packages = ['snf-pithos-backend', 'snf-pithos-tools', 'snf-pithos-app']
174
env.signdebs = False
175 176
env.debrelease = False  # Increase release number in Debian changelogs
env.upstream = 'packaging'
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

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


def _diff_from_master(c,f):
    return local("git log --oneline %s..%s %s" \
                 " | wc -l" % (c, env.upstream, f), capture=True)


def dch(p):
    with co(env.debian_branch):
        local("git merge %s" % env.upstream)
        with lcd(package_root(p)):
            local("if [ ! -d .git ]; then mkdir .git; fi")

            # FIXME:
            # Checking for new changes in packages
            # has been removed temporarily.
            # Always create a new Debian changelog entry.
            ## Check for new changes in package dir
            #diff = _diff_from_master(_last_commit("debian/changelog"), ".")
            #vercmd  = "git describe --tags --abbrev=0"\
            #          " | sed -rn '\''s/^v(.*)/\\1/p'\''"
            #version = local(vercmd, capture=True)
            #if int(diff) > 0:
            if True:
                # Run git-dch in snapshot mode.
                # TODO: Support a --release mode in fabfile
                local(("git-dch --debian-branch=%s --auto %s" %
                       (env.debian_branch,
                        "--release" if env.debrelease else "--snapshot")))
                local(("git commit debian/changelog"
                       " -m 'Updated %s changelog'" % p))
                notice(("Make sure to tag Debian release in %s" %
                        env.debian_branch))

            local("rmdir .git")


def debrelease():
    env.debrelease = True


223 224
def signdebs():
    env.signdebs = True
225 226


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


Kostas Papadimitriou's avatar
Kostas Papadimitriou committed
243
def builddeball(b="debian-0.9"):
244
    for p in env.deb_packages:
245
        builddeb(p=p, branch=b)
246 247 248 249 250 251 252



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