From 5937afa46de5efeb66b054a40cdc1b2ca1f90235 Mon Sep 17 00:00:00 2001 From: Kevin Broch <86068473+kbroch-rivosinc@users.noreply.github.com> Date: Fri, 28 Jun 2024 21:57:52 -0700 Subject: [PATCH] change --copyright-style to --copyright-prefix #973 Signed-off-by: Carmen Bianca BAKKER --- CHANGELOG.md | 1 + docs/man/reuse-annotate.rst | 6 ++-- src/reuse/_annotate.py | 18 ++++++---- src/reuse/_util.py | 26 ++++++++------- tests/test_main_annotate_merge.py | 4 +-- tests/test_util.py | 55 ++++++++++++++++--------------- 6 files changed, 60 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 852e088b6..9884299d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ CLI command and its behaviour. There are no guarantees of stability for the - Perl test (`.t`) (#997) - BATS test (`.bats`) (#997) - Support alternate spelling `--skip-unrecognized` (#974) +- Change `--copyright-style` to `--copyright-prefix` (still supporting former) (#973) ### Changed diff --git a/docs/man/reuse-annotate.rst b/docs/man/reuse-annotate.rst index 7de2a2204..0b079550b 100644 --- a/docs/man/reuse-annotate.rst +++ b/docs/man/reuse-annotate.rst @@ -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 @@ -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:: diff --git a/src/reuse/_annotate.py b/src/reuse/_annotate.py index 16e3b24f8..c45918554 100644 --- a/src/reuse/_annotate.py +++ b/src/reuse/_annotate.py @@ -30,7 +30,7 @@ from . import ReuseInfo from ._util import ( - _COPYRIGHT_STYLES, + _COPYRIGHT_PREFIXES, PathType, StrPath, _determine_license_path, @@ -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 } @@ -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", diff --git a/src/reuse/_util.py b/src/reuse/_util.py index 7f351fa09..d660435ef 100644 --- a/src/reuse/_util.py +++ b/src/reuse/_util.py @@ -6,6 +6,7 @@ # SPDX-FileCopyrightText: 2022 Pietro Albini # SPDX-FileCopyrightText: 2023 DB Systel GmbH # SPDX-FileCopyrightText: 2023 Johannes Zarl-Zierl +# SPDX-FileCopyrightText: 2024 Rivos Inc. # SPDX-FileCopyrightText: © 2020 Liferay, Inc. # # SPDX-License-Identifier: GPL-3.0-or-later @@ -128,7 +129,7 @@ r"(?P.*?))" + _END_PATTERN ), ] -_COPYRIGHT_STYLES = { +_COPYRIGHT_PREFIXES = { "spdx": "SPDX-FileCopyrightText:", "spdx-c": "SPDX-FileCopyrightText: (C)", "spdx-symbol": "SPDX-FileCopyrightText: ©", @@ -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 + # Get the most common prefix. prefix = str( Counter([item["prefix"] for item in copyright_list]).most_common(1)[ 0 ][0] ) - style = "spdx" - for key, value in _COPYRIGHT_STYLES.items(): + prefix = "spdx" + for key, value in _COPYRIGHT_PREFIXES.items(): if prefix == value: - style = key + prefix = key break # get year range if any @@ -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 @@ -492,7 +493,7 @@ 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. @@ -500,10 +501,11 @@ def make_copyright_line( 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'" ) @@ -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: diff --git a/tests/test_main_annotate_merge.py b/tests/test_main_annotate_merge.py index c842e7ce2..7889bfcd7 100644 --- a/tests/test_main_annotate_merge.py +++ b/tests/test_main_annotate_merge.py @@ -1,10 +1,10 @@ # SPDX-FileCopyrightText: 2021 Liam Beguin +# 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 @@ -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", diff --git a/tests/test_util.py b/tests/test_util.py index ab4371601..ab00bd213 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,9 +1,10 @@ # SPDX-FileCopyrightText: 2017 Free Software Foundation Europe e.V. -# SPDX-FileCopyrightText: © 2020 Liferay, Inc. -# SPDX-FileCopyrightText: 2022 Nico Rikken -# SPDX-FileCopyrightText: 2022 Florian Snow # SPDX-FileCopyrightText: 2022 Carmen Bianca Bakker +# SPDX-FileCopyrightText: 2022 Florian Snow +# SPDX-FileCopyrightText: 2022 Nico Rikken # SPDX-FileCopyrightText: 2022 Pietro Albini +# SPDX-FileCopyrightText: 2024 Rivos Inc. +# SPDX-FileCopyrightText: © 2020 Liferay, Inc. # # SPDX-License-Identifier: GPL-3.0-or-later @@ -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"