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
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ Unreleased
becomes 2.0.0 once every template is at 2.0.0. ``--check`` changes nothing and exits
with 1 when there are files to write.
* ``tfmgr add`` writes the generated files of the template before validating it.
* New command ``tfmgr migrate headers ARCHIVE [--needs] [-o TSV]`` reads, with nibabel, the
facts of image headers that the migration to TemplateFlow 2.0 needs (grids of volumes
without ``res``, volumes of 4D probabilistic segmentations, GIFTI intents), from an
Archive checkout whose image data are present (``--needs`` lists the files to fetch).
The table is keyed by md5, so migration plans need no image data; the facts of the
Archive are shipped with tfmanager (``migrate/data/headers-2.0.0.tsv``). Snapshots of
templates, from the v2 skeleton zip or from a checkout, have the same fingerprint.
* New internals for ``tfmgr generate`` and ``tfmgr migrate``: ``GitTree`` reads a template
from a git commit without checking it out, ``collect_archive`` gathers what the
Archive's files and checks need from its templates, and ``renames_problems`` checks a
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tfmanager =
data/*.LICENSE
schema/*.json
schema/*.yaml
migrate/data/*.tsv

[options.packages.find]
exclude =
Expand Down
73 changes: 73 additions & 0 deletions tfmanager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,79 @@ def generate(kind, path, check):
raise SystemExit(1 if check and found else 0)


@cli.group()
def migrate():
"""Move the Archive's templates to TemplateFlow 2.0 (names and metadata)."""


@migrate.command()
@click.argument('archive', type=click.Path(exists=True, file_okay=False), default='.')
@click.option(
'-t', '--template', 'templates', multiple=True, help='Only this template (tpl-<label>).'
)
@click.option(
'--needs',
is_flag=True,
help='List the files whose facts are needed but whose data are absent, and stop.',
)
@click.option(
'-o',
'--output',
type=click.Path(dir_okay=False, writable=True),
help='Write the table to this file (default: the standard output).',
)
def headers(archive, templates, needs, output):
"""Read the facts of image headers that the migration needs, from an Archive checkout.

The files are those whose names the migration cannot settle without their header:
volumes without res (or in templates without res blocks), probabilistic segmentations
without label, and GIFTI files without a suffix the rule set allows. Their data must be
present (git-annex): fetch the files --needs lists, e.g. with `datalad get`.

Exits with 1 when data are missing, and with 2 on errors (tfmanager[content] is needed
to read headers).
"""
from .migrate.headers import Header, dump, needed, read_header
from .migrate.snapshot import load_checkout
from .tree import DirectoryTree

root = Path(archive)
names = templates or sorted(
p.name for p in root.iterdir() if p.name.startswith('tpl-') and (p / '.git').exists()
)
wanted, absent = [], []
for name in names:
snapshot = load_checkout(root / name)
tree = DirectoryTree(root / name, trusted=True)
for path in needed(snapshot):
wanted.append((f'{name}/{path}', snapshot.entries[path].md5))
if tree.local_path(path) is None:
absent.append(f'{name}/{path}')
Comment thread
oesteban marked this conversation as resolved.
Resolved
if needs:
for path in absent:
click.echo(path)
click.echo(f'{len(wanted)} files needed, {len(absent)} without data.', err=True)
return
if absent:
click.echo(f'{len(absent)} files have no data, e.g. {absent[0]}.', err=True)
click.echo('List them with --needs and fetch them (datalad get).', err=True)
raise SystemExit(1)
found = []
try:
for path, md5 in wanted:
# The link's name, not the annex object's, tells nibabel the format
found.append(Header(path=path, md5=md5, **read_header(root / path)))
except (ImportError, ValueError, OSError) as exc:
click.echo(f'tfmgr migrate headers failed: {exc}', err=True)
raise SystemExit(2) from exc
text = dump(found)
if output:
Path(output).write_text(text)
click.echo(f'{len(found)} files read.', err=True)
else:
click.echo(text, nl=False)


@cli.command()
@click.argument(
'template_dir', type=click.Path(exists=True, file_okay=False), default='.', required=False
Expand Down
8 changes: 8 additions & 0 deletions tfmanager/migrate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Migration of the Archive's templates to TemplateFlow 2.0 (``tfmgr migrate``).

A template is read into a :class:`~tfmanager.migrate.snapshot.Snapshot` (from the skeleton
zip of the Python client, or from a checkout), and the migration is planned from the
snapshot, the approved decisions (``config/``) and the facts of image headers (``data/``),
without image data. The plan is then applied to a checkout as git commits, which are
never pushed.
"""
Loading