Skip to content

Commit

Permalink
Fix: Fix overriding company in copyright header via update-header CLI
Browse files Browse the repository at this point in the history
We need a regex that matches all kind of company names not only
Greenbone. This also generalizes the code and makes update-header
independent of Greenbone specific use cases.
  • Loading branch information
bjoernricks committed Feb 26, 2024
1 parent b3ba4b3 commit 3e63962
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
9 changes: 3 additions & 6 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"along with this program; if not, write to the Free Software",
"Foundation, Inc\., 51 Franklin St, Fifth Floor, Boston, MA 02110\-1301 USA\.", # noqa: E501
]
OLD_COMPANY = "Greenbone Networks GmbH"


def _get_modified_year(f: Path) -> str:
Expand Down Expand Up @@ -154,7 +153,7 @@ def update_file(
checks if year is up to date
"""

copyright_regex = _compile_copyright_regex(company=[company, OLD_COMPANY])
copyright_regex = _compile_copyright_regex()
cleanup_regexes = _compile_outdated_regex() if cleanup else None

try:
Expand Down Expand Up @@ -288,14 +287,12 @@ def _compile_outdated_regex() -> list[re.Pattern]:
return [re.compile(rf"^(([#*]|//) ?)?{line}") for line in OLD_LINES]


def _compile_copyright_regex(company: Union[str, list[str]]) -> re.Pattern:
def _compile_copyright_regex() -> re.Pattern:
"""prepare the copyright regex"""
c_str = r"(SPDX-FileCopyrightText:|[Cc]opyright)"
d_str = r"(19[0-9]{2}|20[0-9]{2})"

if isinstance(company, str):
return re.compile(rf"{c_str}.*? {d_str}?-? ?{d_str}? ({company})")
return re.compile(rf"{c_str}.*? {d_str}?-? ?{d_str}? ({'|'.join(company)})")
return re.compile(rf"{c_str}.*? {d_str}?-? ?{d_str}? (.+)")


def main(args: Optional[Sequence[str]] = None) -> None:
Expand Down
42 changes: 41 additions & 1 deletion tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_get_modified_year_error(self):
class FindCopyRightTestCase(TestCase):
def setUp(self) -> None:
self.company = "Greenbone AG"
self.regex = _compile_copyright_regex(self.company)
self.regex = _compile_copyright_regex()

def test_find_copyright(self):
test_line = "# Copyright (C) 1995-2021 Greenbone AG"
Expand Down Expand Up @@ -436,6 +436,46 @@ def test_cleanup_file_spdx_header(self):
new_content = tmp.read_text(encoding="utf-8")
self.assertEqual(expected_content, new_content)

def test_cleanup_file_changed_company(self):
test_content = """
# SPDX-FileCopyrightText: 2021 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
import foo
import bar
foo.baz(bar.boing)
""" # noqa: E501

expected_content = f"""
# SPDX-FileCopyrightText: 2021-{str(datetime.datetime.now().year)} ACME Inc.
#
# SPDX-License-Identifier: GPL-3.0-or-later
import foo
import bar
foo.baz(bar.boing)
""" # noqa: E501

company = "ACME Inc."
year = str(datetime.datetime.now().year)
license_id = "GPL-3.0-or-later"

with temp_file(content=test_content, name="foo.py") as tmp:

update_file(
tmp,
year,
license_id,
company,
cleanup=True,
)

new_content = tmp.read_text(encoding="utf-8")
self.assertEqual(expected_content, new_content)


class ParseArgsTestCase(TestCase):
def test_argparser_files(self):
Expand Down

0 comments on commit 3e63962

Please sign in to comment.