Skip to content

Commit 223cdaf

Browse files
committed
Add migration step to add flake8-datetimez
Automatically adds the flake8-datetimez dependency to migrated projects. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 528d798 commit 223cdaf

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

cookiecutter/migrate.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def main() -> None:
4040
print("Migrating pyproject license metadata to SPDX format...")
4141
migrate_pyproject_license()
4242
print("=" * 72)
43+
print("Adding flake8-datetimez plugin to dev-flake8 dependencies...")
44+
migrate_add_flake8_datetimez()
45+
print("=" * 72)
4346
print("Migration script finished. Remember to follow any manual instructions.")
4447
print("=" * 72)
4548

@@ -277,6 +280,40 @@ def migrate_pyproject_license() -> None: # pylint: disable=too-many-branches
277280
print(" Updated pyproject.toml: migrated license metadata")
278281

279282

283+
def migrate_add_flake8_datetimez() -> None:
284+
"""Add the flake8-datetimez plugin to dev-flake8 dependencies."""
285+
pyproject_path = Path("pyproject.toml")
286+
if not pyproject_path.exists():
287+
print(" Skipping pyproject.toml (file not found)")
288+
return
289+
290+
content = pyproject_path.read_text(encoding="utf-8")
291+
292+
if "flake8-datetimez" in content:
293+
print(" Skipped pyproject.toml (flake8-datetimez already present)")
294+
return
295+
296+
# Look for a pinned flake8 dependency line (e.g. "flake8 == 7.3.0") and
297+
# insert flake8-datetimez right after it.
298+
match = re.search(r'( "flake8\s*==.*",?\n)', content)
299+
if not match:
300+
manual_step(
301+
"Could not find a flake8 pin in pyproject.toml. "
302+
'Please add `"flake8-datetimez == 20.10.0"` to the '
303+
"`dev-flake8` optional dependencies."
304+
)
305+
return
306+
307+
flake8_line = match.group(1)
308+
new_content = content.replace(
309+
flake8_line,
310+
flake8_line + ' "flake8-datetimez == 20.10.0",\n',
311+
1,
312+
)
313+
replace_file_contents_atomically(pyproject_path, content, new_content, count=1)
314+
print(" Updated pyproject.toml: added flake8-datetimez plugin")
315+
316+
280317
def read_project_type() -> str | None:
281318
"""Read the cookiecutter project type from the replay file."""
282319
replay_path = Path(".cookiecutter-replay.json")

0 commit comments

Comments
 (0)