forked from glitchdotcom/WebPutty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
182 lines (157 loc) · 6.14 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
from __future__ import with_statement
import functools
import os
import sys
import shutil
import getpass
import re
import webbrowser
import zipfile
from fabric.api import env, local, abort, prompt
import compress
#Some environment information to customize
if os.name == 'posix':
APPENGINE_PATH = '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine'
PYTHON = '/usr/bin/python2.5'
else:
APPENGINE_PATH = r'C:\Program Files (x86)\Google\google_appengine'
PYTHON = r'C:\Python25\python.exe'
APPENGINE_APP_CFG = os.path.join(APPENGINE_PATH, 'appcfg.py')
print APPENGINE_APP_CFG
env.gae_email = None
env.gae_src = os.path.dirname(__file__)
#default values
env.dryrun = False
EXTRA_PATHS = [
APPENGINE_PATH,
os.path.join(APPENGINE_PATH, 'lib', 'antlr3'),
os.path.join(APPENGINE_PATH, 'lib', 'django'),
os.path.join(APPENGINE_PATH, 'lib', 'fancy_urllib'),
os.path.join(APPENGINE_PATH, 'lib', 'ipaddr'),
os.path.join(APPENGINE_PATH, 'lib', 'webob'),
os.path.join(APPENGINE_PATH, 'lib', 'yaml', 'lib'),
]
sys.path = EXTRA_PATHS + sys.path
from google.appengine.api import appinfo
def _include_appcfg(func):
'''Decorator that ensures the current Fabric env has a GAE app.yaml config
attached to it.'''
@functools.wraps(func)
def decorated_func(*args, **kwargs):
if not hasattr(env, 'app'):
appcfg = appinfo.LoadSingleAppInfo(open(os.path.join(env.gae_src, 'app.yaml')))
env.app = appcfg
return func(*args, **kwargs)
return decorated_func
def dryrun():
env.dryrun = True
@_include_appcfg
def deploy(tag=None):
env.deploy_path = env.gae_src
prompt('Email:', 'gae_email')
compress_js(env.deploy_path)
compress_css(env.deploy_path)
ziplibs(env.deploy_path)
_clean_babel()
if not env.dryrun:
print 'Deploying %s' % env.app.version
local('%s "%s" -A %s -V %s --email=%s update %s' % (PYTHON, APPENGINE_APP_CFG, env.app.application, env.app.version, env.gae_email, env.deploy_path), capture=False)
webbrowser.open('https://%s.appspot.com/' % env.app.application)
else:
print 'This is where we\'d actually deploy to App Engine, but this is a dryrun so we skip that part.'
clean_packages(env.deploy_path)
def compress_js(path=None):
if not path: path = env.gae_src
print 'Compressing JavaScript'
compress.compress_all_javascript(path)
def compress_css(path=None):
if not path: path = env.gae_src
print 'Compressing stylesheets'
compress.compress_all_stylesheets(path)
def clean_packages(base_path=None):
compress.revert_js_css_hashes(base_path)
def update_translations():
local('pybabel extract -F babel.cfg -o app/messages.pot --project=WebPutty --version=%s --copyright-holder="Fog Creek Software, Inc." --msgid-bugs-address=customer-service@fogcreek.com app' % tag)
local('pybabel update -i app/messages.pot -d app/translations')
_update_piglatin()
_update_english()
local('pybabel compile -d app/translations')
def add_locale():
prompt('Locale:', 'locale')
if env.locale:
local('pybabel init -i app/messages.pot -d app/translations -l %s' % env.locale)
else:
print 'You must enter a locale.'
def _update_english():
from babel.messages.pofile import read_po, write_po
from babel.messages.catalog import Catalog
with open('app/messages.pot', 'r') as f:
template = read_po(f)
catalog = Catalog()
for message in template:
catalog.add(message.id, message.id, locations=message.locations)
with open('app/translations/en/LC_MESSAGES/messages.po', 'w') as f:
write_po(f, catalog)
with open('app/translations/en_US/LC_MESSAGES/messages.po', 'w') as f:
write_po(f, catalog)
def _update_piglatin():
from babel.messages.pofile import read_po, write_po
from babel.messages.catalog import Catalog
with open('app/messages.pot', 'r') as f:
template = read_po(f)
catalog = Catalog()
for message in template:
trans = ' '.join([_piglatin_translate(w) for w in message.id.split(' ')])
catalog.add(message.id, trans, locations=message.locations)
with open('app/translations/aa/LC_MESSAGES/messages.po', 'w') as f:
write_po(f, catalog)
def _piglatin_translate(word):
""" convert one word into pig latin """
word = unicode(word)
m = len(word)
vowels = "a", "e", "i", "o", "u", "y"
if m < 3 or word == "the" or word.startswith('%'): # short words are not converted
return word
else:
for i in vowels:
if word.find(i) < m and word.find(i) != -1:
m = word.find(i)
if m==0:
return word + u"way"
else:
return word[m:] + word[:m] + u"ay"
def _clean_babel():
from settings import available_locales
locale_codes = [t[0] for t in available_locales]
if not env.deploy_path:
return
print 'Cleaning up unused localedata.'
localedata_dir = os.path.join(env.deploy_path, 'libs', 'babel', 'localedata')
for name in os.listdir(localedata_dir):
remove = True
for code in locale_codes:
if name.startswith(code):
remove = False
break
if remove:
os.unlink(os.path.join(localedata_dir, name))
def ziplibs(root_dir=None):
if not root_dir:
root_dir = os.path.abspath(os.path.dirname(__file__))
to_zip = os.path.join(root_dir, 'ziplibs')
print 'Cleaning %s of pyc files.' % to_zip
def rem_ext(ext, dirname, names):
for name in names:
if name.endswith(ext):
os.unlink(os.path.join(dirname, name))
os.path.walk(to_zip, rem_ext, '.pyc')
print 'Zipping ziplibs.'
zip_file = zipfile.ZipFile(to_zip + '.zip', 'w', compression=zipfile.ZIP_DEFLATED)
def add_file(args, dir_name, names):
zip_file, common_base = args
for name in names:
zip_file.write(os.path.join(dir_name, name), os.path.join(dir_name[len(common_base):], name))
os.path.walk(to_zip, add_file, (zip_file, os.path.dirname(to_zip)))
zip_file.close()
def lint():
local('pylint --rcfile=.pylintrc app')