Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 15 | """A tool for extracting all jar files from an AAR. |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 16 | |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 17 | An AAR may contain JARs at /classes.jar and /libs/*.jar. This tool extracts all |
| 18 | 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] | 19 | """ |
| 20 | |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 21 | import re |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 22 | import sys |
| 23 | import zipfile |
| 24 | |
| 25 | from third_party.py import gflags |
| 26 | |
| 27 | FLAGS = gflags.FLAGS |
| 28 | |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 29 | gflags.DEFINE_string("input_aar", None, "Input AAR") |
| 30 | gflags.MarkFlagAsRequired("input_aar") |
| 31 | gflags.DEFINE_string( |
| 32 | "output_singlejar_param_file", None, "Output parameter file for singlejar") |
| 33 | gflags.MarkFlagAsRequired("output_singlejar_param_file") |
| 34 | gflags.DEFINE_string("output_dir", None, "Output directory to extract jars in") |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 35 | gflags.MarkFlagAsRequired("output_dir") |
| 36 | |
| 37 | |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 38 | def ExtractEmbeddedJars(aar, singlejar_param_file, output_dir): |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 39 | jar_pattern = re.compile("^(classes|libs/.+)\\.jar$") |
| 40 | singlejar_param_file.write("--exclude_build_data\n") |
| 41 | for name in aar.namelist(): |
| 42 | if jar_pattern.match(name): |
| 43 | singlejar_param_file.write("--sources\n") |
| 44 | singlejar_param_file.write(output_dir + "/" + name + "\n") |
| 45 | aar.extract(name, output_dir) |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def main(): |
Adam Michael | 97cc5be | 2016-10-25 21:25:20 +0000 | [diff] [blame] | 49 | with zipfile.ZipFile(FLAGS.input_aar, "r") as aar: |
| 50 | with open(FLAGS.output_singlejar_param_file, "w") as singlejar_param_file: |
| 51 | ExtractEmbeddedJars(aar, singlejar_param_file, FLAGS.output_dir) |
Adam Michael | 67d736b | 2016-10-06 21:31:57 +0000 | [diff] [blame] | 52 | |
| 53 | if __name__ == "__main__": |
| 54 | FLAGS(sys.argv) |
| 55 | main() |