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

Added new copyright style with spdx and string c as per #948 #979

Merged
merged 9 commits into from
Jul 23, 2024
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
2 changes: 2 additions & 0 deletions changelog.d/added/new-prefixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added new copyright prefixes `spdx-string`, `spdx-string-c`, and
`spdx-string-symbol`. (#979)
2 changes: 2 additions & 0 deletions changelog.d/fixed/prefix-merge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `reuse annotate --merge-copyrights` now works more reliably with copyright
prefixes. This still needs some work, though. (#979)
17 changes: 10 additions & 7 deletions docs/man/reuse-annotate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ Other options

.. code-block::

spdx: SPDX-FileCopyrightText: <year> <statement>
spdx-c: SPDX-FileCopyrightText: (C) <year> <statement>
spdx-symbol: SPDX-FileCopyrightText: © <year> <statement>
string: Copyright <year> <statement>
string-c: Copyright (C) <year> <statement>
string-symbol: Copyright © <year> <statement>
symbol: © <year> <statement>
spdx: SPDX-FileCopyrightText: <year> <statement>
spdx-c: SPDX-FileCopyrightText: (C) <year> <statement>
spdx-symbol: SPDX-FileCopyrightText: © <year> <statement>
spdx-string: SPDX-FileCopyrightText: Copyright <year> <statement>
spdx-string-c: SPDX-FileCopyrightText: Copyright (C) <year> <statement>
spdx-string-symbol: SPDX-FileCopyrightText: Copyright © <year> <statement>
string: Copyright <year> <statement>
string-c: Copyright (C) <year> <statement>
string-symbol: Copyright © <year> <statement>
symbol: © <year> <statement>

.. option:: -t, --template TEMPLATE

Expand Down
9 changes: 7 additions & 2 deletions src/reuse/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@

_COPYRIGHT_PATTERNS = [
re.compile(
r"(?P<copyright>(?P<prefix>SPDX-(File|Snippet)CopyrightText:)\s+"
r"(?P<copyright>(?P<prefix>SPDX-(File|Snippet)CopyrightText:"
r"(\s(\([Cc]\)|©|Copyright(\s(©|\([Cc]\)))?))?)\s+"
r"((?P<year>\d{4} ?- ?\d{4}|\d{4}),?\s+)?"
r"(?P<statement>.*?))" + _END_PATTERN
),
re.compile(
r"(?P<copyright>(?P<prefix>Copyright(\s?\([cC]\))?)\s+"
r"(?P<copyright>(?P<prefix>Copyright(\s(\([Cc]\)|©))?)\s+"
r"((?P<year>\d{4} ?- ?\d{4}|\d{4}),?\s+)?"
r"(?P<statement>.*?))" + _END_PATTERN
),
Expand All @@ -132,6 +133,9 @@
_COPYRIGHT_PREFIXES = {
"spdx": "SPDX-FileCopyrightText:",
"spdx-c": "SPDX-FileCopyrightText: (C)",
"spdx-string-c": "SPDX-FileCopyrightText: Copyright (C)",
"spdx-string": "SPDX-FileCopyrightText: Copyright",
"spdx-string-symbol": "SPDX-FileCopyrightText: Copyright ©",
"spdx-symbol": "SPDX-FileCopyrightText: ©",
"string": "Copyright",
"string-c": "Copyright (C)",
Expand Down Expand Up @@ -311,6 +315,7 @@ def merge_copyright_lines(copyright_lines: Set[str]) -> Set[str]:
"prefix": match.groupdict()["prefix"],
}
)
break

copyright_out = set()
for line_info in copyright_in:
Expand Down
66 changes: 66 additions & 0 deletions tests/test_main_annotate_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from inspect import cleandoc

from reuse._main import main
from reuse._util import _COPYRIGHT_PREFIXES

# pylint: disable=unused-argument

Expand Down Expand Up @@ -191,4 +192,69 @@ def test_annotate_merge_copyrights_no_year_in_existing(
)


def test_annotate_merge_copyrights_all_prefixes(
fake_repository, stringio, mock_date_today
):
"""Test that merging works for all copyright prefixes."""
# TODO: there should probably also be a test for mixing copyright prefixes,
# but this behaviour is really unpredictable to me at the moment, and the
# whole copyright-line-as-string thing needs overhauling.
simple_file = fake_repository / "foo.py"
for copyright_prefix, copyright_string in _COPYRIGHT_PREFIXES.items():
simple_file.write_text("pass")
result = main(
[
"annotate",
"--year",
"2016",
"--license",
"GPL-3.0-or-later",
"--copyright",
"Jane Doe",
"--copyright-style",
copyright_prefix,
"--merge-copyrights",
"foo.py",
],
out=stringio,
)
assert result == 0
assert simple_file.read_text(encoding="utf-8") == cleandoc(
f"""
# {copyright_string} 2016 Jane Doe
#
# SPDX-License-Identifier: GPL-3.0-or-later

pass
"""
)

result = main(
[
"annotate",
"--year",
"2018",
"--license",
"GPL-3.0-or-later",
"--copyright",
"Jane Doe",
"--copyright-style",
copyright_prefix,
"--merge-copyrights",
"foo.py",
],
out=stringio,
)
assert result == 0
assert simple_file.read_text(encoding="utf-8") == cleandoc(
f"""
# {copyright_string} 2016 - 2018 Jane Doe
#
# SPDX-License-Identifier: GPL-3.0-or-later

pass
"""
)


# REUSE-IgnoreEnd
39 changes: 33 additions & 6 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,15 @@ def test_extract_copyright_variations():
"""There are multiple ways to declare copyright. All should be detected."""
text = cleandoc(
"""
SPDX-FileCopyrightText: 2019 Jane Doe
SPDX-FileCopyrightText: © 2019 Jane Doe
© 2019 Jane Doe
Copyright © 2019 Jane Doe
Copyright 2019 Jane Doe
Copyright (C) 2019 Jane Doe
SPDX-FileCopyrightText: 2019 spdx
SPDX-FileCopyrightText: (C) 2019 spdx-c
SPDX-FileCopyrightText: © 2019 spdx-symbol
SPDX-FileCopyrightText: Copyright (C) 2019 spdx-string-c
SPDX-FileCopyrightText: Copyright © 2019 spdx-string-symbol
Copyright 2019 string
Copyright (C) 2019 string-c
Copyright © 2019 string-symbol
© 2019 symbol
"""
)

Expand Down Expand Up @@ -428,6 +431,30 @@ def test_make_copyright_line_prefix_string_c_year():
assert statement == "Copyright (C) 2019 hello"


def test_make_copyright_line_prefix_spdx_string_c_year():
"""Given a simple statement, prefix and a year, make it a copyright line."""
statement = _util.make_copyright_line(
"hello", year=2019, copyright_prefix="spdx-string-c"
)
assert statement == "SPDX-FileCopyrightText: Copyright (C) 2019 hello"


def test_make_copyright_line_prefix_spdx_string_year():
"""Given a simple statement, prefix and a year, make it a copyright line."""
statement = _util.make_copyright_line(
"hello", year=2019, copyright_prefix="spdx-string"
)
assert statement == "SPDX-FileCopyrightText: Copyright 2019 hello"


def test_make_copyright_line_prefix_spdx_string_symbol_year():
"""Given a simple statement, prefix and a year, make it a copyright line."""
statement = _util.make_copyright_line(
"hello", year=2019, copyright_prefix="spdx-string-symbol"
)
assert statement == "SPDX-FileCopyrightText: Copyright © 2019 hello"


def test_make_copyright_line_prefix_string_symbol_year():
"""Given a simple statement, prefix and a year, make it a copyright line."""
statement = _util.make_copyright_line(
Expand Down
Loading