Skip to content
Open
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
14 changes: 10 additions & 4 deletions cr_checker/tool/cr_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def remove_old_header(file_path, encoding, num_of_chars):
shutil.move(temp_file.name, file_path)


def fix_copyright(path, copyright_text, encoding, offset, config=None):
def fix_copyright(path, copyright_text, encoding, offset, config=None) -> bool:
"""
Inserts a copyright header into the specified file, ensuring that existing
content is preserved according to the provided offset.
Expand All @@ -493,6 +493,8 @@ def fix_copyright(path, copyright_text, encoding, offset, config=None):
are preserved.
config (Path): Path to the config JSON file where configuration
variables are stored (e.g. years for copyright headers).
Returns:
bool: True if the copyright header was successfully added, False if there was an error
"""

temporary_file = create_temp_file(path, encoding)
Expand All @@ -503,7 +505,7 @@ def fix_copyright(path, copyright_text, encoding, offset, config=None):

if offset > 0 and offset != byte_array:
LOGGER.error("Invalid offset value: %d, expected: %d", offset, byte_array)
return
return False

with open(path, "w", encoding=encoding) as handle:
temp.seek(0)
Expand All @@ -518,6 +520,7 @@ def fix_copyright(path, copyright_text, encoding, offset, config=None):
for chunk in iter(lambda: temp.read(4096), ""):
handle.write(chunk)
LOGGER.info("Fixed missing header in: %s", path)
return True


def process_files(
Expand Down Expand Up @@ -583,9 +586,12 @@ def process_files(
if fix:
if remove_offset:
remove_old_header(item, encoding, remove_offset)
fix_copyright(item, templates[key], encoding, effective_offset, config)
fix_result = fix_copyright(
item, templates[key], encoding, effective_offset, config
)
results["no_copyright"] += 1
results["fixed"] += 1
if fix_result:
results["fixed"] += 1
else:
LOGGER.error(
"Missing copyright header in: %s, use --fix to introduce it", item
Expand Down