|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +"""Script to verify SPDX license identifiers are present in all Python files.""" |
| 4 | + |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def check_spdx_header(file_path: Path) -> tuple[bool, str]: |
| 10 | + """Check if a Python file has a valid SPDX license identifier. |
| 11 | +
|
| 12 | + Returns: |
| 13 | + Tuple of (has_valid_spdx, error_message) |
| 14 | + """ |
| 15 | + try: |
| 16 | + with open(file_path, encoding='utf-8') as f: |
| 17 | + lines = f.readlines() |
| 18 | + except Exception as e: |
| 19 | + return False, f"Error reading file: {e}" |
| 20 | + |
| 21 | + if not lines: |
| 22 | + return False, "File is empty" |
| 23 | + |
| 24 | + # Check first few lines for SPDX identifier |
| 25 | + for line in lines[:10]: # Check first 10 lines |
| 26 | + if 'SPDX-License-Identifier:' in line: |
| 27 | + # Verify it's the correct MIT license |
| 28 | + if 'MIT' in line: |
| 29 | + # Check that it's a properly formatted comment |
| 30 | + stripped = line.strip() |
| 31 | + if stripped.startswith('#') and 'SPDX-License-Identifier: MIT' in stripped: |
| 32 | + return True, "" |
| 33 | + else: |
| 34 | + return False, f"SPDX header found but incorrectly formatted: '{stripped}'" |
| 35 | + else: |
| 36 | + return False, f"SPDX header found but wrong license: '{line.strip()}'" |
| 37 | + |
| 38 | + return False, "No SPDX license identifier found" |
| 39 | + |
| 40 | + |
| 41 | +def main() -> int: |
| 42 | + """Main function to check SPDX headers in all Python files.""" |
| 43 | + project_root = Path(__file__).parent.parent |
| 44 | + |
| 45 | + # Find all Python files |
| 46 | + python_files = list(project_root.glob("**/*.py")) |
| 47 | + |
| 48 | + # Remove this script itself from the check if it exists |
| 49 | + script_path = Path(__file__).resolve() |
| 50 | + python_files = [f for f in python_files if f.resolve() != script_path] |
| 51 | + |
| 52 | + missing_spdx: list[tuple[Path, str]] = [] |
| 53 | + total_files = len(python_files) |
| 54 | + |
| 55 | + print(f"Checking SPDX license identifiers in {total_files} Python files...") |
| 56 | + |
| 57 | + for py_file in python_files: |
| 58 | + has_spdx, error_msg = check_spdx_header(py_file) |
| 59 | + if not has_spdx: |
| 60 | + missing_spdx.append((py_file, error_msg)) |
| 61 | + |
| 62 | + # Report results |
| 63 | + if missing_spdx: |
| 64 | + print(f"\n❌ SPDX Check FAILED: {len(missing_spdx)} file(s) missing or have invalid SPDX headers:") |
| 65 | + for file_path, error in missing_spdx: |
| 66 | + rel_path = file_path.relative_to(project_root) |
| 67 | + print(f" - {rel_path}: {error}") |
| 68 | + |
| 69 | + print("\nTo fix these issues, add the following line at the top of each file") |
| 70 | + print("(after any shebang line):") |
| 71 | + print(" # SPDX-License-Identifier: MIT") |
| 72 | + |
| 73 | + return 1 # Exit with error code |
| 74 | + else: |
| 75 | + print(f"✅ SPDX Check PASSED: All {total_files} Python files have valid SPDX headers") |
| 76 | + return 0 |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + sys.exit(main()) |
0 commit comments