diff --git a/src/BUILD b/src/BUILD index 9a8fa0c1b2ddfb..4831949d95ceb6 100644 --- a/src/BUILD +++ b/src/BUILD @@ -127,7 +127,10 @@ JAVA_TOOLS = [ "//third_party/ijar:embedded_zipper_sources", "//third_party/ijar:zipper", "//third_party/java/j2objc:embedded_tools_srcs", + "//third_party/py/abseil:srcs", "//third_party/py/concurrent:srcs", + # TODO(laszlocsomor): delete "//third_party/py/gflags:srcs" after + # every script in @bazel_tools was migrated to use Abseil. "//third_party/py/gflags:srcs", "//third_party/py/six:srcs", "//src/conditions:embedded_tools", diff --git a/tools/android/BUILD b/tools/android/BUILD index d9ff665da9641e..44b782e3b363f5 100644 --- a/tools/android/BUILD +++ b/tools/android/BUILD @@ -1,11 +1,10 @@ -load("//tools/python:private/defs.bzl", "py_library", "py_binary", "py_test") +load("//tools/python:private/defs.bzl", "py_binary", "py_library", "py_test") package(default_visibility = ["//tools:__pkg__"]) py_binary( name = "build_incremental_dexmanifest", srcs = [":build_incremental_dexmanifest.py"], - python_version = "PY2", ) sh_test( @@ -27,16 +26,14 @@ sh_test( py_binary( name = "build_split_manifest", srcs = ["build_split_manifest.py"], - python_version = "PY2", deps = [ - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_test( name = "build_split_manifest_test", srcs = ["build_split_manifest_test.py"], - python_version = "PY2", deps = [ ":build_split_manifest", ], @@ -45,17 +42,15 @@ py_test( py_binary( name = "incremental_install", srcs = ["incremental_install.py"], - python_version = "PY2", deps = [ + "//third_party/py/abseil", "//third_party/py/concurrent:futures", - "//third_party/py/gflags", ], ) py_test( name = "incremental_install_test", srcs = ["incremental_install_test.py"], - python_version = "PY2", tags = [ # TODO(laszlocsomor): fix on Windows or describe why it cannot pass. "no_windows", @@ -69,8 +64,7 @@ py_test( py_binary( name = "strip_resources", srcs = ["strip_resources.py"], - python_version = "PY2", - deps = ["//third_party/py/gflags"], + deps = ["//third_party/py/abseil"], ) sh_test( @@ -92,10 +86,9 @@ py_binary( srcs = [ "aar_native_libs_zip_creator.py", ], - python_version = "PY2", deps = [ ":junction_lib", - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) @@ -104,7 +97,6 @@ py_test( srcs = [ "aar_native_libs_zip_creator_test.py", ], - python_version = "PY2", deps = [ ":aar_native_libs_zip_creator", ], @@ -113,16 +105,14 @@ py_test( py_binary( name = "stubify_manifest", srcs = ["stubify_manifest.py"], - python_version = "PY2", deps = [ - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_test( name = "stubify_manifest_test", srcs = ["stubify_manifest_test.py"], - python_version = "PY2", deps = [ ":stubify_manifest", ], @@ -131,61 +121,53 @@ py_test( py_binary( name = "aar_embedded_jars_extractor", srcs = ["aar_embedded_jars_extractor.py"], - python_version = "PY2", deps = [ ":junction_lib", - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_test( name = "aar_embedded_jars_extractor_test", srcs = ["aar_embedded_jars_extractor_test.py"], - python_version = "PY2", deps = [":aar_embedded_jars_extractor"], ) py_binary( name = "aar_resources_extractor", srcs = ["aar_resources_extractor.py"], - python_version = "PY2", deps = [ ":junction_lib", - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_test( name = "aar_resources_extractor_test", srcs = ["aar_resources_extractor_test.py"], - python_version = "PY2", deps = [":aar_resources_extractor"], ) py_binary( name = "resource_extractor", srcs = ["resource_extractor.py"], - python_version = "PY2", ) py_test( name = "resource_extractor_test", srcs = ["resource_extractor_test.py"], - python_version = "PY2", deps = [":resource_extractor"], ) py_binary( name = "instrumentation_test_check", srcs = ["instrumentation_test_check.py"], - python_version = "PY2", - deps = ["//third_party/py/gflags"], + deps = ["//third_party/py/abseil"], ) py_test( name = "instrumentation_test_check_test", srcs = ["instrumentation_test_check_test.py"], - python_version = "PY2", deps = [":instrumentation_test_check"], ) @@ -205,7 +187,6 @@ py_test( "//src/conditions:windows": "junction_test.py", "//conditions:default": "dummy_test.py", }), - python_version = "PY2", deps = [ ":junction_lib", "//src/test/py/bazel:test_base", diff --git a/tools/android/BUILD.tools b/tools/android/BUILD.tools index 4f5fc70357712b..02adb4676f0e4a 100644 --- a/tools/android/BUILD.tools +++ b/tools/android/BUILD.tools @@ -216,13 +216,15 @@ alias( py_binary( name = "instrumentation_test_check", srcs = ["instrumentation_test_check.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", - deps = ["//third_party/py/gflags"], + deps = ["//third_party/py/abseil"], ) py_binary( name = "build_incremental_dexmanifest", srcs = [":build_incremental_dexmanifest.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [], ) @@ -230,28 +232,31 @@ py_binary( py_binary( name = "build_split_manifest", srcs = ["build_split_manifest.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [ - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_binary( name = "incremental_install", srcs = ["incremental_install.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [ "//third_party/py/concurrent:futures", - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_binary( name = "strip_resources", srcs = ["strip_resources.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [ - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) @@ -260,51 +265,58 @@ py_binary( srcs = [ "aar_native_libs_zip_creator.py", ], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [ ":junction_lib", - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_binary( name = "stubify_manifest", srcs = ["stubify_manifest.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [ - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_binary( name = "aar_embedded_jars_extractor", srcs = ["aar_embedded_jars_extractor.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [ ":junction_lib", - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_binary( name = "aar_resources_extractor", srcs = ["aar_resources_extractor.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", deps = [ ":junction_lib", - "//third_party/py/gflags", + "//third_party/py/abseil", ], ) py_binary( name = "resource_extractor", srcs = ["resource_extractor.py"], + # TODO(bazel-team): remove python_version = "PY2" while fixing https://github.com/bazelbuild/bazel/issues/10127. python_version = "PY2", ) py_library( name = "junction_lib", srcs = ["junction.py"], + # TODO(bazel-team): remove srcs_version = "PY2AND3" while fixing https://github.com/bazelbuild/bazel/issues/10127. + srcs_version = "PY2AND3", visibility = ["//visibility:private"], ) diff --git a/tools/android/aar_embedded_jars_extractor.py b/tools/android/aar_embedded_jars_extractor.py index 2fa85b63a82d4a..d8f42d91978330 100644 --- a/tools/android/aar_embedded_jars_extractor.py +++ b/tools/android/aar_embedded_jars_extractor.py @@ -1,3 +1,4 @@ +# Lint as: python2, python3 # pylint: disable=g-direct-third-party-import # Copyright 2016 The Bazel Authors. All rights reserved. # @@ -19,23 +20,31 @@ of the jars and creates a param file for singlejar to merge them into one jar. """ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + import os import re 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_singlejar_param_file", None, "Output parameter file for singlejar") -gflags.MarkFlagAsRequired("output_singlejar_param_file") -gflags.DEFINE_string("output_dir", None, "Output directory to extract jars in") -gflags.MarkFlagAsRequired("output_dir") +flags.DEFINE_string("input_aar", None, "Input AAR") +flags.mark_flag_as_required("input_aar") +flags.DEFINE_string("output_singlejar_param_file", None, + "Output parameter file for singlejar") +flags.mark_flag_as_required("output_singlejar_param_file") +flags.DEFINE_string("output_dir", None, "Output directory to extract jars in") +flags.mark_flag_as_required("output_dir") def ExtractEmbeddedJars(aar, @@ -52,7 +61,7 @@ def ExtractEmbeddedJars(aar, # output_dir may be a temporary junction, so write the original # (unshortened) path to the params file singlejar_param_file.write( - (output_dir_orig + "/" + name + "\n").encode("utf-8")) + six.ensure_binary((output_dir_orig + "/" + name + "\n"), "utf-8")) aar.extract(name, output_dir) @@ -68,7 +77,7 @@ def _Main(input_aar, output_dir_orig) -def main(): +def main(unused_argv): if os.name == "nt": # Shorten paths unconditionally, because the extracted paths in # ExtractEmbeddedJars (which we cannot yet predict, because they depend on @@ -90,4 +99,4 @@ def main(): if __name__ == "__main__": FLAGS(sys.argv) - main() + app.run(main) diff --git a/tools/android/aar_native_libs_zip_creator.py b/tools/android/aar_native_libs_zip_creator.py index 0348418fb5616c..59c3d7b8a1bff1 100644 --- a/tools/android/aar_native_libs_zip_creator.py +++ b/tools/android/aar_native_libs_zip_creator.py @@ -1,3 +1,4 @@ +# Lint as: python2, python3 # pylint: disable=g-direct-third-party-import # Copyright 2016 The Bazel Authors. All rights reserved. # @@ -20,6 +21,8 @@ directory structure of /lib//foo.so. """ +from __future__ import absolute_import +from __future__ import division from __future__ import print_function import os @@ -27,17 +30,21 @@ 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("cpu", None, "CPU architecture to include") -gflags.MarkFlagAsRequired("cpu") -gflags.DEFINE_string("output_zip", None, "Output ZIP of native libs") -gflags.MarkFlagAsRequired("output_zip") +flags.DEFINE_string("input_aar", None, "Input AAR") +flags.mark_flag_as_required("input_aar") +flags.DEFINE_string("cpu", None, "CPU architecture to include") +flags.mark_flag_as_required("cpu") +flags.DEFINE_string("output_zip", None, "Output ZIP of native libs") +flags.mark_flag_as_required("output_zip") class UnsupportedArchitectureException(Exception): @@ -48,7 +55,7 @@ class UnsupportedArchitectureException(Exception): def CreateNativeLibsZip(aar, cpu, native_libs_zip): native_lib_pattern = re.compile("^jni/.+/.+\\.so$") if any(native_lib_pattern.match(filename) for filename in aar.namelist()): - cpu_pattern = re.compile("^jni/" + cpu + "/.+\\.so$") + cpu_pattern = re.compile("^jni/" + six.ensure_str(cpu) + "/.+\\.so$") libs = [name for name in aar.namelist() if cpu_pattern.match(name)] if not libs: raise UnsupportedArchitectureException() @@ -65,12 +72,13 @@ def Main(input_aar_path, output_zip_path, cpu, input_aar_path_for_error_msg): try: CreateNativeLibsZip(input_aar, cpu, native_libs_zip) except UnsupportedArchitectureException: - print("AAR " + input_aar_path_for_error_msg + - " missing native libs for requested architecture: " + cpu) + print("AAR " + six.ensure_str(input_aar_path_for_error_msg) + + " missing native libs for requested architecture: " + + six.ensure_str(cpu)) sys.exit(1) -def main(): +def main(unused_argv): if os.name == "nt": with junction.TempJunction(os.path.dirname(FLAGS.input_aar)) as j_in: with junction.TempJunction(os.path.dirname(FLAGS.output_zip)) as j_out: @@ -84,4 +92,4 @@ def main(): if __name__ == "__main__": FLAGS(sys.argv) - main() + app.run(main) diff --git a/tools/android/aar_resources_extractor.py b/tools/android/aar_resources_extractor.py index b4c85774d2b42d..29d2b4b047d269 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 @@ def ExtractResources(aar, output_res_dir): 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"") @@ -63,8 +73,9 @@ def ExtractAssets(aar, output_assets_dir): # 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 @@ def ExtractOneFile(aar, name, abs_output_dir): 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 @@ def main(): if __name__ == "__main__": FLAGS(sys.argv) - main() + app.run(main) diff --git a/tools/android/aar_resources_extractor_test.py b/tools/android/aar_resources_extractor_test.py index bce4a3dbee1949..9ec3a99390fb81 100644 --- a/tools/android/aar_resources_extractor_test.py +++ b/tools/android/aar_resources_extractor_test.py @@ -1,3 +1,4 @@ +# Lint as: python2, python3 # Copyright 2017 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,12 +15,19 @@ """Tests for aar_resources_extractor.""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + import io import os import shutil import unittest import zipfile +# Do not edit this line. Copybara replaces it with PY2 migration helper. +import six + from tools.android import aar_resources_extractor @@ -44,8 +52,9 @@ def tearDown(self): def DirContents(self, d): return [ - _HostPath(path + "/" + f) - for (path, _, files) in os.walk(d) for f in files + _HostPath(six.ensure_str(path) + "/" + six.ensure_str(f)) + for (path, _, files) in os.walk(d) + for f in files ] def testNoResources(self): diff --git a/tools/android/build_split_manifest.py b/tools/android/build_split_manifest.py index 8c4529ddfd0b88..272853b3a2551a 100644 --- a/tools/android/build_split_manifest.py +++ b/tools/android/build_split_manifest.py @@ -1,3 +1,4 @@ +# Lint as: python2, python3 # pylint: disable=g-direct-third-party-import # Copyright 2015 The Bazel Authors. All rights reserved. # @@ -24,21 +25,22 @@ Writes the old application class into the file designated by the third argument. """ -import sys from xml.etree import ElementTree -from third_party.py import gflags +# Do not edit this line. Copybara replaces it with PY2 migration helper. +from absl import app +from absl import flags +flags.DEFINE_string("main_manifest", None, "The main manifest of the app") +flags.DEFINE_string("split_manifest", None, "The output manifest") +flags.DEFINE_string( + "override_package", None, + "The Android package. Override the one specified in the " + "input manifest") +flags.DEFINE_string("split", None, "The name of the split") +flags.DEFINE_boolean("hascode", False, "Whether this split .apk has dexes") -gflags.DEFINE_string("main_manifest", None, "The main manifest of the app") -gflags.DEFINE_string("split_manifest", None, "The output manifest") -gflags.DEFINE_string("override_package", None, - "The Android package. Override the one specified in the " - "input manifest") -gflags.DEFINE_string("split", None, "The name of the split") -gflags.DEFINE_boolean("hascode", False, "Whether this split .apk has dexes") - -FLAGS = gflags.FLAGS +FLAGS = flags.FLAGS MANIFEST_TEMPLATE = """