Skip to content

Commit

Permalink
WiP
Browse files Browse the repository at this point in the history
  • Loading branch information
y0urself committed Nov 15, 2023
1 parent 20c0e11 commit 03a873e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
29 changes: 17 additions & 12 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"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
Expand Down Expand Up @@ -136,14 +137,17 @@ def _add_header(
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)
i = i - 1

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

View check run for this annotation

Codecov / codecov/patch

pontos/updateheader/updateheader.py#L149-L150

Added lines #L149 - L150 were not covered by tests
for regex in cleanup_regexes:
if regex.match(line):
changed = True
Expand All @@ -152,7 +156,8 @@ def _remove_outdated(
break
i = i + 1
if changed:
return "\n".join(splitted_lines) + "\n"
new_content = "\n".join(splitted_lines) + "\n"
return new_content
return None


Expand Down Expand Up @@ -226,15 +231,6 @@ def _update_file(
"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 Down Expand Up @@ -265,13 +261,22 @@ def _update_file(

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(

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

View check run for this annotation

Codecov / codecov/patch

pontos/updateheader/updateheader.py#L272-L273

Added lines #L272 - L273 were not covered by tests
content=old_content, cleanup_regexes=cleanup_regexes
)
if new_content:
file.write_text(new_content, encoding="utf-8")
print(f"{file}: Cleaned up!")

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

View check run for this annotation

Codecov / codecov/patch

pontos/updateheader/updateheader.py#L277-L278

Added lines #L277 - L278 were not covered by tests
return 0


def _get_exclude_list(
Expand Down
12 changes: 7 additions & 5 deletions tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
_get_modified_year as get_modified_year,
)
from pontos.updateheader.updateheader import _parse_args as parse_args
from pontos.updateheader.updateheader import _remove_outdated as remove_outdated
from pontos.updateheader.updateheader import (
_remove_outdated_lines as remove_outdated_lines,
)
from pontos.updateheader.updateheader import _update_file as update_file
from pontos.updateheader.updateheader import main

Expand Down Expand Up @@ -533,7 +535,7 @@ def test_main_never_happen(self, argparser_mock, mock_stdout):
ret,
)

def test_remove_outdated(self):
def test_remove_outdated_lines(self):
test_content = """* This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
Expand All @@ -547,12 +549,12 @@ def test_remove_outdated(self):

compiled_regexes = compile_outdated_regex()

new_content = remove_outdated(
new_content = remove_outdated_lines(
content=test_content, cleanup_regexes=compiled_regexes
)
self.assertEqual(new_content, "\n")

def test_remove_outdated2(self):
def test_remove_outdated_lines2(self):
test_content = """the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Expand All @@ -566,7 +568,7 @@ def test_remove_outdated2(self):

compiled_regexes = compile_outdated_regex()

new_content = remove_outdated(
new_content = remove_outdated_lines(
content=test_content, cleanup_regexes=compiled_regexes
)
self.assertEqual(new_content, "\n")

0 comments on commit 03a873e

Please sign in to comment.