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 0a530b7 commit a4af7d9
Showing 1 changed file with 35 additions and 14 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

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:
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:
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

0 comments on commit a4af7d9

Please sign in to comment.