|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from pathlib import Path |
| 3 | +import inspect |
| 4 | +from importlib import import_module |
| 5 | +import click |
| 6 | +from looseversion import LooseVersion |
| 7 | +from pydra.engine.core import TaskBase |
| 8 | + |
| 9 | + |
| 10 | +PKG_DIR = Path(__file__).parent.parent |
| 11 | +TASKS_DIR = PKG_DIR / "pydra" / "tasks" / "ants" |
| 12 | +VERSION_GRANULARITY = ( |
| 13 | + 2 # Number of version parts to include: 1 - major, 2 - minor, 3 - micro |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@click.command( |
| 18 | + help="""Increment the latest version or create a new sub-package for interfaces for |
| 19 | +a new release of AFNI depending on whether one already exists or not. |
| 20 | +
|
| 21 | +NEW_VERSION the version of AFNI to create a new sub-package for |
| 22 | +""" |
| 23 | +) |
| 24 | +@click.argument("new_version", type=LooseVersion) |
| 25 | +def increment_tool_version(new_version: LooseVersion): |
| 26 | + |
| 27 | + # Get the name of the sub-package, e.g. "v2_5" |
| 28 | + new_subpkg_name = "_".join(str(p) for p in new_version.version[:VERSION_GRANULARITY]) # type: ignore |
| 29 | + if not new_subpkg_name.startswith("v"): |
| 30 | + new_subpkg_name = "v" + new_subpkg_name |
| 31 | + sub_pkg_dir = TASKS_DIR / new_subpkg_name |
| 32 | + if not sub_pkg_dir.exists(): |
| 33 | + |
| 34 | + prev_version = sorted( |
| 35 | + ( |
| 36 | + p.name |
| 37 | + for p in TASKS_DIR.iterdir() |
| 38 | + if p.is_dir() and p.name.startswith("v") |
| 39 | + ), |
| 40 | + key=lambda x: LooseVersion(".".join(x.split("_"))).version, |
| 41 | + )[-1] |
| 42 | + prev_ver_mod = import_module(f"pydra.tasks.ants.{prev_version}") |
| 43 | + |
| 44 | + mod_attrs = [getattr(prev_ver_mod, a) for a in dir(prev_ver_mod)] |
| 45 | + task_classes = [ |
| 46 | + a for a in mod_attrs if inspect.isclass(a) and issubclass(a, TaskBase) |
| 47 | + ] |
| 48 | + |
| 49 | + code_str = ( |
| 50 | + f"from pydra.tasks.ants import {prev_version}\n" |
| 51 | + "from . import _tool_version\n" |
| 52 | + ) |
| 53 | + |
| 54 | + for task_cls in task_classes: |
| 55 | + code_str += ( |
| 56 | + f"\n\nclass {task_cls.__name__}({prev_version}.{task_cls.__name__}):\n" |
| 57 | + " TOOL_VERSION = _tool_version.TOOL_VERSION\n" |
| 58 | + ) |
| 59 | + |
| 60 | + sub_pkg_dir.mkdir(exist_ok=True) |
| 61 | + with open(sub_pkg_dir / "__init__.py", "w") as f: |
| 62 | + f.write(code_str) |
| 63 | + |
| 64 | + with open(sub_pkg_dir / "_tool_version.py", "w") as f: |
| 65 | + f.write(f'TOOL_VERSION = "{new_version}"\n') |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + increment_tool_version() |
0 commit comments