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
12 changes: 12 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
Unreleased
==========
TF804 follows the renames table the template action writes.

* TF804 renames-table reads the two columns of ``renames.tsv`` (``old_path``,
``new_path``), where ``new_path`` is the current path, ``n/a`` (removed, still served)
or ``purge`` (removed from S3). It no longer requires a ``date`` column, which failed
every row of the Archive's table. It checks the header, duplicates, paths outside the
template, old paths that still exist, new paths that do not, and old paths pointing at
other old paths.
* TF112 says whether a listed file was renamed, removed or purged.

26.0.0rc3 (December 3, 2026)
============================
Index values are not zero-padded.

* New rule TF117 zero-padded-index: ``res``, ``cohort``, ``scale`` and ``inv`` values
Expand Down
6 changes: 3 additions & 3 deletions tfmanager/schema/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ TF111:
TF112:
slug: renamed-file
severity: error
summary: This path was renamed; it is listed as an old name in `renames.tsv`.
fix: Use the new name, or remove the row from the renames table.
summary: This path is listed as an old name in `.renames.tsv` (renamed, removed or purged).
fix: Use the new name, or remove the row from `.renames.tsv`.
TF113:
slug: atlas-label
severity: warning
Expand Down Expand Up @@ -426,4 +426,4 @@ TF804:
slug: renames-table
severity: error
summary: The renames table is inconsistent (old paths still present, new paths missing, duplicates).
fix: Fix `renames.tsv` (old_path, new_path, date).
fix: "Fix the template's `.renames.tsv`: columns `old_path` and `new_path`, where `new_path` is the current path, `n/a` (removed, still served) or `purge` (removed from S3)."
40 changes: 34 additions & 6 deletions tfmanager/tests/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'TF113': 14,
'TF203': 2, # template_sample.tsv
'TF504': 6,
'TF117': 278, # res-0N file names and res keys
}


Expand Down Expand Up @@ -105,12 +106,39 @@ def test_templates_table(tmp_path):
def test_renames(tmp_path):
root = _archive(tmp_path)
(root / 'tpl-A' / 'tpl-A_res-1_T1w.nii.gz').write_text('')
(root / 'tpl-B' / 'y').write_text('')
(root / 'renames.tsv').write_text(
'old_path\tnew_path\tdate\n'
'tpl-A/tpl-A_res-1_T1w.nii.gz\ttpl-A/tpl-A_res-01_T1w.nii.gz\t2027-01-01\n'
'tpl-B/x\ttpl-B/y\t2027-01-01\n'
'tpl-B/y\ttpl-B/x\t2027-01-02\n'
'old_path\tnew_path\n'
'tpl-A/tpl-A_res-01_T1w.nii.gz\ttpl-A/tpl-A_res-1_T1w.nii.gz\n'
'tpl-A/tpl-A_desc-old_T1w.nii.gz\tn/a\n'
'tpl-A/tpl-A_desc-bad_T1w.nii.gz\tpurge\n'
'tpl-B/x\ttpl-B/y\n'
)
assert [i.message for i in check_archive(root) if i.code == 'TF804'] == []


@pytest.mark.parametrize(
('rows', 'message'),
[
('tpl-A/tpl-A_res-1_T1w.nii.gz\tn/a\n', 'still exists'),
('tpl-A/x\ttpl-A/y\n', 'tpl-A/y does not exist'),
('tpl-A/x\ttpl-B/y\n', 'not a path in tpl-A'),
('tpl-A/x\tremoved\n', 'not a path in tpl-A'),
('x\tn/a\n', 'template directory'),
('tpl-A/x\tn/a\ntpl-A/x\tpurge\n', 'listed twice'),
('tpl-A/x\n', 'two non-empty cells'),
('tpl-B/x\ttpl-B/y\ntpl-B/y\ttpl-B/z\n', 'itself an old path'),
],
)
def test_renames_errors(tmp_path, rows, message):
root = _archive(tmp_path)
(root / 'tpl-A' / 'tpl-A_res-1_T1w.nii.gz').write_text('')
(root / 'renames.tsv').write_text('old_path\tnew_path\n' + rows)
messages = [i.message for i in check_archive(root) if i.code == 'TF804']
assert any('still exists' in m for m in messages)
assert any('cycle' in m for m in messages)
assert any(message in m for m in messages), messages


def test_renames_header(tmp_path):
root = _archive(tmp_path)
(root / 'renames.tsv').write_text('old_path\tnew_path\tdate\n')
assert any('header' in i.message for i in check_archive(root) if i.code == 'TF804')
20 changes: 12 additions & 8 deletions tfmanager/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ def test_legacy_pairs_only_in_legacy_mode():
)


