Android,tools: open all files in binary mode
Always open files in binary mode to avoid
automatic conversion between LF and CRLF on
Windows, which is particularly problematic when a
file is written on Windows but consumed on Android
or when a binary file is opened for reading in
text mode and if it happens to have an LF byte it
would be converted to CRLF.
See https://github.com/bazelbuild/bazel/issues/3264
Change-Id: I4d9d885a488b9693eeb3f6d929e3396ef8406d62
PiperOrigin-RevId: 164826587
diff --git a/tools/android/aar_embedded_jars_extractor.py b/tools/android/aar_embedded_jars_extractor.py
index e631c48..305d38a 100644
--- a/tools/android/aar_embedded_jars_extractor.py
+++ b/tools/android/aar_embedded_jars_extractor.py
@@ -1,3 +1,4 @@
+# pylint: disable=g-direct-third-party-import
# Copyright 2016 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -46,8 +47,8 @@
def main():
- with zipfile.ZipFile(FLAGS.input_aar, "r") as aar:
- with open(FLAGS.output_singlejar_param_file, "w") as singlejar_param_file:
+ with zipfile.ZipFile(FLAGS.input_aar, "rb") as aar:
+ with open(FLAGS.output_singlejar_param_file, "wb") as singlejar_param_file:
ExtractEmbeddedJars(aar, singlejar_param_file, FLAGS.output_dir)
if __name__ == "__main__":
diff --git a/tools/android/build_incremental_dexmanifest.py b/tools/android/build_incremental_dexmanifest.py
index 2fe4bfa..6c372656 100644
--- a/tools/android/build_incremental_dexmanifest.py
+++ b/tools/android/build_incremental_dexmanifest.py
@@ -58,7 +58,7 @@
def Checksum(self, filename):
"""Compute the SHA-256 checksum of a file."""
h = hashlib.sha256()
- with file(filename, "r") as f:
+ with open(filename, "rb") as f:
while True:
data = f.read(65536)
if not data:
@@ -99,7 +99,7 @@
if argv[0][0] == "@":
if len(argv) != 1:
raise IOError("A parameter file should be the only argument")
- with file(argv[0][1:]) as param_file:
+ with open(argv[0][1:]) as param_file:
argv = [a.strip() for a in param_file.readlines()]
for input_filename in argv[1:]:
@@ -120,7 +120,7 @@
elif input_filename.endswith(".dex"):
self.AddDex(input_filename, None, input_filename)
- with file(argv[0], "w") as manifest:
+ with open(argv[0], "wb") as manifest:
manifest.write("\n".join(self.manifest_lines))
diff --git a/tools/android/build_split_manifest.py b/tools/android/build_split_manifest.py
index c791e3a..8c4529d 100644
--- a/tools/android/build_split_manifest.py
+++ b/tools/android/build_split_manifest.py
@@ -1,3 +1,4 @@
+# pylint: disable=g-direct-third-party-import
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -92,12 +93,10 @@
def main():
split_manifest = BuildSplitManifest(
- file(FLAGS.main_manifest).read(),
- FLAGS.override_package,
- FLAGS.split,
- FLAGS.hascode)
+ open(FLAGS.main_manifest, "rb").read(), FLAGS.override_package,
+ FLAGS.split, FLAGS.hascode)
- with file(FLAGS.split_manifest, "w") as output_xml:
+ with open(FLAGS.split_manifest, "wb") as output_xml:
output_xml.write(split_manifest)
diff --git a/tools/android/incremental_install.py b/tools/android/incremental_install.py
index b1b91c9..12a2c7e 100644
--- a/tools/android/incremental_install.py
+++ b/tools/android/incremental_install.py
@@ -218,7 +218,7 @@
def PushString(self, contents, remote):
"""Push a given string to a given path on the device in parallel."""
local = self._CreateLocalFile()
- with file(local, "w") as f:
+ with open(local, "wb") as f:
f.write(contents)
return self.Push(local, remote)
@@ -234,7 +234,7 @@
local = self._CreateLocalFile()
try:
self._Exec(["pull", remote, local])
- with file(local) as f:
+ with open(local, "rb") as f:
return f.read()
except (AdbError, IOError):
return None
@@ -336,7 +336,7 @@
def GetAppPackage(stub_datafile):
"""Returns the app package specified in a stub data file."""
- with file(stub_datafile) as f:
+ with open(stub_datafile, "rb") as f:
return f.readlines()[1].strip()
@@ -455,7 +455,7 @@
def Checksum(filename):
"""Compute the SHA-256 checksum of a file."""
h = hashlib.sha256()
- with file(filename, "r") as f:
+ with open(filename, "rb") as f:
while True:
data = f.read(65536)
if not data:
@@ -731,7 +731,7 @@
if not apk:
VerifyInstallTimestamp(adb, app_package)
- with file(hostpath.join(execroot, dexmanifest)) as f:
+ with open(hostpath.join(execroot, dexmanifest), "rb") as f:
dexmanifest = f.read()
UploadDexes(adb, execroot, app_dir, temp_dir, dexmanifest, bool(apk))
# TODO(ahumesky): UploadDexes waits for all the dexes to be uploaded, and
@@ -756,7 +756,7 @@
logging.info("Starting application %s", app_package)
adb.StartApp(app_package, start_type)
- with file(output_marker, "w") as _:
+ with open(output_marker, "wb") as _:
pass
except DeviceNotFoundError:
sys.exit("Error: Device not found")
@@ -811,7 +811,7 @@
FLAGS(sys.argv)
# process any additional flags in --flagfile
if FLAGS.flagfile:
- with open(FLAGS.flagfile) as flagsfile:
+ with open(FLAGS.flagfile, "rb") as flagsfile:
FLAGS.Reset()
FLAGS(sys.argv + [line.strip() for line in flagsfile.readlines()])
diff --git a/tools/android/incremental_install_test.py b/tools/android/incremental_install_test.py
index 46ceb3e..1181a59 100644
--- a/tools/android/incremental_install_test.py
+++ b/tools/android/incremental_install_test.py
@@ -1,3 +1,4 @@
+# pylint: disable=g-direct-third-party-import
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -47,7 +48,7 @@
cmd = args[1]
if cmd == "push":
# "/test/adb push local remote"
- with open(args[2]) as f:
+ with open(args[2], "rb") as f:
content = f.read()
self.files[args[3]] = content
elif cmd == "pull":
@@ -56,7 +57,7 @@
local = args[3]
content = self.files.get(remote)
if content is not None:
- with open(local, "w") as f:
+ with open(local, "wb") as f:
f.write(content)
else:
returncode = 1
@@ -67,7 +68,7 @@
return self._CreatePopenMock(0, "Success", "")
elif cmd == "install-multiple":
if args[3] == "-p":
- with open(args[5]) as f:
+ with open(args[5], "rb") as f:
content = f.read()
self.split_apks.add(content)
else:
@@ -131,11 +132,11 @@
self._mock_adb = MockAdb()
# Write the stub datafile which contains the package name of the app.
- with open(self._STUB_DATAFILE, "w") as f:
+ with open(self._STUB_DATAFILE, "wb") as f:
f.write("\n".join([self._OLD_APP_PACKGE, self._APP_PACKAGE]))
# Write the local resource apk file.
- with open(self._RESOURCE_APK, "w") as f:
+ with open(self._RESOURCE_APK, "wb") as f:
f.write("resource apk")
# Mock out subprocess.Popen to use our mock adb.
@@ -155,7 +156,7 @@
def _CreateLocalManifest(self, *lines):
content = "\n".join(lines)
- with open(self._DEXMANIFEST, "w") as f:
+ with open(self._DEXMANIFEST, "wb") as f:
f.write(content)
return content
@@ -203,7 +204,7 @@
def testUploadToPristineDevice(self):
self._CreateZip()
- with open("dex1", "w") as f:
+ with open("dex1", "wb") as f:
f.write("content3")
manifest = self._CreateLocalManifest(
@@ -215,119 +216,119 @@
resources_checksum_path = self._GetDeviceAppPath("resources_checksum")
self.assertTrue(resources_checksum_path in self._mock_adb.files)
- self.assertEquals(manifest, self._GetDeviceFile("dex/manifest"))
- self.assertEquals("content1", self._GetDeviceFile("dex/ip1"))
- self.assertEquals("content2", self._GetDeviceFile("dex/ip2"))
- self.assertEquals("content3", self._GetDeviceFile("dex/ip3"))
- self.assertEquals("resource apk", self._GetDeviceFile("resources.ap_"))
+ self.assertEqual(manifest, self._GetDeviceFile("dex/manifest"))
+ self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
+ self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
+ self.assertEqual("content3", self._GetDeviceFile("dex/ip3"))
+ self.assertEqual("resource apk", self._GetDeviceFile("resources.ap_"))
def testSplitInstallToPristineDevice(self):
- with open("split1", "w") as f:
+ with open("split1", "wb") as f:
f.write("split_content1")
- with open("main", "w") as f:
+ with open("main", "wb") as f:
f.write("main_Content")
self._CallIncrementalInstall(
incremental=False, split_main_apk="main", split_apks=["split1"])
- self.assertEquals(set(["split_content1"]), self._mock_adb.split_apks)
+ self.assertEqual(set(["split_content1"]), self._mock_adb.split_apks)
def testSplitInstallUnchanged(self):
- with open("split1", "w") as f:
+ with open("split1", "wb") as f:
f.write("split_content1")
- with open("main", "w") as f:
+ with open("main", "wb") as f:
f.write("main_Content")
self._CallIncrementalInstall(
incremental=False, split_main_apk="main", split_apks=["split1"])
- self.assertEquals(set(["split_content1"]), self._mock_adb.split_apks)
+ self.assertEqual(set(["split_content1"]), self._mock_adb.split_apks)
self._mock_adb.split_apks = set()
self._CallIncrementalInstall(
incremental=False, split_main_apk="main", split_apks=["split1"])
- self.assertEquals(set([]), self._mock_adb.split_apks)
+ self.assertEqual(set([]), self._mock_adb.split_apks)
def testSplitInstallChanges(self):
- with open("split1", "w") as f:
+ with open("split1", "wb") as f:
f.write("split_content1")
- with open("main", "w") as f:
+ with open("main", "wb") as f:
f.write("main_Content")
self._CallIncrementalInstall(
incremental=False, split_main_apk="main", split_apks=["split1"])
- self.assertEquals(set(["split_content1"]), self._mock_adb.split_apks)
+ self.assertEqual(set(["split_content1"]), self._mock_adb.split_apks)
- with open("split1", "w") as f:
+ with open("split1", "wb") as f:
f.write("split_content2")
self._mock_adb.split_apks = set()
self._CallIncrementalInstall(
incremental=False, split_main_apk="main", split_apks=["split1"])
- self.assertEquals(set(["split_content2"]), self._mock_adb.split_apks)
+ self.assertEqual(set(["split_content2"]), self._mock_adb.split_apks)
def testMissingNativeManifestWithIncrementalInstall(self):
self._CreateZip()
- with open("liba.so", "w") as f:
+ with open("liba.so", "wb") as f:
f.write("liba_1")
# Upload a library to the device.
native_libs = ["armeabi-v7a:liba.so"]
self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
- self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so"))
+ self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
# Delete the manifest, overwrite the library and check that even an
# incremental install straightens things out.
self._PutDeviceFile("native/liba.so", "GARBAGE")
self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
- self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so"))
+ self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
def testNonIncrementalInstallOverwritesNativeLibs(self):
self._CreateZip()
- with open("liba.so", "w") as f:
+ with open("liba.so", "wb") as f:
f.write("liba_1")
# Upload a library to the device.
native_libs = ["armeabi-v7a:liba.so"]
self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
- self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so"))
+ self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
# Change a library on the device. Incremental install should not replace the
# changed file, because it only checks the manifest.
self._PutDeviceFile("native/liba.so", "GARBAGE")
self._CallIncrementalInstall(incremental=True, native_libs=native_libs)
- self.assertEquals("GARBAGE", self._GetDeviceFile("native/liba.so"))
+ self.assertEqual("GARBAGE", self._GetDeviceFile("native/liba.so"))
# However, a full install should overwrite it.
self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
- self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so"))
+ self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
def testNativeAbiCompatibility(self):
self._CreateZip()
- with open("liba.so", "w") as f:
+ with open("liba.so", "wb") as f:
f.write("liba")
native_libs = ["armeabi:liba.so"]
self._mock_adb.SetAbi("arm64-v8a")
self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
- self.assertEquals("liba", self._GetDeviceFile("native/liba.so"))
+ self.assertEqual("liba", self._GetDeviceFile("native/liba.so"))
def testUploadNativeLibs(self):
self._CreateZip()
- with open("liba.so", "w") as f:
+ with open("liba.so", "wb") as f:
f.write("liba_1")
- with open("libb.so", "w") as f:
+ with open("libb.so", "wb") as f:
f.write("libb_1")
native_libs = ["armeabi-v7a:liba.so", "armeabi-v7a:libb.so"]
self._CallIncrementalInstall(incremental=False, native_libs=native_libs)
- self.assertEquals("liba_1", self._GetDeviceFile("native/liba.so"))
- self.assertEquals("libb_1", self._GetDeviceFile("native/libb.so"))
+ self.assertEqual("liba_1", self._GetDeviceFile("native/liba.so"))
+ self.assertEqual("libb_1", self._GetDeviceFile("native/libb.so"))
# Change a library
- with open("libb.so", "w") as f:
+ with open("libb.so", "wb") as f:
f.write("libb_2")
self._CallIncrementalInstall(incremental=True, native_libs=native_libs)
- self.assertEquals("libb_2", self._GetDeviceFile("native/libb.so"))
+ self.assertEqual("libb_2", self._GetDeviceFile("native/libb.so"))
# Delete a library
self._CallIncrementalInstall(
@@ -337,7 +338,7 @@
# Add the deleted library back
self._CallIncrementalInstall(incremental=True, native_libs=native_libs)
- self.assertEquals("libb_2", self._GetDeviceFile("native/libb.so"))
+ self.assertEqual("libb_2", self._GetDeviceFile("native/libb.so"))
def testUploadWithOneChangedFile(self):
# Existing manifest from a previous install.
@@ -366,8 +367,8 @@
# We just want to make sure that only one file was updated, so to
# distinguish that we force the local and remote content to be different but
# keep the checksum the same.
- self.assertEquals("old content1", self._GetDeviceFile("dex/ip1"))
- self.assertEquals("content2", self._GetDeviceFile("dex/ip2"))
+ self.assertEqual("old content1", self._GetDeviceFile("dex/ip1"))
+ self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
def testFullUploadWithOneChangedFile(self):
@@ -392,8 +393,8 @@
# Even though the checksums for ip1 were the same, the file still got
# updated. This is a bit of a dishonest test because the local and remote
# content for ip1 were different, but their checksums were the same.
- self.assertEquals("content1", self._GetDeviceFile("dex/ip1"))
- self.assertEquals("content2", self._GetDeviceFile("dex/ip2"))
+ self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
+ self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
def testUploadWithNewFile(self):
@@ -410,8 +411,8 @@
self._CallIncrementalInstall(incremental=True)
- self.assertEquals("content1", self._GetDeviceFile("dex/ip1"))
- self.assertEquals("content2", self._GetDeviceFile("dex/ip2"))
+ self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
+ self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
def testDeletesFile(self):
@@ -448,9 +449,9 @@
"dex1 - ip3 0")
self._CallIncrementalInstall(incremental=True)
- self.assertEquals("content1", self._GetDeviceFile("dex/ip1"))
- self.assertEquals("content2", self._GetDeviceFile("dex/ip2"))
- self.assertEquals("content3", self._GetDeviceFile("dex/ip3"))
+ self.assertEqual("content1", self._GetDeviceFile("dex/ip1"))
+ self.assertEqual("content2", self._GetDeviceFile("dex/ip2"))
+ self.assertEqual("content3", self._GetDeviceFile("dex/ip3"))
def testNoResourcesToUpdate(self):
self._CreateRemoteManifest("zip1 zp1 ip1 0")
@@ -468,7 +469,7 @@
self._CreateLocalManifest("zip1 zp1 ip1 0")
self._CallIncrementalInstall(incremental=True)
- self.assertEquals("resources", self._GetDeviceFile("resources.ap_"))
+ self.assertEqual("resources", self._GetDeviceFile("resources.ap_"))
def testUpdateResources(self):
self._CreateRemoteManifest("zip1 zp1 ip1 0")
@@ -482,7 +483,7 @@
self._CreateLocalManifest("zip1 zp1 ip1 0")
self._CallIncrementalInstall(incremental=True)
- self.assertEquals("resource apk", self._GetDeviceFile("resources.ap_"))
+ self.assertEqual("resource apk", self._GetDeviceFile("resources.ap_"))
def testNoDevice(self):
self._mock_adb.SetError(1, "", "device not found")
@@ -518,7 +519,7 @@
# Based on testUploadToPristineDevice
self._CreateZip()
- with open("dex1", "w") as f:
+ with open("dex1", "wb") as f:
f.write("content3")
self._CreateLocalManifest(
@@ -534,7 +535,7 @@
def testDebugStart(self):
self._CreateZip()
- with open("dex1", "w") as f:
+ with open("dex1", "wb") as f:
f.write("content3")
self._CreateLocalManifest(
diff --git a/tools/android/merge_manifests.py b/tools/android/merge_manifests.py
index e5b093a..880c55f 100644
--- a/tools/android/merge_manifests.py
+++ b/tools/android/merge_manifests.py
@@ -1,3 +1,4 @@
+# pylint: disable=g-direct-third-party-import
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -121,7 +122,7 @@
if self._exclude_permissions:
exclude_all_permissions = EXCLUDE_ALL_ARG in self._exclude_permissions
for element in (dom.getElementsByTagName(self._USES_PERMISSION) +
- dom.getElementsByTagName(self._USES_PERMISSION_SDK_23)):
+ dom.getElementsByTagName(self._USES_PERMISSION_SDK_23)):
if element.hasAttribute(self._ANDROID_NAME):
attrib = element.getAttribute(self._ANDROID_NAME)
if exclude_all_permissions or attrib in self._exclude_permissions:
@@ -424,7 +425,7 @@
def _ReadFile(file_name):
- with open(file_name, 'r') as my_file:
+ with open(file_name, 'rb') as my_file:
return (my_file.read(), file_name,)
@@ -455,7 +456,7 @@
FLAGS.exclude_permission
).Merge()
- with open(FLAGS.output, 'w') as out_file:
+ with open(FLAGS.output, 'wb') as out_file:
for line in merged_manifests.split('\n'):
if not line.strip():
continue
diff --git a/tools/android/stubify_manifest.py b/tools/android/stubify_manifest.py
index 7e48d84..f415774 100644
--- a/tools/android/stubify_manifest.py
+++ b/tools/android/stubify_manifest.py
@@ -138,24 +138,24 @@
def main():
if FLAGS.mode == "mobile_install":
- with file(FLAGS.input_manifest) as input_manifest:
+ with open(FLAGS.input_manifest, "rb") as input_manifest:
new_manifest, old_application, app_package = (
StubifyMobileInstall(input_manifest.read()))
if FLAGS.override_package:
app_package = FLAGS.override_package
- with file(FLAGS.output_manifest, "w") as output_xml:
+ with open(FLAGS.output_manifest, "wb") as output_xml:
output_xml.write(new_manifest)
- with file(FLAGS.output_datafile, "wb") as output_file:
+ with open(FLAGS.output_datafile, "wb") as output_file:
output_file.write("\n".join([old_application, app_package]))
elif FLAGS.mode == "instant_run":
- with file(FLAGS.input_manifest) as input_manifest:
+ with open(FLAGS.input_manifest, "rb") as input_manifest:
new_manifest = StubifyInstantRun(input_manifest.read())
- with file(FLAGS.output_manifest, "w") as output_xml:
+ with open(FLAGS.output_manifest, "wb") as output_xml:
output_xml.write(new_manifest)