Skip to content

Commit

Permalink
Add alternate unzip implementation (#1692)
Browse files Browse the repository at this point in the history
Turns out Linux and the `zipfile` package don't get along, while Windows and `unzip` don't get along.

Provide both implementations, so it always works.
  • Loading branch information
Julian-O authored Sep 16, 2023
1 parent a289958 commit 0b938d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
29 changes: 18 additions & 11 deletions buildozer/buildops.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tarfile
from threading import Thread
from urllib.request import Request, urlopen
from zipfile import ZipFile

from buildozer.exceptions import BuildozerCommandException
from buildozer.logger import Logger
Expand Down Expand Up @@ -127,17 +128,23 @@ def file_extract(archive, env, cwd="."):

if path.suffix == ".zip":
LOGGER.debug("Extracting {0} to {1}".format(archive, cwd))
assert platform != "win32", "unzip unavailable on Windows"
# This should use python's zipfile library, with suitable handling of
# Unix permissions.
# However, this lead to unexpected config script issues, so sticking to
# unzip for now.
return_code = cmd(
["unzip", "-q", join(cwd, archive)], cwd=cwd, env=env
).return_code
if return_code != 0:
raise BuildozerCommandException(
"Unzip gave bad return code: {}".format(return_code))
if platform == "win32":
# This won't work on Unix/OSX, because Android NDK (for example)
# relies on non-standard handling of file permissions and symbolic
# links that Python's zipfile doesn't support.
# Windows doesn't support them either.
with ZipFile(path, "r") as compressed_file:
compressed_file.extractall(cwd)
return
else:
# This won't work on Windows, because there is no unzip command
# there
return_code = cmd(
["unzip", "-q", join(cwd, archive)], cwd=cwd, env=env
).return_code
if return_code != 0:
raise BuildozerCommandException(
"Unzip gave bad return code: {}".format(return_code))
return

if path.suffix == ".bin":
Expand Down
10 changes: 7 additions & 3 deletions tests/test_buildops.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,14 @@ def test_extract_file(self):
m_logger.reset_mock()

nonexistent_path = Path(base_dir) / "nonexistent.zip"
with self.assertRaises(BuildozerCommandException):
try:
buildops.file_extract(nonexistent_path, environ)
m_logger.debug.assert_called()
m_logger.error.assert_called()
self.fail("No exception raised")
except FileNotFoundError:
pass # This is raised by zipfile on Windows.
except BuildozerCommandException:
pass # This is raised by cmd when not on Windows.

m_logger.reset_mock()

# Create a zip file and unzip it.
Expand Down

0 comments on commit 0b938d3

Please sign in to comment.