|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +from typing import Set |
| 7 | + |
| 8 | + |
| 9 | +def parse_args() -> argparse.Namespace: |
| 10 | + """Parses command-line arguments.""" |
| 11 | + parser = argparse.ArgumentParser(description='Check license headers in files') |
| 12 | + parser.add_argument('--files', required=True, nargs="+", type=Path, |
| 13 | + help='List of files to check') |
| 14 | + parser.add_argument('--license', required=True, |
| 15 | + help='License to check for') |
| 16 | + parser.add_argument('--check-lines', type=int, default=10, |
| 17 | + help='Number of lines to check (default: %(default)s)') |
| 18 | + parser.add_argument('--extensions', required=True, nargs="+", |
| 19 | + help='List of file extensions to check') |
| 20 | + parser.add_argument('--verbose', action='store_true', |
| 21 | + help='Print verbose output (default: %(default)s)') |
| 22 | + return parser.parse_args() |
| 23 | + |
| 24 | + |
| 25 | +def should_check_file(file_path: Path, allowed_extensions: Set[str]) -> bool: |
| 26 | + return file_path.suffix in allowed_extensions |
| 27 | + |
| 28 | + |
| 29 | +def check_license_header(file_path: Path, license_header: str, check_lines: int) -> bool: |
| 30 | + try: |
| 31 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 32 | + for _ in range(check_lines): |
| 33 | + line = f.readline() |
| 34 | + if license_header in line: |
| 35 | + return True |
| 36 | + return False |
| 37 | + except (UnicodeDecodeError, StopIteration): |
| 38 | + # Handle files that can't be read as text or have fewer lines |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +def main() -> int: |
| 43 | + args = parse_args() |
| 44 | + |
| 45 | + if not args.files: |
| 46 | + print("No files to check") |
| 47 | + return 0 |
| 48 | + |
| 49 | + num_errors = 0 |
| 50 | + |
| 51 | + for file_path in args.files: |
| 52 | + # Skip non-existent files |
| 53 | + if not file_path.exists(): |
| 54 | + continue |
| 55 | + |
| 56 | + # Skip files with non-matching extensions |
| 57 | + if not should_check_file(file_path, args.extensions): |
| 58 | + print(f"ℹ️ Skipping file with unchecked extension: {file_path}") |
| 59 | + continue |
| 60 | + |
| 61 | + # Check license header |
| 62 | + if check_license_header(file_path, args.license, args.check_lines): |
| 63 | + if args.verbose: |
| 64 | + print(f"✅ License header found in: {file_path}") |
| 65 | + else: |
| 66 | + print(f"❌ Missing license header in: {file_path}") |
| 67 | + num_errors += 1 |
| 68 | + |
| 69 | + if num_errors > 0: |
| 70 | + sys.exit(1) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + main() |
0 commit comments