Process a single file. Returns True if the file is compliant.
(path: Path, root: Path, *, check: bool, verbose: bool)
| 265 | |
| 266 | |
| 267 | def process_file(path: Path, root: Path, *, check: bool, verbose: bool) -> bool: |
| 268 | """Process a single file. Returns True if the file is compliant.""" |
| 269 | rel = path.relative_to(root) |
| 270 | comment = get_comment_style(rel) |
| 271 | if comment is None: |
| 272 | return True |
| 273 | |
| 274 | content = path.read_text(encoding="utf-8") |
| 275 | lines = content.splitlines() |
| 276 | |
| 277 | if has_header(lines): |
| 278 | if verbose: |
| 279 | print(f" ok: {rel}") |
| 280 | return True |
| 281 | |
| 282 | if check: |
| 283 | print(f" MISSING: {rel}") |
| 284 | return False |
| 285 | |
| 286 | # Insert the header. |
| 287 | new_content = insert_header(content, comment, rel) |
| 288 | path.write_text(new_content, encoding="utf-8") |
| 289 | if verbose: |
| 290 | print(f" added: {rel}") |
| 291 | return True |
| 292 | |
| 293 | |
| 294 | def main() -> int: |
no test coverage detected