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.32
template_version: 0.1.33
migrations:
- from: 0.0.0
to: 0.0.1
Expand Down Expand Up @@ -102,3 +102,6 @@ migrations:
- from: 0.1.31
to: 0.1.32
script: migrations/v0_1_31_to_v0_1_32.py
- from: 0.1.32
to: 0.1.33
script: migrations/v0_1_32_to_v0_1_33.py
118 changes: 118 additions & 0 deletions .template_sync/migrations/v0_1_32_to_v0_1_33.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""Template 0.1.32 -> 0.1.33: Tools v0.3.0-alpha.3 (Fiji opens results of 3 or 4 planes); the Windows Apps list shows the right version and icon.

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

Tools v0.3.0-alpha.2 wrote a result image of 3 or 4 planes (3 channels, 3 timepoints) as one page of colour samples, which Fiji cannot open
unless it is 8-bit (LabConstrictor-Tools #163); v0.3.0-alpha.3 writes it as grey pages. What this changes in an app repository (each step is
independent, idempotent and never deletes anything):

1. `LC_TOOLS_PIN` in app/bash_bat_scripts/post_install.sh and post_install.bat moves from `v0.3.0-alpha.2` to `v0.3.0-alpha.3`, ONLY when the
line still holds exactly the old template value. An app with its own pin, a script without a pin line or without the Tools hook stays as it is,
with a note.

2. post_install.bat registered the app in the Windows Apps list with the version written at initialisation (`DISPLAY_VERSION`; no
release ever changed it) and the icon `logo.ico`, a file no installer contains. The version is now read at install time from the
bundled construct.yaml (the setup-time value stays as a fallback), and the icon is the first .ico the installer copied into the app
folder; without one, no icon entry is written. Only the template's exact lines are replaced; a changed script stays as it is, with a note.

Since template 0.1.32 the Tools guide no longer names the pin, so no document changes. Both 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_33.md"
TAG = "[migration 0.1.33]"
OLD_PIN = "v0.3.0-alpha.2"
NEW_PIN = "v0.3.0-alpha.3"
POST_INSTALL_SH = Path("app/bash_bat_scripts/post_install.sh")
POST_INSTALL_BAT = Path("app/bash_bat_scripts/post_install.bat")
SH_PIN_LINE = re.compile(r'^([ \t]*LC_TOOLS_PIN=")([^"]*)(".*)$')
# The Windows Apps list block of post_install.bat (CRLF in the script; matched without line endings). The app's own setup-time version is kept.
OLD_ARP = re.compile(r'^SET "DISPLAY_ICON=%PROJECT_ROOT%\\logo\.ico"\r?\nSET "DISPLAY_VERSION=([^"\r\n]*)"(?=\r?\n)', re.MULTILINE)
NEW_ARP_LINES = (
"REM Windows Apps list: the version of this installer (the version: line of the bundled construct.yaml, written by each release),",
"REM and the app icon the installer copied (the first .ico in the app folder); no icon entry when there is none.",
'SET "DISPLAY_VERSION={version}"',
'IF EXIST "%PROJECT_ROOT%\\construct.yaml" FOR /F "usebackq tokens=1,* delims=: " %%A IN (`findstr /B /C:"version:" "%PROJECT_ROOT%\\construct.yaml"`) DO SET "DISPLAY_VERSION=%%~B"',
'SET "DISPLAY_ICON="',
'FOR %%I IN ("%PROJECT_ROOT%\\*.ico") DO IF NOT DEFINED DISPLAY_ICON SET "DISPLAY_ICON=%%~fI"',
)
OLD_ICON_REG = 'reg add "%ARP_KEY%" /v DisplayIcon /d "%DISPLAY_ICON%" /f >> "%LOG_FILE%" 2>&1'
NEW_ICON_REG = "IF DEFINED DISPLAY_ICON " + OLD_ICON_REG
BAT_PIN_LINE = re.compile(r'^([ \t]*SET "LC_TOOLS_PIN=)([^"]*)(".*)$', re.IGNORECASE)


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


def read(path: Path) -> str:
"""The file's text with its line endings as they are (bytes decoded as UTF-8)."""
return path.read_bytes().decode("utf-8")


def write(path: Path, text: str) -> None:
path.write_bytes(text.encode("utf-8"))


