blob: 472bbadcc9d854133c55a73c55a3c4741a77b4c9 [file] [log] [blame]
bromanoc8c0d942021-02-26 04:17:57 +01001# 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
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import os
22import sys
23import zipfile
24
25# Do not edit this line. Copybara replaces it with PY2 migration helper.
26from absl import app
27from absl import flags
28
29from tools.android import junction
30
31FLAGS = flags.FLAGS
32
33flags.DEFINE_string("input_aar", None, "Input AAR")
34flags.mark_flag_as_required("input_aar")
35flags.DEFINE_string("output_proguard_file", None,
36 "Output parameter file for proguard")
37flags.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
42def 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
49def _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
55def 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
73if __name__ == "__main__":
74 FLAGS(sys.argv)
75 app.run(main)