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

Adding --json to lint #654

Merged
merged 60 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
cb94206
Rework `lint`
linozen Jan 11, 2023
3ae8af7
Add --json` argument
linozen Jan 24, 2023
2e6805e
Slim down data dictionary creation
linozen Jan 24, 2023
5a03e9e
Rework plaintext formatter
linozen Jan 24, 2023
21d0026
Move JSON formatter and re-add `lint` function
linozen Jan 24, 2023
0b473f0
Remove old output generation functions
linozen Jan 24, 2023
74fd00e
Adapt tests
linozen Jan 24, 2023
fb32ead
Add new tests
linozen Jan 24, 2023
ff3d3c3
Satisfy pylint
linozen Jan 24, 2023
cfcac8e
Satisfy isort
linozen Jan 24, 2023
d749d51
Make showing of license (not path) work on Windows
linozen Jan 24, 2023
5dcd872
Satisfy pylint (again)
linozen Jan 24, 2023
e03fb3e
Add changelog entry
linozen Jan 24, 2023
89dbd8b
Fix typo
linozen Jan 24, 2023
034a9af
Move `collect_data_from_report` to `ProjectReport.to_dict`
linozen Feb 15, 2023
6bb2a47
(convenience) Add `--format` to easily switch between plain and JSON …
linozen Feb 15, 2023
6bc172a
Get `__REUSE_version__` from report object
linozen Feb 15, 2023
2a2e44f
Add newline at the end of the output
linozen Feb 15, 2023
5c25198
Update tests according to code changes
linozen Feb 15, 2023
f697c73
Fix minor issues in `report.py`
linozen Apr 5, 2023
f20429f
Fix plaintext output
linozen Apr 5, 2023
1b2bd90
Add custom JSON serializer
linozen Apr 5, 2023
d98d162
Adapt `SpdxInfo` namedtuple to contain source path
linozen Apr 5, 2023
9c106f1
Remove useless line
linozen Apr 5, 2023
9beac70
Actually return license/copyright source path
linozen Apr 5, 2023
c43c91f
Determine license source correctly
linozen Apr 5, 2023
6fc5ec1
Allow returning empty arrays
linozen Apr 5, 2023
3ad1fe3
Satisfy black
linozen Apr 6, 2023
2fcc8eb
Ensure consistency of the JSON output
linozen Apr 6, 2023
d50d15e
Ensure consistency of the plaintext output
linozen Apr 6, 2023
fbf6e53
Move logic to FileReport.to_dict and rename method to to_dict_lint
carmenbianca Apr 10, 2023
bd59465
Create ProjectReport.is_compliant
carmenbianca Apr 10, 2023
8f3b5d0
Rename json_version to lint_version
carmenbianca Apr 10, 2023
8feed9e
Remove superfluous function
carmenbianca Apr 10, 2023
7423f62
Make tests run by importing an existing function
carmenbianca May 11, 2023
f648356
Repair boolean check of SpdxInfo; object not iterable
carmenbianca May 21, 2023
c75a3e3
Correctly use format_plain
carmenbianca May 21, 2023
5bc2c58
fixup! Rename json_version to lint_version
carmenbianca May 21, 2023
54a8a66
fixup! fixup! Rename json_version to lint_version
carmenbianca May 21, 2023
d3208db
Don't pass string where (optional) set is expected
carmenbianca May 21, 2023
8a40fb6
fixup! Don't pass string where (optional) set is expected
carmenbianca May 21, 2023
a769306
fixup! fixup! Don't pass string where (optional) set is expected
carmenbianca May 21, 2023
cc5a383
Create SpdxInfo.contains_copyright_or_licensing
carmenbianca May 21, 2023
23fa4cc
Make lint version a global constant
carmenbianca May 21, 2023
8fa808a
Add tool version to json output
carmenbianca May 21, 2023
c94b5a0
Refactoring precedence for license path
linozen May 25, 2023
7493ed1
Add two `--json` specific test to `tests/test_lint.py`
linozen May 25, 2023
bb3217a
Adapting two tests to new precedence behaviour
linozen May 25, 2023
f2f1ef0
Adding a warning when information is overriden
linozen May 25, 2023
cd820e1
Import `Path` instead of `PosixPath`
linozen May 25, 2023
6c51545
Use `StringIO` object instead of normal string object
linozen May 25, 2023
d1c2146
Make sure that some keys are on top of sorted output
linozen May 25, 2023
c8baa6e
Make `summary_contents` a dictionary
linozen May 25, 2023
983b0c4
Simplify `lint --quiet`
linozen May 26, 2023
d7af2e2
Move dictionary sorting and test it
linozen May 26, 2023
33456cb
Rename `SpdxInfo` to `ReuseInfo`
linozen May 26, 2023
962521f
Rename `ReuseInfo.license_path` to `ReuseInfo.source_path`
linozen May 26, 2023
852b3fb
Also encode `source_type` in `ReuseInfo`
linozen May 26, 2023
322dd8a
Rename `spdx_info_of` to `reuse_info_of`
linozen May 26, 2023
7346234
Add SourceType enum and use it for source_type in ReuseInfo
linozen Jun 1, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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
12 changes: 9 additions & 3 deletions src/reuse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import re
from dataclasses import dataclass, field
from typing import NamedTuple, Set
from typing import NamedTuple, Optional, Set

try:
from importlib.metadata import PackageNotFoundError, version
Expand Down Expand Up @@ -89,12 +89,18 @@


@dataclass(frozen=True)
class SpdxInfo:
"""Simple class holding SPDX information"""
class ReuseInfo:
"""Simple dataclass holding licensing and copyright information"""
linozen marked this conversation as resolved.
Show resolved Hide resolved

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[str] = None
linozen marked this conversation as resolved.
Show resolved Hide resolved

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