diff --git a/pontos/updateheader/updateheader.py b/pontos/updateheader/updateheader.py index b17e46cb8..bdccd2724 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: + 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