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

Apply hook changes after add-license-headers runs #108

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Changes from all 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
102 changes: 68 additions & 34 deletions src/ansys/pre_commit_hooks/add_license_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"""
import argparse
from datetime import date as dt
import difflib
import filecmp
import json
import os
Expand Down Expand Up @@ -301,15 +300,16 @@ def check_exists(
args = set_header_args(parser, year, files[i], copyright, template)
header.run(args, proj, tmp)

# check diffs between files and do modifications afterwards
add_hook_changes(before_hook, files[i])
# Check if the file before add-license-headers was run is the same as the one
# after add-license-headers was run. If not, apply the syntax changes
# from other hooks before add-license-headers was run to the file
if check_same_content(before_hook, files[i]) == False:
add_hook_changes(before_hook, files[i])

# Compare the tempfile with the updated file
# same_content is True if the two files that are being compared are the same
same_content = filecmp.cmp(before_hook, files[i], shallow=False)

# Print header was successfully changed if it was modified
if same_content == False:
# Check if the file content before add-license-headers was run has been changed
# Assuming the syntax was fixed in the above if statement, this check is
# solely for the file's content
if check_same_content(before_hook, files[i]) == False:
changed_headers = 1
print(f"Successfully changed header of {files[i]}")

Expand All @@ -318,43 +318,77 @@ def check_exists(
return changed_headers


def add_hook_changes(before_hook: str, after_hook: str) -> None:
def check_same_content(before_hook, after_hook):
"""
Add earlier hook changes to updated file with header.
Check if file before the hook ran is the same as after the hook ran.

Parameters
----------
before_hook: str
Path to file before add-license-headers was run.
after_hook: str
Path to file after add-license-headers was run.
"""
# Open files
before_file = open(before_hook, "r")
after_file = open(after_hook, "r")

# Compare the files before and after the hook ran
diff = difflib.ndiff(after_file.readlines(), before_file.readlines())
Returns
-------
bool
``True`` if the files have the same content.
``False`` if the files have different content.
"""
# Check if the files have the same content
same_files = filecmp.cmp(before_hook, after_hook, shallow=False)
# If the files are different, return False. Otherwise, return True
if same_files == False:
return False
else:
return True

before_file.close()
after_file.close()

# Combine the changes from before the hook with the changes after the hook
with NamedTemporaryFile(mode="w", delete=False) as tmp:
while True:
try:
line = str(next(diff))
if "+" in line[0] and line[2:] == "\n":
tmp.write(line[2:])
elif "-" in line[0]:
tmp.write(line[2:])
elif "?" in line[0]:
continue
elif " " in line[0]:
tmp.write(line[2:])
except StopIteration:
break
def add_hook_changes(before_hook: str, after_hook: str) -> None:
"""
Add earlier hook changes to updated file with header.

Parameters
----------
before_hook: str
Path to file before add-license-headers was run.
after_hook: str
Path to file after add-license-headers was run.
"""
# Compare each line of the file before and after add-license-headers was run,
# and combine the files into one
with open(before_hook, "r") as before_edit, open(
after_hook, "r"
) as after_edit, NamedTemporaryFile(mode="w", delete=False) as tmp:
for before_edit_line, after_edit_line2 in zip(before_edit, after_edit):
klmcadams marked this conversation as resolved.
Show resolved Hide resolved
# If the lines are different,
if before_edit_line != after_edit_line2:
# If the after_edit_line is empty, and the before_edit_line is the
# same as the next after_edit_line, remove the space by writing the
# before_edit_line. For example:
#
# before_edit file after_edit file
# line 1: """ line 1:
# line 2: print("example") line 2: """
# We want to remove the blank space, so we take line 1 from the before_edit file.
if not after_edit_line2.strip() and (before_edit_line == next(after_edit)):
tmp.write(before_edit_line)
# If the before_edit_line is empty, write that line and the next line in
# the before_edit file. For example:
#
# before_edit file after_edit file
# line 1: line 1: print("example")
# line 2: print("example") line 2: print("example 2")
# We want to keep the blank space, so we take lines 1 & 2 from the before_edit file.
elif not before_edit_line.strip():
tmp.write(f"{before_edit_line}{next(before_edit)}")
# Otherwise, write the line from the after_edit file
else:
tmp.write(after_edit_line2)
else:
tmp.write(before_edit_line)

# Close the tmp file after writing to it
tmp.close()

# Copy the file with combined changes to the file that was
Expand Down
Loading