Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: build.py

Issue 29365856: Issue 4679 - Refactoring of build.py in adblockbrowser-build (Closed)
Patch Set: Created Nov. 30, 2016, 5:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import glob 3 import glob
4 import json 4 import json
5 import os 5 import os
6 import re 6 import re
7 import shutil 7 import shutil
8 import string 8 import string
9 import subprocess 9 import subprocess
10 import sys 10 import sys
11 import tempfile 11 import tempfile
12 12
13 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 13 _BASE_DIR = os.path.dirname(os.path.abspath(__file__))
14 ENSURE_DEPENDENCIES_PATH = os.path.join(BASE_DIR, "ensure_dependencies.py") 14 _ENSURE_DEPENDENCIES_PATH = os.path.join(_BASE_DIR, "ensure_dependencies.py")
15 GENERATED_MOZCONFIG_PATH = os.path.join(BASE_DIR, "generated", "mozconfig") 15 _GENERATED_MOZCONFIG_PATH = os.path.join(_BASE_DIR, "generated", "mozconfig")
16 MOZCONFIG_COMMON_PATH = os.path.join(BASE_DIR, "mozconfig-common") 16 _MOZCONFIG_COMMON_PATH = os.path.join(_BASE_DIR, "mozconfig-common")
17 MOZCONFIG_ARM_PATH = os.path.join(BASE_DIR, "mozconfig-arm") 17 _MOZCONFIG_ARM_PATH = os.path.join(_BASE_DIR, "mozconfig-arm")
18 MOZCONFIG_X86_PATH = os.path.join(BASE_DIR, "mozconfig-x86") 18 _MOZCONFIG_X86_PATH = os.path.join(_BASE_DIR, "mozconfig-x86")
19 MOZCONFIG_STORE_PATH = os.path.join(BASE_DIR, "mozconfig-store") 19 _MOZCONFIG_STORE_PATH = os.path.join(_BASE_DIR, "mozconfig-store")
20 MOZCONFIG_RELEASE_PATH = os.path.join(BASE_DIR, "mozconfig-release") 20 _MOZCONFIG_RELEASE_PATH = os.path.join(_BASE_DIR, "mozconfig-release")
21 MOZCONFIG_CUSTOM_PATH = os.path.join(BASE_DIR, "mozconfig-custom") 21 _MOZCONFIG_CUSTOM_PATH = os.path.join(_BASE_DIR, "mozconfig-custom")
22 MULTI_L10N_PATH = os.path.join(BASE_DIR, "mozharness", "scripts", 22 _MULTI_L10N_PATH = os.path.join(_BASE_DIR, "mozharness", "scripts",
23 "multil10n.py") 23 "multil10n.py")
24 ABB_PATH = os.path.join(BASE_DIR, "adblockbrowser") 24 _ABB_PATH = os.path.join(_BASE_DIR, "adblockbrowser")
25 ARCH_ARM = "arm" 25 _CMD_BUILD = "build"
26 ARCH_X86 = "x86" 26 _CMD_SIGN = "sign"
27 ARCH_X86_I386 = "i386" 27 _CMD_BUILD_SIGN = "build-and-sign"
28 DIST_STORE = "store" 28 _ARCH_ARM = "arm"
29 BUILD_RELEASE = "release" 29 _ARCH_X86 = "x86"
30 OBJDIR_ARM = "obj-arm-linux-androideabi" 30 _ARCH_X86_I386 = "i386"
31 OBJDIR_X86 = "obj-i386-linux-android" 31 _ARCHS = (_ARCH_ARM, _ARCH_X86)
32 _DIST_STANDALONE = "standalone"
33 _DIST_STORE = "store"
34 _DIST_MODES = (_DIST_STANDALONE, _DIST_STORE)
35 _BUILD_DEVBUILD = "devbuild"
36 _BUILD_RELEASE = "release"
37 _BUILD_MODES = (_BUILD_DEVBUILD, _BUILD_RELEASE)
38 _OBJDIR_ARM = "obj-arm-linux-androideabi"
39 _OBJDIR_X86 = "obj-i386-linux-android"
32 40
33 41
34 def print_usage(): 42 def print_usage():
35 print >>sys.stderr, string.Template("""\ 43 print >>sys.stderr, string.Template("""\
36 Usage: $name build 44 Usage: $name %s
37 $name sign APK_PATH 45 $name %s APK_PATH
38 $name build-and-sign\ 46 $name %s APK_PATH\
anton 2016/12/01 06:50:15 shouldn't it be `$name %s` here (APK_PATH is not r
diegocarloslima 2016/12/01 10:36:18 Actually the APK_PATH was missing in the error mes
Felix Dahlke 2016/12/12 08:03:19 No, APK_PATH is not required for build-and-sign: F
diegocarloslima 2016/12/12 09:42:43 Yeah, my bad, it isn't required. Actually, it alre
39 """).substitute({"name": os.path.basename(sys.argv[0])}) 47 """ % (_CMD_BUILD, _CMD_SIGN,
48 _CMD_BUILD_SIGN)).substitute({"name": os.path.basename(sys.argv[0])})
40 49
41 50
42 def generate_mozconfig(architecture, distribution_mode, build_mode): 51 def _generate_mozconfig(architecture, distribution_mode, build_mode):
43 mozconfig_path = GENERATED_MOZCONFIG_PATH 52 mozconfig_path = _GENERATED_MOZCONFIG_PATH
44 with open(mozconfig_path, "w+") as mozconfig: 53 with open(mozconfig_path, "w+") as mozconfig:
45 mozconfig.write(". \"%s\"\n" % MOZCONFIG_COMMON_PATH) 54 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_COMMON_PATH)
46 if architecture == ARCH_X86: 55 if architecture == _ARCH_X86:
47 mozconfig.write(". \"%s\"\n" % MOZCONFIG_X86_PATH) 56 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_X86_PATH)
48 else: 57 else:
49 mozconfig.write(". \"%s\"\n" % MOZCONFIG_ARM_PATH) 58 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_ARM_PATH)
50 if distribution_mode == DIST_STORE: 59 if distribution_mode == _DIST_STORE:
51 mozconfig.write(". \"%s\"\n" % MOZCONFIG_STORE_PATH) 60 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_STORE_PATH)
52 if build_mode == BUILD_RELEASE: 61 if build_mode == _BUILD_RELEASE:
53 mozconfig.write(". \"%s\"\n" % MOZCONFIG_RELEASE_PATH) 62 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_RELEASE_PATH)
54 mozconfig.write(". \"%s\"\n" % MOZCONFIG_CUSTOM_PATH) 63 mozconfig.write(". \"%s\"\n" % _MOZCONFIG_CUSTOM_PATH)
55 return mozconfig_path 64 return mozconfig_path
56 65
57 66
58 def build(architecture, distribution_mode, build_mode, sdk_path, ndk_path): 67 def _build(architecture, distribution_mode, build_mode, sdk_path, ndk_path):
59 build_environment = os.environ.copy() 68 build_environment = os.environ.copy()
60 build_environment["MOZCONFIG"] = generate_mozconfig(
61 architecture, distribution_mode, build_mode)
62 build_environment["ANDROID_SDK_PATH"] = sdk_path 69 build_environment["ANDROID_SDK_PATH"] = sdk_path
63 build_environment["ANDROID_NDK_PATH"] = ndk_path 70 build_environment["ANDROID_NDK_PATH"] = ndk_path
64 obj_dir = OBJDIR_X86 if architecture == ARCH_X86 else OBJDIR_ARM 71 build_environment["MOZCONFIG"] = _generate_mozconfig(
72 architecture, distribution_mode, build_mode)
73 obj_dir = _OBJDIR_X86 if architecture == _ARCH_X86 else _OBJDIR_ARM
65 build_environment["OBJDIR"] = obj_dir 74 build_environment["OBJDIR"] = obj_dir
66 subprocess.check_call([os.path.join(ABB_PATH, "mach"), "clobber"], 75 subprocess.check_call([os.path.join(_ABB_PATH, "mach"), "clobber"],
67 env=build_environment) 76 env=build_environment)
68 subprocess.check_call([MULTI_L10N_PATH, "--cfg", "adblockbrowser-cfg.py"], 77 subprocess.check_call([_MULTI_L10N_PATH, "--cfg", "adblockbrowser-cfg.py"],
69 env=build_environment) 78 env=build_environment)
70 79
71 dist_path = os.path.join(ABB_PATH, obj_dir, "dist") 80 dist_path = os.path.join(_ABB_PATH, obj_dir, "dist")
72 arch_suffix = ARCH_X86_I386 if architecture == ARCH_X86 else ARCH_ARM 81 arch_suffix = _ARCH_X86_I386 if architecture == _ARCH_X86 else _ARCH_ARM
73 [manifest_path] = glob.glob(os.path.join( 82 [manifest_path] = glob.glob(os.path.join(
74 dist_path, "fennec-*.multi.android-%s.json" % arch_suffix)) 83 dist_path, "fennec-*.multi.android-%s.json" % arch_suffix))
75 with open(manifest_path) as manifest_file: 84 with open(manifest_path) as manifest_file:
76 manifest = json.load(manifest_file) 85 manifest = json.load(manifest_file)
77 86
78 apk_path = os.path.join(dist_path, "gecko-unsigned-unaligned.apk") 87 apk_path = os.path.join(dist_path, "gecko-unsigned-unaligned.apk")
79 if build_mode == "release": 88 if build_mode == _BUILD_RELEASE:
80 target_apk_name = "adblockbrowser-%s-%s.apk" % ( 89 target_apk_name = "adblockbrowser-%s-%s.apk" % (
81 manifest["moz_app_version"], architecture) 90 manifest["moz_app_version"], architecture)
82 else: 91 else:
83 target_apk_name = "adblockbrowser-%s.%s-%s.apk" % ( 92 target_apk_name = "adblockbrowser-%s.%s-%s.apk" % (
84 manifest["moz_app_version"], manifest["buildid"], architecture) 93 manifest["moz_app_version"], manifest["buildid"], architecture)
85 target_apk_path = os.path.join(dist_path, target_apk_name) 94 target_apk_path = os.path.join(dist_path, target_apk_name)
86 shutil.copyfile(apk_path, target_apk_path) 95 shutil.copyfile(apk_path, target_apk_path)
87 96
88 target_manifest_path = re.sub(r".apk$", ".json", target_apk_path) 97 target_manifest_path = re.sub(r".apk$", ".json", target_apk_path)
89 shutil.copyfile(manifest_path, target_manifest_path) 98 shutil.copyfile(manifest_path, target_manifest_path)
90 99
91 return target_apk_path 100 return target_apk_path
92 101
93 102
94 def sign(apk_path, key_store, key_name): 103 def _sign(apk_path, key_store, key_name):
95 temp_apk_path = tempfile.NamedTemporaryFile().name 104 temp_apk_path = tempfile.NamedTemporaryFile().name
96 shutil.copyfile(apk_path, temp_apk_path) 105 shutil.copyfile(apk_path, temp_apk_path)
97 try: 106 try:
98 subprocess.check_call(["jarsigner", "-verbose", "-sigalg", "SHA1withRSA" , 107 subprocess.check_call(["jarsigner", "-verbose",
99 "-digestalg", "SHA1", "-keystore", key_store, 108 "-sigalg", "SHA1withRSA",
109 "-digestalg", "SHA1",
110 "-keystore", key_store,
100 temp_apk_path, key_name]) 111 temp_apk_path, key_name])
101 os.remove(apk_path) 112 os.remove(apk_path)
102 subprocess.check_call(["zipalign", "-v", "4", temp_apk_path, apk_path]) 113 subprocess.check_call(["zipalign", "-v", "4", temp_apk_path, apk_path])
103 finally: 114 finally:
104 os.remove(temp_apk_path) 115 os.remove(temp_apk_path)
105 116
106 if __name__ == "__main__": 117 if __name__ == "__main__":
107 if len(sys.argv) < 2: 118 if len(sys.argv) < 2:
108 print_usage() 119 print_usage()
109 sys.exit(1) 120 sys.exit(1)
110 121
111 mode = sys.argv[1] 122 mode = sys.argv[1]
112 do_build = mode in ("build", "build-and-sign") 123 do_build = mode in (_CMD_BUILD, _CMD_BUILD_SIGN)
113 do_sign = mode in ("sign", "build-and-sign") 124 do_sign = mode in (_CMD_SIGN, _CMD_BUILD_SIGN)
114 if not do_build and not do_sign: 125 if not do_build and not do_sign:
115 print_usage() 126 print_usage()
116 sys.exit(2) 127 sys.exit(2)
117 128
118 if do_sign: 129 if do_sign:
119 if len(sys.argv) < 3: 130 if len(sys.argv) < 3:
120 print_usage() 131 print_usage()
121 sys.exit(3) 132 sys.exit(3)
122 apk_path = sys.argv[2] 133 apk_path = sys.argv[2]
123 134
124 subprocess.check_call([ENSURE_DEPENDENCIES_PATH]) 135 subprocess.check_call([_ENSURE_DEPENDENCIES_PATH])
125 import config 136 import config
126 137
138 error_msg = "Invalid %s, check config.py. Supported %s: %s"
127 architecture = config.ARCHITECTURE 139 architecture = config.ARCHITECTURE
128 if architecture not in (ARCH_ARM, ARCH_X86): 140 if architecture not in _ARCHS:
129 print >>sys.stderr, "Invalid architecture, check config.py" 141 print >>sys.stderr, error_msg % (
142 "architecture", "architectures", _ARCHS)
130 sys.exit(4) 143 sys.exit(4)
131 144
132 distribution_mode = config.DISTRIBUTION_MODE 145 distribution_mode = config.DISTRIBUTION_MODE
133 if distribution_mode not in ("standalone", "store"): 146 if distribution_mode not in _DIST_MODES:
134 print >>sys.stderr, "Invalid distribution mode, check config.py" 147 print >>sys.stderr, error_msg % (
148 "distribution mode", "distribution modes", _DIST_MODES)
135 sys.exit(5) 149 sys.exit(5)
136 150
137 build_mode = config.BUILD_MODE 151 build_mode = config.BUILD_MODE
138 if build_mode not in ("devbuild", "release"): 152 if build_mode not in _BUILD_MODES:
139 print >>sys.stderr, "Invalid build mode, check config.py" 153 print >>sys.stderr, error_msg % (
154 "build mode", "build modes", _BUILD_MODES)
140 sys.exit(6) 155 sys.exit(6)
141 156
142 if do_build: 157 if do_build:
143 apk_path = build(architecture, distribution_mode, build_mode, 158 apk_path = _build(architecture, distribution_mode, build_mode,
144 config.ANDROID_SDK_PATH, config.ANDROID_NDK_PATH) 159 config.ANDROID_SDK_PATH, config.ANDROID_NDK_PATH)
145 if do_sign: 160 if do_sign:
146 sign(apk_path, config.ANDROID_KEYSTORE_PATH, config.ANDROID_KEY_NAME) 161 _sign(apk_path, config.ANDROID_KEYSTORE_PATH, config.ANDROID_KEY_NAME)
147 else: 162 else:
148 print apk_path 163 print apk_path
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld