forked from crosswalk-project/crosswalk-demos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
make_webapp.py
executable file
·303 lines (251 loc) · 10.2 KB
/
make_webapp.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
#!/usr/bin/env python
# Copyright (c) 2013 Intel Corporation. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Make webapps for both android and tizen
Sample usage from shell script:
Build all apps for both Tizen and Android
python make_webapp.py
Build specified app
python make_webapp.py --app=MemoryGame
Only build for Android
python make_webapp.py --target=android
Specify build tool version
python make_webapp.py -v 2.31.27.0
Specify build tool with the download url
python make_webapp.py --version=2.31.27.0 --url=https://download.01.org/crosswalk/releases/android-x86/canary
Only checkout the webapps with patches patched
python make_webapp.py --no-build
The build result will be under out directory.
"""
import optparse
import os
import shutil
import subprocess
import sys
import android.android_build_app
def RunCommandShell(command, app):
proc = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True)
out, _ = proc.communicate()
print ('[' + app + ']: ' + out)
def FindApps(app_list):
for i in os.listdir('.'):
if os.path.isdir(i):
check_file = os.path.join('.', i, 'manifest.json')
check_file2 = os.path.join('.', i, 'src', 'manifest.json')
if (os.path.exists(check_file) or
os.path.exists(check_file2)):
app_list.append(i)
def BuildForAndroidApp(current_real_path, app, build_result):
return_value = android.android_build_app.BuildApp(current_real_path, app)
if not return_value:
build_result = build_result + app + ' :OK\n'
else:
build_result = build_result + app + ' :Failed, error code = ' + str(return_value) + '\n'
return build_result
def RevertManifestFile(current_real_path, app):
src_folder = os.path.join(current_real_path, app, 'src')
renamed_jsonfile = os.path.join(src_folder, '_original_manifest.json_')
target_jsonfile = os.path.join(src_folder, 'manifest.json')
if os.path.exists(os.path.join(current_real_path, app, 'manifest.json')):
os.remove(target_jsonfile)
if os.path.exists(renamed_jsonfile):
shutil.move(renamed_jsonfile, target_jsonfile)
def RevertPatchFiles(current_real_path, app):
# Check whether it's a git submodule.
git_file = os.path.join(current_real_path, app, 'src', '.git')
if not os.path.exists(git_file):
# It's not a git submodule, no patch files applied.
return
# cd to submodule dir.
previous_cwd = os.getcwd()
os.chdir(os.path.join(current_real_path, app, 'src'))
# Checkout to for_crosswalk branch.
RunCommandShell('git checkout for_crosswalk', app)
# Revert cd.
os.chdir(previous_cwd)
def RevertPatches(current_real_path, app):
RevertManifestFile(current_real_path, app)
RevertPatchFiles(current_real_path, app)
def CopyManifestFile(current_real_path, app):
jsonfile = os.path.join(current_real_path, app, 'manifest.json')
src_folder = os.path.join(current_real_path, app, 'src')
target_jsonfile = os.path.join(src_folder, 'manifest.json')
renamed_jsonfile = os.path.join(src_folder, '_original_manifest.json_')
# Check whether manifest.json exists in webapp folder.
if os.path.exists(jsonfile):
# We need to copy manifest.json to src folder.
# Check whether there is manifest.json under src.
if os.path.exists(target_jsonfile):
# Rename current manifest.json.
shutil.move(target_jsonfile, renamed_jsonfile)
# Copy manifest.json to src.
shutil.copy2(jsonfile, target_jsonfile)
def FindPatchFiles(current_real_path, app, patch_list):
app_path = os.path.join(current_real_path, app)
# Patch files will be sorted after below scenario.
# Because patches should be orderly patched one by one.
for i in sorted(os.listdir(app_path)):
if os.path.isfile(os.path.join(app_path, i)):
extension = os.path.splitext(i)[1][1:].lower()
if extension == 'patch':
patch_list.append(i)
def ApplyPatchFiles(current_real_path, app):
# Check whether it's a git submodule.
git_file = os.path.join(current_real_path, app, 'src', '.git')
if not os.path.exists(git_file):
# It's not a git submodule, no patch files needed.
return
patch_list = []
FindPatchFiles(current_real_path, app, patch_list)
if len(patch_list) == 0:
return
# cd to submodule dir.
previous_cwd = os.getcwd()
os.chdir(os.path.join(current_real_path, app, 'src'))
# Checkout to for_crosswalk branch.
RunCommandShell('git checkout for_crosswalk', app)
# Delete previous auto_patch branch.
RunCommandShell('git branch -D auto_patch', app)
# Create auto_patch branch.
RunCommandShell('git checkout -b auto_patch for_crosswalk', app)
# Apply all the patches.
for patch in patch_list:
patch_path = os.path.join(current_real_path, app, patch)
RunCommandShell('git am ' + patch_path, app)
# Revert cd.
os.chdir(previous_cwd)
def ApplyPatches(current_real_path, app):
ApplyPatchFiles(current_real_path, app)
CopyManifestFile(current_real_path, app)
def BuildApps(func, current_real_path, app_list, build_result):
for app in app_list:
print ('Build ' + app + ':')
ApplyPatches(current_real_path, app)
build_result = func(current_real_path, app, build_result)
RevertPatches(current_real_path, app)
return build_result
def RunGetBuildToolScript(options, current_real_path):
xwalk_app_template_path = os.path.join(current_real_path, 'android', 'xwalk_app_template')
# Remove build tool, we need to get new one.
if os.path.exists(xwalk_app_template_path):
shutil.rmtree(xwalk_app_template_path)
if not options.version:
print ('Please use --version or -v argument to specify xwalk application template version\n'
'Or you can run android/get_xwalk_app_template.py to download')
return False
print ('Downloading xwalk_app_template...')
version = '--version=' + options.version
# The '--url' is valid only when '-v' or '--version' is specified.
if options.url:
url = '--url=' + options.url
else:
url = ''
download_script = os.path.join(current_real_path, 'android', 'get_xwalk_app_template.py')
proc = subprocess.Popen(['python', download_script, version, url],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out, _ = proc.communicate()
print (out)
# Check whether download xwalk_app_template succeed.
if not os.path.exists(xwalk_app_template_path):
return False;
return True
def CheckAndroidBuildTool(options, current_real_path):
xwalk_app_template_path = os.path.join(current_real_path, 'android', 'xwalk_app_template')
# If the version of build tool is specified.
# We need to switch to the specified version.
if options.version:
return RunGetBuildToolScript(options, current_real_path)
# No build tool version specified.
# Use previous build tool.
if os.path.exists(xwalk_app_template_path):
return True
else:
# No build tool found, download one.
return RunGetBuildToolScript(options, current_real_path)
def InitWebApps(current_real_path, app_list):
print ('Init submodules..')
RunCommandShell('git submodule update --init', '')
# The submodule/master branch will always be the latest version.
# The branch 'for_crosswalk' will track the workable commit id we specified.
for app in app_list:
# Check whether it's a git submodule.
git_file = os.path.join(current_real_path, app, 'src', '.git')
if not os.path.exists(git_file):
# It's not a git submodule, no patch files needed.
continue
# cd to submodule dir.
previous_cwd = os.getcwd()
os.chdir(os.path.join(current_real_path, app, 'src'))
RunCommandShell('git checkout -b for_crosswalk', app)
os.chdir(previous_cwd)
def Build_WebApps(options, current_real_path, build_result):
app_list = []
if options.app:
app_list.append(options.app)
else:
# No app specified, loop to find all available apps.
FindApps(app_list)
if len(app_list) == 0:
build_result += 'No available apps\n'
return build_result
# Init git submodules at the first time.
# (git will automatically check whether need init the next time).
InitWebApps(current_real_path, app_list)
# If no build needed
if options.no_build:
for app in app_list:
ApplyPatchFiles(current_real_path, app)
build_result = 'Webapps are checked out, and patches are patched.'
return build_result
# Build apps.
if options.target == 'android':
if CheckAndroidBuildTool(options, current_real_path):
build_result = BuildApps(BuildForAndroidApp, current_real_path, app_list, build_result)
else:
build_result += 'No Build tools\n'
elif options.target == 'tizen':
print ('Tizen build not implemented')
else:
if CheckAndroidBuildTool(options, current_real_path):
build_result = BuildApps(BuildForAndroidApp, current_real_path, app_list, build_result)
else:
build_result += ('No Build tools\n')
return build_result
def main():
build_result = '\nBuild Result:\n'
parser = optparse.OptionParser()
parser.add_option('--app',
help='The app name. '
'If no app specified, all apps will be built. '
'Such as: --app=MemoryGame')
parser.add_option('--target',
help='Build target. '
'If no target specified, all targets will be built. '
'Such as: --target=android')
parser.add_option('-v', '--version', action='store', dest='version',
help='The xwalk application template version. '
'Such as: --version=2.31.27.0')
parser.add_option('-u', '--url', action='store', dest='url',
help='The xwalk application template basic url address. Such as: '
'--url=https://download.01.org/crosswalk/releases/android-x86/canary')
parser.add_option('--no-build', action='store_true',
dest='no_build', default=False,
help = 'Only checkout the webapps with patches patched.')
options, _ = parser.parse_args()
current_real_path = os.path.abspath(os.path.dirname(sys.argv[0]))
previous_cwd = os.getcwd()
os.chdir(current_real_path)
try:
build_result = Build_WebApps(options, current_real_path, build_result)
except:
print ('Unexpected error:', sys.exc_info()[0])
finally:
os.chdir(previous_cwd)
print (build_result)
return 0
if __name__ == '__main__':
sys.exit(main())