Skip to content

Commit

Permalink
py2to3: update tools/android/*.py to PY3
Browse files Browse the repository at this point in the history
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 #10127

PiperOrigin-RevId: 278320319
  • Loading branch information
laszlocsomor authored and copybara-github committed Nov 4, 2019
1 parent d6e6164 commit ad628ec
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 182 deletions.
3 changes: 3 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 9 additions & 28 deletions tools/android/BUILD
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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",
],
Expand All @@ -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",
Expand All @@ -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(
Expand All @@ -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",
],
)

Expand All @@ -104,7 +97,6 @@ py_test(
srcs = [
"aar_native_libs_zip_creator_test.py",
],
python_version = "PY2",
deps = [
":aar_native_libs_zip_creator",
],
Expand All @@ -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",
],
Expand All @@ -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"],
)

Expand All @@ -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",
Expand Down
28 changes: 20 additions & 8 deletions tools/android/BUILD.tools
Original file line number Diff line number Diff line change
Expand Up @@ -216,42 +216,47 @@ 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 = [],
)

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",
],
)

Expand All @@ -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"],
)

Expand Down
32 changes: 20 additions & 12 deletions tools/android/aar_embedded_jars_extractor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Lint as: python2, python3
# pylint: disable=g-direct-third-party-import
# Copyright 2016 The Bazel Authors. All rights reserved.
#
Expand All @@ -19,23 +20,30 @@
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

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,
Expand All @@ -52,7 +60,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)


Expand All @@ -68,7 +76,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
Expand All @@ -90,4 +98,4 @@ def main():

if __name__ == "__main__":
FLAGS(sys.argv)
main()
app.run(main)
Loading

0 comments on commit ad628ec

Please sign in to comment.