|
1 | | -# Copyright 2025 CoderDeltaLAN |
2 | | -# SPDX-License-Identifier: MIT |
3 | | - |
4 | 1 | from __future__ import annotations |
5 | 2 |
|
| 3 | +from collections.abc import Iterator, Sequence |
6 | 4 | from pathlib import Path |
7 | 5 |
|
| 6 | +DEFAULT_IGNORES: tuple[str, ...] = ( |
| 7 | + ".git", |
| 8 | + ".venv", |
| 9 | + "venv", |
| 10 | + "env", |
| 11 | + "dist", |
| 12 | + "build", |
| 13 | + "__pycache__", |
| 14 | + ".mypy_cache", |
| 15 | + ".pytest_cache", |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def _iter_files(root: Path, exts: Sequence[str]) -> Iterator[Path]: |
| 20 | + exts_l = {e.lower() for e in exts} |
| 21 | + for path in root.rglob("*"): |
| 22 | + if any(part in DEFAULT_IGNORES for part in path.parts): |
| 23 | + continue |
| 24 | + if path.is_file() and path.suffix.lower() in exts_l: |
| 25 | + yield path |
| 26 | + |
| 27 | + |
| 28 | +_COMMENT_MAP: dict[str, str] = { |
| 29 | + ".py": "# ", |
| 30 | + ".sh": "# ", |
| 31 | + ".ts": "// ", |
| 32 | + ".js": "// ", |
| 33 | + ".c": "// ", |
| 34 | + ".h": "// ", |
| 35 | + ".cpp": "// ", |
| 36 | + ".hpp": "// ", |
| 37 | + ".java": "// ", |
| 38 | + ".go": "// ", |
| 39 | + ".rs": "// ", |
| 40 | +} |
8 | 41 |
|
9 | | -def ping() -> str: |
10 | | - return "pong" |
| 42 | + |
| 43 | +def _comment_prefix(ext: str) -> str: |
| 44 | + return _COMMENT_MAP.get(ext.lower(), "# ") |
11 | 45 |
|
12 | 46 |
|
13 | 47 | def _normalize(s: str) -> str: |
14 | | - return s.replace("\r\n", "\n").replace("\r", "\n") |
| 48 | + return "\n".join(line.strip() for line in s.strip().splitlines()) |
| 49 | + |
| 50 | + |
| 51 | +def default_header_text(license_id: str = "MIT", ext: str = ".py") -> str: |
| 52 | + # Cabecera mínima (puede expandirse con Copyright si se desea) |
| 53 | + return f"{_comment_prefix(ext)}SPDX-License-Identifier: {license_id}\n" |
| 54 | + |
15 | 55 |
|
| 56 | +def header_for_path(path: Path, license_id: str = "MIT") -> str: |
| 57 | + return default_header_text(license_id=license_id, ext=path.suffix) |
16 | 58 |
|
17 | | -def has_header(path: Path, header_text: str) -> bool: |
18 | | - data = path.read_text(encoding="utf-8", errors="strict") |
19 | | - return _normalize(data).startswith(_normalize(header_text)) |
20 | 59 |
|
| 60 | +def has_header(path: Path, header: str) -> bool: |
| 61 | + text = path.read_text(encoding="utf-8", errors="ignore") |
| 62 | + head = "\n".join(text.splitlines()[:50]) |
| 63 | + return _normalize(header) in _normalize(head) |
21 | 64 |
|
22 | | -def ensure_header(path: Path, header_text: str, autofix: bool = False) -> bool: |
23 | | - if has_header(path, header_text): |
| 65 | + |
| 66 | +# Compat: acepta Path o str (contenido). Si str y header no dado, busca cadena SPDX. |
| 67 | +def has_spdx_header(obj: Path | str, header: str | None = None, *, license_id: str = "MIT") -> bool: |
| 68 | + if isinstance(obj, Path): |
| 69 | + h = header or header_for_path(obj, license_id=license_id) |
| 70 | + return has_header(obj, h) |
| 71 | + text = obj |
| 72 | + if header is not None: |
| 73 | + return _normalize(header) in _normalize("\n".join(text.splitlines()[:50])) |
| 74 | + return "SPDX-License-Identifier:" in text.splitlines()[0:50].__str__() |
| 75 | + |
| 76 | + |
| 77 | +def ensure_header(path: Path, header: str, *, autofix: bool = False) -> bool: |
| 78 | + # Semántica "ensure": True si el archivo TERMINA con header (lo tuviera o lo agreguemos) |
| 79 | + if has_header(path, header): |
24 | 80 | return True |
25 | 81 | if not autofix: |
26 | 82 | return False |
27 | | - original = path.read_text(encoding="utf-8", errors="strict") |
28 | | - path.write_text(_normalize(header_text) + original, encoding="utf-8") |
| 83 | + text = path.read_text(encoding="utf-8", errors="ignore") |
| 84 | + # Evita duplicados si corre en paralelo |
| 85 | + if not has_header(path, header): |
| 86 | + path.write_text(f"{header}{'' if header.endswith('\n') else '\n'}{text}", encoding="utf-8") |
29 | 87 | return True |
| 88 | + |
| 89 | + |
| 90 | +def check_headers(root: Path, exts: Sequence[str], license_id: str) -> list[Path]: |
| 91 | + missing: list[Path] = [] |
| 92 | + for fp in _iter_files(root, exts): |
| 93 | + hdr = header_for_path(fp, license_id=license_id) |
| 94 | + if not has_header(fp, hdr): |
| 95 | + missing.append(fp) |
| 96 | + return missing |
| 97 | + |
| 98 | + |
| 99 | +def fix_headers(root: Path, exts: Sequence[str], license_id: str) -> list[Path]: |
| 100 | + fixed: list[Path] = [] |
| 101 | + for fp in _iter_files(root, exts): |
| 102 | + hdr = header_for_path(fp, license_id=license_id) |
| 103 | + if ensure_header(fp, hdr, autofix=True): |
| 104 | + fixed.append(fp) |
| 105 | + return fixed |
| 106 | + |
| 107 | + |
| 108 | +__all__ = [ |
| 109 | + "DEFAULT_IGNORES", |
| 110 | + "default_header_text", |
| 111 | + "header_for_path", |
| 112 | + "has_header", |
| 113 | + "has_spdx_header", |
| 114 | + "ensure_header", |
| 115 | + "check_headers", |
| 116 | + "fix_headers", |
| 117 | +] |
0 commit comments