blob: d8f42d919783300478e8ec1b8f9fc1e36549042b [file] [log] [blame]
laszlocsomord93a1462019-11-04 09:14:39 -08001# Lint as: python2, python3
Laszlo Csomord456fca2017-08-10 10:21:53 +02002# pylint: disable=g-direct-third-party-import
Adam Michael67d736b2016-10-06 21:31:57 +00003# Copyright 2016 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Adam Michael97cc5be2016-10-25 21:25:20 +000017"""A tool for extracting all jar files from an AAR.
Adam Michael67d736b2016-10-06 21:31:57 +000018
Adam Michael97cc5be2016-10-25 21:25:20 +000019An AAR may contain JARs at /classes.jar and /libs/*.jar. This tool extracts all
20of the jars and creates a param file for singlejar to merge them into one jar.
Adam Michael67d736b2016-10-06 21:31:57 +000021"""
22
laszlocsomord93a1462019-11-04 09:14:39 -080023from __future__ import absolute_import
24from __future__ import division
25from __future__ import print_function
26
Laszlo Csomor567e9ab2017-09-27 10:13:43 -040027import os
Adam Michael97cc5be2016-10-25 21:25:20 +000028import re
Adam Michael67d736b2016-10-06 21:31:57 +000029import sys
30import zipfile
31
laszlocsomord93a1462019-11-04 09:14:39 -080032# Do not edit this line. Copybara replaces it with PY2 migration helper.
33from absl import app
34from absl import flags
35import six
36
Laszlo Csomor567e9ab2017-09-27 10:13:43 -040037from tools.android import junction
Adam Michael67d736b2016-10-06 21:31:57 +000038
laszlocsomord93a1462019-11-04 09:14:39 -080039FLAGS = flags.FLAGS
Adam Michael67d736b2016-10-06 21:31:57 +000040
laszlocsomord93a1462019-11-04 09:14:39 -080041flags.DEFINE_string("input_aar", None, "Input AAR")
42flags.mark_flag_as_required("input_aar")
43flags.DEFINE_string("output_singlejar_param_file", None,
44 "Output parameter file for singlejar")
45flags.mark_flag_as_required("output_singlejar_param_file")
46flags.DEFINE_string("output_dir", None, "Output directory to extract jars in")
47flags.mark_flag_as_required("output_dir")
Adam Michael67d736b2016-10-06 21:31:57 +000048
49
Laszlo Csomor567e9ab2017-09-27 10:13:43 -040050def ExtractEmbeddedJars(aar,
51 singlejar_param_file,
52 output_dir,
53 output_dir_orig=None):
54 if not output_dir_orig:
55 output_dir_orig = output_dir
Adam Michael97cc5be2016-10-25 21:25:20 +000056 jar_pattern = re.compile("^(classes|libs/.+)\\.jar$")
Googler246ee562017-12-19 11:04:21 -080057 singlejar_param_file.write(b"--exclude_build_data\n")
Adam Michael97cc5be2016-10-25 21:25:20 +000058 for name in aar.namelist():
59 if jar_pattern.match(name):
Googler246ee562017-12-19 11:04:21 -080060 singlejar_param_file.write(b"--sources\n")
Laszlo Csomor567e9ab2017-09-27 10:13:43 -040061 # output_dir may be a temporary junction, so write the original
62 # (unshortened) path to the params file
Googler246ee562017-12-19 11:04:21 -080063 singlejar_param_file.write(
laszlocsomord93a1462019-11-04 09:14:39 -080064 six.ensure_binary((output_dir_orig + "/" + name + "\n"), "utf-8"))
Adam Michael97cc5be2016-10-25 21:25:20 +000065 aar.extract(name, output_dir)
Adam Michael67d736b2016-10-06 21:31:57 +000066
67
Laszlo Csomor567e9ab2017-09-27 10:13:43 -040068def _Main(input_aar,
69 output_singlejar_param_file,
70 output_dir,
71 output_dir_orig=None):
72 if not output_dir_orig:
73 output_dir_orig = output_dir
74 with zipfile.ZipFile(input_aar, "r") as aar:
Googler246ee562017-12-19 11:04:21 -080075 with open(output_singlejar_param_file, "wb") as singlejar_param_file:
Laszlo Csomor567e9ab2017-09-27 10:13:43 -040076 ExtractEmbeddedJars(aar, singlejar_param_file, output_dir,
77 output_dir_orig)
78
79
laszlocsomord93a1462019-11-04 09:14:39 -080080def main(unused_argv):
Laszlo Csomor567e9ab2017-09-27 10:13:43 -040081 if os.name == "nt":
82 # Shorten paths unconditionally, because the extracted paths in
83 # ExtractEmbeddedJars (which we cannot yet predict, because they depend on
84 # the names of the Zip entries) may be longer than MAX_PATH.
85 aar_long = os.path.abspath(FLAGS.input_aar)
86 params_long = os.path.abspath(FLAGS.output_singlejar_param_file)
87 out_long = os.path.abspath(FLAGS.output_dir)
88 with junction.TempJunction(os.path.dirname(aar_long)) as aar_junc:
89 with junction.TempJunction(os.path.dirname(params_long)) as params_junc:
90 with junction.TempJunction(os.path.dirname(out_long)) as out_junc:
91 _Main(
92 os.path.join(aar_junc, os.path.basename(aar_long)),
93 os.path.join(params_junc, os.path.basename(params_long)),
94 os.path.join(out_junc, os.path.basename(out_long)),
95 FLAGS.output_dir)
96 else:
97 _Main(FLAGS.input_aar, FLAGS.output_singlejar_param_file, FLAGS.output_dir)
98
Adam Michael67d736b2016-10-06 21:31:57 +000099
100if __name__ == "__main__":
101 FLAGS(sys.argv)
laszlocsomord93a1462019-11-04 09:14:39 -0800102 app.run(main)