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