Skip to content

Commit

Permalink
Fix some python lint violations in aar-related tools
Browse files Browse the repository at this point in the history
Unblocks lint-related problems in #18496.

PiperOrigin-RevId: 553886150
Change-Id: Ie7f5d369def2b5556fefe24e78e7a57d94921766
  • Loading branch information
ted-xie authored and Copybara-Service committed Aug 4, 2023
1 parent 9d5a398 commit d0620d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions tools/android/aar_embedded_jars_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def ExtractEmbeddedJars(aar,
singlejar_param_file,
output_dir,
output_dir_orig=None):
"""Extracts all embedded jars from an AAR.
Args:
aar: The aar to extract from
singlejar_param_file: The param file to pass to singlejar
output_dir: Where to extract do
output_dir_orig: The original unshortened path to the params file
"""
if not output_dir_orig:
output_dir_orig = output_dir
jar_pattern = re.compile("^(classes|libs/.+)\\.jar$")
Expand Down
16 changes: 13 additions & 3 deletions tools/android/aar_native_libs_zip_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,28 @@
flags.mark_flag_as_required("output_zip")


class UnsupportedArchitectureException(Exception):
class UnsupportedArchitectureError(Exception):
"""Exception thrown when an AAR does not support the requested CPU."""
pass


def CreateNativeLibsZip(aar, cpu, native_libs_zip):
"""Creates a zip containing native libs for the requested CPU.
Args:
aar: aar file to extract
cpu: The requested CPU architecture
native_libs_zip: The zip file to package native libs into.
Raises:
UnsupportedArchitectureError: CPU architecture is invalid.
"""
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$")
libs = [name for name in aar.namelist() if cpu_pattern.match(name)]
if not libs:
raise UnsupportedArchitectureException()
raise UnsupportedArchitectureError()
for lib in libs:
# Only replaces the first instance of jni, in case the AAR contains
# something like /jni/x86/jni.so.
Expand All @@ -74,7 +84,7 @@ def Main(input_aar_path, output_zip_path, cpu, input_aar_path_for_error_msg):
with zipfile.ZipFile(output_zip_path, "w") as native_libs_zip:
try:
CreateNativeLibsZip(input_aar, cpu, native_libs_zip)
except UnsupportedArchitectureException:
except UnsupportedArchitectureError:
print("AAR " + input_aar_path_for_error_msg +
" missing native libs for requested architecture: " +
cpu)
Expand Down
2 changes: 1 addition & 1 deletion tools/android/aar_native_libs_zip_creator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def testAarWithMissingLibs(self):
aar.writestr("jni/armeabi/foo.so", "")
outzip = zipfile.ZipFile(io.BytesIO(), "w")
self.assertRaises(
aar_native_libs_zip_creator.UnsupportedArchitectureException,
aar_native_libs_zip_creator.UnsupportedArchitectureError,
aar_native_libs_zip_creator.CreateNativeLibsZip,
aar, "x86", outzip)

Expand Down

0 comments on commit d0620d1

Please sign in to comment.