-
Notifications
You must be signed in to change notification settings - Fork 32
/
rakefile
491 lines (414 loc) · 12.4 KB
/
rakefile
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env ruby
ROOT = File.expand_path('.')
TMP = File.join(ROOT, 'tmp')
DEPLOY = File.join(ROOT, '.deploy')
DATASTORE = File.join(TMP, 'drydrop.datastore')
HISTORY = File.join(TMP, 'drydrop.datastore.history')
DRYAPP = File.join(ROOT, 'dryapp')
GAE = File.join(ROOT, 'gae')
GAEEXEC = File.join(ROOT, 'gae', 'python')
NEED_GEMS = %w(rubyzip)
NEED_EGGS = %w()
PLATFORM = RUBY_PLATFORM unless defined? PLATFORM
OSX = PLATFORM =~ /darwin/
WIN = PLATFORM =~ /win32/
NIX = !(OSX || WIN)
APPSERVER_OPTS = "" #"--debug --debug_imports"
APPCFG_OPTS = ""
DRYAPP_TERM_COLOR = [10000,0,10000]
require "rubygems"
require 'yaml'
# http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
begin
require 'Win32/Console/ANSI' if WIN
rescue LoadError
raise 'You must "sudo gem install win32console" to use terminal colors on Windows'
end
begin
require 'appscript' if OSX
include Appscript if OSX
rescue LoadError
raise 'You must "sudo gem install rb-appscript"'
end
begin
require 'zip/zip'
rescue LoadError
raise 'You must "sudo gem install rubyzip"'
end
def colorize(text, color_code)
"#{color_code}#{text}\e[0m"
end
def red(text); colorize(text, "\e[31m"); end
def green(text); colorize(text, "\e[32m"); end
def yellow(text); colorize(text, "\e[33m"); end
def blue(text); colorize(text, "\e[34m"); end
def magenta(text); colorize(text, "\e[35m"); end
def azure(text); colorize(text, "\e[36m"); end
def white(text); colorize(text, "\e[37m"); end
def black(text); colorize(text, "\e[30m"); end
def file_color(text); yellow(text); end
def dir_color(text); blue(text); end
def cmd_color(text); azure(text); end
#
# you can use FileUtils: http://corelib.rubyonrails.org/classes/FileUtils.html
#
require 'find'
def cp_dir(src, dst)
puts "#{cmd_color('Copying')} #{dir_color(src)}"
puts " -> #{dir_color(dst)}"
Find.find(src) do |fn|
next if fn =~ /\.(svn|git)/
next if fn =~ /\.pyc$/
next if fn =~ /\.DS_Store$/
next if fn =~ /Thumbs\.db$/
next if fn =~ /^\./
r = fn[src.size..-1]
if File.directory? fn
mkdir(File.join(dst,r), {:verbose => false}) unless File.exist? File.join(dst,r)
else
cp(fn, File.join(dst,r), {:verbose => false})
end
end
end
def cp_file(src, dst)
puts "#{cmd_color('Copying')} #{file_color(src)}"
puts " -> #{file_color(dst)}"
cp(src, dst, {:verbose => false})
end
def my_mkdir(dir)
puts "#{cmd_color('Creating directory')} #{dir_color(dir)}"
mkdir dir, {:verbose => false}
end
def dep_dir(src)
cp_dir(File.join(DRYAPP, src), File.join($DEPLOY_DIR, src))
end
def dep_file(src)
cp_file(File.join(DRYAPP, src), File.join($DEPLOY_DIR, src))
end
def sanitize_path(path)
path = path.gsub('/', '\\') if WIN
path = path.gsub('sudo ', '') if WIN
path
end
def system(path, *params)
path = sanitize_path(path)
puts yellow(">> #{path} #{params.join(' ')}")
Kernel.system(path, *params)
end
def sys(cmd, verbose = true)
puts yellow("> #{cmd}") if verbose
`#{sanitize_path(cmd)}`
end
#############################################################################
# helpers
def die(s)
puts red(s)
exit(1)
end
def update_sources()
sys("cd \"#{ROOT}\" && git pull origin master 1>&2")
end
def install_gems(gems)
gems.each do |g|
sys("sudo gem install #{g} 1>&2")
end
end
#
def install_eggs(eggs)
eggs.each do |e|
sys("sudo easy_install #{e} 1>&2")
end
end
def prepare_gae()
return if File.exists?(GAE)
sys("svn co http://googleappengine.googlecode.com/svn/trunk/ \"#{GAE}\" 1>&2")
end
def update_gae()
die "GAE not present at #{GAE}" unless File.exists?(GAE)
sys("cd \"#{GAE}\" && svn up 1>&2")
end
def remove_gae()
return unless File.exists?(GAE)
die "dir \"#{GAE}\" seems to be invalid" if GAE.size<"/gae".size
sys("rm -Rf \"#{GAE}\" 1>&2")
end
def get_app_server_pid()
if NIX
res = sys("ps au|grep dev_appserver")
return unless res
lines = res.split("\n")
return unless lines[1]
return lines[1].split(' ')[1] unless lines[1]=~/grep/i
nil
else
res = sys("ps -a|grep dev_appserver")
return unless res
lines = res.split("\n")
lines.each do |line|
return line.split(' ').first unless line=~/grep/i
end
nil
end
end
def shutdown_dryapp()
pid = get_app_server_pid()
return unless pid
puts "Shutdowning DryApp (PID=#{pid})"
sys("kill #{pid} 1>&2")
end
def run_in_terminal(cmd, title, background=[0,0,0])
if OSX
# see http://www.nach-vorne.de/2007/11/22/terminal-trick
# and http://onrails.org/articles/2007/11/28/scripting-the-leopard-terminal
# and http://blog.cbciweb.com/articles/2008/05/02/scripting-mac-terminal-using-ruby
begin
puts yellow("!> #{cmd}")
term = app('Terminal')
term.activate()
current_window = term.windows.first
old_tab = current_window.selected_tab.get()
tab = find_terminal_tab(background)
unless tab
app("System Events").application_processes["Terminal.app"].keystroke("t", :using => :command_down)
tab = current_window.tabs.last
end
term.do_script(cmd, :in => tab)
tab.background_color.set(background)
tab.title_displays_custom_title.set(true)
tab.custom_title.set(title)
current_window.selected_tab.set(old_tab)
sleep 0.1
rescue
die "OSX command failed"
end
else
system(cmd)
end
end
def find_terminal_tab(color)
return unless OSX
term = app('Terminal')
term.windows.get.each do |win|
begin
win.tabs.get.each do |tab|
return tab if color == tab.background_color.get()
end
rescue
end
end
nil
end
def app_server_cmd(opts=nil)
server = 'dev_appserver.py'
server = "#{GAEEXEC}/#{server}" if File.exists?(GAEEXEC)
opts = opts || APPSERVER_OPTS
"#{server} #{opts} --datastore_path=#{DATASTORE} --history_path=#{HISTORY} #{DRYAPP}"
end
def appcfg_cmd(command='')
server = 'appcfg.py'
server = "#{GAEEXEC}/#{server}" if File.exists?(GAEEXEC)
"#{server} #{APPCFG_OPTS} #{command}"
end
def run_dryapp()
sys("mkdir \"#{TMP}\" 1>&2") unless File.exists?(TMP)
puts 'Running DryApp (server-side) ...'
run_in_terminal("cd #{DRYAPP} && clear && #{app_server_cmd}", 'DryApp', DRYAPP_TERM_COLOR)
end
def clear_datastore()
sys("#{app_server_cmd('--clear_datastore')} 1>&2")
end
def test_git_status()
sys("git status")
sys("echo $?").strip
end
def git_version
sys("git --version|cut -d\" \" -f3").strip
end
def zip_python_module(name)
dep_dir(name)
$modules << name
end
def patch_main_py()
path = File.expand_path(File.join($DEPLOY_DIR, 'drydrop_handler.py'))
puts "#{cmd_color('Patching')} #{file_color(path)}"
lines = []
File.open(path, "r") do |f|
f.each do |line|
if line =~ /# GENERATED: here we will setup import paths for baked version !!!/
$modules.each do |m|
lines << line.gsub(/# GENERATED: here we will setup import paths for baked version !!!/,
"if '#{m}.zip' not in sys.path: sys.path.insert(0, '#{m}.zip');")
end
else
lines << line
end
end
end
File.open(path, "w") do |f|
lines.each do |line|
f.puts(line)
end
end
end
# force persistent timestamps inside zip for uploading only incremental changes
module Zip
class ZipFile
def add(entry, srcPath, &continueOnExistsProc)
continueOnExistsProc ||= proc { false }
check_entry_exists(entry, continueOnExistsProc, "add")
newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@name, entry.to_s, "", "", 0, 0,
ZipEntry::DEFLATED, 0, time = DOSTime.local(2000,"jan",1,0,0,0))
newEntry.gather_fileinfo_from_srcpath(srcPath)
@entrySet << newEntry
end
end
end
def zipit(fn, prefix = "")
puts "#{cmd_color('Zipping')} #{dir_color(fn)}"
dst = "#{fn}.zip"
Zip::ZipFile.open(dst, Zip::ZipFile::CREATE) { |zipfile|
Find.find(fn) do |f|
next if File.directory? f
r = f[fn.size+1..-1]
r = prefix+"/"+r if prefix.size>0
zipfile.add(r, f)
end
}
File.chmod(0644, dst)
sys("rm -rf \"#{fn}\"", false)
end
def bake(name)
$modules = []
git_status = test_git_status()
die('not under git version control!') if git_status != "0"
version = git_version()
$DEPLOY_DIR = File.join(DEPLOY, "#{version}")
die "something bad with deploy dir #{deploy_dir}" if $DEPLOY_DIR.size<=".deploy".size
sys("rm -rf \"#{$DEPLOY_DIR}\" 1>&2") if File.exists?($DEPLOY_DIR)
my_mkdir($DEPLOY_DIR)
dep_file('app.yaml')
dep_file('index.yaml')
dep_file('drydrop_handler.py')
zip_python_module('drydrop')
zip_python_module('routes')
zip_python_module('yaml')
zip_python_module('simplejson')
zip_python_module('jinja2')
zip_python_module('firepython')
zip_python_module('pygments')
$modules.each do |mod|
src = File.join($DEPLOY_DIR, mod)
zipit(src, mod)
end
patch_app_yaml(version, name)
patch_main_py()
sys("cd \"#{$DEPLOY_DIR}\" && find . -exec touch -am -t 01010000.00 '{}' ';'")
end
def remove_from_deploy_dir(path)
die "something bad with dir #{path}" if path.size<"deploy".size
puts "#{cmd_color('Removing')} #{dir_color(path)}"
sys("rm -rf \"#{path}\"")
end
def patch_app_yaml(version, name)
path = File.expand_path(File.join($DEPLOY_DIR, 'app.yaml'))
puts "#{cmd_color('Patching')} #{file_color(path)}"
lines = []
File.open(path, "r") do |f|
f.each do |line|
lines << line.gsub(/^application:.*$/, "application: #{name}")
end
end
File.open(path, "w") do |f|
lines.each do |line|
f.puts(line)
end
end
end
def clear_pyc()
puts "#{cmd_color('Removing all *.pyc in '+DRYAPP)}"
sys("cd \"#{ROOT}\" && find . -name \"*.pyc\" -exec rm '{}' ';'")
end
def touch_js()
puts "#{cmd_color('Touching all *.js in '+DRYAPP)}"
sys("cd \"#{ROOT}\" && find . -name \"*.js\" -exec touch '{}' ';'")
end
def touch_html()
puts "#{cmd_color('Touching all *.html in '+DRYAPP)}"
sys("cd \"#{ROOT}\" && find . -name \"*.html\" -exec touch '{}' ';'")
end
def cache_buster()
clear_pyc()
touch_js()
touch_html()
end
#############################################################################
#----------------------------------------------------------------------------
desc "(re)starts server (=> http://localhost:8080)"
task :start do
shutdown_dryapp()
cache_buster()
run_dryapp()
puts blue('-> http://localhost:8080/')
end
#----------------------------------------------------------------------------
desc "stops server"
task :stop do
shutdown_dryapp()
end
#----------------------------------------------------------------------------
desc "updates project sources from github"
task :update do
update_sources()
end
#----------------------------------------------------------------------------
desc "installs dependencies"
task :setup do
install_gems(NEED_GEMS)
install_eggs(NEED_EGGS)
end
#----------------------------------------------------------------------------
desc "switches to edge GAE (svn checkout from code.google.com)"
task :edge_gae do
prepare_gae()
update_gae()
end
#----------------------------------------------------------------------------
desc "switches to system GAE (!!! removes $drydrop/gae directory)"
task :site_gae do
remove_gae()
end
#----------------------------------------------------------------------------
desc "clears datastore"
task :clear do
clear_datastore()
end
#----------------------------------------------------------------------------
desc "removes all *.pyc in subtree, touches *.(js|html)"
task :cache_buster do
cache_buster()
end
#----------------------------------------------------------------------------
desc "bakes version for deploy"
task :bake do
name = ENV["project"]
die("Please specify appengine project name: rake upload project=drydroptest") unless name
answer = sys("git status")
is_clean = !!answer.match(/working directory clean/)
puts 'Baking version ...'
sys("mkdir \"#{DEPLOY}\" 1>&2") unless File.exists?(DEPLOY)
bake(name)
puts red("Your working copy is not clean. You should commit all changes.") unless is_clean
end
#----------------------------------------------------------------------------
desc "uploads baked version to appspot"
task :upload => :bake do
puts 'Uploading version to appspot ...'
system(appcfg_cmd("update \"#{$DEPLOY_DIR}\""))
end
#----------------------------------------------------------------------------
desc "removes deploy directory"
task :clean_deploy do
sys("rm -rf \"#{DEPLOY}\"")
end
#----------------------------------------------------------------------------
task :default => :start