Insert the SPDX header into file content, returning the new content.
(content: str, comment: str, path: Path)
| 242 | |
| 243 | |
| 244 | def insert_header(content: str, comment: str, path: Path) -> str: |
| 245 | """Insert the SPDX header into file content, returning the new content.""" |
| 246 | header = make_header(comment) |
| 247 | lines = content.splitlines(keepends=True) |
| 248 | insert_at = find_insertion_point(lines, path) |
| 249 | |
| 250 | if insert_at == 0: |
| 251 | # Header at top, blank line before existing content (if any). |
| 252 | if lines: |
| 253 | return header + "\n" + content |
| 254 | return header |
| 255 | else: |
| 256 | # Insert after a first-line directive (shebang / # syntax=). |
| 257 | before = lines[:insert_at] |
| 258 | after = lines[insert_at:] |
| 259 | return "".join(before) + "\n" + header + "\n" + "".join(after) |
| 260 | |
| 261 | |
| 262 | # --------------------------------------------------------------------------- |
no test coverage detected