laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 1 | # Lint as: python2, python3 |
Laszlo Csomor | d456fca | 2017-08-10 10:21:53 +0200 | [diff] [blame] | 2 | # pylint: disable=g-direct-third-party-import |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 3 | # Copyright 2016 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 17 | """A tool for extracting all jar files from an AAR. |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 18 | |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 19 | An AAR may contain JARs at /classes.jar and /libs/*.jar. This tool extracts all |
| 20 | of the jars and creates a param file for singlejar to merge them into one jar. |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 21 | """ |
| 22 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 23 | from __future__ import absolute_import |
| 24 | from __future__ import division |
| 25 | from __future__ import print_function |
| 26 | |
Laszlo Csomor | 567e9ab | 2017-09-27 10:13:43 -0400 | [diff] [blame] | 27 | import os |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 28 | import re |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 29 | import sys |
| 30 | import zipfile |
| 31 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 32 | # Do not edit this line. Copybara replaces it with PY2 migration helper. |
| 33 | from absl import app |
| 34 | from absl import flags |
| 35 | import six |
| 36 | |
Laszlo Csomor | 567e9ab | 2017-09-27 10:13:43 -0400 | [diff] [blame] | 37 | from tools.android import junction |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 38 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 39 | FLAGS = flags.FLAGS |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 40 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 41 | flags.DEFINE_string("input_aar", None, "Input AAR") |
| 42 | flags.mark_flag_as_required("input_aar") |
| 43 | flags.DEFINE_string("output_singlejar_param_file", None, |
| 44 | "Output parameter file for singlejar") |
| 45 | flags.mark_flag_as_required("output_singlejar_param_file") |
| 46 | flags.DEFINE_string("output_dir", None, "Output directory to extract jars in") |
| 47 | flags.mark_flag_as_required("output_dir") |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 48 | |
| 49 | |
Laszlo Csomor | 567e9ab | 2017-09-27 10:13:43 -0400 | [diff] [blame] | 50 | def ExtractEmbeddedJars(aar, |
| 51 | singlejar_param_file, |
| 52 | output_dir, |
| 53 | output_dir_orig=None): |
| 54 | if not output_dir_orig: |
| 55 | output_dir_orig = output_dir |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 56 | jar_pattern = re.compile("^(classes|libs/.+)\\.jar$") |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 57 | singlejar_param_file.write(b"--exclude_build_data\n") |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 58 | for name in aar.namelist(): |
| 59 | if jar_pattern.match(name): |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 60 | singlejar_param_file.write(b"--sources\n") |
Laszlo Csomor | 567e9ab | 2017-09-27 10:13:43 -0400 | [diff] [blame] | 61 | # output_dir may be a temporary junction, so write the original |
| 62 | # (unshortened) path to the params file |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 63 | singlejar_param_file.write( |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 64 | six.ensure_binary((output_dir_orig + "/" + name + "\n"), "utf-8")) |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 65 | aar.extract(name, output_dir) |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 66 | |
| 67 | |
Laszlo Csomor | 567e9ab | 2017-09-27 10:13:43 -0400 | [diff] [blame] | 68 | def _Main(input_aar, |
| 69 | output_singlejar_param_file, |
| 70 | output_dir, |
| 71 | output_dir_orig=None): |
| 72 | if not output_dir_orig: |
| 73 | output_dir_orig = output_dir |
| 74 | with zipfile.ZipFile(input_aar, "r") as aar: |
Googler | 246ee56 | 2017-12-19 11:04:21 -0800 | [diff] [blame] | 75 | with open(output_singlejar_param_file, "wb") as singlejar_param_file: |
Laszlo Csomor | 567e9ab | 2017-09-27 10:13:43 -0400 | [diff] [blame] | 76 | ExtractEmbeddedJars(aar, singlejar_param_file, output_dir, |
| 77 | output_dir_orig) |
| 78 | |
| 79 | |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 80 | def main(unused_argv): |
Laszlo Csomor | 567e9ab | 2017-09-27 10:13:43 -0400 | [diff] [blame] | 81 | if os.name == "nt": |
| 82 | # Shorten paths unconditionally, because the extracted paths in |
| 83 | # ExtractEmbeddedJars (which we cannot yet predict, because they depend on |
| 84 | # the names of the Zip entries) may be longer than MAX_PATH. |
| 85 | aar_long = os.path.abspath(FLAGS.input_aar) |
| 86 | params_long = os.path.abspath(FLAGS.output_singlejar_param_file) |
| 87 | out_long = os.path.abspath(FLAGS.output_dir) |
| 88 | with junction.TempJunction(os.path.dirname(aar_long)) as aar_junc: |
| 89 | with junction.TempJunction(os.path.dirname(params_long)) as params_junc: |
| 90 | with junction.TempJunction(os.path.dirname(out_long)) as out_junc: |
| 91 | _Main( |
| 92 | os.path.join(aar_junc, os.path.basename(aar_long)), |
| 93 | os.path.join(params_junc, os.path.basename(params_long)), |
| 94 | os.path.join(out_junc, os.path.basename(out_long)), |
| 95 | FLAGS.output_dir) |
| 96 | else: |
| 97 | _Main(FLAGS.input_aar, FLAGS.output_singlejar_param_file, FLAGS.output_dir) |
| 98 | |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 99 | |
| 100 | if __name__ == "__main__": |
| 101 | FLAGS(sys.argv) |
laszlocsomor | d93a146 | 2019-11-04 09:14:39 -0800 | [diff] [blame] | 102 | app.run(main) |