Skip to content

Commit

Permalink
change --copyright-style to --copyright-prefix fsfe#973
Browse files Browse the repository at this point in the history
Signed-off-by: Carmen Bianca BAKKER <carmenbianca@fsfe.org>
  • Loading branch information
kbroch-rivosinc authored and carmenbianca committed Jul 1, 2024
1 parent 33c331b commit 359b1b3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 53 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ CLI command and its behaviour. There are no guarantees of stability for the
- Zsh (`.zshrc`) (#997)
- Perl test (`.t`) (#997)
- BATS test (`.bats`) (#997)
- Support alternate spelling `--skip-unrecognized` (#974)
- Support alternate spelling `--skip-unrecognized`. (#974)
- In `annotate`, rename `--copyright-style` to `--copyright-prefix`. The former
parameter is still supported. (#973)

### Changed

Expand Down
6 changes: 3 additions & 3 deletions docs/man/reuse-annotate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ information which the tool will add to the file(s).
.. option:: -c, --copyright COPYRIGHT

A copyright holder. This does not contain the year or the copyright prefix.
See :option:`--year` and :option:`--copyright-style` for the year and prefix.
See :option:`--year` and :option:`--copyright-prefix` for the year and prefix.
This option can be repeated.

.. option:: -l, --license LICENSE
Expand All @@ -77,10 +77,10 @@ Other options
files. This is useful when a file extension is not recognised, or when a file
extension is associated with a comment style that you disagree with.

.. option:: --copyright-style STYLE
.. option:: --copyright-prefix PREFIX

The prefix to use in the copyright statement. If not defined, ``spdx`` is used
as prefix. The available copyright styles are:
as prefix. The available copyright prefixes are:

.. code-block::
Expand Down
18 changes: 12 additions & 6 deletions src/reuse/_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from . import ReuseInfo
from ._util import (
_COPYRIGHT_STYLES,
_COPYRIGHT_PREFIXES,
PathType,
StrPath,
_determine_license_path,
Expand Down Expand Up @@ -305,13 +305,13 @@ def get_reuse_info(args: Namespace, year: Optional[str]) -> ReuseInfo:
--contributor.
"""
expressions = set(args.license) if args.license is not None else set()
copyright_style = (
args.copyright_style if args.copyright_style is not None else "spdx"
copyright_prefix = (
args.copyright_prefix if args.copyright_prefix is not None else "spdx"
)
copyright_lines = (
{
make_copyright_line(
item, year=year, copyright_style=copyright_style
item, year=year, copyright_prefix=copyright_prefix
)
for item in args.copyright
}
Expand Down Expand Up @@ -410,11 +410,17 @@ def add_arguments(parser: ArgumentParser) -> None:
choices=list(NAME_STYLE_MAP),
help=_("comment style to use, optional"),
)
parser.add_argument(
"--copyright-prefix",
action="store",
choices=list(_COPYRIGHT_PREFIXES.keys()),
help=_("copyright prefix to use, optional"),
)
parser.add_argument(
"--copyright-style",
action="store",
choices=list(_COPYRIGHT_STYLES.keys()),
help=_("copyright style to use, optional"),
dest="copyright_prefix",
help=SUPPRESS,
)
parser.add_argument(
"--template",
Expand Down
30 changes: 16 additions & 14 deletions src/reuse/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# 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: 2024 Rivos Inc.
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
Expand Down Expand Up @@ -128,7 +129,7 @@
r"(?P<statement>.*?))" + _END_PATTERN
),
]
_COPYRIGHT_STYLES = {
_COPYRIGHT_PREFIXES = {
"spdx": "SPDX-FileCopyrightText:",
"spdx-c": "SPDX-FileCopyrightText: (C)",
"spdx-symbol": "SPDX-FileCopyrightText: ©",
Expand Down Expand Up @@ -317,16 +318,16 @@ def merge_copyright_lines(copyright_lines: Set[str]) -> Set[str]:
item for item in copyright_in if item["statement"] == statement
]

# Get the style of the most common prefix
prefix = str(
# Get the most common prefix.
most_common = str(
Counter([item["prefix"] for item in copyright_list]).most_common(1)[
0
][0]
)
style = "spdx"
for key, value in _COPYRIGHT_STYLES.items():
if prefix == value:
style = key
prefix = "spdx"
for key, value in _COPYRIGHT_PREFIXES.items():
if most_common == value:
prefix = key
break

# get year range if any
Expand All @@ -341,7 +342,7 @@ def merge_copyright_lines(copyright_lines: Set[str]) -> Set[str]:
else:
year = f"{min(years)} - {max(years)}"

copyright_out.add(make_copyright_line(statement, year, style))
copyright_out.add(make_copyright_line(statement, year, prefix))
return copyright_out


Expand Down Expand Up @@ -492,18 +493,19 @@ def contains_reuse_info(text: str) -> bool:


def make_copyright_line(
statement: str, year: Optional[str] = None, copyright_style: str = "spdx"
statement: str, year: Optional[str] = None, copyright_prefix: str = "spdx"
) -> str:
"""Given a statement, prefix it with ``SPDX-FileCopyrightText:`` if it is
not already prefixed with some manner of copyright tag.
"""
if "\n" in statement:
raise RuntimeError(f"Unexpected newline in '{statement}'")

copyright_prefix = _COPYRIGHT_STYLES.get(copyright_style)
if copyright_prefix is None:
prefix = _COPYRIGHT_PREFIXES.get(copyright_prefix)
if prefix is None:
# TODO: Maybe translate this. Also maybe reduce DRY here.
raise RuntimeError(
"Unexpected copyright style: Need 'spdx', 'spdx-c', "
"Unexpected copyright prefix: Need 'spdx', 'spdx-c', "
"'spdx-symbol', 'string', 'string-c', "
"'string-symbol', or 'symbol'"
)
Expand All @@ -513,8 +515,8 @@ def make_copyright_line(
if match is not None:
return statement
if year is not None:
return f"{copyright_prefix} {year} {statement}"
return f"{copyright_prefix} {statement}"
return f"{prefix} {year} {statement}"
return f"{prefix} {statement}"


def _checksum(path: StrPath) -> str:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_main_annotate_merge.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# SPDX-FileCopyrightText: 2021 Liam Beguin <liambeguin@gmail.com>
# SPDX-FileCopyrightText: 2024 Rivos Inc.
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Tests for reuse._main: annotate merge-copyrights option"""


from inspect import cleandoc

from reuse._main import main
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_annotate_merge_copyrights_multi_prefix(fake_repository, stringio):
str(2015 + i),
"--license",
"GPL-3.0-or-later",
"--copyright-style",
"--copyright-prefix",
"string-c",
"--copyright",
"Mary Sue",
Expand Down
55 changes: 28 additions & 27 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. <https://fsfe.org>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
# 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: 2024 Rivos Inc.
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -381,64 +382,64 @@ def test_make_copyright_line_year():
)


def test_make_copyright_line_style_spdx():
"""Given a simple statement and style, make it a copyright line."""
statement = _util.make_copyright_line("hello", copyright_style="spdx")
def test_make_copyright_line_prefix_spdx():
"""Given a simple statement and prefix, make it a copyright line."""
statement = _util.make_copyright_line("hello", copyright_prefix="spdx")
assert statement == "SPDX-FileCopyrightText: hello"


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


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


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


def test_make_copyright_line_style_string_year():
"""Given a simple statement, style and a year, make it a copyright line."""
def test_make_copyright_line_prefix_string_year():
"""Given a simple statement, prefix and a year, make it a copyright line."""
statement = _util.make_copyright_line(
"hello", year=2019, copyright_style="string"
"hello", year=2019, copyright_prefix="string"
)
assert statement == "Copyright 2019 hello"


def test_make_copyright_line_style_string_c_year():
"""Given a simple statement, style and a year, make it a copyright line."""
def test_make_copyright_line_prefix_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_style="string-c"
"hello", year=2019, copyright_prefix="string-c"
)
assert statement == "Copyright (C) 2019 hello"


def test_make_copyright_line_style_string_symbol_year():
"""Given a simple statement, style and a year, make it a copyright line."""
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(
"hello", year=2019, copyright_style="string-symbol"
"hello", year=2019, copyright_prefix="string-symbol"
)
assert statement == "Copyright © 2019 hello"


def test_make_copyright_line_style_symbol_year():
"""Given a simple statement, style and a year, make it a copyright line."""
def test_make_copyright_line_prefix_symbol_year():
"""Given a simple statement, prefix and a year, make it a copyright line."""
statement = _util.make_copyright_line(
"hello", year=2019, copyright_style="symbol"
"hello", year=2019, copyright_prefix="symbol"
)
assert statement == "© 2019 hello"

Expand Down

0 comments on commit 359b1b3

Please sign in to comment.