Skip to content

Commit

Permalink
Display notice when copyrighted file is copied or renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleFromNVIDIA committed Aug 26, 2024
1 parent 8028f1b commit be50381
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/rapids_pre_commit_hooks/copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,33 @@ def apply_copyright_revert(
).add_replacement(new_match.span(), old_match.group())


def apply_copyright_update(linter: Linter, match: re.Match, year: int) -> None:
linter.add_warning(match.span("years"), "copyright is out of date").add_replacement(
def apply_copyright_update(
linter: Linter,
change_type: str,
old_filename: Optional[str],
match: re.Match,
year: int,
) -> None:
CHANGE_VERBS = {
"C": "copied",
"R": "renamed",
}
w = linter.add_warning(match.span("years"), "copyright is out of date")
w.add_replacement(
match.span(),
COPYRIGHT_REPLACEMENT.format(
first_year=match.group("first_year"),
last_year=year,
),
)
try:
change_verb = CHANGE_VERBS[change_type]
except KeyError:
pass
else:
w.add_note(
(0, len(linter.content)), f"file was {change_verb} from '{old_filename}'"
)


def apply_copyright_check(
Expand Down Expand Up @@ -108,7 +127,9 @@ def apply_copyright_check(
int(match.group("last_year") or match.group("first_year"))
< current_year
):
apply_copyright_update(linter, match, current_year)
apply_copyright_update(
linter, change_type, old_filename, match, current_year
)
else:
linter.add_warning((0, 0), "no copyright notice found")

Expand Down
100 changes: 99 additions & 1 deletion test/rapids_pre_commit_hooks/test_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from freezegun import freeze_time

from rapids_pre_commit_hooks import copyright
from rapids_pre_commit_hooks.lint import Linter, LintWarning, Replacement
from rapids_pre_commit_hooks.lint import Linter, LintWarning, Note, Replacement


def test_match_copyright():
Expand Down Expand Up @@ -265,6 +265,104 @@ def test_strip_copyright():
),
],
),
(
"R",
"file1.txt",
dedent(
r"""
Copyright (c) 2021-2023 NVIDIA CORPORATION
Copyright (c) 2023 NVIDIA CORPORATION
Copyright (c) 2024 NVIDIA CORPORATION
Copyright (c) 2025 NVIDIA CORPORATION
This file has not been changed
"""
),
"file2.txt",
dedent(
r"""
Copyright (c) 2021-2023 NVIDIA CORPORATION
Copyright (c) 2023 NVIDIA CORPORATION
Copyright (c) 2024 NVIDIA CORPORATION
Copyright (c) 2025 NVIDIA CORPORATION
This file has been changed
"""
),
[
LintWarning(
(15, 24),
"copyright is out of date",
notes=[
Note((0, 185), "file was renamed from 'file1.txt'"),
],
replacements=[
Replacement(
(1, 43), "Copyright (c) 2021-2024, NVIDIA CORPORATION"
),
],
),
LintWarning(
(58, 62),
"copyright is out of date",
notes=[
Note((0, 185), "file was renamed from 'file1.txt'"),
],
replacements=[
Replacement(
(44, 81), "Copyright (c) 2023-2024, NVIDIA CORPORATION"
),
],
),
],
),
(
"C",
"file1.txt",
dedent(
r"""
Copyright (c) 2021-2023 NVIDIA CORPORATION
Copyright (c) 2023 NVIDIA CORPORATION
Copyright (c) 2024 NVIDIA CORPORATION
Copyright (c) 2025 NVIDIA CORPORATION
This file has not been changed
"""
),
"file2.txt",
dedent(
r"""
Copyright (c) 2021-2023 NVIDIA CORPORATION
Copyright (c) 2023 NVIDIA CORPORATION
Copyright (c) 2024 NVIDIA CORPORATION
Copyright (c) 2025 NVIDIA CORPORATION
This file has been changed
"""
),
[
LintWarning(
(15, 24),
"copyright is out of date",
notes=[
Note((0, 185), "file was copied from 'file1.txt'"),
],
replacements=[
Replacement(
(1, 43), "Copyright (c) 2021-2024, NVIDIA CORPORATION"
),
],
),
LintWarning(
(58, 62),
"copyright is out of date",
notes=[
Note((0, 185), "file was copied from 'file1.txt'"),
],
replacements=[
Replacement(
(44, 81), "Copyright (c) 2023-2024, NVIDIA CORPORATION"
),
],
),
],
),
],
)
@freeze_time("2024-01-18")
Expand Down

0 comments on commit be50381

Please sign in to comment.