Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Find Copyright headers with old company #927

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@
"GNU Affero General Public License for more details.",
"GNU General Public License for more details.",
"You should have received a copy of the GNU Affero General Public License",
"You should have received a copy of the GNU General Public License",
"along with this program. If not, see <http://www.gnu.org/licenses/>.",
"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 +98,10 @@

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 @@ -135,29 +137,34 @@
raise ValueError


def _remove_outdated(
def _remove_outdated_lines(
content: str, cleanup_regexes: List[re.Pattern]
) -> Optional[str]:
"""Remove lines that contain outdated copyright header ..."""
changed = False
splitted_lines = content.splitlines()
i = 0
for line in splitted_lines[:20]:
if i > 3 and re.match(r"^(([#*]|//) ?$)", line):
splitted_lines.pop(i)
continue
for regex in cleanup_regexes:
if regex.match(line):
("match")
y0urself marked this conversation as resolved.
Show resolved Hide resolved
changed = True
splitted_lines.pop(i)
i = i - 1
break
i = i + 1
if changed:
return "\n".join(splitted_lines)
new_content = "\n".join(splitted_lines) + "\n"
return new_content
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,
Expand Down Expand Up @@ -186,7 +193,9 @@
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
)
i = i - 1
# header not found, add header
if i == 0 and not found:
Expand All @@ -201,9 +210,7 @@
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 All @@ -217,15 +224,6 @@
"is not existing."
)
return 1
# old header existing - cleanup?
if cleanup_regexes:
old_content = file.read_text(encoding="utf-8")
new_content = _remove_outdated(
content=old_content, cleanup_regexes=cleanup_regexes
)
if new_content:
file.write_text(new_content, encoding="utf-8")
print(f"{file}: Cleaned up!")
# replace found header and write it to file
if copyright_match and (
not copyright_match["modification_year"]
Expand All @@ -236,9 +234,9 @@
copyright_term = (
f"SPDX-FileCopyrightText: "
f'{copyright_match["creation_year"]}'
f'-{parsed_args.year} {copyright_match["company"]}'
f"-{parsed_args.year} {parsed_args.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 All @@ -256,13 +254,22 @@

else:
print(f"{file}: License Header is ok.")
return 0
except FileNotFoundError as e:
print(f"{file}: File is not existing.")
raise e
except UnicodeDecodeError as e:
print(f"{file}: Ignoring binary file.")
raise e
# old header existing - cleanup?
if cleanup_regexes:
old_content = file.read_text(encoding="utf-8")
new_content = _remove_outdated_lines(
content=old_content, cleanup_regexes=cleanup_regexes
)
if new_content:
file.write_text(new_content, encoding="utf-8")
print(f"{file}: Cleaned up!")
return 0


def _get_exclude_list(
Expand Down Expand Up @@ -405,6 +412,16 @@
return regexes


def _compile_copyright_regex(company: Union[str, List[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})"

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

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

View check run for this annotation

Codecov / codecov/patch

pontos/updateheader/updateheader.py#L421

Added line #L421 was not covered by tests
return re.compile(rf"{c_str}.*? {d_str}?-? ?{d_str}? ({'|'.join(company)})")


def main() -> None:
parsed_args = _parse_args()
exclude_list = []
Expand Down Expand Up @@ -443,10 +460,10 @@
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]
)

cleanup_regexes: Optional[List[re.Pattern]] = None
if parsed_args.cleanup:
cleanup_regexes = _compile_outdated_regex()
Expand All @@ -458,7 +475,7 @@
else:
_update_file(
file=file,
regex=regex,
copyright_regex=copyright_regex,
parsed_args=parsed_args,
term=term,
cleanup_regexes=cleanup_regexes,
Expand Down
Loading