Write file only if content differs from existing file. This prevents unnecessary file modifications that would trigger Meson regeneration. If the file doesn't exist or content has changed, writes the new content atomically. Args: path: Path to file to write con
(path: Path, content: str, mode: Optional[int] = None)
| 47 | |
| 48 | |
| 49 | def write_if_different(path: Path, content: str, mode: Optional[int] = None) -> bool: |
| 50 | """ |
| 51 | Write file only if content differs from existing file. |
| 52 | |
| 53 | This prevents unnecessary file modifications that would trigger Meson |
| 54 | regeneration. If the file doesn't exist or content has changed, writes |
| 55 | the new content atomically. |
| 56 | |
| 57 | Args: |
| 58 | path: Path to file to write |
| 59 | content: Content to write |
| 60 | mode: Optional file permissions (Unix only) |
| 61 | |
| 62 | Returns: |
| 63 | True if file was written (created or modified), False if unchanged |
| 64 | """ |
| 65 | try: |
| 66 | if path.exists(): |
| 67 | existing_content = path.read_text(encoding="utf-8") |
| 68 | if existing_content == content: |
| 69 | return False # Content unchanged, skip write |
| 70 | |
| 71 | # Content changed or file doesn't exist - write it atomically |
| 72 | atomic_write_text(path, content) |
| 73 | |
| 74 | # Set permissions if specified (Unix only) |
| 75 | if mode is not None and not sys.platform.startswith("win"): |
| 76 | path.chmod(mode) |
| 77 | |
| 78 | return True |
| 79 | except (OSError, IOError) as e: |
| 80 | # On error, try to write anyway - caller will handle failures |
| 81 | _ts_print(f"[MESON] Warning: Error checking file {path}: {e}") |
| 82 | atomic_write_text(path, content) |
| 83 | if mode is not None and not sys.platform.startswith("win"): |
| 84 | path.chmod(mode) |
| 85 | return True |
no test coverage detected