This repository has been archived by the owner on Sep 17, 2018. It is now read-only.
/
build
executable file
·368 lines (298 loc) · 10.9 KB
/
build
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#! /usr/bin/env python
# Public Domain (-) 2008-2014 The Wikifactory Authors.
# See the Wikifactory UNLICENSE file for details.
from cStringIO import StringIO
from hashlib import sha1
from inspect import getargspec
from json import dumps as encode_json, loads as decode_json
from os import chmod, listdir, mkdir, remove
from os.path import dirname, exists, isfile, join
from shutil import rmtree
from sys import argv, exit, platform, stdout
from time import sleep
from urllib import urlopen
from zipfile import ZipFile
from mako import exceptions
from mako.lookup import TemplateLookup
from plumbum import FG, local
from plumbum.cmd import assetgen
from yaml import load as load_yaml
# ------------------------------------------------------------------------------
# Print Utilities
# ------------------------------------------------------------------------------
def error(msg):
print "\033[1;31m!! ERROR: %s !!\033[0m" % msg
exit(1)
def start(msg):
print "\033[1;34m>> %s\033[0m" % msg
def progress(msg):
print "\033[1;30m## %s\033[0m" % msg
def success(msg):
print "\033[32m** %s\033[0m" % msg
# ------------------------------------------------------------------------------
# Platform Detection
# ------------------------------------------------------------------------------
if platform.startswith('darwin'):
PLATFORM = 'darwin'
elif platform.startswith('linux'):
PLATFORM = 'linux'
elif platform == 'win32':
PLATFORM = 'windows'
else:
error("The %r operating system isn't currently supported" % platform)
# ------------------------------------------------------------------------------
# File Handling
# ------------------------------------------------------------------------------
def get_yaml_conf(filename):
f = open(join(dirname(__file__), filename), 'rb')
conf = load_yaml(f)
f.close()
return conf
def read(filename):
f = open(filename, 'rb')
data = f.read()
f.close()
return data
# ------------------------------------------------------------------------------
# Global Constants
# ------------------------------------------------------------------------------
META = get_yaml_conf('meta.yaml')
JAVA_SDK_VERSION = META.get('java-sdk-version') or META['gae-version']
GAE_SDK_FILES = {
'java': "appengine-java-sdk-%s.zip" % JAVA_SDK_VERSION,
'go': "go_appengine_sdk_%s_amd64-%s.zip" % (PLATFORM, META['gae-version']),
'python': "google_appengine_%s.zip" % META['gae-version']
}
GAE_SDK_PATH_ROOT = (
"https://commondatastorage.googleapis.com/appengine-sdks/featured/"
)
SCRIPT_ROOT = dirname(__file__)
# -----------------------------------------------------------------------------
# Command Decorator
# -----------------------------------------------------------------------------
COMMANDS = {}
def register(func):
COMMANDS[func.__name__.replace('_', '-')] = (getargspec(func), func)
# ------------------------------------------------------------------------------
# Utility Functions/Commands
# ------------------------------------------------------------------------------
def get_path(*path):
return join(SCRIPT_ROOT, *path)
appcfg = local[get_path('.appengine_python_sdk', 'appcfg.py')]
dev_appserver = local[get_path('.appengine_python_sdk', 'dev_appserver.py')]
# ------------------------------------------------------------------------------
# Download Handlers
# ------------------------------------------------------------------------------
def download_gae_sdk(runtime):
name = "%s App Engine SDK" % runtime.title()
filename = GAE_SDK_FILES[runtime]
target_dir = get_path('.appengine_%s_sdk' % runtime)
version_file = join(target_dir, 'VERSION')
if runtime == 'java':
user_dir = join(target_dir, 'lib', 'user')
if exists(user_dir):
if exists(join(
user_dir,
'appengine-api-labs-%s.jar' % JAVA_SDK_VERSION
)):
success("%s is up-to-date." % name)
return
for subdir in listdir(user_dir):
if subdir.startswith('appengine-api-labs-'):
sdk_version = subdir.rsplit('.')[0].split('-')[-1]
elif exists(version_file):
sdk_version_file = open(version_file, 'rb')
sdk_version = sdk_version_file.readline().split()
sdk_version = sdk_version and sdk_version[-1] or ''
sdk_version_file.close()
if sdk_version == '"%s"' % META['gae-version-name']:
success("%s is up-to-date." % name)
return
start("Installing %s" % name)
if exists(target_dir):
progress("Removing Existing %s %s" % (name, sdk_version))
rmtree(target_dir)
progress("Downloading %s..." % name)
try:
req = urlopen(GAE_SDK_PATH_ROOT + filename)
if runtime == 'go':
checksum = META['go-%s-sdk' % PLATFORM]
else:
checksum = META['%s-sdk' % runtime]
sdk_file = validate_download(req, checksum)
except Exception, err:
error("Couldn't Download the %s: %r" % (name, err))
progress("Extracting %s" % name)
try:
mkdir(target_dir, 0777)
sdk_zip = ZipFile(sdk_file)
for iname in sdk_zip.namelist():
newname = iname.split("/", 1)
if len(newname) != 2:
continue
newname = newname[1]
if not newname:
continue
if iname.endswith('/'):
mkdir(join(target_dir, newname))
else:
target_path = join(target_dir, newname)
newfile = open(target_path, 'wb')
newfile.write(sdk_zip.read(iname))
newfile.close()
info = sdk_zip.getinfo(iname)
if info.external_attr:
chmod(target_path, info.external_attr >> 16)
except Exception, err:
error("Couldn't Extract the %s: %r" % (name, err))
success("%s Successfully Installed." % name)
def validate_download(req, checksum):
data = req.read()
if sha1(data).hexdigest() != checksum:
error("Mismatched checksum for downloaded file")
return StringIO(data)
# ------------------------------------------------------------------------------
# Build
# ------------------------------------------------------------------------------
pregen_template_dir = get_path('pregen')
def build(profile):
progress("Running assetgen")
if profile:
with local.cwd(SCRIPT_ROOT):
assetgen["assetgen.yaml", "--profile", profile] & FG
else:
with local.cwd(SCRIPT_ROOT):
assetgen["assetgen.yaml"] & FG
progress("Generating pregen.py")
assets = decode_json(read(get_path('app', 'assets.json')))
lookup = TemplateLookup(
directories=[pregen_template_dir],
input_encoding='utf-8',
output_encoding='utf-8'
)
def get_asset(name):
return read(get_path('app', 'build', assets[name]))
kwargs = {
'assets': assets,
'encode_json': encode_json,
'get_asset': get_asset,
'read': read
}
templates = ['# DO NOT EDIT.\n# Auto-generated file.']
out = templates.append
for template in listdir(pregen_template_dir):
if template.startswith('_') or not template.endswith('.mako'):
continue
tmpl = lookup.get_template(template)
try:
content = repr(tmpl.render(**kwargs)).replace('\\n', '\n')[1:-1]
out('%s = """%s"""' % (template[:-5].upper(), content))
except Exception:
print exceptions.text_error_template().render()
exit(1)
pregen_file = open(get_path('app', 'pregen.py'), 'wb')
pregen_file.write('\n\n'.join(templates))
pregen_file.close()
# ------------------------------------------------------------------------------
# Core Tasks
# ------------------------------------------------------------------------------
@register
def app(profile='', watch=False):
"""build the app files"""
start("Building app files")
if watch:
try:
watch = float(watch)
except Exception:
watch = 1.0
try:
while 1:
build(profile)
if not watch:
break
start("Sleeping ...")
sleep(watch)
except KeyboardInterrupt:
stdout.write('\b\b')
stdout.flush()
exit(0)
success("App files successfully built")
@register
def clean():
"""remove built app files"""
start("Removing built app files")
with local.cwd(SCRIPT_ROOT):
progress("Running assetgen --clean")
assetgen["assetgen.yaml", "--clean"] & FG
pregen_path = get_path('app', 'pregen.py')
if isfile(pregen_path):
progress("Removing pregen.py")
remove(pregen_path)
success("Built files successfully removed")
@register
def deploy(app='all'):
"""deploy a production build"""
start("Deploying %s" % app)
all = 0
if app == 'all':
all = 1
if all or app == 'python':
progress("Deploying the Python app")
with local.cwd(SCRIPT_ROOT):
appcfg["update", "app"] & FG
success("Finished deploying %s" % app)
@register
def install():
"""install the various dependencies"""
for runtime in ('python', 'go', 'java'):
download_gae_sdk(runtime)
@register
def run(profile='dev'):
"""build and run a local instance"""
start("Running servers under the %s profile" % profile)
with local.cwd(SCRIPT_ROOT):
dev_appserver["app"] & FG
# -----------------------------------------------------------------------------
# Script Runner
# -----------------------------------------------------------------------------
if __name__ == '__main__':
argv = argv[1:]
if not argv or argv[0] in ('-h', '--help', 'help'):
out = []
print "Usage: build [command]\n"
print "Commands:\n"
for cmd in sorted(COMMANDS):
spec, func = COMMANDS[cmd]
if spec.args:
extra = "/%s" % '/'.join("<%s>" % arg for arg in spec.args)
else:
extra = ""
out.append(("%s%s" % (cmd, extra), func.__doc__))
max = 0
for name, _ in out:
spacing = len(name)
if spacing > max:
max = spacing
max += 5
for name, doc in out:
pad = (max - len(name)) * ' '
print "\t%s%s%s" % (name, pad, doc)
print
exit(0)
for cmd in argv:
cmd = cmd.split('/', 1)[0]
if cmd not in COMMANDS:
error("Couldn't find the %s command" % cmd)
for cmd in argv:
args = cmd.split('/')
cmd = args.pop(0)
spec, func = COMMANDS[cmd]
slen = len(args)
req = len(spec.args)
if slen > req:
error("The %s command only takes %d arguments" % (cmd, req))
if spec.defaults:
req -= len(spec.defaults)
if slen < req:
error("The %s command requires at least %d arguments" % (cmd, req))
func(*args)