Skip to content

Commit

Permalink
More updates to tools/android modules for compatibility with python 3.
Browse files Browse the repository at this point in the history
RELNOTES: None.
PiperOrigin-RevId: 179577497
  • Loading branch information
Googler authored and Copybara-Service committed Dec 19, 2017
1 parent 56d5ba1 commit 246ee56
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 78 deletions.
9 changes: 5 additions & 4 deletions tools/android/aar_embedded_jars_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ def ExtractEmbeddedJars(aar,
if not output_dir_orig:
output_dir_orig = output_dir
jar_pattern = re.compile("^(classes|libs/.+)\\.jar$")
singlejar_param_file.write("--exclude_build_data\n")
singlejar_param_file.write(b"--exclude_build_data\n")
for name in aar.namelist():
if jar_pattern.match(name):
singlejar_param_file.write("--sources\n")
singlejar_param_file.write(b"--sources\n")
# 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")
singlejar_param_file.write(
(output_dir_orig + "/" + name + "\n").encode("utf-8"))
aar.extract(name, output_dir)


Expand All @@ -62,7 +63,7 @@ def _Main(input_aar,
if not output_dir_orig:
output_dir_orig = output_dir
with zipfile.ZipFile(input_aar, "r") as aar:
with open(output_singlejar_param_file, "w") as singlejar_param_file:
with open(output_singlejar_param_file, "wb") as singlejar_param_file:
ExtractEmbeddedJars(aar, singlejar_param_file, output_dir,
output_dir_orig)

Expand Down
46 changes: 26 additions & 20 deletions tools/android/aar_embedded_jars_extractor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

"""Tests for aar_embedded_jars_extractor."""

import io
import os
import shutil
import StringIO
import unittest
import zipfile

Expand All @@ -26,54 +26,60 @@
class AarEmbeddedJarsExtractor(unittest.TestCase):
"""Unit tests for aar_embedded_jars_extractor.py."""

# Python 2 alias
if not hasattr(unittest.TestCase, "assertCountEqual"):

def assertCountEqual(self, *args):
return self.assertItemsEqual(*args)

def setUp(self):
os.chdir(os.environ["TEST_TMPDIR"])

def tearDown(self):
shutil.rmtree("out_dir")

def testNoJars(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
param_file = StringIO.StringIO()
aar = zipfile.ZipFile(io.BytesIO(), "w")
param_file = io.BytesIO()
os.makedirs("out_dir")
aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
self.assertEqual([], os.listdir("out_dir"))
param_file.seek(0)
self.assertEqual("--exclude_build_data\n", param_file.read())
self.assertEqual(b"--exclude_build_data\n", param_file.read())

def testClassesJarAndLibsJars(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
aar = zipfile.ZipFile(io.BytesIO(), "w")
aar.writestr("classes.jar", "")
aar.writestr("libs/a.jar", "")
aar.writestr("libs/b.jar", "")
param_file = StringIO.StringIO()
param_file = io.BytesIO()
os.makedirs("out_dir")
aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
self.assertItemsEqual(["classes.jar", "libs"], os.listdir("out_dir"))
self.assertItemsEqual(["a.jar", "b.jar"], os.listdir("out_dir/libs"))
self.assertCountEqual(["classes.jar", "libs"], os.listdir("out_dir"))
self.assertCountEqual(["a.jar", "b.jar"], os.listdir("out_dir/libs"))
param_file.seek(0)
self.assertEqual(
["--exclude_build_data\n",
"--sources\n",
"out_dir/classes.jar\n",
"--sources\n",
"out_dir/libs/a.jar\n",
"--sources\n",
"out_dir/libs/b.jar\n"],
[b"--exclude_build_data\n",
b"--sources\n",
b"out_dir/classes.jar\n",
b"--sources\n",
b"out_dir/libs/a.jar\n",
b"--sources\n",
b"out_dir/libs/b.jar\n"],
param_file.readlines())

def testOnlyClassesJar(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
aar = zipfile.ZipFile(io.BytesIO(), "w")
aar.writestr("classes.jar", "")
param_file = StringIO.StringIO()
param_file = io.BytesIO()
os.makedirs("out_dir")
aar_embedded_jars_extractor.ExtractEmbeddedJars(aar, param_file, "out_dir")
self.assertEqual(["classes.jar"], os.listdir("out_dir"))
param_file.seek(0)
self.assertEqual(
["--exclude_build_data\n",
"--sources\n",
"out_dir/classes.jar\n"],
[b"--exclude_build_data\n",
b"--sources\n",
b"out_dir/classes.jar\n"],
param_file.readlines())


Expand Down
6 changes: 4 additions & 2 deletions tools/android/aar_native_libs_zip_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
directory structure of /lib/<cpu>/foo.so.
"""

from __future__ import print_function

import os
import re
import sys
Expand Down Expand Up @@ -63,8 +65,8 @@ 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 " + input_aar_path_for_error_msg +
" missing native libs for requested architecture: " + cpu)
sys.exit(1)


Expand Down
14 changes: 7 additions & 7 deletions tools/android/aar_native_libs_zip_creator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""Tests for aar_native_libs_zip_creator."""

import StringIO
import io
import unittest
import zipfile

Expand All @@ -25,25 +25,25 @@ class AarNativeLibsZipCreatorTest(unittest.TestCase):
"""Unit tests for aar_native_libs_zip_creator.py."""

def testAarWithNoLibs(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
outzip = zipfile.ZipFile(StringIO.StringIO(), "w")
aar = zipfile.ZipFile(io.BytesIO(), "w")
outzip = zipfile.ZipFile(io.BytesIO(), "w")
aar_native_libs_zip_creator.CreateNativeLibsZip(aar, "x86", outzip)
self.assertEquals([], outzip.namelist())

def testAarWithMissingLibs(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
aar = zipfile.ZipFile(io.BytesIO(), "w")
aar.writestr("jni/armeabi/foo.so", "")
outzip = zipfile.ZipFile(StringIO.StringIO(), "w")
outzip = zipfile.ZipFile(io.BytesIO(), "w")
self.assertRaises(
aar_native_libs_zip_creator.UnsupportedArchitectureException,
aar_native_libs_zip_creator.CreateNativeLibsZip,
aar, "x86", outzip)

def testAarWithAllLibs(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
aar = zipfile.ZipFile(io.BytesIO(), "w")
aar.writestr("jni/x86/foo.so", "")
aar.writestr("jni/armeabi/foo.so", "")
outzip = zipfile.ZipFile(StringIO.StringIO(), "w")
outzip = zipfile.ZipFile(io.BytesIO(), "w")
aar_native_libs_zip_creator.CreateNativeLibsZip(aar, "x86", outzip)
self.assertIn("lib/x86/foo.so", outzip.namelist())
self.assertNotIn("lib/armeabi/foo.so", outzip.namelist())
Expand Down
4 changes: 2 additions & 2 deletions tools/android/aar_resources_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ def ExtractResources(aar, output_res_dir):
with junction.TempJunction(os.path.dirname(empty_xml_filename)) as junc:
xmlpath = os.path.join(junc, os.path.basename(empty_xml_filename))
with open(xmlpath, "wb") as empty_xml:
empty_xml.write("<resources/>")
empty_xml.write(b"<resources/>")
else:
os.makedirs(os.path.dirname(empty_xml_filename))
with open(empty_xml_filename, "wb") as empty_xml:
empty_xml.write("<resources/>")
empty_xml.write(b"<resources/>")


def main():
Expand Down
14 changes: 10 additions & 4 deletions tools/android/aar_resources_extractor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

"""Tests for aar_resources_extractor."""

import io
import os
import shutil
import StringIO
import unittest
import zipfile

Expand All @@ -30,6 +30,12 @@ def _HostPath(path):
class AarResourcesExtractorTest(unittest.TestCase):
"""Unit tests for aar_resources_extractor.py."""

# Python 2 alias
if not hasattr(unittest.TestCase, "assertCountEqual"):

def assertCountEqual(self, *args):
return self.assertItemsEqual(*args)

def setUp(self):
os.chdir(os.environ["TEST_TMPDIR"])

Expand All @@ -43,7 +49,7 @@ def DirContents(self, d):
]

def testNoResources(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
aar = zipfile.ZipFile(io.BytesIO(), "w")
os.makedirs("out_dir")
aar_resources_extractor.ExtractResources(aar, "out_dir")
self.assertEqual([_HostPath("out_dir/res/values/empty.xml")],
Expand All @@ -52,7 +58,7 @@ def testNoResources(self):
self.assertEqual("<resources/>", empty_xml.read())

def testContainsResources(self):
aar = zipfile.ZipFile(StringIO.StringIO(), "w")
aar = zipfile.ZipFile(io.BytesIO(), "w")
aar.writestr("res/values/values.xml", "some values")
aar.writestr("res/layouts/layout.xml", "some layout")
os.makedirs("out_dir")
Expand All @@ -61,7 +67,7 @@ def testContainsResources(self):
_HostPath("out_dir/res/values/values.xml"),
_HostPath("out_dir/res/layouts/layout.xml")
]
self.assertItemsEqual(expected_resources, self.DirContents("out_dir"))
self.assertCountEqual(expected_resources, self.DirContents("out_dir"))
with open("out_dir/res/values/values.xml", "r") as values_xml:
self.assertEqual("some values", values_xml.read())
with open("out_dir/res/layouts/layout.xml", "r") as layout_xml:
Expand Down
2 changes: 1 addition & 1 deletion tools/android/build_incremental_dexmanifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def Run(self, argv):
self.AddDex(input_filename, None, input_filename)

with open(argv[0], "wb") as manifest:
manifest.write("\n".join(self.manifest_lines))
manifest.write(("\n".join(self.manifest_lines)).encode("utf-8"))


def main(argv):
Expand Down
18 changes: 9 additions & 9 deletions tools/android/incremental_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def PushString(self, contents, remote):
"""Push a given string to a given path on the device in parallel."""
local = self._CreateLocalFile()
with open(local, "wb") as f:
f.write(contents)
f.write(contents.encode("utf-8"))
return self.Push(local, remote)

def Pull(self, remote):
Expand All @@ -237,7 +237,7 @@ def Pull(self, remote):
try:
self._Exec(["pull", remote, local])
with open(local, "rb") as f:
return f.read()
return f.read().decode("utf-8")
except (AdbError, IOError):
return None

Expand Down Expand Up @@ -339,7 +339,7 @@ def ParseManifest(contents):
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].strip()
return f.readlines()[1].decode("utf-8").strip()


def UploadDexes(adb, execroot, app_dir, temp_dir, dexmanifest, full_install):
Expand Down Expand Up @@ -605,7 +605,7 @@ def UploadNativeLibs(adb, native_lib_args, app_dir, full_install):
f.result()

install_manifest = [
name + " " + checksum for name, checksum in install_checksums.iteritems()]
name + " " + checksum for name, checksum in install_checksums.items()]
adb.PushString("\n".join(install_manifest),
targetpath.join(app_dir, "native",
"native_manifest")).result()
Expand Down Expand Up @@ -693,7 +693,7 @@ def SplitIncrementalInstall(adb, app_package, execroot, split_main_apk,
adb.InstallMultiple(targetpath.join(execroot, apk), app_package)

install_manifest = [
name + " " + checksum for name, checksum in install_checksums.iteritems()]
name + " " + checksum for name, checksum in install_checksums.items()]
adb.PushString("\n".join(install_manifest),
targetpath.join(app_dir, "split_manifest")).result()

Expand Down Expand Up @@ -744,7 +744,7 @@ def IncrementalInstall(adb_path,
VerifyInstallTimestamp(adb, app_package)

with open(hostpath.join(execroot, dexmanifest), "rb") as f:
dexmanifest = f.read()
dexmanifest = f.read().decode("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
Expand Down Expand Up @@ -776,16 +776,16 @@ def IncrementalInstall(adb_path,
sys.exit("Error: Device unauthorized. Please check the confirmation "
"dialog on your device.")
except MultipleDevicesError as e:
sys.exit("Error: " + e.message + "\nTry specifying a device serial with "
sys.exit("Error: " + str(e) + "\nTry specifying a device serial with "
"\"blaze mobile-install --adb_arg=-s --adb_arg=$ANDROID_SERIAL\"")
except OldSdkException as e:
sys.exit("Error: The device does not support the API level specified in "
"the application's manifest. Check minSdkVersion in "
"AndroidManifest.xml")
except TimestampException as e:
sys.exit("Error:\n%s" % e.message)
sys.exit("Error:\n%s" % str(e))
except AdbError as e:
sys.exit("Error:\n%s" % e.message)
sys.exit("Error:\n%s" % str(e))
finally:
shutil.rmtree(temp_dir, True)

Expand Down
Loading

0 comments on commit 246ee56

Please sign in to comment.