Merge branch 'packaging' into debian-0.8
[pithos] / fabfile.py
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-pithos-lib', 'snf-pithos-backend', 'snf-pithos-app',
46                         'snf-pithos-tools']
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 # coloured 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 def dev():
62     env.develop = True
63
64
65 # wrap local to respect global capturing setting from env.capture
66 oldlocal = local
67 def local(cmd, capture="default"):
68     if capture != "default":
69         capture = capture
70     else:
71         capture = env.capture
72     return oldlocal(cmd, capture=capture)
73
74
75 def package_root(p):
76     return os.path.join(env.project_root, p)
77
78
79 def remove_pkg(p):
80     notice("uninstalling package: %s" % p)
81     with lcd(package_root(p)):
82         with settings(warn_only=True):
83             local("pip uninstall %s -y" % p, env.capture)
84
85
86 def build_pkg(p):
87     info ("building package: %s" % p)
88     with lcd(package_root(p)):
89         local("rm -r dist build")
90         local("python setup.py egg_info -d sdist")
91
92
93 def install_pkg(p):
94     info("installing package: %s" % p)
95     with lcd(package_root(p)):
96         if env.develop:
97             local("python setup.py develop")
98         else:
99             local("python setup.py install")
100
101
102 def install(*packages):
103     for p in packages:
104         install_pkg("snf-%s" % p)
105
106
107 def buildall():
108     for p in env.packages:
109         build_pkg(p)
110     collectdists()
111
112
113 def installall():
114     for p in env.packages:
115         install_pkg(p)
116
117
118 def collectdists():
119     if os.path.exists("./packages"):
120         notice("removing 'packages' directory")
121         local("rm -r packages");
122
123     local("mkdir packages");
124     for p in env.packages:
125         local("cp %s/dist/*.tar.gz ./packages/" % package_root(p));
126
127
128 def removeall():
129     for p in env.packages:
130         remove_pkg(p)
131
132
133 def remove(*packages):
134     for p in packages:
135         remove_pkg("snf-%s" % p)
136
137
138 #
139 # GIT helpers
140 #
141
142
143 def git(params, locl=True):
144     cmd = local if locl else run
145     return cmd("git %s" % params, capture=True)
146
147
148 def branch():
149     return git("symbolic-ref HEAD").split("/")[-1]
150
151
152 @contextmanager
153 def co(c):
154     current_branch = branch();
155     git("checkout %s" % c)
156     # Use a try block to make sure we checkout the original branch.
157     try:
158         yield
159     finally:
160         try:
161             git("checkout %s" % current_branch)
162         except Exception:
163             error("Could not checkout %s, you're still left at %s" % c)
164
165 #
166 # Debian packaging helpers
167 #
168
169 env.debian_branch = 'debian-0.8'
170 env.deb_packages = ['snf-pithos-lib', 'snf-pithos-backend',
171                     'snf-pithos-tools', 'snf-pithos-app']
172 env.signdebs = False
173 env.debrelease = False  # Increase release number in Debian changelogs
174 env.upstream = 'packaging'
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..%s %s" \
184                  " | wc -l" % (c, env.upstream, f), capture=True)
185
186
187 def dch(p):
188     with co(env.debian_branch):
189         local("git merge %s" % env.upstream)
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 signdebs():
222     env.signdebs = True
223
224
225 def builddeb(p, master="packaging", branch="debian-0.8"):
226     with co(branch):
227         info("Building debian package for %s" % p)
228         with lcd(package_root(p)):
229             local("git merge %s" % master)
230             local("if [ ! -d .git ]; then mkdir .git; fi")
231             local("python setup.py clean")
232             local("git add ./*/*/version.py -f")
233             local(("git-buildpackage --git-upstream-branch=%s --git-debian-branch=%s"
234                    " --git-export=INDEX --git-ignore-new %s") %
235                    (master, branch, "" if env.signdebs else "-us -uc"))
236             local("rm -rf .git")
237             local("git reset ./*/*/version.py")
238         info("Done building debian package for %s" % p)
239
240
241 def builddeball(b="debian-0.8"):
242     for p in env.deb_packages:
243         builddeb(p=p, branch=b)
244
245
246
247 @roles('pypi')
248 def uploadtars():
249     put("packages/*.tar.gz", 'www/pypi/')
250
251