Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbvll committed Jun 19, 2024
1 parent c9e288f commit 5324aa0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/py/flwr_tool/check_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def _get_file_creation_year(filepath: str):
["git", "log", "--diff-filter=A", "--format=%ai", "--", filepath],
stdout=subprocess.PIPE,
text=True,
check=True,
)
date_str = result.stdout.splitlines()[-1] # Get the first commit date
creation_year = date_str.split("-")[0] # Extract the year
Expand Down
39 changes: 23 additions & 16 deletions src/py/flwr_tool/fix_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,33 @@
from flwr_tool.init_py_check import get_init_dir_list_and_warnings


def _insert_or_edit_copyright(py_file: Path) -> None:
contents = py_file.read_text()
lines = contents.splitlines()
creation_year = _get_file_creation_year(str(py_file.absolute()))
expected_copyright = COPYRIGHT_FORMAT.format(creation_year)

if expected_copyright not in contents:
if "Copyright" in lines[0]:
end_index = 0
for idx, line in enumerate(lines):
if (
line.strip()
== COPYRIGHT_FORMAT.rsplit("\n", maxsplit=1)[-1].strip()
):
end_index = idx + 1
break
lines = lines[end_index:]

lines.insert(0, expected_copyright)
py_file.write_text("\n".join(lines))


def _fix_copyright(dir_list: List[str]) -> None:
for valid_dir in dir_list:
dir_path = Path(valid_dir)
for py_file in dir_path.glob("*.py"):
contents = py_file.read_text()
lines = contents.splitlines()
creation_year = _get_file_creation_year(str(py_file.absolute()))
expected_copyright = COPYRIGHT_FORMAT.format(creation_year)

if expected_copyright not in contents:
if "Copyright" in lines[0]:
end_index = 0
for i, line in enumerate(lines):
if line.strip() == COPYRIGHT_FORMAT.split("\n")[-1].strip():
end_index = i + 1
break
lines = lines[end_index:]

lines.insert(0, expected_copyright)
py_file.write_text("\n".join(lines))
_insert_or_edit_copyright(py_file)


if __name__ == "__main__":
Expand Down

0 comments on commit 5324aa0

Please sign in to comment.