Skip to content

Commit

Permalink
Merge pull request #654 from fsfe/lint-json
Browse files Browse the repository at this point in the history
Adding `--json` to lint
  • Loading branch information
linozen committed Jun 1, 2023
2 parents 2251a34 + 7346234 commit 7362734
Show file tree
Hide file tree
Showing 15 changed files with 696 additions and 517 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ CLI command and its behaviour. There are no guarantees of stability for the
SPDX license expression remains the same:
`Apache-2.0 AND CC0-1.0 AND CC-BY-SA-4.0 AND GPL-3.0-or-later`. (#733)
- Added `--contributor` option to `annotate`. (#669)
- Added `--json` flag to `lint` command (#654).

### Changed

Expand Down
28 changes: 25 additions & 3 deletions src/reuse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import os
import re
from dataclasses import dataclass, field
from typing import NamedTuple, Set
from enum import Enum, auto
from typing import NamedTuple, Optional, Set

try:
from importlib.metadata import PackageNotFoundError, version
Expand Down Expand Up @@ -88,13 +89,34 @@
_IGNORE_FILE_PATTERNS.extend(_IGNORE_SPDX_PATTERNS)


class SourceType(Enum):
"""
An enumeration representing the types of sources for license information.
Potential values:
DOT_LICENSE_FILE: A .license file containing license information.
FILE_HEADER: A file header containing license information.
DEP5_FILE: A .reuse/dep5 file containing license information.
"""

DOT_LICENSE_FILE = ".license file"
FILE_HEADER = "file header"
DEP5_FILE = ".reuse/dep5 file"


@dataclass(frozen=True)
class SpdxInfo:
"""Simple class holding SPDX information"""
class ReuseInfo:
"""Simple dataclass holding licensing and copyright information"""

spdx_expressions: Set[Expression] = field(default_factory=set)
copyright_lines: Set[str] = field(default_factory=set)
contributor_lines: Set[str] = field(default_factory=set)
source_path: Optional[str] = None
source_type: Optional[SourceType] = None

def contains_copyright_or_licensing(self) -> bool:
"""Either *spdx_expressions* or *copyright_lines* is non-empty."""
return bool(self.spdx_expressions or self.copyright_lines)

def __bool__(self):
return any(self.__dict__.values())
Expand Down
15 changes: 9 additions & 6 deletions src/reuse/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from debian.copyright import Copyright
from license_expression import ExpressionError, Licensing

from . import SpdxInfo
from . import ReuseInfo
from ._licenses import ALL_NON_DEPRECATED_MAP
from .comment import _all_style_classes

Expand Down Expand Up @@ -203,16 +203,19 @@ def _determine_license_suffix_path(path: PathLike) -> Path:
return Path(f"{path}.license")


def _copyright_from_dep5(path: PathLike, dep5_copyright: Copyright) -> SpdxInfo:
def _copyright_from_dep5(
path: PathLike, dep5_copyright: Copyright
) -> ReuseInfo:
"""Find the reuse information of *path* in the dep5 Copyright object."""
result = dep5_copyright.find_files_paragraph(Path(path).as_posix())

if result is None:
return SpdxInfo(set(), set())
return ReuseInfo(set(), set(), source_path=str(path))

return SpdxInfo(
return ReuseInfo(
set(map(_LICENSING.parse, [result.license.synopsis])),
set(map(str.strip, result.copyright.splitlines())),
source_path=str(path),
)


Expand Down Expand Up @@ -289,7 +292,7 @@ def merge_copyright_lines(copyright_lines: Set[str]) -> Set[str]:
return copyright_out


def extract_spdx_info(text: str) -> SpdxInfo:
def extract_spdx_info(text: str) -> ReuseInfo:
"""Extract SPDX information from comments in a string.
:raises ExpressionError: if an SPDX expression could not be parsed
Expand All @@ -316,7 +319,7 @@ def extract_spdx_info(text: str) -> SpdxInfo:
copyright_matches.add(match.groupdict()["copyright"].strip())
break

return SpdxInfo(expressions, copyright_matches)
return ReuseInfo(expressions, copyright_matches, "")


def find_license_identifiers(text: str) -> Iterator[str]:
Expand Down
17 changes: 9 additions & 8 deletions src/reuse/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from jinja2.exceptions import TemplateNotFound
from license_expression import ExpressionError

from . import SpdxInfo
from . import ReuseInfo
from ._util import (
_COPYRIGHT_STYLES,
PathType,
Expand Down Expand Up @@ -81,7 +81,7 @@ class MissingSpdxInfo(Exception):

# TODO: Add a template here maybe.
def _create_new_header(
spdx_info: SpdxInfo,
spdx_info: ReuseInfo,
template: Template = None,
template_is_commented: bool = False,
style: CommentStyle = None,
Expand Down Expand Up @@ -131,7 +131,7 @@ def _create_new_header(

# pylint: disable=too-many-arguments
def create_header(
spdx_info: SpdxInfo,
spdx_info: ReuseInfo,
header: str = None,
template: Template = None,
template_is_commented: bool = False,
Expand Down Expand Up @@ -174,10 +174,11 @@ def create_header(
)

# TODO: This behaviour does not match the docstring.
spdx_info = SpdxInfo(
spdx_info = ReuseInfo(
spdx_info.spdx_expressions.union(existing_spdx.spdx_expressions),
spdx_copyrights,
spdx_info.contributor_lines.union(existing_spdx.contributor_lines),
"",
)

new_header += _create_new_header(
Expand Down Expand Up @@ -249,7 +250,7 @@ def _extract_shebang(prefix: str, text: str) -> Tuple[str, str]:
# pylint: disable=too-many-arguments
def find_and_replace_header(
text: str,
spdx_info: SpdxInfo,
spdx_info: ReuseInfo,
template: Template = None,
template_is_commented: bool = False,
style: CommentStyle = None,
Expand Down Expand Up @@ -326,7 +327,7 @@ def find_and_replace_header(
# pylint: disable=too-many-arguments
def add_new_header(
text: str,
spdx_info: SpdxInfo,
spdx_info: ReuseInfo,
template: Template = None,
template_is_commented: bool = False,
style: CommentStyle = None,
Expand Down Expand Up @@ -463,7 +464,7 @@ def _find_template(project: Project, name: str) -> Template:

def _add_header_to_file(
path: PathLike,
spdx_info: SpdxInfo,
spdx_info: ReuseInfo,
template: Template,
template_is_commented: bool,
style: Optional[str],
Expand Down Expand Up @@ -784,7 +785,7 @@ def run(args, project: Project, out=sys.stdout) -> int:
set(args.contributor) if args.contributor is not None else set()
)

spdx_info = SpdxInfo(expressions, copyright_lines, contributors)
spdx_info = ReuseInfo(expressions, copyright_lines, contributors, "")

result = 0
for path in paths:
Expand Down
Loading

0 comments on commit 7362734

Please sign in to comment.