Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .template_sync/manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
template_version: 0.1.41
template_version: 0.1.42
migrations:
- from: 0.0.0
to: 0.0.1
Expand Down Expand Up @@ -129,3 +129,6 @@ migrations:
- from: 0.1.40
to: 0.1.41
script: migrations/v0_1_40_to_v0_1_41.py
- from: 0.1.41
to: 0.1.42
script: migrations/v0_1_41_to_v0_1_42.py
126 changes: 126 additions & 0 deletions .template_sync/migrations/v0_1_41_to_v0_1_42.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""Template 0.1.41 -> 0.1.42: no menuinst 2.5.2 in the installer, and the release names its own version on the installer's last page
when construct.yaml has conclusion_text wrapped over several lines.

Design note: https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_42.md

1. environment.yaml asks for `menuinst>=2,!=2.5.2` (was `menuinst>=2`): menuinst 2.5.2 writes `Icon={_escape_desktop_string(self.render_key('icon')}`
unrendered into the Linux .desktop file, so the menu entry has no icon (conda/menuinst#535, fixed upstream but not released).
2. bump_version.py joins a `conclusion_text` written as a plain scalar over several lines (the repository initialiser wraps it for a long
project name) before it updates the version in it: the one-line update missed it, and the installer of LabConstrictor-CAREamics 0.0.3
said 0.0.2 on its last page.

What this changes in an app repository: in environment.yaml, only the line `- menuinst>=2` is changed (the app may have changed other
lines); a line with another menuinst requirement is left as it is (SKIPPED, with a note of what to change by hand), and no line is added
when there is none. bump_version.py is replaced by the 0.1.42 file when it is still the template file last shipped (the 0.1.41 copy;
compared with LF line endings; the file keeps its own line endings); a file the app changed stays as it is (SKIPPED, with a note). A
missing file is not created, and nothing is deleted. The changes reach users with the app's next release.
"""
from __future__ import annotations

import re
from pathlib import Path
from typing import Any

DOCS_URL = "https://github.com/CellMigrationLab/LabConstrictor/blob/main/.tools/docs/migration_0_1_42.md"
TAG = "[migration 0.1.42]"
RESOURCE_ROOT = Path(__file__).resolve().parents[1] / "resources" / "v0_1_41_to_v0_1_42"
PREVIOUS_ROOT = RESOURCE_ROOT / "previous"

ENVIRONMENT = Path("environment.yaml")
BUMP = Path(".tools/python/bump_version.py")
FILES = (BUMP,) # replaced whole when they are the 0.1.41 template file

MENUINST_OLD = "menuinst>=2"
MENUINST_NEW = "menuinst>=2,!=2.5.2"
# A dependency line naming menuinst (with or without a channel and a version): ` - menuinst>=2 # comment`
MENUINST_LINE_RE = re.compile(
r"^(?P<head>[ \t]*-[ \t]*)(?P<spec>[\"']?(?:[\w.-]+::)?menuinst(?![\w.-])[^#]*?)(?P<tail>[ \t]*(?:#.*)?)$"
)

BY_HAND = {
ENVIRONMENT: f"exclude menuinst 2.5.2 in the menuinst requirement of environment.yaml (the template has `- {MENUINST_NEW}`)",
BUMP: (
"in bump_construct_text, join a conclusion_text written over several lines (continuation lines more indented than the key) into "
"one line before the version is updated, as join_wrapped_conclusion of the template's script does; or write conclusion_text on "
"one line in construct.yaml"
),
}


def log(message: str) -> None:
print(f"{TAG} {message}")


def lf_text(path: Path) -> str:
return path.read_bytes().decode("utf-8").replace("\r\n", "\n")


def skipped(relative: Path, reason: str) -> str:
return f"SKIPPED: {reason}, so it was left unchanged. By hand: {BY_HAND[relative]}. See {DOCS_URL}"


def replaced_file(relative: Path, text_lf: str) -> tuple[str, str]:
"""The 0.1.42 text of `relative` for the app's `text_lf`, and what was done. Unchanged when it is not the 0.1.41 template file."""
new = lf_text(RESOURCE_ROOT / relative)
if text_lf == new:
return text_lf, "already up to date"
if text_lf == lf_text(PREVIOUS_ROOT / relative):
return new, "replaced by the 0.1.42 file"
return text_lf, skipped(relative, "customised (not the 0.1.41 template file); compare it with the template's file")


def updated_environment(text_lf: str) -> tuple[str, str]:
"""environment.yaml with `- menuinst>=2` changed to exclude 2.5.2, and what was done. Only that line changes."""
lines = text_lf.split("\n")
found = []
for index, line in enumerate(lines):
match = MENUINST_LINE_RE.match(line)
if match:
found.append((index, match))
if not found:
return text_lf, "no menuinst requirement (nothing was added); the template's environment.yaml has `- " + MENUINST_NEW + "`"
specs = [match.group("spec").strip() for _, match in found]
others = [spec for spec in specs if spec not in (MENUINST_OLD, MENUINST_NEW)]
if others:
return text_lf, skipped(ENVIRONMENT, f"customised menuinst requirement ({', '.join(others)})")
if all(spec == MENUINST_NEW for spec in specs):
return text_lf, "already up to date"
for index, match in found:
if match.group("spec").strip() == MENUINST_OLD:
lines[index] = f"{match.group('head')}{MENUINST_NEW}{match.group('tail')}"
return "\n".join(lines), f"`{MENUINST_OLD}` changed to `{MENUINST_NEW}`"


def update(path: Path, relative: Path) -> str:
data = path.read_bytes().decode("utf-8")
newline = "\r\n" if "\r\n" in data else "\n"
original = data.replace("\r\n", "\n")
if relative == ENVIRONMENT:
text, note = updated_environment(original)
else:
text, note = replaced_file(relative, original)
if text == original:
return note if note.startswith("SKIPPED") else f"unchanged: {note}"
path.write_bytes(text.replace("\n", newline).encode("utf-8"))
return f"updated: {note}"


def migrate(repo_root: Path, context: dict[str, Any]) -> None:
"""Apply the 0.1.42 changes (idempotent, never raises for a customised file, creates and deletes nothing)."""
_ = context
changed = 0
for relative in (ENVIRONMENT, *FILES):
path = repo_root / relative
if not path.is_file():
log(f"{relative}: not present in this repository (nothing was created)")
continue
try:
status = update(path, relative)
except (OSError, UnicodeDecodeError) as exc:
status = f"SKIPPED: {exc}; left unchanged. See {DOCS_URL}"
changed += status.startswith("updated")
log(f"{relative}: {status}")
if changed:
log(f"done: {changed} file(s) changed. The changes reach users with the app's next release.")
else:
log("done: no repository changes were required.")
Loading
Loading