-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfabfile.py
248 lines (189 loc) · 6.49 KB
/
fabfile.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
89
90
91
92
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
120
121
122
123
124
125
126
127
128
129
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# -*- coding: utf-8 -*-
import os
import re
import json
import datetime
import fileinput
import subprocess
import pkg_resources
from os.path import join as j
# Fabric imports
from fabric.api import env
from fabric.api import lcd
from fabric.api import task
from fabric.api import local
from fabric.colors import yellow
from fabric.colors import green
from fabric.tasks import execute
# -----------------------------------------------------------------------------
# CUSTOMER CUSTOM
# -----------------------------------------------------------------------------
REQUIREMENTS = "".split()
ENVIRONMENT = "HOME".split()
VERSION_PACKAGES = "plone.jsonapi.routes".split()
# -----------------------------------------------------------------------------
# FABRIC BOOTSTRAP
# -----------------------------------------------------------------------------
def cmd_exists(cmd):
""" check if the command exists
"""
return subprocess.call("type " + cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) == 0
# load the package distribution info into the environment
if not os.path.exists("package.json"):
raise RuntimeError("No package.json file found in %s" % os.getcwd())
# check requirements
for req in REQUIREMENTS:
if not cmd_exists(req):
raise RuntimeError("Requirement `%s` not found in PATH" % req)
# check enviornment variables
for req in ENVIRONMENT:
if req not in os.environ:
raise RuntimeError("Environment Variable `%s` not set" % req)
# load the json config into the fabric environment
env.package = json.loads(file("package.json", "r").read())
# -----------------------------------------------------------------------------
# HELPER
# -----------------------------------------------------------------------------
def get_section(name="main"):
""" returns the value of the section or an empty dict
"""
return env.package.get(name, {})
def get_option(name, section="main", default=None):
""" returns the value of the option
"""
return get_section(section).get(name, default)
def get_customer():
""" returns the customer name
"""
return get_option("customer")
def get_packages():
""" returns the packages
"""
return get_option("packages", default=[])
def get_src_dir():
""" returns the absolute path to the src directory
"""
path = get_option("src_dir")
return j(os.getcwd(), path)
def get_dist_dir():
""" returns the absolute path to the dist directory
"""
path = get_option("dist_dir")
return j(os.getcwd(), path)
def get_docs_dir():
""" returns the absolute path to the docs directory
"""
path = get_option("docs_dir")
return j(os.getcwd(), path)
def get_package_dir(package):
""" returns the absolute path to the python package
"""
src = get_src_dir()
path = j(os.getcwd(), src, package)
if os.path.exists(path):
return path
# different layout -- we are already inside the package
return src
def get_distribution(package):
""" get the pkg_resources distribution
"""
try:
return pkg_resources.get_distribution(package)
except pkg_resources.DistributionNotFound:
# setup pkg_resources to find the distribution
pkg_resources.working_set.add_entry(get_package_dir(package))
return pkg_resources.get_distribution(package)
def get_version(package):
""" return the (stripped) version of the given package
"""
dist = get_distribution(package)
return dist.version.strip()
def get_version_file(package):
""" return the version file of package or None
"""
package_dir = get_package_dir(package)
subdir = package.split(".")
subdir.append("version.py")
return j(package_dir, *subdir)
def write_version_info(package):
""" updates the build and date of the version module
The version.py must contain the version in the following format:
__date__ = "2015-07-09"
__build__ = 4711
__version__ = 42
"""
f = get_version_file(package)
if not os.path.exists(f):
raise RuntimeError("File '%s' does not exist." % f)
for line in fileinput.input(f, inplace=1):
if re.findall("__build__.*=", line):
build = int(line.split("=")[1])
line = "__build__ = %d" % (build + 1)
elif re.findall("__date__.*=", line):
now = datetime.datetime.now().strftime("%Y-%m-%d")
line = "__date__ = '%s'" % now
print line.strip("\n")
def get_full_version(package):
""" returns the verion from the verion file
"""
vf = get_version_file(package)
out = {}
lines = file(vf).readlines()
for l in lines:
if "=" in l and l.split("=")[0].strip() in \
("__build__", "__date__", "__version__"):
name, value = l.split("=")
name = name.strip(" _\n\r")
value = value.strip(' "\'\n\r')
# handle version method call gracefully
out[name] = value.replace("version()", get_version(package))
return out
# -----------------------------------------------------------------------------
# PUBLIC API
# -----------------------------------------------------------------------------
@task
def reload():
execute(bump_version)
local("wget --delete-after http://admin:admin@localhost:8080/@@reload?action=code")
print green("RELOADED CODE")
@task
def make_docs():
with lcd("docs"):
local("make html")
@task
def preview_docs():
with lcd("docs"):
local("open _build/html/index.html")
@task
def bump_version():
""" Bump up the version number
"""
for package in VERSION_PACKAGES:
version_file = get_version_file(package)
print yellow("bumping version ...")
write_version_info(package)
print green("adding file '%s' to next commit." % version_file)
local("git add " + version_file)
print green(get_full_version(package))
@task
def versions():
""" print the versions of the listed packages
"""
for package in get_packages():
dist = get_distribution(package)
print green(
"{} -> {}".format(
package, dist.version.strip()))
@task
def build():
""" Build all listed packages
"""
# build all package
for package in get_packages():
print yellow("Building Package '%s'" % package)
# build the package
local("python setup.py sdist bdist_egg")
print green("Finished Package '%s'" % package)