in atr/tasks/checks/license.py [0:0]
def _headers_check_core_logic(artifact_path: str) -> dict[str, Any]:
"""Verify Apache License headers in source files within an archive."""
# We could modify @Lucas-C/pre-commit-hooks instead for this
# But hopefully this will be robust enough, at least for testing
files_checked = 0
files_with_valid_headers = 0
errors = []
# First find and validate the root directory
try:
root_dir = targz.root_directory(artifact_path)
except ValueError as e:
return {
"files_checked": 0,
"files_with_valid_headers": 0,
"errors": [],
"error_message": None,
"warning_message": f"Could not determine root directory: {e!s}",
"valid": False,
}
# Check files in the archive
with tarfile.open(artifact_path, mode="r|gz") as tf:
for member in tf:
if member.name and member.name.split("/")[-1].startswith("._"):
# Metadata convention
continue
processed, result = _headers_check_core_logic_process_file(tf, member, root_dir)
if not processed:
continue
files_checked += 1
if result.get("error"):
errors.append(result["error"])
elif result.get("valid"):
files_with_valid_headers += 1
else:
# Should be impossible
raise RuntimeError("Logic error")
# Prepare result message
if files_checked == 0:
message = "No source files found to check for license headers"
# No files to check is not a failure
valid = True
else:
# Require all files to have valid headers
valid = files_checked == files_with_valid_headers
message = f"Checked {files_checked} files, found {files_with_valid_headers} with valid headers"
return {
"files_checked": files_checked,
"files_with_valid_headers": files_with_valid_headers,
"errors": errors,
"message": message,
"valid": valid,
}