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/incremental_install.py b/tools/android/incremental_install.py
index d7b9c48..225f9d0 100644
--- a/tools/android/incremental_install.py
+++ b/tools/android/incremental_install.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
# pylint: disable=g-direct-third-party-import
# pylint: disable=g-bad-file-header
# Copyright 2015 The Bazel Authors. All rights reserved.
@@ -16,7 +17,12 @@
"""Installs an Android application, possibly in an incremental way."""
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
import collections
+from concurrent import futures
import hashlib
import logging
import os
@@ -29,38 +35,41 @@
import time
import zipfile
-from third_party.py import gflags
-from third_party.py.concurrent import futures
+# Do not edit this line. Copybara replaces it with PY2 migration helper.
+from absl import app
+from absl import flags
+import six
+flags.DEFINE_string("split_main_apk", None, "The main APK for split install")
+flags.DEFINE_multi_string("split_apk", [], "Split APKs to install")
+flags.DEFINE_string("dexmanifest", None, "The .dex manifest")
+flags.DEFINE_multi_string("native_lib", None, "Native libraries to install")
+flags.DEFINE_string("resource_apk", None, "The resource .apk")
+flags.DEFINE_string(
+ "apk", None, "The app .apk. If not specified, "
+ "do incremental deployment")
+flags.DEFINE_string("adb", None, "ADB to use")
+flags.DEFINE_string("stub_datafile", None, "The stub data file")
+flags.DEFINE_string("output_marker", None, "The output marker file")
+flags.DEFINE_multi_string("extra_adb_arg", [], "Extra arguments to adb")
+flags.DEFINE_string("execroot", ".", "The exec root")
+flags.DEFINE_integer(
+ "adb_jobs",
+ 2, "The number of instances of adb to use in parallel to "
+ "update files on the device",
+ lower_bound=1)
+flags.DEFINE_enum(
+ "start", "no", ["no", "cold", "warm", "debug"],
+ "Whether/how to start the app after installing it. 'cold' "
+ "and 'warm' will both cause the app to be started, 'warm' "
+ "will start it with previously saved application state, "
+ "'debug' will wait for the debugger before a clean start.")
+flags.DEFINE_boolean("start_app", False, "Deprecated, use 'start'.")
+flags.DEFINE_string("user_home_dir", None, "Path to the user's home directory")
+flags.DEFINE_string("flagfile", None,
+ "Path to a file to read additional flags from")
-gflags.DEFINE_string("split_main_apk", None, "The main APK for split install")
-gflags.DEFINE_multistring("split_apk", [], "Split APKs to install")
-gflags.DEFINE_string("dexmanifest", None, "The .dex manifest")
-gflags.DEFINE_multistring("native_lib", None, "Native libraries to install")
-gflags.DEFINE_string("resource_apk", None, "The resource .apk")
-gflags.DEFINE_string("apk", None, "The app .apk. If not specified, "
- "do incremental deployment")
-gflags.DEFINE_string("adb", None, "ADB to use")
-gflags.DEFINE_string("stub_datafile", None, "The stub data file")
-gflags.DEFINE_string("output_marker", None, "The output marker file")
-gflags.DEFINE_multistring("extra_adb_arg", [], "Extra arguments to adb")
-gflags.DEFINE_string("execroot", ".", "The exec root")
-gflags.DEFINE_integer("adb_jobs", 2,
- "The number of instances of adb to use in parallel to "
- "update files on the device",
- lower_bound=1)
-gflags.DEFINE_enum("start", "no", ["no", "cold", "warm", "debug"],
- "Whether/how to start the app after installing it. 'cold' "
- "and 'warm' will both cause the app to be started, 'warm' "
- "will start it with previously saved application state, "
- "'debug' will wait for the debugger before a clean start.")
-gflags.DEFINE_boolean("start_app", False, "Deprecated, use 'start'.")
-gflags.DEFINE_string("user_home_dir", None, "Path to the user's home directory")
-gflags.DEFINE_string("flagfile", None,
- "Path to a file to read additional flags from")
-gflags.DEFINE_string("verbosity", None, "Logging verbosity")
-
-FLAGS = gflags.FLAGS
+FLAGS = flags.FLAGS
DEVICE_DIRECTORY = "/data/local/tmp/incrementaldeployment"
@@ -173,8 +182,8 @@
# Check these first so that the more specific error gets raised instead of
# the more generic AdbError.
- stdout = stdout.decode()
- stderr = stderr.decode()
+ stdout = six.ensure_str(stdout)
+ stderr = six.ensure_str(stderr)
if "device not found" in stderr:
raise DeviceNotFoundError()
elif "device unauthorized" in stderr:
@@ -204,7 +213,8 @@
def GetInstallTime(self, package):
"""Get the installation time of a package."""
_, stdout, _, _ = self._Shell("dumpsys package %s" % package)
- match = re.search("firstInstallTime=(.*)$", stdout, re.MULTILINE)
+ match = re.search("firstInstallTime=(.*)$", six.ensure_str(stdout),
+ re.MULTILINE)
if match:
return match.group(1)
else:
@@ -239,7 +249,7 @@
try:
self._Exec(["pull", remote, local])
with open(local, "rb") as f:
- return f.read().decode("utf-8")
+ return six.ensure_str(f.read(), "utf-8")
except (AdbError, IOError):
return None
@@ -341,7 +351,7 @@
def GetAppPackage(stub_datafile):
"""Returns the app package specified in a stub data file."""
with open(stub_datafile, "rb") as f:
- return f.readlines()[1].decode("utf-8").strip()
+ return six.ensure_str(f.readlines()[1], "utf-8").strip()
def UploadDexes(adb, execroot, app_dir, temp_dir, dexmanifest, full_install):
@@ -507,7 +517,7 @@
native_libs = {}
if args is not None:
for native_lib in args:
- abi, path = native_lib.split(":")
+ abi, path = six.ensure_str(native_lib).split(":")
if abi not in native_libs:
native_libs[abi] = set()
@@ -539,7 +549,7 @@
native_libs = ConvertNativeLibs(native_lib_args)
libs = set()
if native_libs:
- abi = FindAbi(adb.GetAbi(), native_libs.keys())
+ abi = FindAbi(adb.GetAbi(), list(native_libs.keys()))
if abi:
libs = native_libs[abi]
@@ -613,7 +623,9 @@
f.result()
install_manifest = [
- name + " " + checksum for name, checksum in install_checksums.items()]
+ six.ensure_str(name) + " " + checksum
+ for name, checksum in install_checksums.items()
+ ]
adb.PushString("\n".join(install_manifest),
targetpath.join(app_dir, "native",
"native_manifest")).result()
@@ -701,7 +713,9 @@
adb.InstallMultiple(targetpath.join(execroot, apk), app_package)
install_manifest = [
- name + " " + checksum for name, checksum in install_checksums.items()]
+ six.ensure_str(name) + " " + checksum
+ for name, checksum in install_checksums.items()
+ ]
adb.PushString("\n".join(install_manifest),
targetpath.join(app_dir, "split_manifest")).result()
@@ -752,7 +766,7 @@
VerifyInstallTimestamp(adb, app_package)
with open(hostpath.join(execroot, dexmanifest), "rb") as f:
- dexmanifest = f.read().decode("utf-8")
+ dexmanifest = six.ensure_str(f.read(), "utf-8")
UploadDexes(adb, execroot, app_dir, temp_dir, dexmanifest, bool(apk))
# TODO(ahumesky): UploadDexes waits for all the dexes to be uploaded, and
# then UploadResources is called. We could instead enqueue everything
@@ -798,8 +812,8 @@
shutil.rmtree(temp_dir, True)
-def main():
- if FLAGS.verbosity == "1":
+def main(unused_argv):
+ if FLAGS.verbosity == "1": # 'verbosity' flag is defined in absl.logging
level = logging.DEBUG
fmt = "%(levelname)-5s %(asctime)s %(module)s:%(lineno)3d] %(message)s"
else:
@@ -836,4 +850,4 @@
FLAGS.Reset()
FLAGS(sys.argv + [line.strip() for line in flagsfile.readlines()])
- main()
+ app.run(main)