|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Update copyright year in source files to the current year. |
| 4 | +""" |
| 5 | + |
| 6 | +import re |
| 7 | +import sys |
| 8 | +from datetime import datetime |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +# Set current year |
| 13 | +YEAR = str(datetime.now().year) |
| 14 | + |
| 15 | +EXTENSIONS = {".py", ".md"} |
| 16 | + |
| 17 | +# Directories to exclude |
| 18 | +EXCLUDE_DIRS = { |
| 19 | + ".venv", |
| 20 | + "venv", |
| 21 | + "node_modules", |
| 22 | + ".git", |
| 23 | + "__pycache__", |
| 24 | + ".tox", |
| 25 | + "dist", |
| 26 | + "build", |
| 27 | + ".egg-info", |
| 28 | +} |
| 29 | + |
| 30 | +# Regex patterns to match copyright lines |
| 31 | +COPYRIGHT_PATTERNS = [ |
| 32 | + re.compile(r"(# Copyright )(\d{4})( The HuggingFace Team\.)"), |
| 33 | + re.compile(r"(# Copyright \(c\) )(\d{4})( The HuggingFace Team\.)"), |
| 34 | + re.compile(r"(Copyright )(\d{4})( The HuggingFace Team\.)"), |
| 35 | + re.compile(r"(Copyright \(c\) )(\d{4})( The HuggingFace Team\.)"), |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +def should_exclude(path: Path) -> bool: |
| 40 | + """Check if path should be excluded.""" |
| 41 | + return any(excluded in path.parts for excluded in EXCLUDE_DIRS) |
| 42 | + |
| 43 | + |
| 44 | +def update_file(file_path: Path) -> int: |
| 45 | + """Update copyright year in a single file. Returns number of updates.""" |
| 46 | + try: |
| 47 | + content = file_path.read_text(encoding="utf-8") |
| 48 | + except Exception as e: |
| 49 | + print(f"Warning: Could not read {file_path}: {e}", file=sys.stderr) |
| 50 | + return 0 |
| 51 | + |
| 52 | + new_content = content |
| 53 | + total_count = 0 |
| 54 | + |
| 55 | + for pattern in COPYRIGHT_PATTERNS: |
| 56 | + new_content, count = pattern.subn(rf"\g<1>{YEAR}\g<3>", new_content) |
| 57 | + total_count += count |
| 58 | + |
| 59 | + if total_count > 0: |
| 60 | + try: |
| 61 | + file_path.write_text(new_content, encoding="utf-8") |
| 62 | + print(f"✓ Updated {total_count} line(s) in {file_path}") |
| 63 | + except Exception as e: |
| 64 | + print(f"Error: Could not write {file_path}: {e}", file=sys.stderr) |
| 65 | + return 0 |
| 66 | + |
| 67 | + return total_count |
| 68 | + |
| 69 | + |
| 70 | +def main(): |
| 71 | + repo_root = Path(".").resolve() |
| 72 | + |
| 73 | + print(f"Updating copyright to {YEAR}...") |
| 74 | + |
| 75 | + total_files = 0 |
| 76 | + total_updates = 0 |
| 77 | + |
| 78 | + for file_path in repo_root.rglob("*"): |
| 79 | + if not file_path.is_file() or should_exclude(file_path): |
| 80 | + continue |
| 81 | + |
| 82 | + if file_path.suffix in EXTENSIONS: |
| 83 | + updates = update_file(file_path) |
| 84 | + if updates > 0: |
| 85 | + total_files += 1 |
| 86 | + total_updates += updates |
| 87 | + |
| 88 | + print(f"\nSummary: Updated {total_updates} line(s) in {total_files} file(s)") |
| 89 | + |
| 90 | + # Exit with 0 if updates were made, 1 if no updates needed |
| 91 | + return 0 if total_updates > 0 else 1 |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + sys.exit(main()) |
0 commit comments