riftenlabs-indexer/linters/copyright_check.py

53 lines
2.1 KiB
Python
Raw Normal View History

2024-03-14 10:51:25 +01:00
#!/usr/bin/env python3
2026-01-21 12:34:59 +01:00
# Copyright (C) 2024-2026 Whiterun LLC,
2024-03-14 10:51:25 +01:00
# This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.,
# A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
import os
2026-01-21 12:34:59 +01:00
import re
2024-03-14 10:51:25 +01:00
import sys
required_lines = [
"This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.",
"A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html"
]
2026-01-21 12:34:59 +01:00
# Regex pattern to match copyright lines with Whiterun LLC
# Matches: "Copyright (C) 2026 Whiterun LLC" or "Copyright (C) 2024-2026 Whiterun LLC"
copyright_pattern = re.compile(r"Copyright \(C\) (\d{4}-)?\d{4} Whiterun LLC")
2024-03-14 10:51:25 +01:00
OUR_PATH = os.path.dirname(os.path.realpath(__file__))
2026-01-21 12:34:59 +01:00
def check_file_for_lines(file_path, lines, copyright_pattern):
"""Check if the file contains all the specified lines and a valid copyright line."""
2024-03-14 10:51:25 +01:00
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Check if all required lines are present
required_present = all(line in content for line in lines)
2026-01-21 12:34:59 +01:00
# Check if copyright pattern matches
copyright_present = bool(copyright_pattern.search(content))
return required_present and copyright_present
2024-03-14 10:51:25 +01:00
def check_files(directories):
"""Traverse the src directory and check each .rs file."""
missing = 0
for directory in directories:
for root, dirs, files in os.walk(os.path.join(OUR_PATH, "..", directory)):
for file in files:
if file.endswith('.rs') or file.endswith('.py'):
file_path = os.path.join(root, file)
2026-01-21 12:34:59 +01:00
if not check_file_for_lines(file_path, required_lines, copyright_pattern):
2024-03-14 10:51:25 +01:00
print(f"{file_path}: Missing")
missing += 1
if missing:
print(f"{missing} file(s) are missing copyright headers")
sys.exit(1)
else:
print(f"OK")
sys.exit(0)
check_files(['src', 'contrib', 'linters'])