-
Notifications
You must be signed in to change notification settings - Fork 21
/
webed.py
executable file
·289 lines (204 loc) · 8.9 KB
/
webed.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
#!bin/python
###############################################################################
###############################################################################
from flask.ext.script import Manager, Command, Option
from flask.ext.assets import ManageAssets
from gzip import GzipFile
from webed.app import app
from webed.ext import db
from webed.ext import std_cache
from webed.ext import obj_cache
from webed.ext import sss_cache
from webed.ext import dbs_cache
from webed.ext import assets
from webed.util import Q
from webed.models import User
import os
import shutil
import subprocess
###############################################################################
###############################################################################
manager = Manager (app, with_default_commands=False)
###############################################################################
###############################################################################
@manager.shell
def make_context ():
from webed import app
from webed import admin
from webed import config
from webed import ext
from webed import models
from webed import session
from webed import test
from webed import util
from webed import views
return dict (app=app, admin=admin, config=config, ext=ext, models=models,
session=session, test=test, util=util, views=views)
###############################################################################
###############################################################################
@manager.command
def run (address='127.0.0.1', port=5000, debug=False, config=None):
"""Runs the Flask server i.e. app.run (debug=True|False)"""
if config:
app.config.from_pyfile (config, silent=False)
app.run (host=address, port=int (port), debug=debug)
###############################################################################
@manager.command
def execute (source):
"""Execute source in application context"""
with app.app_context (): exec source
###############################################################################
class DbSetup (Command):
"""Setup database (with a default admin) [!!]"""
def get_options (self):
return [
Option ('-n', '--name', dest='name', default=u'admin'),
Option ('-m', '--mail', dest='mail', default=u'[email protected]'),
]
def run (self, *args, **kwargs):
db.create_all ()
name = kwargs['name']
assert name
mail = kwargs['mail']
assert mail
user = Q (User.query).one_or_default (mail=mail)
if not user: db.session.add (User (name=name, mail=mail))
db.session.script (['webed', 'models', 'sql', 'npv_create.sql'])
db.session.script (['webed', 'models', 'sql', 'npt_insert.sql'])
db.session.script (['webed', 'models', 'sql', 'npt_delete.sql'])
db.session.commit ()
manager.add_command ('setup-db', DbSetup ())
###############################################################################
class DbClear (Command):
"""Clear database [!!]"""
def run (self):
db.session.script (['webed', 'models', 'sql', 'npv_drop.sql'])
db.session.script (['webed', 'models', 'sql', 'npt_drop.sql'])
db.session.commit ()
db.drop_all ()
manager.add_command ('clear-db', DbClear ())
###############################################################################
class StdCacheClear (Command):
"""Clear standard cache (views, templates etc.)"""
def run (self):
std_cache.flush_all () ## memcached
manager.add_command ('clear-cache-std', StdCacheClear ())
class ObjCacheClear (Command):
"""Clear object cache (archives etc.)"""
def run (self):
obj_cache.flush_all () ## memcached
manager.add_command ('clear-cache-obj', ObjCacheClear ())
class SssCacheClear (Command):
"""Clear session cache (anchor etc.) [!!]"""
def run (self):
sss_cache.connection.flushdb () ## redis
manager.add_command ('clear-cache-sss', SssCacheClear ())
class DbsCacheClear (Command):
"""Clear database system cache (version-ing etc.) [!!]"""
def run (self):
dbs_cache.connection.flushdb () ## redis
manager.add_command ('clear-cache-dbs', DbsCacheClear ())
class FsbClear (Command):
"""Clear file system backend (data etc.) [!!]"""
def run (self):
if os.path.exists (app.config['COW_ROOT']):
for path, dns, fns in os.walk (app.config['COW_ROOT']):
for fn in fns: os.unlink (os.path.join (path, fn))
for dn in dns: shutil.rmtree (os.path.join (path, dn))
if os.path.exists (app.config['VCS_ROOT']):
for path, dns, fns in os.walk (app.config['VCS_ROOT']):
for fn in fns: os.unlink (os.path.join (path, fn))
for dn in dns: shutil.rmtree (os.path.join (path, dn))
manager.add_command ('clear-fsb', FsbClear ())
###############################################################################
class AppReset (Command):
"""Reset application: Clear all caches and reset DB [!!]"""
def get_options (self):
return [
Option ('-n', '--name', dest='name', default=u'admin'),
Option ('-m', '--mail', dest='mail', default=u'[email protected]'),
]
def run (self, *args, **kwargs):
name = kwargs['name']
assert name
mail = kwargs['mail']
assert mail
StdCacheClear ().run ()
ObjCacheClear ().run ()
SssCacheClear ().run ()
DbsCacheClear ().run ()
FsbClear ().run ()
DbClear ().run ()
DbSetup ().run (name=name, mail=mail)
manager.add_command ('reset', AppReset ())
###############################################################################
class AppRefresh (Command):
"""Refresh application: Clears standard and object cache"""
def run (self):
StdCacheClear ().run ()
ObjCacheClear ().run ()
manager.add_command ('refresh', AppRefresh ())
###############################################################################
###############################################################################
class AssetsSprite (Command):
"""Creates a CSS sprite using `spritemapper`"""
def get_options (self):
return [
Option ('-n', '--name', dest='name', default='sprite-main'),
Option ('-p', '--padding', dest='padding', default=2, type=int),
]
def run (self, *args, **kwargs):
theme_path = os.path.join (
'webed', 'static', 'webed-ext', 'resources', 'theme')
name = kwargs['name']
assert name
padding = kwargs['padding']
assert padding
ini_path = os.path.join (theme_path, '%s.ini' % name)
src_path = os.path.join (theme_path, '%s-in.css' % name)
css_path = os.path.join (theme_path, '%s.css' % name)
subprocess.check_call (['spritemapper',
'--padding=%d' % padding, '--conf=%s' % ini_path, src_path
])
subprocess.check_call ([
'sed', '--in-place', 's/; \}/ !important; \}/g', css_path
])
class AssetsGzip (Command):
"""Gzip assets: Pre-compresses assets"""
def get_options (self):
return [
Option ('-l', '--compress-level', dest='level', default=9,
choices=range (1, 10), type=int),
Option ('-f', '--force', dest='force', action='store_true',
default=False, help='Overrides existing compressed asset')
]
def run (self, *args, **kwargs):
level = kwargs['level']
assert 1 <= level <= 9
force = kwargs['force']
assert type (force) == bool
directory = assets.get_directory ()
assert directory
for asset in assets:
source_path = asset.resolve_output ()
target_path = '%s.gz' % source_path
bundle_path = target_path[len (directory) + 1:]
if not os.path.exists (target_path) or force:
print 'Compressing bundle: %s' % bundle_path
with open (source_path, 'rb') as source:
with open (target_path, 'wb') as target:
gz = GzipFile (
mode='wb', compresslevel=level, fileobj=target)
try: gz.write (source.read ())
finally: gz.close ()
else:
print 'Skipped bundle: %s' % bundle_path
manager.add_command ('assets-sprite', AssetsSprite ())
manager.add_command ('assets-gzip', AssetsGzip ())
manager.add_command ('assets', ManageAssets ())
###############################################################################
###############################################################################
if __name__ == '__main__':
manager.run ()
###############################################################################
###############################################################################