forked from gae-init/gae-init-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·323 lines (258 loc) · 8.75 KB
/
run.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
import argparse
from main import config
import os
import shutil
import sys
import time
################################################################################
# Options
################################################################################
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--watch', dest='watch', action='store_true',
help='watch files for changes when running the development web server',
)
parser.add_argument('-c', '--clean', dest='clean', action='store_true',
help='''recompiles files when running the development web server, but
obsolete if -s is used''',
)
parser.add_argument('-m', '--minify', dest='minify', action='store_true',
help='compiles files into minified version before deploying'
)
parser.add_argument('-s', '--start', dest='start', action='store_true',
help='starts the dev_appserver.py with storage_path pointing to temp',
)
parser.add_argument('-o', '--host', dest='host', action='store', default='127.0.0.1',
help='the host to start the dev_appserver.py',
)
parser.add_argument('-p', '--port', dest='port', action='store', default='8080',
help='the port to start the dev_appserver.py',
)
parser.add_argument('-f', '--flush', dest='flush', action='store_true',
help='clears the datastore, blobstore, etc',
)
args = parser.parse_args()
################################################################################
# Directories
################################################################################
DIR_MAIN = 'main'
DIR_STATIC = 'static'
DIR_SRC = 'src'
DIR_STYLE = 'style'
DIR_SCRIPT = 'script'
DIR_MIN = 'min'
DIR_DST = 'dst'
DIR_LIB = 'lib'
DIR_NODE_MODULES = 'node_modules'
DIR_BIN = '.bin'
DIR_TEMP = 'temp'
DIR_STORAGE = 'storage'
FILE_ZIP = '%s.zip' % DIR_LIB
FILE_COFFEE = 'coffee'
FILE_LESS = 'lessc'
FILE_UGLIFYJS = 'uglifyjs'
dir_static = os.path.join(DIR_MAIN, DIR_STATIC)
dir_src = os.path.join(dir_static, DIR_SRC)
dir_src_script = os.path.join(dir_src, DIR_SCRIPT)
dir_src_style = os.path.join(dir_src, DIR_STYLE)
dir_dst = os.path.join(dir_static, DIR_DST)
dir_dst_style = os.path.join(dir_dst, DIR_STYLE)
dir_dst_script = os.path.join(dir_dst, DIR_SCRIPT)
dir_min = os.path.join(dir_static, DIR_MIN)
dir_min_style = os.path.join(dir_min, DIR_STYLE)
dir_min_script = os.path.join(dir_min, DIR_SCRIPT)
dir_lib = os.path.join(DIR_MAIN, DIR_LIB)
file_lib = os.path.join(DIR_MAIN, FILE_ZIP)
dir_bin = os.path.join(DIR_NODE_MODULES, DIR_BIN)
file_coffee = os.path.join(dir_bin, FILE_COFFEE)
file_less = os.path.join(dir_bin, FILE_LESS)
file_uglifyjs = os.path.join(dir_bin, FILE_UGLIFYJS)
dir_storage = os.path.join(DIR_TEMP, DIR_STORAGE)
################################################################################
# Helpers
################################################################################
def print_out(script, filename=''):
timestamp = datetime.now().strftime('%H:%M:%S')
if not filename:
filename = '-' * 46
script = script.rjust(12, '-')
print '[%s] %12s %s' % (timestamp, script, filename)
def make_dirs(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def remove_dir(directory):
if os.path.isdir(directory):
shutil.rmtree(directory)
def clean_files():
bad_endings = ['pyc', '~']
print_out(
'CLEAN FILES',
'Removing files: %s' % ', '.join(['*%s' % e for e in bad_endings]),
)
for home, dirs, files in os.walk(DIR_MAIN):
for f in files:
for b in bad_endings:
if f.endswith(b):
os.remove(os.path.join(home, f))
def merge_files(source, target):
fout = open(target, 'a')
for line in open(source):
fout.write(line)
fout.close()
def os_execute(executable, params, source, target, append=False):
operator = '>>' if append else '>'
os.system('"%s" %s %s %s %s' % (executable, params, source, operator, target))
def compile_script(source, target_dir):
if not os.path.isfile(source):
print_out('NOT FOUND', source)
return
target = source.replace(dir_src_script, target_dir).replace('.coffee', '.js')
if not is_dirty(source, target):
return
make_dirs(os.path.dirname(target))
if not source.endswith('.coffee'):
print_out('COPYING', source)
shutil.copy(source, target)
return
print_out('COFFEE', source)
os_execute(file_coffee, '-cp', source, target)
def compile_style(source, target_dir, check_modified=False):
if not os.path.isfile(source):
print_out('NOT FOUND', source)
return
target = source.replace(dir_src_style, target_dir).replace('.less', '.css')
minified = ''
if not source.endswith('.less'):
return
if check_modified and not is_style_modified(target):
return
if target_dir == dir_min_style:
minified = '-x'
target = target.replace('.css', '.min.css')
print_out('LESS MIN', source)
else:
print_out('LESS', source)
make_dirs(os.path.dirname(target))
os_execute(file_less, minified, source, target)
def make_lib_zip(force=False):
if force and os.path.isfile(file_lib):
os.remove(file_lib)
if not os.path.isfile(file_lib):
print_out('ZIP', file_lib)
shutil.make_archive(dir_lib, 'zip', dir_lib)
def is_dirty(source, target):
if not os.access(target, os.O_RDONLY):
return True
return os.stat(source).st_mtime - os.stat(target).st_mtime > 0
def is_style_modified(target):
for folder, folders, files in os.walk(dir_src):
for file_ in files:
path = os.path.join(folder, file_)
if path.endswith('.less') and is_dirty(path, target):
return True
return False
def compile_all_dst():
for source in STYLES:
compile_style(os.path.join(dir_static, source), dir_dst_style, True)
for module in config.SCRIPTS:
for source in config.SCRIPTS[module]:
compile_script(os.path.join(dir_static, source), dir_dst_script)
def update_path_separators():
def fixit(path):
return path.replace('\\', '/').replace('/', os.sep)
for idx in xrange(len(STYLES)):
STYLES[idx] = fixit(STYLES[idx])
for module in SCRIPTS:
for idx in xrange(len(SCRIPTS[module])):
SCRIPTS[module][idx] = fixit(SCRIPTS[module][idx])
def install_dependencies():
missing = False
if not os.path.exists(file_coffee):
missing = True
if not os.path.exists(file_less):
missing = True
if not os.path.exists(file_uglifyjs):
missing = True
if not os.path.exists(os.path.join(DIR_NODE_MODULES, 'grunt')):
missing = True
if missing:
os.system('npm install')
def update_missing_args():
if args.start:
args.clean = True
################################################################################
# Main
################################################################################
SCRIPTS = config.SCRIPTS
STYLES = config.STYLES
os.chdir(os.path.dirname(os.path.realpath(__file__)))
update_path_separators()
install_dependencies()
update_missing_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
if args.clean:
print_out('CLEAN')
clean_files()
make_lib_zip(force=True)
remove_dir(dir_dst)
make_dirs(dir_dst)
compile_all_dst()
print_out('DONE')
if args.minify:
print_out('MINIFY')
clean_files()
make_lib_zip(force=True)
remove_dir(dir_min)
make_dirs(dir_min_script)
for source in STYLES:
compile_style(os.path.join(dir_static, source), dir_min_style)
for module in config.SCRIPTS:
coffees = ' '.join([os.path.join(dir_static, script)
for script in config.SCRIPTS[module] if script.endswith('.coffee')
])
pretty_js = os.path.join(dir_min_script, '%s.js' % module)
ugly_js = os.path.join(dir_min_script, '%s.min.js' % module)
print_out('COFFEE MIN', ugly_js)
if len(coffees):
os_execute(file_coffee, '--join -cp', coffees, pretty_js, append=True)
for script in config.SCRIPTS[module]:
if not script.endswith('.js'):
continue
script_file = os.path.join(dir_static, script)
merge_files(script_file, pretty_js)
os_execute(file_uglifyjs, '-nc', pretty_js, ugly_js)
os.remove(pretty_js)
print_out('DONE')
if args.watch:
print_out('WATCHING')
make_lib_zip()
make_dirs(dir_dst)
compile_all_dst()
print_out('DONE', 'and watching for changes (Ctrl+C to stop)')
while True:
time.sleep(0.5)
reload(config)
SCRIPTS = config.SCRIPTS
STYLES = config.STYLES
compile_all_dst()
if args.flush:
remove_dir(dir_storage)
print_out('STORAGE CLEARED')
if args.start:
make_dirs(dir_storage)
clear = 'yes' if args.flush else 'no'
port = int(args.port)
run_command = '''
dev_appserver.py %s
--host %s
--port %s
--admin_port %s
--storage_path=%s
--clear_datastore=%s
--skip_sdk_update_check
''' % (DIR_MAIN, args.host, port, port + 1, dir_storage, clear)
os.system(run_command.replace('\n', ' '))