Skip to content

Commit 9392a5a

Browse files
committed
added tool to increment the tool version supported by the package
1 parent c1cfe3b commit 9392a5a

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

tools/increment_tool_version.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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()

tools/rename_template.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def load_gitignore(repo):
2424

2525
cmd, new_name, *_ = sys.argv
2626

27-
for root, dirs, files in os.walk(PACKAGE_ROOT):
27+
for root_, dirs, files in os.walk(PACKAGE_ROOT):
2828
ignore = load_gitignore(PACKAGE_ROOT).search
2929
for d in [d for d in dirs if ignore(f"{d}/")]:
3030
dirs.remove(d)
3131
for f in [f for f in files if ignore(f)]:
3232
files.remove(f)
3333

34-
root = Path(root)
34+
root = Path(root_)
3535
for src in list(dirs):
3636
if "CHANGEME" in src:
3737
dst = src.replace("CHANGEME", new_name)
@@ -40,7 +40,6 @@ def load_gitignore(repo):
4040
dirs.remove(src)
4141
dirs.append(dst)
4242
for fname in files:
43-
f = root / fname
4443
text = Path.read_text(root / fname)
4544
if "CHANGEME" in text:
4645
print(f"Rewriting: {root / fname}")

tools/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
click >= 8.1.3
2+
looseversion >= 1.1
3+
pydra >= 0.23

0 commit comments

Comments
 (0)