Skip to content

Commit 033ef81

Browse files
committed
Add a function to calculate the SHA256 to the migration template
This is useful to only apply migrations when the contents of the file are well known. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 7d21f0b commit 033ef81

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

.github/cookiecutter-migrate.template.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
And remember to follow any manual instructions for each run.
2121
""" # noqa: E501
2222

23+
import hashlib
2324
import os
2425
import subprocess
2526
import tempfile
@@ -98,6 +99,26 @@ def replace_file_contents_atomically( # noqa; DOC501
9899
raise
99100

100101

102+
def calculate_file_sha256_skip_lines(filepath: Path, skip_lines: int) -> str | None:
103+
"""Calculate SHA256 of file contents excluding the first N lines.
104+
105+
Args:
106+
filepath: Path to the file to hash
107+
skip_lines: Number of lines to skip at the beginning
108+
109+
Returns:
110+
The SHA256 hex digest, or None if the file doesn't exist
111+
"""
112+
if not filepath.exists():
113+
return None
114+
115+
# Read file and normalize line endings to LF
116+
content = filepath.read_text(encoding="utf-8").replace("\r\n", "\n")
117+
# Skip first N lines and ensure there's a trailing newline
118+
remaining_content = "\n".join(content.splitlines()[skip_lines:]) + "\n"
119+
return hashlib.sha256(remaining_content.encode()).hexdigest()
120+
121+
101122
def manual_step(message: str) -> None:
102123
"""Print a manual step message in yellow."""
103124
print(f"\033[0;33m>>> {message}\033[0m")

0 commit comments

Comments
 (0)