Updated fabfile
[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.deb_packages = ['snf-pithos-lib', 'snf-pithos-backend',
48 'snf-pithos-tools', 'snf-pithos-app']
49 env.capture = False
50 env.colors = True
51 env.pypi_root = 'pypi'
52 env.roledefs = {
53     'docs': ['docs.dev.grnet.gr'],
54     'pypi': ['docs.dev.grnet.gr']
55 }
56
57 # coloured logging
58 notice = lambda x: sys.stdout.write(yellow(x) + "\n")
59 info = lambda x: sys.stdout.write(green(x) + "\n")
60 error = lambda x: sys.stdout.write(red(x) + "\n")
61
62
63 def dev():
64     env.develop = True
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         with settings(warn_only=True):
91             local("rm -r dist build")
92         local("python setup.py egg_info -d sdist")
93
94
95 def install_pkg(p):
96     info("installing package: %s" % p)
97     with lcd(package_root(p)):
98         print local('pwd');
99         if env.develop:
100             local("python setup.py develop")
101         else:
102             local("python setup.py install")
103
104
105 def install(*packages):
106     for p in packages:
107         install_pkg("snf-%s" % p)
108
109
110 def buildall():
111     for p in env.packages:
112         build_pkg(p)
113     collectdists()
114
115
116 def installall():
117     for p in env.packages:
118         install_pkg(p)
119
120 def collectdists():
121     if os.path.exists("./packages"):
122         notice("removing 'packages' directory")
123         local("rm -r packages");
124
125     local("mkdir packages");
126     for p in env.packages:
127         local("cp %s/dist/*.tar.gz ./packages/" % package_root(p));
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     yield
158     git("checkout %s" % current_branch)
159
160
161 #
162 # Debian packaging helpers
163 #
164
165
166 def builddeb(p, master="master", branch="debian-0.8"):
167     with lcd(package_root(p)):
168         with settings(warn_only=True):
169             local("mkdir .git")
170             local("python setup.py clean")
171             local("git add synnefo/versions/*.py -f")
172             local("git-buildpackage --git-upstream-branch=%s --git-debian-branch=%s \
173 --git-export=INDEX --git-ignore-new" % (master, branch))
174             local("rm -rf .git")
175             local("git reset synnefo/versions/*.py")
176
177
178 def builddeball(b="debian-0.8"):
179     with co(b):
180         for p in env.deb_packages:
181             builddeb(p, b)
182     collectdebs()
183
184
185 def collectdebs():
186     build_area = env.get('build_area', '../build-area')
187     for p in env.deb_packages:
188         local("cp %s/%s*.deb ./packages/" % (build_area, p))
189
190 @roles('pypi')
191 def uploadtars():
192     put("packages/*.tar.gz", 'www/pypi/')
193