Skip to content

Commit

Permalink
Exception message formatting updates and refactor simplifying
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 committed Jun 10, 2024
1 parent eb1d5ab commit 69231e0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ no-cover-if-is-macos = "'darwin' == os_environ.get('COVERAGE_PLATFORM', sys_plat
no-cover-if-not-macos = "'darwin' != os_environ.get('COVERAGE_PLATFORM', sys_platform) and os_environ.get('COVERAGE_EXCLUDE_PLATFORM') != 'disable'"
no-cover-if-is-windows = "'win32' == os_environ.get('COVERAGE_PLATFORM', sys_platform) and os_environ.get('COVERAGE_EXCLUDE_PLATFORM') != 'disable'"
no-cover-if-not-windows = "'win32' != os_environ.get('COVERAGE_PLATFORM', sys_platform) and os_environ.get('COVERAGE_EXCLUDE_PLATFORM') != 'disable'"
no-cover-if-gte-py312 = "sys_version_info > (3, 12) and os_environ.get('COVERAGE_EXCLUDE_PYTHON_VERSION') != 'disable'"
no-cover-if-is-py312 = "python_version == '3.12' and os_environ.get('COVERAGE_EXCLUDE_PYTHON_VERSION') != 'disable'"
no-cover-if-lt-py312 = "sys_version_info < (3, 12) and os_environ.get('COVERAGE_EXCLUDE_PYTHON_VERSION') != 'disable'"
no-cover-if-lt-py311 = "sys_version_info < (3, 11) and os_environ.get('COVERAGE_EXCLUDE_PYTHON_VERSION') != 'disable'"
Expand Down
10 changes: 5 additions & 5 deletions src/briefcase/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, requested, choices):

def __str__(self):
choices = ", ".join(sorted(self.choices, key=str.lower))
return f"Invalid format '{self.requested}'; (choose from: {choices})"
return f"Invalid format {self.requested!r}; (choose from: {choices})"


class UnsupportedCommandError(BriefcaseError):
Expand Down Expand Up @@ -156,18 +156,18 @@ def __init__(self, platform):
class InvalidSupportPackage(BriefcaseCommandError):
def __init__(self, filename):
self.filename = filename
super().__init__(f"Unable to unpack support package '{filename}'.")
super().__init__(f"Unable to unpack support package {str(filename)!r}.")


class InvalidStubBinary(BriefcaseCommandError):
def __init__(self, filename):
self.filename = filename
super().__init__(f"Unable to unpack or copy stub binary '{filename}'.")
super().__init__(f"Unable to unpack or copy stub binary {str(filename)!r}.")


class MissingAppMetadata(BriefcaseCommandError):
def __init__(self, app_bundle_path):
super().__init__(f"Unable to find '{app_bundle_path / 'briefcase.toml'}'")
super().__init__(f"Unable to find {str(app_bundle_path / 'briefcase.toml')!r}")


class MissingSupportPackage(BriefcaseCommandError):
Expand Down Expand Up @@ -229,7 +229,7 @@ class InvalidDeviceError(BriefcaseCommandError):
def __init__(self, id_type, device):
self.id_type = id_type
self.device = device
super().__init__(msg=f"Invalid device {id_type} '{device}'")
super().__init__(msg=f"Invalid device {id_type} {device!r}")


class CorruptToolError(BriefcaseCommandError):
Expand Down
30 changes: 13 additions & 17 deletions src/briefcase/integrations/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,31 @@ def unpack_archive(
):
"""Unpack an archive file in to a destination directory.
Additional protections for unpacking tar files were introduced in Python 3.12.
Since tarballs can contain anything valid in a UNIX file system, these
protections prevent unpacking potentially dangerous files. This behavior will be
the default in Python 3.14. However, the protections can only be enabled for tar
files...not zip files.
:param filename: File path for the archive
:param extract_dir: Target file path for where to unpack archive
:param kwargs: additional arguments for shutil.unpack_archive
"""
is_zip = Path(filename).suffix == ".zip"
if sys.version_info >= (3, 12): # pragma: no-cover-if-lt-py312
unpack_kwargs = {"filter": "data"} if not is_zip else {}
else: # pragma: no-cover-if-gte-py312
unpack_kwargs = {}

self.tools.shutil.unpack_archive(
filename=filename,
extract_dir=extract_dir,
**{
**self._unpack_archive_kwargs(filename),
**unpack_kwargs,
**kwargs,
},
)

def _unpack_archive_kwargs(self, archive_path: str | os.PathLike) -> dict[str, str]:
"""Additional options for unpacking archives based on the archive's type.
Additional protections for unpacking tar files were introduced in Python 3.12.
Since tarballs can contain anything valid in a UNIX file system, these
protections prevent unpacking potentially dangerous files. This behavior will be
the default in Python 3.14. However, the protections can only be enabled for tar
files...not zip files.
"""
is_zip = Path(archive_path).suffix == ".zip"
if sys.version_info >= (3, 12) and not is_zip: # pragma: no-cover-if-lt-py312
unpack_kwargs = {"filter": "data"}
else:
unpack_kwargs = {}
return unpack_kwargs

def download(self, url: str, download_path: Path, role: str | None = None) -> Path:
"""Download a given URL, caching it. If it has already been downloaded, return
the value that has been cached.
Expand Down

0 comments on commit 69231e0

Please sign in to comment.