From 20c0e114d10fd69bea8877d9235747edbde7f4e4 Mon Sep 17 00:00:00 2001 From: Jaspar Stach Date: Tue, 14 Nov 2023 13:27:32 +0100 Subject: [PATCH] Fix: Find Copyright headers with old company --- pontos/updateheader/updateheader.py | 49 +++++++++++++++++------- tests/updateheader/test_header.py | 59 +++++++++++++++++++++-------- 2 files changed, 78 insertions(+), 30 deletions(-) diff --git a/pontos/updateheader/updateheader.py b/pontos/updateheader/updateheader.py index b17e46cb8..896d874a5 100644 --- a/pontos/updateheader/updateheader.py +++ b/pontos/updateheader/updateheader.py @@ -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: @@ -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, @@ -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 if parsed_args.changed: try: @@ -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( + 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: @@ -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: @@ -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) @@ -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 = [] @@ -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() @@ -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 diff --git a/tests/updateheader/test_header.py b/tests/updateheader/test_header.py index fa6d26cb9..398245780 100644 --- a/tests/updateheader/test_header.py +++ b/tests/updateheader/test_header.py @@ -107,7 +107,9 @@ 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") @@ -115,7 +117,9 @@ def test_find_copyright(self): 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") @@ -123,7 +127,9 @@ def test_find_copyright(self): 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) @@ -136,7 +142,9 @@ 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") @@ -144,7 +152,9 @@ def test_find_spdx_copyright(self): 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") @@ -152,7 +162,9 @@ def test_find_spdx_copyright(self): 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) @@ -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, ) @@ -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) @@ -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) @@ -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, ) @@ -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, ) @@ -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() @@ -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) @@ -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) @@ -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 @@ -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")