Skip to content

Commit

Permalink
Fix: Find Copyright headers with old company
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself committed Nov 14, 2023
1 parent 84ee098 commit 20c0e11
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 30 deletions.
49 changes: 35 additions & 14 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"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 All @@ -96,10 +97,10 @@ def _get_modified_year(f: Path) -> str:

def _find_copyright(
line: str,
regex: re.Pattern,
copyright_regex: re.Pattern,
) -> Tuple[bool, Union[Dict[str, Union[str, None]], None]]:
"""Match the line for the regex"""
copyright_match = re.search(regex, line)
"""Match the line for the copyright_regex"""
copyright_match = re.search(copyright_regex, line)
if copyright_match:
return (
True,
Expand Down Expand Up @@ -151,22 +152,26 @@ def _remove_outdated(
break
i = i + 1
if changed:
return "\n".join(splitted_lines)
return "\n".join(splitted_lines) + "\n"
return None


def _update_file(
file: Path,
regex: re.Pattern,
copyright_regex: re.Pattern,
parsed_args: Namespace,
term: Terminal,
cleanup_regexes: Optional[List[re.Pattern]] = None,
old_company_copyright_regex: Optional[re.Pattern] = None,
) -> int:
"""Function to update the given file.
Checks if header exists. If not it adds an
header to that file, else it checks if year
is up to date
"""
cleanup = False
if cleanup_regexes and old_company_copyright_regex:
cleanup = True

Check warning on line 174 in pontos/updateheader/updateheader.py

View check run for this annotation

Codecov / codecov/patch

pontos/updateheader/updateheader.py#L174

Added line #L174 was not covered by tests

if parsed_args.changed:
try:
Expand All @@ -186,7 +191,13 @@ def _update_file(
if line == "":
i = 0
continue
found, copyright_match = _find_copyright(line=line, regex=regex)
found, copyright_match = _find_copyright(
line=line, copyright_regex=copyright_regex
)
if cleanup and not found:
found, copyright_match = _find_copyright(

Check warning on line 198 in pontos/updateheader/updateheader.py

View check run for this annotation

Codecov / codecov/patch

pontos/updateheader/updateheader.py#L198

Added line #L198 was not covered by tests
line=line, copyright_regex=old_company_copyright_regex # type: ignore # noqa: E501
)
i = i - 1
# header not found, add header
if i == 0 and not found:
Expand All @@ -201,9 +212,7 @@ def _update_file(
fp.seek(0) # back to beginning of file
rest_of_file = fp.read()
fp.seek(0)
fp.write(header)
fp.write("\n")
fp.write(rest_of_file)
fp.write(header + "\n" + rest_of_file)
print(f"{file}: Added license header.")
return 0
except ValueError:
Expand Down Expand Up @@ -238,7 +247,7 @@ def _update_file(
f'{copyright_match["creation_year"]}'
f'-{parsed_args.year} {copyright_match["company"]}'
)
new_line = re.sub(regex, copyright_term, line)
new_line = re.sub(copyright_regex, copyright_term, line)
fp_write = fp.tell() - len(line) # save position to insert
rest_of_file = fp.read()
fp.seek(fp_write)
Expand Down Expand Up @@ -405,6 +414,14 @@ def _compile_outdated_regex() -> List[re.Pattern]:
return regexes


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

return re.compile(rf"{c_str}.*? {d_str}?-? ?{d_str}? ({company})")


def main() -> None:
parsed_args = _parse_args()
exclude_list = []
Expand Down Expand Up @@ -443,10 +460,13 @@ def main() -> None:
term.error("Specify files to update!")
sys.exit(1)

regex: re.Pattern = re.compile(
"(SPDX-FileCopyrightText:|[Cc]opyright).*?(19[0-9]{2}|20[0-9]{2}) "
f"?-? ?(19[0-9]{{2}}|20[0-9]{{2}})? ({parsed_args.company})"
copyright_regex: re.Pattern = _compile_copyright_regex(
company=parsed_args.company
)
old_company_copyright_regex: re.Pattern = _compile_copyright_regex(
company=OLD_COMPANY
)

cleanup_regexes: Optional[List[re.Pattern]] = None
if parsed_args.cleanup:
cleanup_regexes = _compile_outdated_regex()
Expand All @@ -458,10 +478,11 @@ def main() -> None:
else:
_update_file(
file=file,
regex=regex,
copyright_regex=copyright_regex,
parsed_args=parsed_args,
term=term,
cleanup_regexes=cleanup_regexes,
old_company_copyright_regex=old_company_copyright_regex,
)
except (FileNotFoundError, UnicodeDecodeError, ValueError):
continue
Expand Down
59 changes: 43 additions & 16 deletions tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,29 @@ def test_find_copyright(self):
)

# Full match
found, match = find_copyright(regex=self.regex, line=test_line)
found, match = find_copyright(
copyright_regex=self.regex, line=test_line
)
self.assertTrue(found)
self.assertIsNotNone(match)
self.assertEqual(match["creation_year"], "1995")
self.assertEqual(match["modification_year"], "2021")
self.assertEqual(match["company"], self.args.company)

# No modification Date
found, match = find_copyright(regex=self.regex, line=test2_line)
found, match = find_copyright(
copyright_regex=self.regex, line=test2_line
)
self.assertTrue(found)
self.assertIsNotNone(match)
self.assertEqual(match["creation_year"], "1995")
self.assertEqual(match["modification_year"], None)
self.assertEqual(match["company"], self.args.company)

# No match
found, match = find_copyright(regex=self.regex, line=invalid_line)
found, match = find_copyright(
copyright_regex=self.regex, line=invalid_line
)
self.assertFalse(found)
self.assertIsNone(match)

Expand All @@ -136,23 +142,29 @@ def test_find_spdx_copyright(self):
)

# Full match
found, match = find_copyright(regex=self.regex, line=test_line)
found, match = find_copyright(
copyright_regex=self.regex, line=test_line
)
self.assertTrue(found)
self.assertIsNotNone(match)
self.assertEqual(match["creation_year"], "1995")
self.assertEqual(match["modification_year"], "2021")
self.assertEqual(match["company"], self.args.company)

# No modification Date
found, match = find_copyright(regex=self.regex, line=test2_line)
found, match = find_copyright(
copyright_regex=self.regex, line=test2_line
)
self.assertTrue(found)
self.assertIsNotNone(match)
self.assertEqual(match["creation_year"], "1995")
self.assertEqual(match["modification_year"], None)
self.assertEqual(match["company"], self.args.company)

# No match
found, match = find_copyright(regex=self.regex, line=invalid_line)
found, match = find_copyright(
copyright_regex=self.regex, line=invalid_line
)
self.assertFalse(found)
self.assertIsNone(match)

Expand Down Expand Up @@ -201,7 +213,7 @@ def test_update_file_not_existing(self, mock_stdout):
with self.assertRaises(FileNotFoundError):
update_file(
file=test_file,
regex=self.regex,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)
Expand All @@ -224,7 +236,10 @@ def test_update_file_wrong_license(self, mock_stdout):
test_file.touch()

code = update_file(
file=test_file, regex=self.regex, parsed_args=self.args, term=term
file=test_file,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)
self.assertEqual(code, 1)

Expand All @@ -249,7 +264,10 @@ def test_update_file_suffix_invalid(self, mock_stdout):
test_file.touch()

code = update_file(
file=test_file, regex=self.regex, parsed_args=self.args, term=term
file=test_file,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)
self.assertEqual(code, 1)

Expand Down Expand Up @@ -281,7 +299,7 @@ def test_update_file_binary_file(self, mock_stdout):
with self.assertRaises(UnicodeDecodeError):
code = update_file(
file=test_file,
regex=self.regex,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)
Expand Down Expand Up @@ -310,7 +328,7 @@ def test_update_file_changed(self, mock_stdout):
with self.assertRaises(FileNotFoundError):
code = update_file(
file=test_file,
regex=self.regex,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)
Expand Down Expand Up @@ -338,7 +356,10 @@ def test_update_create_header(self, mock_stdout):
test_file.touch()

code = update_file(
file=test_file, regex=self.regex, parsed_args=self.args, term=term
file=test_file,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)

ret = mock_stdout.getvalue()
Expand Down Expand Up @@ -367,7 +388,10 @@ def test_update_header_in_file(self, mock_stdout):
test_file.write_text(header, encoding="utf-8")

code = update_file(
file=test_file, regex=self.regex, parsed_args=self.args, term=term
file=test_file,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)

self.assertEqual(code, 0)
Expand Down Expand Up @@ -401,7 +425,10 @@ def test_update_header_ok_in_file(self, mock_stdout):
test_file.write_text(header, encoding="utf-8")

code = update_file(
file=test_file, regex=self.regex, parsed_args=self.args, term=term
file=test_file,
copyright_regex=self.regex,
parsed_args=self.args,
term=term,
)

self.assertEqual(code, 0)
Expand Down Expand Up @@ -523,7 +550,7 @@ def test_remove_outdated(self):
new_content = remove_outdated(
content=test_content, cleanup_regexes=compiled_regexes
)
self.assertEqual(new_content, "")
self.assertEqual(new_content, "\n")

def test_remove_outdated2(self):
test_content = """the Free Software Foundation, either version 3 of the License, or
Expand All @@ -542,4 +569,4 @@ def test_remove_outdated2(self):
new_content = remove_outdated(
content=test_content, cleanup_regexes=compiled_regexes
)
self.assertEqual(new_content, "")
self.assertEqual(new_content, "\n")

0 comments on commit 20c0e11

Please sign in to comment.