def test_renamed_file():
row = f'{T}/{T}_res-1_T1w.nii.gz\t{T}/x.nii.gz\t2027-01-01\n'
tree = make_tree(
**{
'.renames.tsv': 'old_path\tnew_path\tdate\n' + row,
}
)
assert 'TF112' in codes(tree)
@pytest.mark.parametrize(
('new', 'message'),
[
('x.nii.gz', 'Renamed to x.nii.gz.'),
('n/a', 'Recorded as removed.'),
('purge', 'Recorded as purged.'),
],
)
def test_renamed_file(new, message):
tree = make_tree(**{'.renames.tsv': f'old_path\tnew_path\n{T}_res-1_T1w.nii.gz\t{new}\n'})
issues = [i for i in validate_template(tree, mode='strict') if i.code == 'TF112']
assert [i.message for i in issues] == [message]


LAYOUT_CASES = [
Expand Down
69 changes: 45 additions & 24 deletions tfmanager/validate/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from __future__ import annotations

import configparser
import csv
import io
import json
from pathlib import Path

Expand Down Expand Up @@ -96,33 +94,56 @@ def check_archive(root: str | Path, legacy: bool = False) -> list[Issue]:
return issues


#: ``new_path`` keywords: removed but still served, and removed from S3
RENAME_KEYWORDS = ('n/a', 'purge')


def check_renames(root: Path, legacy: bool = False) -> list[Issue]:
"""TF804: the root renames table (old_path, new_path, date)."""
"""TF804: the root renames table, which the template action writes.

Two columns, ``old_path`` and ``new_path``, with paths that start with the template
directory. ``new_path`` is the file's current path, ``n/a`` (removed from the
template, still served) or ``purge`` (removed from S3). Old paths point at current
names, never at other old paths.
"""
table = root / 'renames.tsv'
if not table.exists():
return []
issues = []
seen: dict[str, str] = {}
rows = list(csv.DictReader(io.StringIO(table.read_text()), delimiter='\t'))
for n, row in enumerate(rows, start=2):
old, new = row.get('old_path') or '', row.get('new_path') or ''
if not old or not new or not row.get('date'):
issues.append(_issue('TF804', 'renames.tsv', f'row {n}: incomplete.', legacy))

def report(message):
issues.append(_issue('TF804', 'renames.tsv', message, legacy))

lines = table.read_text().splitlines()
if not lines or lines[0].split('\t') != ['old_path', 'new_path']:
report('The header must be old_path<TAB>new_path.')
return issues
rows: dict[str, str] = {}
for n, line in enumerate(lines[1:], start=2):
cells = line.split('\t')
if len(cells) != 2 or not all(cells):
report(f'row {n}: needs two non-empty cells.')
continue
if old in seen and seen[old] != new:
issues.append(_issue('TF804', 'renames.tsv', f'row {n}: {old} renamed twice.', legacy))
seen[old] = new
old, new = cells
if old in rows:
report(f'row {n}: {old} is listed twice.')
continue
rows[old] = new
template = old.split('/')[0]
if (root / template).is_dir() and any((root / template).iterdir()):
if (root / old).is_symlink() or (root / old).exists():
issues.append(
_issue('TF804', 'renames.tsv', f'row {n}: {old} still exists.', legacy)
)
# Chains are resolved by the client; a cycle would never resolve
for old in seen:
hops, current = 0, old
while current in seen and hops <= len(seen):
current, hops = seen[current], hops + 1
if hops > len(seen):
issues.append(_issue('TF804', 'renames.tsv', f'{old}: renames form a cycle.', legacy))
if not template.startswith('tpl-') or '/' not in old:
report(f'row {n}: {old} does not start with a template directory.')
continue
if new not in RENAME_KEYWORDS and new.split('/')[0] != template:
report(f'row {n}: {new} is not a path in {template}, n/a or purge.')
continue
installed = (root / template).is_dir() and any((root / template).iterdir())
if not installed:
continue
if (root / old).is_symlink() or (root / old).exists():
report(f'row {n}: {old} still exists.')
if new not in RENAME_KEYWORDS and not ((root / new).is_symlink() or (root / new).exists()):
report(f'row {n}: {new} does not exist.')
for old, new in rows.items():
if new in rows:
report(f'{old}: points at {new}, which is itself an old path.')
return issues
4 changes: 3 additions & 1 deletion tfmanager/validate/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,6 @@ def check_renamed(ctx: Context):
for row in csv.DictReader(io.StringIO(data), delimiter='\t'):
old = (row.get('old_path') or '').removeprefix(f'{ctx.tree.name}/')
if old in present:
ctx.report('TF112', old, f'Renamed to {row.get("new_path")}.')
new = row.get('new_path')
what = {'n/a': 'Recorded as removed', 'purge': 'Recorded as purged'}
ctx.report('TF112', old, f'{what.get(new, f"Renamed to {new}")}.')