Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling for DEP5 parse errors #841

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!--
SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
SPDX-FileCopyrightText: 2023 DB Systel GmbH
SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>

SPDX-License-Identifier: CC-BY-SA-4.0
-->
Expand Down Expand Up @@ -80,6 +80,7 @@ CLI command and its behaviour. There are no guarantees of stability for the

### Fixed

- Syntax errors in .reuse/dep5 now have better error handling. (#841)
- Reduced python-debian minimum version to 0.1.34. (#808)
- Fix issue in `annotate` where `--single-line` and `--multi-line` would not
correctly raise an error with an incompatible comment style. (#853)
Expand Down
35 changes: 29 additions & 6 deletions src/reuse/_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
# SPDX-FileCopyrightText: 2022 Florian Snow <florian@familysnow.net>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -11,8 +11,11 @@
import sys
import warnings
from gettext import gettext as _
from pathlib import Path
from typing import IO, Callable, List, Optional, Type, cast

from debian.copyright import Error as DebianError

from . import (
__REUSE_version__,
__version__,
Expand All @@ -25,7 +28,8 @@
)
from ._format import INDENT, fill_all, fill_paragraph
from ._util import PathType, setup_logging
from .project import Project, create_project
from .project import Project
from .vcs import find_root

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -277,10 +281,29 @@ def main(args: Optional[List[str]] = None, out: IO[str] = sys.stdout) -> int:
out.write(f"reuse {__version__}\n")
return 0

if parsed_args.root:
project = Project(parsed_args.root)
else:
project = create_project()
root = parsed_args.root
if root is None:
root = find_root()
if root is None:
root = Path.cwd()
try:
project = Project.from_directory(root)
# FileNotFoundError and NotADirectoryError don't need to be caught because
# argparse already made sure of these things.
except UnicodeDecodeError:
main_parser.error(
_("'{dep5}' could not be decoded as UTF-8.").format(
dep5=root / ".reuse/dep5"
)
)
except DebianError as error:
main_parser.error(
_(
"'{dep5}' could not be parsed. We received the following error"
" message: {message}"
).format(dep5=root / ".reuse/dep5", message=str(error))
)

project.include_submodules = parsed_args.include_submodules
project.include_meson_subprojects = parsed_args.include_meson_subprojects

Expand Down
31 changes: 28 additions & 3 deletions src/reuse/_util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
# SPDX-FileCopyrightText: 2020 Tuomas Siipola <tuomas@zpl.fi>
# SPDX-FileCopyrightText: 2022 Nico Rikken <nico.rikken@fsfe.org>
# SPDX-FileCopyrightText: 2022 Florian Snow <florian@familysnow.net>
# SPDX-FileCopyrightText: 2022 Carmen Bianca Bakker <carmenbianca@fsfe.org>
# SPDX-FileCopyrightText: 2022 Florian Snow <florian@familysnow.net>
# SPDX-FileCopyrightText: 2022 Nico Rikken <nico.rikken@fsfe.org>
# SPDX-FileCopyrightText: 2022 Pietro Albini <pietro.albini@ferrous-systems.com>
# SPDX-FileCopyrightText: 2023 DB Systel GmbH
# SPDX-FileCopyrightText: 2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -44,6 +44,7 @@
from binaryornot.check import is_binary
from boolean.boolean import Expression, ParseError
from debian.copyright import Copyright
from debian.copyright import Error as DebianError
from license_expression import ExpressionError, Licensing

from . import ReuseInfo, SourceType
Expand Down Expand Up @@ -244,6 +245,30 @@ def _determine_license_suffix_path(path: StrPath) -> Path:
return Path(f"{path}.license")


def _parse_dep5(path: StrPath) -> Copyright:
"""Parse the dep5 file and create a dep5 Copyright object.

Raises:
FileNotFoundError: file doesn't exist.
DebianError: file could not be parsed.
UnicodeDecodeError: could not decode file as UTF-8.
"""
path = Path(path)
try:
with path.open(encoding="utf-8") as fp:
return Copyright(fp)
except FileNotFoundError:
_LOGGER.debug(_("no '{}' file, or could not read it").format(path))
raise
# TODO: Remove ValueError once
# <https://salsa.debian.org/python-debian-team/python-debian/-/merge_requests/123>
# is closed
except (DebianError, ValueError) as error:
if error.__class__ == ValueError:
raise DebianError(str(error)) from error
raise


def _copyright_from_dep5(path: StrPath, dep5_copyright: Copyright) -> ReuseInfo:
"""Find the reuse information of *path* in the dep5 Copyright object."""
path = PurePath(path).as_posix()
Expand Down
Loading
Loading