Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 178 lines (152 sloc) 6.145 kb
bcf4ea9 @tghw Add gitattributes
tghw authored
1 from __future__ import with_statement
2
3 import functools
4 import os
5 import sys
6 import webbrowser
7 import zipfile
8 from fabric.api import env, local, prompt
9
10 import compress
11
12 #Some environment information to customize
13 if os.name == 'posix':
14 APPENGINE_PATH = '/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine'
15 PYTHON = '/usr/bin/python2.7'
16 else:
17 APPENGINE_PATH = r'C:\Program Files (x86)\Google\google_appengine'
18 PYTHON = r'C:\Python27\python.exe'
19 APPENGINE_APP_CFG = os.path.join(APPENGINE_PATH, 'appcfg.py')
20 print APPENGINE_APP_CFG
21
22 env.gae_src = os.path.dirname(__file__)
23
24 #default values
25 env.dryrun = False
26
27 EXTRA_PATHS = [
28 APPENGINE_PATH,
29 os.path.join(APPENGINE_PATH, 'lib', 'antlr3'),
30 os.path.join(APPENGINE_PATH, 'lib', 'django'),
31 os.path.join(APPENGINE_PATH, 'lib', 'fancy_urllib'),
32 os.path.join(APPENGINE_PATH, 'lib', 'ipaddr'),
33 os.path.join(APPENGINE_PATH, 'lib', 'webob'),
34 os.path.join(APPENGINE_PATH, 'lib', 'yaml', 'lib'),
35 ]
36
37 sys.path = EXTRA_PATHS + sys.path
38
39 from google.appengine.api import appinfo
40
41 def _include_appcfg(func):
42 '''Decorator that ensures the current Fabric env has a GAE app.yaml config
43 attached to it.'''
44 @functools.wraps(func)
45 def decorated_func(*args, **kwargs):
46 if not hasattr(env, 'app'):
47 appcfg = appinfo.LoadSingleAppInfo(open(os.path.join(env.gae_src, 'app.yaml')))
48 env.app = appcfg
49 return func(*args, **kwargs)
50 return decorated_func
51
52 def dryrun():
53 env.dryrun = True
54
55 @_include_appcfg
56 def deploy():
57 env.deploy_path = env.gae_src
58
59 compress_js(env.deploy_path)
60 compress_css(env.deploy_path)
61 ziplibs(env.deploy_path)
62 _clean_babel()
63
64 if not env.dryrun:
65 print 'Deploying %s' % env.app.version
66 local('%s "%s" -A %s -V %s --oauth2 update %s' % (PYTHON, APPENGINE_APP_CFG, env.app.application, env.app.version, env.deploy_path), capture=False)
67 webbrowser.open('https://%s.appspot.com/' % env.app.application)
68 else:
69 print 'This is where we\'d actually deploy to App Engine, but this is a dryrun so we skip that part.'
70
71 clean_packages(env.deploy_path)
72
73 def compress_js(path=None):
74 if not path: path = env.gae_src
75 print 'Compressing JavaScript'
76 compress.compress_all_javascript(path)
77
78 def compress_css(path=None):
79 if not path: path = env.gae_src
80 print 'Compressing stylesheets'
81 compress.compress_all_stylesheets(path)
82
83 def clean_packages(base_path=None):
84 compress.revert_js_css_hashes(base_path)
85
86 def update_translations():
87 local('pybabel extract -F babel.cfg -o app/messages.pot --project=WebPutty --copyright-holder="Fog Creek Software, Inc." --msgid-bugs-address=customer-service@fogcreek.com app')
88 local('pybabel update -i app/messages.pot -d app/translations')
89 _update_piglatin()
90 _update_english()
91 local('pybabel compile -d app/translations')
92
93 def add_locale():
94 prompt('Locale:', 'locale')
95 if env.locale:
96 local('pybabel init -i app/messages.pot -d app/translations -l %s' % env.locale)
97 else:
98 print 'You must enter a locale.'
99
100 def _update_english():
101 from babel.messages.pofile import read_po, write_po
102 from babel.messages.catalog import Catalog
103 with open('app/messages.pot', 'r') as f:
104 template = read_po(f)
105 catalog = Catalog()
106 for message in template:
107 catalog.add(message.id, message.id, locations=message.locations)
108 with open('app/translations/en/LC_MESSAGES/messages.po', 'w') as f:
109 write_po(f, catalog)
110 with open('app/translations/en_US/LC_MESSAGES/messages.po', 'w') as f:
111 write_po(f, catalog)
112
113 def _update_piglatin():
114 from babel.messages.pofile import read_po, write_po
115 from babel.messages.catalog import Catalog
116 with open('app/messages.pot', 'r') as f:
117 template = read_po(f)
118 catalog = Catalog()
119 for message in template:
120 trans = ' '.join([_piglatin_translate(w) for w in message.id.split(' ')])
121 catalog.add(message.id, trans, locations=message.locations)
122 with open('app/translations/aa/LC_MESSAGES/messages.po', 'w') as f:
123 write_po(f, catalog)
124
125 def _piglatin_translate(word):
126 """ convert one word into pig latin """
127 word = unicode(word)
128 m = len(word)
129 vowels = "a", "e", "i", "o", "u", "y"
130 if m < 3 or word == "the" or word.startswith('%'): # short words are not converted
131 return word
132 else:
133 for i in vowels:
134 if word.find(i) < m and word.find(i) != -1:
135 m = word.find(i)
136 if m==0:
137 return word + u"way"
138 else:
139 return word[m:] + word[:m] + u"ay"
140
141 def _clean_babel():
142 from settings import available_locales
143 locale_codes = [t[0] for t in available_locales]
144 if not env.deploy_path:
145 return
146 print 'Cleaning up unused localedata.'
147 localedata_dir = os.path.join(env.deploy_path, 'libs', 'babel', 'localedata')
148 for name in os.listdir(localedata_dir):
149 remove = True
150 for code in locale_codes:
151 if name.startswith(code):
152 remove = False
153 break
154 if remove:
155 os.unlink(os.path.join(localedata_dir, name))
156
157 def ziplibs(root_dir=None):
158 if not root_dir:
159 root_dir = os.path.abspath(os.path.dirname(__file__))
160 to_zip = os.path.join(root_dir, 'ziplibs')
161 print 'Cleaning %s of pyc files.' % to_zip
162 def rem_ext(ext, dirname, names):
163 for name in names:
164 if name.endswith(ext):
165 os.unlink(os.path.join(dirname, name))
166 os.path.walk(to_zip, rem_ext, '.pyc')
167 print 'Zipping ziplibs.'
168 zip_file = zipfile.ZipFile(to_zip + '.zip', 'w', compression=zipfile.ZIP_DEFLATED)
169 def add_file(args, dir_name, names):
170 zip_file, common_base = args
171 for name in names:
172 zip_file.write(os.path.join(dir_name, name), os.path.join(dir_name[len(common_base):], name))
173 os.path.walk(to_zip, add_file, (zip_file, os.path.dirname(to_zip)))
174 zip_file.close()
175
176 def lint():
177 local('pylint --rcfile=.pylintrc app')
Something went wrong with that request. Please try again.