def move_pin(path: Path, pattern: re.Pattern[str]) -> str:
text = read(path)
if "labconstrictor_tools" not in text:
return "has no Tools bridge hook: nothing to move"
lines = text.splitlines(keepends=True)
hits = [(index, match) for index, line in enumerate(lines) if (match := pattern.match(line.rstrip("\r\n")))]
if not hits:
return "has no LC_TOOLS_PIN line (unpinned source or its own Tools source): left as it is"
if len(hits) > 1:
return f"has {len(hits)} LC_TOOLS_PIN lines: left as it is, set the pin by hand to {NEW_PIN}"
index, match = hits[0]
current = match.group(2)
if current == NEW_PIN:
return f"already pins Tools {NEW_PIN}: unchanged"
if current != OLD_PIN:
return f"has its own pin {current}: left as it is (the template pin is now {NEW_PIN})"
ending = lines[index][len(lines[index].rstrip("\r\n")) :]
lines[index] = f"{match.group(1)}{NEW_PIN}{match.group(3)}{ending}"
write(path, "".join(lines))
return f"updated: Tools pin {OLD_PIN} -> {NEW_PIN}"


def fix_apps_list(path: Path) -> str:
text = read(path)
if NEW_ICON_REG in text:
return "already reads the Apps-list version and icon at install time: unchanged"
match = OLD_ARP.search(text)
if match is None or text.count(OLD_ICON_REG) != 1:
return "has no template Apps-list block (changed by hand): left as it is"
newline = "\r\n" if "\r\n" in text else "\n"
block = newline.join(NEW_ARP_LINES).replace("{version}", match.group(1))
text = text[: match.start()] + block + text[match.end() :]
write(path, text.replace(OLD_ICON_REG, NEW_ICON_REG))
return "updated: the Windows Apps list shows the installed version and the app icon"


def migrate(repo_root: Path, context: dict[str, Any]) -> None:
"""Move the Tools pin to v0.3.0-alpha.3 and fix the Windows Apps-list entry (idempotent, surgical, never raises for a customised file)."""
_ = context
changed = 0
steps = ((POST_INSTALL_SH, lambda p: move_pin(p, SH_PIN_LINE)), (POST_INSTALL_BAT, lambda p: move_pin(p, BAT_PIN_LINE)),
(POST_INSTALL_BAT, fix_apps_list))
for relative, step in steps:
path = repo_root / relative
if not path.is_file():
log(f"{relative}: not present in this repository (nothing was created)")
continue
try:
status = step(path)
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} change(s). They reach users with the next release of the app. See {DOCS_URL}")
else:
log("done: no repository changes were required.")
25 changes: 25 additions & 0 deletions .tools/docs/migration_0_1_33.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Template 0.1.33: Tools v0.3.0-alpha.3, and the right version and icon in the Windows Apps list

