bromano | c8c0d94 | 2021-02-26 04:17:57 +0100 | [diff] [blame] | 1 | # pylint: disable=g-direct-third-party-import |
| 2 | # Copyright 2021 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 | """A tool for extracting the proguard spec file from an AAR.""" |
| 16 | |
| 17 | from __future__ import absolute_import |
| 18 | from __future__ import division |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import os |
| 22 | import sys |
| 23 | import zipfile |
| 24 | |
| 25 | # Do not edit this line. Copybara replaces it with PY2 migration helper. |
| 26 | from absl import app |
| 27 | from absl import flags |
| 28 | |
| 29 | from tools.android import junction |
| 30 | |
| 31 | FLAGS = flags.FLAGS |
| 32 | |
| 33 | flags.DEFINE_string("input_aar", None, "Input AAR") |
| 34 | flags.mark_flag_as_required("input_aar") |
| 35 | flags.DEFINE_string("output_proguard_file", None, |
| 36 | "Output parameter file for proguard") |
| 37 | flags.mark_flag_as_required("output_proguard_file") |
| 38 | |
| 39 | |
| 40 | # Attempt to extract proguard spec from AAR. If the file doesn't exist, an empty |
| 41 | # proguard spec file will be created |
| 42 | def ExtractEmbeddedProguard(aar, output): |
| 43 | proguard_spec = "proguard.txt" |
| 44 | |
| 45 | if proguard_spec in aar.namelist(): |
| 46 | output.write(aar.read(proguard_spec)) |
| 47 | |
| 48 | |
| 49 | def _Main(input_aar, output_proguard_file): |
| 50 | with zipfile.ZipFile(input_aar, "r") as aar: |
| 51 | with open(output_proguard_file, "wb") as output: |
| 52 | ExtractEmbeddedProguard(aar, output) |
| 53 | |
| 54 | |
| 55 | def main(unused_argv): |
| 56 | if os.name == "nt": |
| 57 | # Shorten paths unconditionally, because the extracted paths in |
| 58 | # ExtractEmbeddedJars (which we cannot yet predict, because they depend on |
| 59 | # the names of the Zip entries) may be longer than MAX_PATH. |
| 60 | aar_long = os.path.abspath(FLAGS.input_aar) |
| 61 | proguard_long = os.path.abspath(FLAGS.output_proguard_file) |
| 62 | |
| 63 | with junction.TempJunction(os.path.dirname(aar_long)) as aar_junc: |
| 64 | with junction.TempJunction( |
| 65 | os.path.dirname(proguard_long)) as proguard_junc: |
| 66 | _Main( |
| 67 | os.path.join(aar_junc, os.path.basename(aar_long)), |
| 68 | os.path.join(proguard_junc, os.path.basename(proguard_long))) |
| 69 | else: |
| 70 | _Main(FLAGS.input_aar, FLAGS.output_proguard_file) |
| 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | FLAGS(sys.argv) |
| 75 | app.run(main) |