py2to3: update tools/android/*.py to PY3
This is a rollback of commit 91b26dc4f3b327f115787c3ec35278c299480d0c and a
roll-forward of commit ad628ecfb8ed43870fb354e5d8f7da68594f95bd. Original commit
message is below.
Details:
- use six.ensure_str / six.ensure_bytes
- use absl.flags instead of gflags
- put third_party/py/abseil:srcs into @bazel_tools
so the embedded Android tools can also use it
- remove python_version = "PY2" from py_binary and
py_test rules (the default is "PY3"), except in
BUILD.tools, to preserve PY2 compatibility for
running Bazel (its embedded Python tools)
- added srcs_version = "PY2AND3" to BUILD.tools to
make it explicit that these libraries are meant
to be py2 and py3 compatible
See https://github.com/bazelbuild/bazel/issues/10127
PiperOrigin-RevId: 278394040
diff --git a/tools/android/aar_resources_extractor.py b/tools/android/aar_resources_extractor.py
index b4c8577..29d2b4b 100644
--- a/tools/android/aar_resources_extractor.py
+++ b/tools/android/aar_resources_extractor.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
# pylint: disable=g-direct-third-party-import
# Copyright 2017 The Bazel Authors. All rights reserved.
#
@@ -22,20 +23,28 @@
In the future, this script may be extended to also extract assets.
"""
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
import os
import sys
import zipfile
+# Do not edit this line. Copybara replaces it with PY2 migration helper.
+from absl import app
+from absl import flags
+import six
+
from tools.android import junction
-from third_party.py import gflags
-FLAGS = gflags.FLAGS
+FLAGS = flags.FLAGS
-gflags.DEFINE_string("input_aar", None, "Input AAR")
-gflags.MarkFlagAsRequired("input_aar")
-gflags.DEFINE_string("output_res_dir", None, "Output resources directory")
-gflags.MarkFlagAsRequired("output_res_dir")
-gflags.DEFINE_string("output_assets_dir", None, "Output assets directory")
+flags.DEFINE_string("input_aar", None, "Input AAR")
+flags.mark_flag_as_required("input_aar")
+flags.DEFINE_string("output_res_dir", None, "Output resources directory")
+flags.mark_flag_as_required("output_res_dir")
+flags.DEFINE_string("output_assets_dir", None, "Output assets directory")
def ExtractResources(aar, output_res_dir):
@@ -47,7 +56,8 @@
ExtractOneFile(aar, name, output_res_dir_abs)
aar_contains_no_resources = False
if aar_contains_no_resources:
- empty_xml_filename = output_res_dir + "/res/values/empty.xml"
+ empty_xml_filename = six.ensure_str(
+ output_res_dir) + "/res/values/empty.xml"
WriteFileWithJunctions(empty_xml_filename, b"<resources/>")
@@ -63,8 +73,9 @@
# aapt will ignore this file and not print an error message, because it
# thinks that it is a swap file. We need to create at least one file so that
# Bazel does not complain that the output tree artifact was not created.
- empty_asset_filename = (output_assets_dir +
- "/assets/empty_asset_generated_by_bazel~")
+ empty_asset_filename = (
+ six.ensure_str(output_assets_dir) +
+ "/assets/empty_asset_generated_by_bazel~")
WriteFileWithJunctions(empty_asset_filename, b"")
@@ -116,7 +127,7 @@
aar.extract(name, abs_output_dir)
-def main():
+def main(unused_argv):
with zipfile.ZipFile(FLAGS.input_aar, "r") as aar:
ExtractResources(aar, FLAGS.output_res_dir)
if FLAGS.output_assets_dir is not None:
@@ -124,4 +135,4 @@
if __name__ == "__main__":
FLAGS(sys.argv)
- main()
+ app.run(main)