Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Adds the code parts to a resource APK.""" | 7 """Adds the code parts to a resource APK.""" |
| 8 | 8 |
| 9 import argparse | 9 import argparse |
| 10 import itertools | 10 import itertools |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 def _AddAssets(apk, paths, disable_compression=False): | 80 def _AddAssets(apk, paths, disable_compression=False): |
| 81 """Adds the given paths to the apk. | 81 """Adds the given paths to the apk. |
| 82 | 82 |
| 83 Args: | 83 Args: |
| 84 apk: ZipFile to write to. | 84 apk: ZipFile to write to. |
| 85 paths: List of paths (with optional :zipPath suffix) to add. | 85 paths: List of paths (with optional :zipPath suffix) to add. |
| 86 disable_compression: Whether to disable compression. | 86 disable_compression: Whether to disable compression. |
| 87 """ | 87 """ |
| 88 # Group all uncompressed assets together in the hope that it will increase | 88 # Group all uncompressed assets together in the hope that it will increase |
| 89 # locality of mmap'ed files. | 89 # locality of mmap'ed files. |
| 90 for target_compress_type in (zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED): | 90 for target_compress in (False, True): |
| 91 for path in paths: | 91 for path in paths: |
| 92 src_path, dest_path = _SplitAssetPath(path) | 92 src_path, dest_path = _SplitAssetPath(path) |
| 93 | 93 |
| 94 compress_type = zipfile.ZIP_DEFLATED | 94 compress = not disable_compression and ( |
|
jbudorick
2015/11/17 14:15:15
nit: Can we flip this to enable_compression? I thi
agrieve
2015/11/18 20:36:34
Normally agree, but in this case I think it's a bi
| |
| 95 if disable_compression or ( | 95 os.path.splitext(src_path)[1] not in _NO_COMPRESS_EXTENSIONS) |
| 96 os.path.splitext(src_path)[1] in _NO_COMPRESS_EXTENSIONS): | 96 if target_compress == compress: |
| 97 compress_type = zipfile.ZIP_STORED | |
| 98 | |
| 99 if target_compress_type == compress_type: | |
| 100 apk_path = 'assets/' + dest_path | 97 apk_path = 'assets/' + dest_path |
| 101 try: | 98 try: |
| 102 apk.getinfo(apk_path) | 99 apk.getinfo(apk_path) |
| 103 # Should never happen since write_build_config.py handles merging. | 100 # Should never happen since write_build_config.py handles merging. |
| 104 raise Exception('Multiple targets specified the asset path: %s' % | 101 raise Exception('Multiple targets specified the asset path: %s' % |
| 105 apk_path) | 102 apk_path) |
| 106 except KeyError: | 103 except KeyError: |
| 107 apk.write(src_path, apk_path, compress_type) | 104 build_utils.AddToZipHermetic(apk, apk_path, src_path=src_path, |
| 105 compress=compress) | |
| 108 | 106 |
| 109 | 107 |
| 110 def main(args): | 108 def main(args): |
| 111 args = build_utils.ExpandFileArgs(args) | 109 args = build_utils.ExpandFileArgs(args) |
| 112 options = _ParseArgs(args) | 110 options = _ParseArgs(args) |
| 113 | 111 |
| 114 native_libs = [] | 112 native_libs = [] |
| 115 if options.native_libs_dir: | 113 if options.native_libs_dir: |
| 116 native_libs = _ListSubPaths(options.native_libs_dir) | 114 native_libs = _ListSubPaths(options.native_libs_dir) |
| 117 | 115 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 133 shutil.copyfile(options.resource_apk, tmp_apk) | 131 shutil.copyfile(options.resource_apk, tmp_apk) |
| 134 | 132 |
| 135 # TODO(agrieve): It would be more efficient to combine this step | 133 # TODO(agrieve): It would be more efficient to combine this step |
| 136 # with finalize_apk(), which sometimes aligns and uncompresses the | 134 # with finalize_apk(), which sometimes aligns and uncompresses the |
| 137 # native libraries. | 135 # native libraries. |
| 138 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: | 136 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: |
| 139 _AddAssets(apk, options.assets, disable_compression=False) | 137 _AddAssets(apk, options.assets, disable_compression=False) |
| 140 _AddAssets(apk, options.uncompressed_assets, disable_compression=True) | 138 _AddAssets(apk, options.uncompressed_assets, disable_compression=True) |
| 141 for path in native_libs: | 139 for path in native_libs: |
| 142 basename = os.path.basename(path) | 140 basename = os.path.basename(path) |
| 143 apk.write(path, 'lib/%s/%s' % (options.android_abi, basename)) | 141 apk_path = 'lib/%s/%s' % (options.android_abi, basename) |
| 142 build_utils.AddToZipHermetic(apk, apk_path, src_path=path) | |
| 144 if options.create_placeholder_lib: | 143 if options.create_placeholder_lib: |
| 145 # Make it non-empty so that its checksum is non-zero and is not | 144 # Make it non-empty so that its checksum is non-zero and is not |
| 146 # ignored by md5_check. | 145 # ignored by md5_check. |
| 147 apk.writestr('lib/%s/libplaceholder.so' % options.android_abi, ':-)') | 146 apk_path = 'lib/%s/libplaceholder.so' % options.android_abi |
| 147 build_utils.AddToZipHermetic(apk, apk_path, data=':)') | |
| 148 if options.dex_file: | 148 if options.dex_file: |
| 149 apk.write(options.dex_file, 'classes.dex') | 149 build_utils.AddToZipHermetic(apk, 'classes.dex', |
| 150 src_path=options.dex_file) | |
| 150 | 151 |
| 151 shutil.move(tmp_apk, options.output_apk) | 152 shutil.move(tmp_apk, options.output_apk) |
| 152 finally: | 153 finally: |
| 153 if os.path.exists(tmp_apk): | 154 if os.path.exists(tmp_apk): |
| 154 os.unlink(tmp_apk) | 155 os.unlink(tmp_apk) |
| 155 | 156 |
| 156 build_utils.CallAndWriteDepfileIfStale( | 157 build_utils.CallAndWriteDepfileIfStale( |
| 157 on_stale_md5, | 158 on_stale_md5, |
| 158 options, | 159 options, |
| 159 input_paths=input_paths, | 160 input_paths=input_paths, |
| 160 input_strings=input_strings, | 161 input_strings=input_strings, |
| 161 output_paths=[options.output_apk]) | 162 output_paths=[options.output_apk]) |
| 162 | 163 |
| 163 | 164 |
| 164 if __name__ == '__main__': | 165 if __name__ == '__main__': |
| 165 main(sys.argv[1:]) | 166 main(sys.argv[1:]) |
| OLD | NEW |