Design note for the 0.1.33 migration (`.template_sync/migrations/v0_1_32_to_v0_1_33.py`). Context: [LabConstrictor-Tools #163](https://github.com/CellMigrationLab/LabConstrictor-Tools/pull/163).

## Why

LabConstrictor-Tools v0.3.0-alpha.2 wrote a result image with exactly 3 or 4 planes (for example 3 channels or 3 timepoints) as one page of colour
samples. Fiji cannot open such a file unless it is 8-bit, so the result of a tool like *Align channels* on a 3-channel image did not appear in Fiji
(napari showed it). v0.3.0-alpha.3 writes every result image of 3 or more dimensions as grey planes, except an 8-bit colour image
([LabConstrictor-Tools #163](https://github.com/CellMigrationLab/LabConstrictor-Tools/pull/163)).

On Windows, `post_install.bat` also registers the app in *Settings > Apps*. It wrote the version given when the repository was initialised
(`DISPLAY_VERSION`), which no release ever changed (the release script looked for a `PKG_VERSION` line the script no longer has), and the
icon `logo.ico`, which no installer contains: the Apps list showed, for example, VLab4Mic 0.1.3 after installing 0.1.8, without an icon.

## What changes in an app repository

* `LC_TOOLS_PIN` in `app/bash_bat_scripts/post_install.sh` and `post_install.bat` moves from `v0.3.0-alpha.2` to `v0.3.0-alpha.3`, only when the
line still holds the template's old value. A pin you set yourself is left as it is, and the sync log says so.
* `post_install.bat` reads the version at install time from the `version:` line of the bundled `construct.yaml` (which each release
writes), keeping the initialisation value as a fallback, and uses the first `.ico` file the installer copied into the app folder as the
icon; without one it writes no icon entry. Only the template's exact lines are replaced; a block you changed is left as it is, with a note.

Nothing else changes. Both changes reach users with the app's next release. The tag `v0.3.0-alpha.3` must exist before such an installer
is built: an installer that pins a missing tag installs the app without its napari / Fiji / QuPath tools (the installer log says so).
10 changes: 7 additions & 3 deletions app/bash_bat_scripts/post_install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,19 @@ IF ERRORLEVEL 1 (

SET "ARP_KEY=HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\UNDERSCORED_PROJECT_NAME"
SET "UNINSTALL_EXE=%PREFIX%\Uninstall-UNDERSCORED_PROJECT_NAME.exe"
SET "DISPLAY_ICON=%PROJECT_ROOT%\logo.ico"
REM Windows Apps list: the version of this installer (the version: line of the bundled construct.yaml, written by each release),
REM and the app icon the installer copied (the first .ico in the app folder); no icon entry when there is none.
SET "DISPLAY_VERSION=VERSION_NUMBER"
IF EXIST "%PROJECT_ROOT%\construct.yaml" FOR /F "usebackq tokens=1,* delims=: " %%A IN (`findstr /B /C:"version:" "%PROJECT_ROOT%\construct.yaml"`) DO SET "DISPLAY_VERSION=%%~B"
SET "DISPLAY_ICON="
FOR %%I IN ("%PROJECT_ROOT%\*.ico") DO IF NOT DEFINED DISPLAY_ICON SET "DISPLAY_ICON=%%~fI"
SET "PUBLISHER=GITHUB_OWNER"
echo Registering PROJECT_NAME in Windows Apps list >> "%LOG_FILE%"
reg add "%ARP_KEY%" /v DisplayName /d "PROJECT_NAME" /f >> "%LOG_FILE%" 2>&1
reg add "%ARP_KEY%" /v DisplayVersion /d "%DISPLAY_VERSION%" /f >> "%LOG_FILE%" 2>&1
reg add "%ARP_KEY%" /v Publisher /d "%PUBLISHER%" /f >> "%LOG_FILE%" 2>&1
reg add "%ARP_KEY%" /v InstallLocation /d "%PREFIX%" /f >> "%LOG_FILE%" 2>&1
reg add "%ARP_KEY%" /v DisplayIcon /d "%DISPLAY_ICON%" /f >> "%LOG_FILE%" 2>&1
IF DEFINED DISPLAY_ICON reg add "%ARP_KEY%" /v DisplayIcon /d "%DISPLAY_ICON%" /f >> "%LOG_FILE%" 2>&1
reg add "%ARP_KEY%" /v UninstallString /d "\"%UNINSTALL_EXE%\"" /f >> "%LOG_FILE%" 2>&1
reg add "%ARP_KEY%" /v QuietUninstallString /d "\"%UNINSTALL_EXE%\" /S" /f >> "%LOG_FILE%" 2>&1
reg add "%ARP_KEY%" /v NoModify /t REG_DWORD /d 1 /f >> "%LOG_FILE%" 2>&1
Expand Down Expand Up @@ -235,7 +239,7 @@ REM This step must never fail the installation: a failure is logged as a WARNING
REM LC_TOOLS_SPEC can point to another source (wheel, git URL, mirror).
REM Default source: the GitHub archive of the labconstrictor-tools release TAG below (a plain zip: no git needed on the user's computer),
REM because the package is not on PyPI yet. Once it is, use "labconstrictor-tools" here. To move the pin, change only this tag.
SET "LC_TOOLS_PIN=v0.3.0-alpha.2"
SET "LC_TOOLS_PIN=v0.3.0-alpha.3"
IF NOT DEFINED LC_TOOLS_SPEC SET "LC_TOOLS_SPEC=https://github.com/CellMigrationLab/LabConstrictor-Tools/archive/refs/tags/%LC_TOOLS_PIN%.zip"
SET "LC_TOOLS_READY=0"
"%PYTHON_EXE%" -c "import importlib.util, sys; sys.exit(0 if importlib.util.find_spec('PYTHON_PROJ_NAME_lc_tools') else 1)" >> "%LOG_FILE%" 2>&1
Expand Down
2 changes: 1 addition & 1 deletion app/bash_bat_scripts/post_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ if [ -f "$PROJECT_ROOT/setup.py" ]; then
# (it never fails the installation by itself). LC_TOOLS_SPEC can point to another source (wheel, git URL, mirror).
# Default source: the GitHub archive of the labconstrictor-tools release TAG below (a plain zip: no git needed on the user's computer),
# because the package is not on PyPI yet. Once it is, use "labconstrictor-tools" here. To move the pin, change only this tag.
LC_TOOLS_PIN="v0.3.0-alpha.2"
LC_TOOLS_PIN="v0.3.0-alpha.3"
LC_TOOLS_MODULE="PYTHON_PROJ_NAME_lc_tools"
LC_TOOLS_READY=0
if "$PYTHON_EXE" -c "import importlib.util, sys; sys.exit(0 if importlib.util.find_spec('$LC_TOOLS_MODULE') else 1)" >> "$LOG_FILE" 2>&1; then
Expand Down
3 changes: 3 additions & 0 deletions tests/_v0_1_32.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pathlib import Path
from types import ModuleType

from _v0_1_33 import before_0_1_33

ROOT = Path(__file__).resolve().parents[1]
MIGRATION_PATH = ROOT / ".template_sync/migrations/v0_1_31_to_v0_1_32.py"

Expand All @@ -26,6 +28,7 @@ def _migration() -> ModuleType:

def before_0_1_32(relative: Path | str, data: bytes) -> bytes:
"""`data` (a live template file at `relative`) without the change 0.1.32 made to it; any other file is returned as it is."""
data = before_0_1_33(relative, data)
migration = _migration()
relative = Path(relative)
if relative not in migration.FILES:
Expand Down
40 changes: 40 additions & 0 deletions tests/_v0_1_33.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Helper of the tests: a live template file as it was before template 0.1.33.

Template 0.1.33 moves the Tools pin (`LC_TOOLS_PIN` in post_install.sh / .bat) from v0.3.0-alpha.2 to v0.3.0-alpha.3, and rewrites the
Windows Apps-list block of post_install.bat. The tests of the older migrations compare with the live files; `before_0_1_33` gives the bytes of
a live file with both changes undone, so they keep checking what they checked. The changes are tested in test_template_sync_0_1_33.py.
"""
from __future__ import annotations

import importlib.util
from pathlib import Path

PIN_0_1_28 = "v0.3.0-alpha.2"
PIN_0_1_33 = "v0.3.0-alpha.3"
PINNED_FILES = (Path("app/bash_bat_scripts/post_install.sh"), Path("app/bash_bat_scripts/post_install.bat"))


MIGRATION_PATH = Path(__file__).resolve().parents[1] / ".template_sync/migrations/v0_1_32_to_v0_1_33.py"


def _apps_list_blocks() -> tuple[str, str, str, str]:
"""(new block, old block, new icon line, old icon line) of the template's post_install.bat, from the migration itself."""
spec = importlib.util.spec_from_file_location("migration_0_1_33_for_older_tests", MIGRATION_PATH)
if spec is None or spec.loader is None:
raise RuntimeError(f"Unable to load {MIGRATION_PATH}")
migration = importlib.util.module_from_spec(spec)
spec.loader.exec_module(migration)
new = "\r\n".join(migration.NEW_ARP_LINES).replace("{version}", "VERSION_NUMBER")
old = 'SET "DISPLAY_ICON=%PROJECT_ROOT%\\logo.ico"\r\nSET "DISPLAY_VERSION=VERSION_NUMBER"'
return new, old, migration.NEW_ICON_REG, migration.OLD_ICON_REG


def before_0_1_33(relative: Path | str, data: bytes) -> bytes:
"""`data` (a live template file at `relative`) with the 0.1.33 pin moved back and the old Apps-list block; any other file as it is."""
if Path(relative) not in PINNED_FILES:
return data
data = data.replace(PIN_0_1_33.encode(), PIN_0_1_28.encode())
if Path(relative).suffix == ".bat":
new, old, new_reg, old_reg = _apps_list_blocks()
data = data.replace(new.encode(), old.encode()).replace(new_reg.encode(), old_reg.encode())
return data
21 changes: 20 additions & 1 deletion tests/smoke/post_install_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
("UNDERSCORED_PROJECT_NAME", "Demo_App"),
("PYTHON_PROJ_NAME", "demoapp"),
("PROJECT_NAME", APP),
("VERSION_NUMBER", "1.2.3"),
("VERSION_NUMBER", "0.0.1"), # the initialisation value; construct.yaml says 1.2.3 (the Apps list must show 1.2.3)
("GITHUB_OWNER", "CellMigrationLab"),
)
SETUP_PY = 'from setuptools import find_packages, setup\nsetup(name="demoapp", version="1.2.3", package_dir={"": "src"}, packages=find_packages("src"))\n'
Expand Down Expand Up @@ -100,6 +100,7 @@ def write_project(root: Path) -> None:
(root / "requirements.txt").write_text("setuptools\n", encoding="utf-8")
(root / "construct.yaml").write_text("name: Demo_App\nversion: 1.2.3\n", encoding="utf-8")
(root / "notebook_launcher.json").write_text("{}", encoding="utf-8")
(root / "demo_app.ico").write_bytes(b"") # the icon the installer copies: the Windows Apps list points at it


def render_script(base: Path) -> Path:
Expand Down Expand Up @@ -155,6 +156,22 @@ def check_undeclared(name: str, entry: dict[str, object], log: str) -> None:
print(f"OK {name}: no hosts, and the log says so", flush=True)


def check_apps_list(prefix: Path) -> None:
"""Windows: the Apps-list entry has the version of construct.yaml (1.2.3, not the initialisation value) and the copied .ico."""
key = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\Demo_App"
found: dict[str, str] = {}
for value in ("DisplayVersion", "DisplayIcon"):
out = subprocess.run(["reg", "query", key, "/v", value], capture_output=True, text=True, check=False).stdout
line = next((row for row in out.splitlines() if row.strip().startswith(value)), "")
found[value] = line.split("REG_SZ", 1)[1].strip() if "REG_SZ" in line else ""
subprocess.run(["reg", "delete", key, "/f"], capture_output=True, check=False)
if found["DisplayVersion"] != "1.2.3":
raise AssertionError(f"Apps list: DisplayVersion {found['DisplayVersion']!r}, expected the construct.yaml version 1.2.3")
if Path(found["DisplayIcon"]) != prefix / APP / "demo_app.ico":
raise AssertionError(f"Apps list: DisplayIcon {found['DisplayIcon']!r}, expected {prefix / APP / 'demo_app.ico'}")
print(f"OK Windows Apps list: version 1.2.3, icon {found['DisplayIcon']}", flush=True)


def install_case(script: Path, prefix: Path, lc_home: Path) -> tuple[dict[str, object], str]:
"""Install once into a fresh registry home: (the registry entry of the app, the installer log)."""
log = install(script, prefix, lc_home)
Expand All @@ -174,6 +191,8 @@ def main() -> int:
target.write_bytes(manifest.read_bytes())
entry, log = install_case(script, prefix, base / f"home_{manifest.stem}")
check_declared(manifest.name, manifest, entry, log)
if WINDOWS and manifest == manifests[0]:
check_apps_list(prefix)
target.write_bytes(b"\xef\xbb\xbfschema: 1\nname: Demo\n\"hosts\": [notebook, napari]\n")
entry, log = install_case(script, prefix, base / "home_bom_quoted_key")
if entry.get("hosts") != ["notebook", "napari"]:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_template_sync_0_1_28.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def load(path: Path, name: str) -> ModuleType:


def live(relative: Path) -> bytes:
"""The live template file without the later change of 0.1.32 (the guide no longer names the pin: tests/_v0_1_32.py)."""
"""The live template file without the later changes of 0.1.32 (the guide no longer names the pin) and 0.1.33 (the next pin): tests/_v0_1_32.py."""
return before_0_1_32(relative, (ROOT / relative).read_bytes())


class TemplateTests(unittest.TestCase):
def test_the_template_pins_the_new_tag_once_per_script_and_names_it_in_the_guide(self) -> None:
for relative in (SH, BAT):
text = (ROOT / relative).read_bytes().decode("utf-8")
text = live(relative).decode("utf-8")
with self.subTest(file=str(relative)):
self.assertEqual(text.count(NEW), 1)
self.assertNotIn(OLD, text)
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_the_sync_script_runs_the_migration_from_0_1_27(self) -> None:
result = subprocess.run(command, capture_output=True, text=True, check=False)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("[migration 0.1.28]", result.stdout)
self.assertEqual((self.repo / SH).read_bytes(), (ROOT / SH).read_bytes())
self.assertEqual((self.repo / SH).read_bytes(), live(SH))


if __name__ == "__main__":
Expand Down
Loading
Loading