|
1 | | -#!/usr/bin/env python3 |
2 | | -import sys |
3 | | -import os.path |
4 | | -from warnings import warn |
5 | | -from pathlib import Path |
6 | | -import shutil |
7 | | -from importlib import import_module |
8 | | -import yaml |
9 | | -from tqdm import tqdm |
10 | | -import nipype |
11 | | -import nipype2pydra.utils |
12 | | -from nipype2pydra.task import get_converter |
13 | | - |
14 | | - |
15 | | -SPECS_DIR = Path(__file__).parent / "specs" |
16 | | -PKG_ROOT = Path(__file__).parent.parent |
17 | | -PKG_NAME = "fsl" |
18 | | - |
19 | | -if ".dev" in nipype.__version__: |
20 | | - raise RuntimeError( |
21 | | - f"Cannot use a development version of Nipype {nipype.__version__}" |
22 | | - ) |
23 | | - |
24 | | -if ".dev" in nipype2pydra.__version__: |
25 | | - warn( |
26 | | - f"using development version of nipype2pydra ({nipype2pydra.__version__}), " |
27 | | - f"development component will be dropped in {PKG_NAME} package version" |
28 | | - ) |
29 | | - |
30 | | -# Insert specs dir into path so we can load callables modules |
31 | | -sys.path.insert(0, str(SPECS_DIR)) |
32 | | - |
33 | | -auto_init = f"# Auto-generated by {__file__}, do not edit as it will be overwritten\n\n" |
34 | | - |
35 | | -auto_dir = PKG_ROOT / "pydra" / "tasks" / PKG_NAME / "auto" |
36 | | -if auto_dir.exists(): |
37 | | - shutil.rmtree(auto_dir) |
38 | | - |
39 | | -all_interfaces = [] |
40 | | -for fspath in tqdm( |
41 | | - sorted(SPECS_DIR.glob("**/*.yaml")), "converting interfaces from Nipype to Pydra" |
42 | | -): |
43 | | - with open(fspath) as f: |
44 | | - spec = yaml.load(f, Loader=yaml.SafeLoader) |
45 | | - |
46 | | - rel_pkg_path = str(fspath.parent.relative_to(SPECS_DIR)).replace(os.path.sep, ".") |
47 | | - if rel_pkg_path == ".": |
48 | | - rel_pkg_path = fspath.stem |
49 | | - else: |
50 | | - rel_pkg_path += "." + fspath.stem |
51 | | - |
52 | | - callables = import_module(rel_pkg_path + "_callables") |
53 | | - |
54 | | - module_name = nipype2pydra.utils.to_snake_case(spec["task_name"]) |
55 | | - |
56 | | - converter = get_converter( |
57 | | - output_module=f"pydra.tasks.{PKG_NAME}.auto.{module_name}", |
58 | | - callables_module=callables, # type: ignore |
59 | | - **spec, |
60 | | - ) |
61 | | - converter.generate(PKG_ROOT) |
62 | | - auto_init += f"from .{module_name} import {converter.task_name}\n" |
63 | | - all_interfaces.append(converter.task_name) |
64 | | - |
65 | | - |
66 | | -with open(PKG_ROOT / "pydra" / "tasks" / PKG_NAME / "auto" / "_version.py", "w") as f: |
67 | | - f.write( |
68 | | - f"""# Auto-generated by {__file__}, do not edit as it will be overwritten |
69 | | -
|
70 | | -nipype_version = "{nipype.__version__.split('.dev')[0]}" |
71 | | -nipype2pydra_version = "{nipype2pydra.__version__.split('.dev')[0]}" |
72 | | -post_release = (nipype_version + nipype2pydra_version).replace(".", "") |
73 | | -""" |
74 | | - ) |
75 | | - |
76 | | -auto_init += ( |
77 | | - "\n\n__all__ = [\n" + "\n".join(f' "{i}",' for i in all_interfaces) + "\n]\n" |
78 | | -) |
79 | | - |
80 | | -with open(PKG_ROOT / "pydra" / "tasks" / PKG_NAME / "auto" / "__init__.py", "w") as f: |
81 | | - f.write(auto_init) |
| 1 | +#!/usr/bin/env bash |
| 2 | +conv_dir=$(dirname $0) |
| 3 | +nipype2pydra convert $conv_dir/specs $conv_dir/.. |
0 commit comments