forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_binaries.py
executable file
·344 lines (317 loc) · 11.3 KB
/
package_binaries.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python
import argparse
import getnwisrelease
import getnwversion
import gzip
import os
import platform
import shutil
import sys
import tarfile
import zipfile
from subprocess import call
steps = ['nw', 'chromedriver', 'symbol', 'others']
################################
# Parse command line args
parser = argparse.ArgumentParser(description='Package nw binaries.')
parser.add_argument('-p','--path', help='Where to find the binaries, like out/Release', required=False)
parser.add_argument('-a','--arch', help='target arch', required=False)
group = parser.add_mutually_exclusive_group()
group.add_argument('-s','--step', choices=steps, help='Execute specified step.', required=False)
group.add_argument('-n','--skip', choices=steps, help='Skip specified step.', required=False)
args = parser.parse_args()
################################
# Init variables.
binaries_location = None # .../out/Release
platform_name = None # win/linux/osx
arch = None # ia32/x64
step = None # nw/chromedriver/symbol
skip = None
nw_ver = None # x.xx
dist_dir = None # .../out/Release/dist
is_headers_ok = False # record whether nw-headers generated
step = args.step
skip = args.skip
binaries_location = args.path
# If the binaries location is not given, calculate it from script related dir.
if binaries_location == None:
binaries_location = os.path.join(os.path.dirname(__file__),
os.pardir, os.pardir, os.pardir, 'out', 'Release')
if not os.path.isabs(binaries_location):
binaries_location = os.path.join(os.getcwd(), binaries_location)
if not os.path.isdir(binaries_location):
print 'Invalid path: ' + binaries_location
exit(-1)
binaries_location = os.path.normpath(binaries_location)
dist_dir = os.path.join(binaries_location, 'dist')
print 'Working on ' + binaries_location
if sys.platform.startswith('linux'):
platform_name = 'linux'
elif sys.platform in ('win32', 'cygwin'):
platform_name = 'win'
elif sys.platform == 'darwin':
platform_name = 'osx'
else:
print 'Unsupported platform: ' + sys.platform
exit(-1)
_arch = platform.architecture()[0]
if _arch == '64bit':
arch = 'x64'
elif _arch == '32bit':
arch = 'ia32'
else:
print 'Unsupported arch: ' + _arch
exit(-1)
if platform_name == 'win':
arch = 'ia32'
if platform_name == 'osx':
# detect output arch
nw_bin = binaries_location + '/nwjs.app/Contents/MacOS/nwjs'
import subprocess
if 'i386' in subprocess.check_output(['file',nw_bin]):
arch = 'ia32'
else: # should be 'x86_64'
arch = 'x64'
if args.arch != None:
arch = args.arch
nw_ver = getnwversion.nw_version
if getnwisrelease.release == 0:
nw_ver += getnwisrelease.postfix
################################
# Generate targets
#
# target example:
# {
# 'input' : [ 'nw', 'nw.pak', ... ]
# 'output' : 'nwjs-v0.9.2-linux-x64'
# 'compress' : 'tar.gz'
# 'folder' : True # Optional. More than 2 files will be put into a seprate folder
# # normally, if you want do to this for only 1 file, set this flag.
# }
def generate_target_nw(platform_name, arch, version):
target = {}
# Output
target['output'] = ''.join([
'nwjs-',
'v', version,
'-', platform_name,
'-', arch])
# Compress type
if platform_name == 'linux':
target['compress'] = 'tar.gz'
else:
target['compress'] = 'zip'
# Input
if platform_name == 'linux':
target['input'] = [
'credits.html',
'libffmpegsumo.so',
'nw.pak',
'nwjc',
'nw',
'icudtl.dat',
'locales',
]
elif platform_name == 'win':
target['input'] = [
'd3dcompiler_47.dll',
'ffmpegsumo.dll',
'icudtl.dat',
'libEGL.dll',
'libGLESv2.dll',
'pdf.dll',
'nw.exe',
'nw.pak',
'locales',
'nwjc.exe',
'credits.html',
]
elif platform_name == 'osx':
target['input'] = [
'nwjs.app',
'nwjc',
'credits.html',
]
else:
print 'Unsupported platform: ' + platform_name
exit(-1)
return target
def generate_target_chromedriver(platform_name, arch, version):
target = {}
# Output
target['output'] = ''.join([
'chromedriver-nw-',
'v', version,
'-', platform_name,
'-', arch])
# Compress type
if platform_name == 'linux':
target['compress'] = 'tar.gz'
else:
target['compress'] = 'zip'
# Input
if platform_name == 'win':
target['input'] = ['chromedriver.exe']
else:
target['input'] = ['chromedriver']
target['folder'] = True # always create a folder
return target
def generate_target_symbols(platform_name, arch, version):
target = {}
target['output'] = ''.join(['nwjs-symbol-',
'v', version,
'-', platform_name,
'-', arch])
if platform_name == 'linux':
target['compress'] = 'tar.gz'
target['input'] = ['nw.breakpad.' + arch]
target['folder'] = True
elif platform_name == 'win':
target['compress'] = None
target['input'] = ['nw.sym.7z']
target['output'] = ''.join(['nwjs-symbol-',
'v', version,
'-', platform_name,
'-', arch, '.7z'])
elif platform_name == 'osx':
target['compress'] = 'zip'
target['input'] = [
'nwjs.breakpad.tar'
]
target['folder'] = True
else:
print 'Unsupported platform: ' + platform_name
exit(-1)
return target
def generate_target_others(platform_name, arch, version):
target = {}
target['output'] = ''
target['compress'] = None
if platform_name == 'win':
target['input'] = ['nw.exp', 'nw.lib']
elif platform_name == 'linux' :
target['input'] = []
# here , call make-nw-headers.py to generate nw headers
# after generated, move to dist_dir
make_nw_header = os.path.join(os.path.dirname(__file__), \
'make-nw-headers.py')
print make_nw_header
res = call(['python', make_nw_header])
if res == 0:
global is_headers_ok
is_headers_ok = True
print 'nw-headers generated'
else:
print 'nw-headers generate failed'
else:
target['input'] = []
return target
################################
# Make packages
def compress(from_dir, to_dir, fname, compress):
from_dir = os.path.normpath(from_dir)
to_dir = os.path.normpath(to_dir)
_from = os.path.join(from_dir, fname)
_to = os.path.join(to_dir, fname)
if compress == 'zip':
z = zipfile.ZipFile(_to + '.zip', 'w', compression=zipfile.ZIP_DEFLATED)
if os.path.isdir(_from):
for root, dirs, files in os.walk(_from):
for f in files:
_path = os.path.join(root, f)
z.write(_path, _path.replace(from_dir+os.sep, ''))
else:
z.write(_from, fname)
z.close()
elif compress == 'tar.gz': # only for folders
if not os.path.isdir(_from):
print 'Will not create tar.gz for a single file: ' + _from
exit(-1)
with tarfile.open(_to + '.tar.gz', 'w:gz') as tar:
tar.add(_from, arcname=os.path.basename(_from))
elif compress == 'gz': # only for single file
if os.path.isdir(_from):
print 'Will not create gz for a folder: ' + _from
exit(-1)
f_in = open(_from, 'rb')
f_out = gzip.open(_to + '.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
else:
print 'Unsupported compression format: ' + compress
exit(-1)
def make_packages(targets):
# check file existance
for t in targets:
for f in t['input']:
src = os.path.join(binaries_location, f)
if not os.path.exists(src):
print 'File does not exist: ', src
exit(-1)
# clear the output folder
if os.path.exists(dist_dir):
if not os.path.isdir(dist_dir):
print 'Invalid path: ' + dist_dir
exit(-1)
else:
shutil.rmtree(dist_dir)
# now let's do it
os.mkdir(dist_dir)
# here check whether nw headers generated
# if generated, mv to dist_dir
if is_headers_ok:
nw_headers_name = 'nw-headers-v' + nw_ver + '.tar.gz'
nw_headers_path = os.path.join(os.path.dirname(__file__), \
os.pardir, 'tmp', nw_headers_name)
print 'Moving "' + nw_headers_name + '"'
shutil.move(nw_headers_path, dist_dir)
for t in targets:
if len(t['input']) == 0:
continue
if t['compress'] == None:
for f in t['input']:
src = os.path.join(binaries_location, f)
if t['output'] != '':
dest = os.path.join(dist_dir, t['output'])
else:
dest = os.path.join(dist_dir, f)
print "Copying " + f
shutil.copy(src, dest)
elif (t.has_key('folder') and t['folder'] == True) or len(t['input']) > 1:
print 'Making "' + t['output'] + '.' + t['compress'] + '"'
# copy files into a folder then pack
folder = os.path.join(dist_dir, t['output'])
os.mkdir(folder)
for f in t['input']:
src = os.path.join(binaries_location, f)
dest = os.path.join(folder, f)
if os.path.isdir(src): # like nw.app
shutil.copytree(src, dest)
else:
shutil.copy(src, dest)
compress(dist_dir, dist_dir, t['output'], t['compress'])
# remove temp folders
shutil.rmtree(folder)
else:
# single file
print 'Making "' + t['output'] + '.' + t['compress'] + '"'
compress(binaries_location, dist_dir, t['input'][0], t['compress'])
# must be aligned with steps
generators = {}
generators['nw'] = generate_target_nw
generators['chromedriver'] = generate_target_chromedriver
generators['symbol'] = generate_target_symbols
generators['others'] = generate_target_others
################################
# Process targets
targets = []
for s in steps:
if (step != None) and (s != step):
continue
if (skip != None) and (s == skip):
continue
targets.append(generators[s](platform_name, arch, nw_ver))
print 'Creating packages...'
make_packages(targets)
# vim: et:ts=4:sw=4