blob: e631c481869933f236bf026d18a9c5ed0469028e [file] [log] [blame]
Adam Michael67d736b2016-10-06 21:31:57 +00001# 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 Michael97cc5be2016-10-25 21:25:20 +000015"""A tool for extracting all jar files from an AAR.
Adam Michael67d736b2016-10-06 21:31:57 +000016
Adam Michael97cc5be2016-10-25 21:25:20 +000017An AAR may contain JARs at /classes.jar and /libs/*.jar. This tool extracts all
18of the jars and creates a param file for singlejar to merge them into one jar.
Adam Michael67d736b2016-10-06 21:31:57 +000019"""
20
Adam Michael97cc5be2016-10-25 21:25:20 +000021import re
Adam Michael67d736b2016-10-06 21:31:57 +000022import sys
23import zipfile
24
25from third_party.py import gflags
26
27FLAGS = gflags.FLAGS
28
Adam Michael97cc5be2016-10-25 21:25:20 +000029gflags.DEFINE_string("input_aar", None, "Input AAR")
30gflags.MarkFlagAsRequired("input_aar")
31gflags.DEFINE_string(
32 "output_singlejar_param_file", None, "Output parameter file for singlejar")
33gflags.MarkFlagAsRequired("output_singlejar_param_file")
34gflags.DEFINE_string("output_dir", None, "Output directory to extract jars in")
Adam Michael67d736b2016-10-06 21:31:57 +000035gflags.MarkFlagAsRequired("output_dir")
36
37
Adam Michael97cc5be2016-10-25 21:25:20 +000038def ExtractEmbeddedJars(aar, singlejar_param_file, output_dir):
Adam Michael97cc5be2016-10-25 21:25:20 +000039 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 Michael67d736b2016-10-06 21:31:57 +000046
47
48def main():
Adam Michael97cc5be2016-10-25 21:25:20 +000049 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 Michael67d736b2016-10-06 21:31:57 +000052
53if __name__ == "__main__":
54 FLAGS(sys.argv)
55 main()