-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
pavement.py
325 lines (254 loc) · 9.8 KB
/
pavement.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
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
# =================================================================
#
# Authors: Tom Kralidis <tomkralidis@gmail.com>
#
# Copyright (c) 2023 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================
import codecs
import glob
import os
import shutil
import tempfile
from io import BytesIO
from urllib.request import urlopen
import zipfile
from paver.easy import (Bunch, call_task, cmdopts, info, options,
path, pushd, sh, task)
BASEDIR = os.path.abspath(os.path.dirname(__file__))
options(
base=Bunch(
home=path(BASEDIR),
docs=path('%s/docs' % BASEDIR),
instance=path('%s/instance' % BASEDIR),
pot=path('%s/GeoHealthCheck/translations/en/LC_MESSAGES/messages.po' %
BASEDIR),
static_docs=path('%s/GeoHealthCheck/static/docs' % BASEDIR),
static_lib=path('%s/GeoHealthCheck/static/lib' % BASEDIR),
tmp=path(tempfile.mkdtemp()),
translations=path('%s/GeoHealthCheck/translations' % BASEDIR)
),
)
@task
def setup():
"""setup plugin dependencies"""
config_file = options.base.home / 'GeoHealthCheck/config_main.py'
config_site = options.base.instance / 'config_site.py'
# setup dirs
if not os.path.exists(options.base.static_lib):
options.base.static_lib.mkdir()
if not os.path.exists(options.base.instance):
options.base.instance.mkdir()
data_dir = options.base.instance / 'data'
data_dir.mkdir()
# data_dir.chmod(0777) gives failure on Python 2.7 Paver 1.2.1
os.chmod(path(data_dir), 0o777)
# setup config
config_file.copy(config_site)
# setup deps
sh('pip install -r requirements.txt')
skin = 'http://github.com/BlackrockDigital/startbootstrap-sb-admin-2/archive/v3.3.7+1.zip' # noqa
skin_dirs = ['dist', 'vendor']
need_to_fetch = False
for skin_dir in skin_dirs:
skin_dir_path = os.sep.join(
['startbootstrap-sb-admin-2-3.3.7-1', skin_dir])
if not os.path.exists(skin_dir_path):
need_to_fetch = True
if need_to_fetch:
zipstr = BytesIO(urlopen(skin).read())
zipfile_obj = zipfile.ZipFile(zipstr)
zipfile_obj.extractall(options.base.static_lib)
for zf_mem in skin_dirs:
src_loc = path(options.base.static_lib /
'startbootstrap-sb-admin-2-3.3.7-1' / zf_mem)
dest_loc = path(options.base.static_lib / zf_mem)
if not os.path.exists(dest_loc):
src_loc.move(dest_loc)
else:
info('directory already exists. Skipping')
shutil.rmtree(path(options.base.static_lib /
'startbootstrap-sb-admin-2-3.3.7-1'))
# install bootstrap-tagsinput to static/lib
info('Getting select2')
select2 = 'https://github.com/select2/select2/archive/4.0.3.zip'
zipstr = BytesIO(urlopen(select2).read())
zipfile_obj = zipfile.ZipFile(zipstr)
zipfile_obj.extractall(options.base.static_lib)
dirname = glob.glob(options.base.static_lib / 'select2-*')[0]
dstdir = ''.join(dirname.rsplit('-', 1)[:-1])
try:
os.rename(dirname, dstdir)
except OSError:
shutil.rmtree(dstdir)
os.rename(dirname, dstdir)
# install leafletjs to static/lib
info('Getting leaflet')
leafletjs = 'http://cdn.leafletjs.com/downloads/leaflet-0.7.5.zip'
zipstr = BytesIO(urlopen(leafletjs).read())
zipfile_obj = zipfile.ZipFile(zipstr)
zipfile_obj.extractall(options.base.static_lib / 'leaflet')
# install html5shiv to static/lib
cdn_url = 'https://cdn.jsdelivr.net/npm'
with open(path(options.base.static_lib / 'html5shiv.min.js'), 'w') as f:
url = \
'{}/html5shiv.min.js@3.7.2/html5shiv.min.js'.format(cdn_url)
content = urlopen(url).read().decode()
f.write(content)
# install respond to static/lib
with open(path(options.base.static_lib / 'respond.min.js'), 'w') as f:
url = '{}/respond.min.js@1.4.2/respond.min.js'.format(cdn_url)
content = urlopen(url).read().decode()
f.write(content)
# build i18n .mo files
call_task('compile_translations')
# build local docs
call_task('refresh_docs')
# message user
info('GeoHealthCheck is now built. Edit settings in %s' % config_site)
info('before deploying the application. Alternatively, you can start a')
info('development instance with "python GeoHealthCheck/app.py"')
@task
def create_secret_key():
"""create secret key for SECRET_KEY in instance/config_site.py"""
info('Secret key: \'%s\'' % codecs.encode(os.urandom(24), 'hex').decode())
info('Copy/paste this key to set the SECRET_KEY')
info('value in instance/config_site.py')
@task
@cmdopts([
('email=', 'e', 'email'),
('username=', 'u', 'username'),
('password=', 'p', 'password')
])
def create(options):
"""create database objects and superuser account"""
args = ''
username = options.get('username', None)
password = options.get('password', None)
email = options.get('email', None)
if all([username, password, email]):
args = '%s %s %s' % (username, password, email)
sh('python3 %s create %s' % (path('GeoHealthCheck/models.py'), args))
@task
@cmdopts([
('password=', 'p', 'password')
])
def create_hash(options):
"""
Create hash, mainly for passwords.
Usage: paver create_hash -p mypass
"""
import sys
sys.path.insert(0, BASEDIR + '/GeoHealthCheck')
from util import create_hash
token = create_hash(options.get('password', None))
info('Copy/paste the entire token below for example to set password')
info(token)
@task
def upgrade():
"""upgrade database if changed; be sure to backup first!"""
info('Upgrading database...')
with pushd(path('%s/GeoHealthCheck' % BASEDIR)):
sh('python3 manage.py db upgrade')
@task
def create_wsgi():
"""create WSGI wrapper and Apache2 configuration"""
wsgi_script = '%s%sGeoHealthCheck.wsgi' % (options.base.instance, os.sep)
with open(wsgi_script, 'w') as ff:
ff.write('import sys\n')
ff.write('sys.path.insert(0, \'%s\')\n' % BASEDIR)
ff.write('from GeoHealthCheck.app import APP as application')
wsgi_conf = '%s%sGeoHealthCheck.conf' % (options.base.instance, os.sep)
with open(wsgi_conf, 'w') as ff:
ff.write('WSGIScriptAlias / %s%sGeoHealthCheck.wsgi\n' %
(options.base.instance, os.sep))
ff.write('<Directory %s%s>\n' % (BASEDIR, os.sep))
ff.write('Order deny,allow\n')
ff.write('Allow from all\n')
ff.write('</Directory>')
@task
def refresh_docs():
"""Build sphinx docs from scratch"""
make = sphinx_make()
if os.path.exists(options.base.static_docs):
shutil.rmtree(options.base.static_docs)
with pushd(options.base.docs):
sh('%s clean' % make)
sh('%s html' % make)
source_html_dir = path('%s/docs/_build/html' % BASEDIR)
source_html_dir.copytree(options.base.static_docs)
@task
def clean():
"""clean environment"""
if os.path.exists(options.base.static_lib):
shutil.rmtree(options.base.static_lib)
if os.path.exists(options.base.tmp):
shutil.rmtree(options.base.tmp)
if os.path.exists(options.base.static_docs):
shutil.rmtree(options.base.static_docs)
@task
def extract_translations():
"""extract translations wrapped in _() or gettext()"""
pot_dir = path('GeoHealthCheck/translations/en/LC_MESSAGES')
if not os.path.exists(pot_dir):
pot_dir.makedirs()
sh('pybabel extract -F babel.cfg -o %s GeoHealthCheck' % options.base.pot)
@task
@cmdopts([
('lang=', 'l', '2-letter language code'),
])
def add_language_catalogue(options):
"""adds new language profile"""
lang = options.get('lang', None)
if lang is None:
raise RuntimeError('missing lang argument')
sh('pybabel init -i %s -d %s -l %s' % (
options.base.pot, options.base.translations, lang))
@task
def compile_translations():
"""build .mo files"""
sh('pybabel compile -d %s' % options.base.translations)
@task
def update_translations():
"""update language strings"""
call_task('extract_translations')
sh('pybabel update -i %s -d %s' % (
options.base.pot, options.base.translations))
@task
def runner_daemon():
"""Run the HealthCheck runner daemon scheduler"""
sh('python3 %s' % path('GeoHealthCheck/scheduler.py'))
@task
def run_healthchecks():
"""Run all HealthChecks directly"""
sh('python3 %s' % path('GeoHealthCheck/healthcheck.py'))
def sphinx_make():
"""return what command Sphinx is using for make"""
if os.name == 'nt':
return 'make.bat'
return 'make'
@task
def run_tests():
"""Run all tests"""
sh('python3 %s' % path('tests/run_tests.py'))