forked from Deathamns/Viewhance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
421 lines (304 loc) · 10.9 KB
/
build.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
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
#!/usr/bin/env python
from __future__ import unicode_literals
import sys
import os
import json
from io import open
from sys import argv
from glob import glob
from copy import deepcopy
from datetime import datetime
from collections import OrderedDict
from shutil import rmtree, copy
sys.dont_write_bytecode = True
# Makes it runnable from any directory
os.chdir(os.path.split(os.path.abspath(__file__))[0])
pj = os.path.join
src_dir = os.path.abspath('src')
build_dir = os.path.abspath('build')
platform_dir = os.path.abspath('platform')
l10n_dir = os.path.abspath('l10n')
if not os.path.isdir(src_dir) or not os.path.isdir(platform_dir):
raise SystemExit('src or platform directory not found')
if not os.path.isdir(build_dir):
os.makedirs(build_dir)
locale_list = None
languages = OrderedDict({})
l10n_strings_sparse = OrderedDict({})
l10n_strings_full = OrderedDict({})
app_desc_string = 'appDescriptionShort'
common_app_code = None
platforms = []
params = {
'-meta': False,
'-pack': False,
'-useln': False
}
def add_platform(platform):
if os.path.exists(pj(platform_dir, platform, 'build.py')):
platforms.append(platform)
return True
return False
for i in range(1, len(argv)):
arg = argv[i];
if arg in params:
params[arg] = True
elif not add_platform(arg):
sys.stderr.write('Invalid argument: ' + arg + '\n')
if params['-useln'] and params['-pack']:
params['-useln'] = False
if len(platforms) == 0:
for f in os.listdir(platform_dir):
if os.path.isdir(pj(platform_dir, f)):
add_platform(f)
if len(platforms) == 0:
raise SystemExit('No platforms were found.')
with open(os.path.abspath('config.json'), encoding='utf-8') as f:
config = json.load(f)
if not config:
raise SystemExit('Config file failed to load!')
def_lang = config['def_lang']
config['version'] = datetime.utcnow().strftime("%Y.%m%d.%H%M")
def read_locales(locale_glob, exclude=None):
global locale_list, languages, l10n_strings_sparse
mandatory_locale_groups = ['options']
if locale_list is None:
locale_names_path = pj(l10n_dir, 'locale_names.json')
with open(locale_names_path, encoding='utf-8') as f:
locale_list = json.load(f)
locale_glob = pj(l10n_dir, 'locales', locale_glob + '.json')
for locale_file_name in glob(locale_glob):
alpha2 = os.path.basename(locale_file_name).replace('.json', '')
if alpha2 not in locale_list:
continue
if exclude and alpha2 in exclude:
continue
with open(locale_file_name, encoding='utf-8') as f:
locale = json.load(f, object_pairs_hook=OrderedDict)
if not locale:
continue
translators = locale['_translators']
del locale['_translators']
locale_name = locale_list[alpha2]
languages[alpha2] = {
'name': ('{} [{}]' if 'native' in locale_name else '{}').format(
locale_name['english'],
locale_name['native'] if 'native' in locale_name else ''
),
'translators': translators
}
groups = OrderedDict({})
l10n_strings_sparse[alpha2] = groups
for grp in locale:
is_def = alpha2 == def_lang
if not is_def and grp not in l10n_strings_sparse[def_lang]:
continue
# Ignore group description
if '?' in locale[grp]:
del locale[grp]['?']
groups[grp] = OrderedDict({})
if not is_def:
def_strings = l10n_strings_sparse[def_lang][grp]
for string in locale[grp]:
# Ignore redundant strings
if not is_def:
if string not in def_strings:
continue
if locale[grp][string]['>'] == def_strings[string]:
continue
groups[grp][string] = locale[grp][string]['>']
if len(groups[grp]) == 0:
del groups[grp]
if len(groups) == 0:
del languages[alpha2]
del l10n_strings_sparse[alpha2]
continue
# Add groups if they're missing
for grp in mandatory_locale_groups:
if grp not in groups:
groups[grp] = OrderedDict({})
if 'groupless' in groups:
grp = groups['groupless']
else:
grp = {}
if app_desc_string not in grp:
grp = languages[def_lang]
if app_desc_string not in grp:
grp = None
languages[alpha2][app_desc_string] = app_desc_string
if grp:
languages[alpha2][app_desc_string] = grp[app_desc_string]
# Some platforms are able to use strings from the default locale.
# Some not, and this fills their missing strings from the default language.
def add_missing_strings():
def_strings = l10n_strings_sparse[def_lang]
for alpha2 in l10n_strings_sparse:
l10n_strings_full[alpha2] = OrderedDict({})
if alpha2 == def_lang:
l10n_strings_full[alpha2] = def_strings
continue
locale_strings = l10n_strings_sparse[alpha2]
for grp in def_strings:
filled_grp = OrderedDict({})
defaults_used = False
for string in def_strings[grp]:
if string in locale_strings[grp]:
filled_grp[string] = locale_strings[grp][string]
else:
defaults_used = True
filled_grp[string] = def_strings[grp][string]
if defaults_used:
l10n_strings_full[alpha2][grp] = filled_grp
else:
l10n_strings_full[alpha2][grp] = locale_strings[grp]
read_locales(def_lang)
if def_lang not in languages:
raise SystemExit('Default language not found!')
read_locales('*', [def_lang])
for alpha2 in l10n_strings_sparse:
if 'groupless' in l10n_strings_sparse[alpha2]:
del l10n_strings_sparse[alpha2]['groupless']
locales_json = os.path.abspath(pj('build', 'locales.json'))
with open(locales_json, 'wt', encoding='utf-8', newline='\n') as f:
locales = {}
for alpha2 in languages:
language = languages[alpha2]
locales[alpha2] = {
'name': language['name']
}
if not language['translators']:
continue
locales[alpha2]['translators'] = deepcopy(language['translators'])
for i, translator in enumerate(language['translators']):
if 'realname' in translator and 'name' in translator:
translator['realname'] = '({})'.format(translator['realname'])
if 'web' in translator:
translator['web'] = '[{}]'.format(translator['web'])
elif 'email' in translator:
translator['web'] = '<{}>'.format(translator['email'])
del translator['email']
language['translators'][i] = ' '.join(translator.values())
language['translators'] = ', '.join(language['translators'])
locales['_'] = def_lang
f.write(
json.dumps(
locales,
separators=(',', ':'),
sort_keys=True,
ensure_ascii=False
)
)
def copytree(src, dst, symlinks=False):
try:
os.makedirs(dst)
except:
pass
for name in os.listdir(src):
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
if os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
elif symlinks:
os.symlink(srcname, dstname)
else:
copy(srcname, dstname)
for platform_name in platforms:
try:
open(pj(platform_dir, '__init__.py'), 'a').close()
open(pj(platform_dir, platform_name, '__init__.py'), 'a').close()
platform = __import__(
'platform.' + platform_name + '.build',
fromlist=['build']
)
finally:
os.remove(pj(platform_dir, '__init__.py'))
os.remove(pj(platform_dir, platform_name, '__init__.py'))
platform = platform.Platform(
build_dir,
config,
languages,
app_desc_string,
os.path.abspath(pj(
build_dir,
config['name'].lower() + '-' + config['version']
))
)
if not params['-meta']:
try:
rmtree(platform.build_dir)
except:
pass
try:
os.makedirs(platform.build_dir)
except:
pass
if not os.path.exists(platform.build_dir):
sys.stderr.write(
'Failed to create platform directory for ' + platform_name + '\n'
)
del platform
continue
platform.write_manifest()
locale_dir = pj(platform.build_dir, platform.l10n_dir)
if os.path.exists(locale_dir):
try:
rmtree(locale_dir)
except:
pass
try:
os.makedirs(locale_dir)
except:
sys.stderr.write(
'Failed to create locales directory for ' + platform_name + '\n'
)
del platform
continue
if platform.requires_all_strings:
if len(l10n_strings_full) == 0:
add_missing_strings()
platform.write_locales(l10n_strings_full)
else:
platform.write_locales(l10n_strings_sparse)
copy(locales_json, platform.build_dir)
if not params['-meta']:
copytree(
src_dir,
platform.build_dir,
params['-useln']
)
platform_js_dir = pj(platform_dir, platform_name, 'js')
if params['-useln']:
os.symlink(
pj(platform_js_dir, 'app_bg.js'),
pj(platform.build_dir, 'js', 'app_bg.js')
)
else:
copy(
pj(platform_js_dir, 'app_bg.js'),
pj(platform.build_dir, 'js')
)
# app.js is extended with app_common.js, so symlink is not applicable
copy(pj(platform_js_dir, 'app.js'), pj(platform.build_dir, 'includes'))
if common_app_code is None:
f_path = pj(platform_dir, 'app_common.js')
with open(f_path, 'rt', encoding='utf-8', newline='\n') as f:
common_app_code = f.read()
f_path = pj(platform.build_dir, 'includes', 'app.js')
with open(f_path, 'at', encoding='utf-8', newline='\n') as f:
f.write(common_app_code)
platform.write_files(params['-useln'])
if params['-pack']:
platform.write_package()
platform.write_update_file()
print('Package is ready for ' + platform_name +
' @ ' + platform.build_dir)
else:
print('Files are ready for ' + platform_name +
' @ ' + platform.build_dir)
else:
if params['-pack']:
platform.write_update_file()
print('Meta-data has been generated for ' + platform_name)
del platform
if os.path.isfile(locales_json):
os.remove(locales_json)