forked from binaryage/firelogger.py
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rakefile
192 lines (170 loc) · 5.48 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
ROOT = File.expand_path('.')
ADDON = File.expand_path(File.join(ROOT, '..', 'firelogger')) # firelogger is expected to be at same directory level as firepython project
FIREFOX = File.join(ADDON, 'firefox')
DST = File.join(ROOT, 'build')
TMP = File.join(ROOT, 'tmp')
JSONPICKLE = File.join(ROOT, 'jsonpickle')
PYTHON = ROOT
unless defined? OSX then
OSX = PLATFORM =~ /darwin/
WIN = PLATFORM =~ /win32/
NIX = !(OSX || WIN)
end
begin
require 'term/ansicolor'
include Term::ANSIColor
rescue LoadError
raise 'Run "gem install term-ansicolor"'
end
# http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
if WIN then
begin
require 'win32console'
include Win32::Console::ANSI
rescue LoadError
raise 'Run "gem install win32console" to use terminal colors on Windows'
end
end
#
# you can use FileUtils: http://corelib.rubyonrails.org/classes/FileUtils.html
#
require 'find'
# copies directory tree without .svn, .git and other temporary files
def cp_dir(src, dst)
puts "#{cmd_color('copying')} #{dir_color(src)}"
puts " -> #{dir_color(dst)}"
Find.find(src) do |fn|
next if fn =~ /\/\./
next if fn =~ /Thumbs\.db/
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 dep(src)
s = File.expand_path src
rs = s[FIREFOX.size..-1]
d = File.join(TMP, rs)
puts "#{cmd_color('copying')} #{file_color(s)}"
puts " -> #{file_color(d)}"
cp(s, d, {:verbose => false})
end
def dep2(src, add='')
s = File.expand_path src
rs = s[ROOT.size..-1]
d = File.join(TMP, add, rs)
puts "#{cmd_color('copying')} #{file_color(s)}"
puts " -> #{file_color(d)}"
cp(s, d, {:verbose => false})
end
def my_mkdir(dir)
puts "#{cmd_color('creating directory')} #{dir_color(dir)}"
mkdir(dir, {:verbose => false})
end
def parse_version()
f = File.new(File.join(FIREFOX, 'install.rdf'))
text = f.read
unless text=~/<em:version>([^<]*)<\/em:version>/
puts "#{red('Version not found')}"
exit
end
$1
end
def die(s)
puts(red(s))
exit(1)
end
def patch(filepath, matcher, replacer)
puts "Patching #{blue(filepath[ROOT.size+1..-1])} with #{yellow(replacer.to_s)}"
applied = false
lines = []
File.open(filepath, 'r') do |f|
f.each do |line|
lines << line.gsub(matcher, replacer)
applied ||= lines[-1]!=line
end
end
File.open(filepath, "w") do |f|
f << lines
end
applied
end
def check_if_addon_exists()
die("firepython-addon not found!\n expected to be in #{ADDON}") unless File.exists?(ADDON)
end
################################################################################
desc "Prepare XPI"
task :default do
check_if_addon_exists()
Dir.chdir(ADDON) do
system('rake')
end
end
desc "Update PyPI index and upload library sources"
task :pypi do
remove_dir(TMP) if File.exists?(TMP) # recursive!
mkdir(TMP, {:verbose => false})
dep2(File.join(ROOT, 'readme.md'))
dep2(File.join(ROOT, 'license.txt'))
dep2(File.join(ROOT, 'setup.py'))
firepython_dir = File.join(TMP, 'firepython')
my_mkdir(firepython_dir) unless File.exist?(firepython_dir)
dep2(File.join(ROOT, '__init__.py'), 'firepython')
dep2(File.join(ROOT, 'middleware.py'), 'firepython')
dep2(File.join(ROOT, 'utils.py'), 'firepython')
dep2(File.join(ROOT, 'handlers.py'), 'firepython')
dep2(File.join(ROOT, 'gprof2dot.py'), 'firepython')
Dir.chdir(TMP) do
system("python setup.py register") or die("register failed")
system("python setup.py sdist upload") or die("upload failed")
end
remove_dir(TMP) if File.exist?(TMP) # recursive!
end
desc "Resets version in all relevant sources"
task :version do
version = ARGV[1] or die("Please specify a version as first parameter")
init_py_path = File.join(PYTHON, '__init__.py')
if not patch(init_py_path, /__version__ = '([0-9\.])+'/, "__version__ = '#{version}'")
puts " #{red("patching had no effect")}"
end
exit(0)
end
# Python is a hostile language for me
# google app engine is breaking after setting sys.path (various handler caching issues and bugs)
# witout sys.path I see no other chance than to prepare my own patched version of jsonpickle
desc "Prepares jsonpicke version"
task :jsonpickle do
repo = ARGV[1] || File.expand_path("~/code/jsonpickle-read-only")
remove_dir(JSONPICKLE) if File.exists?(JSONPICKLE) # recursive!
mkdir(JSONPICKLE)
Dir.chdir(JSONPICKLE) do
["COPYING"].each { |file| system("cp \"#{File.join(repo, "src", file)}\" .") }
lines = []
# bake all into one huge file to get rid of import paths issues
# also remove import statements and module prefixes
['__init__.py', 'util.py', "tags.py", 'pickler.py', 'unpickler.py'].each do |filepath|
File.open(File.join(repo, "src", "jsonpickle", filepath), 'r') do |f|
f.each do |line|
line = line.gsub('from jsonpickle.pickler import Pickler', '')
line = line.gsub('from jsonpickle.unpickler import Unpickler', '')
line = line.gsub('import jsonpickle.util as util', '')
line = line.gsub('import jsonpickle.tags as tags', '')
line = line.gsub('util.', '')
line = line.gsub('tags.', '')
lines << line
end
end
end
File.open('__init__.py', "w") do |f|
f << lines
end
end
end