shexec 2.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shexec-2.2.0/LICENSE +21 -0
- shexec-2.2.0/PKG-INFO +190 -0
- shexec-2.2.0/README.rst +161 -0
- shexec-2.2.0/pyproject.toml +50 -0
- shexec-2.2.0/setup.cfg +9 -0
- shexec-2.2.0/shexec/__init__.py +52 -0
- shexec-2.2.0/shexec/__main__.py +156 -0
- shexec-2.2.0/shexec/models.py +152 -0
- shexec-2.2.0/shexec/plugin.py +144 -0
- shexec-2.2.0/shexec/shexec.py +105 -0
- shexec-2.2.0/shexec.egg-info/PKG-INFO +190 -0
- shexec-2.2.0/shexec.egg-info/SOURCES.txt +18 -0
- shexec-2.2.0/shexec.egg-info/dependency_links.txt +1 -0
- shexec-2.2.0/shexec.egg-info/entry_points.txt +2 -0
- shexec-2.2.0/shexec.egg-info/requires.txt +9 -0
- shexec-2.2.0/shexec.egg-info/top_level.txt +1 -0
- shexec-2.2.0/tests/test_cli.py +64 -0
- shexec-2.2.0/tests/test_plugin.py +92 -0
- shexec-2.2.0/tests/test_shexec.py +97 -0
shexec-2.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 FNNDSC / BCH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
shexec-2.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shexec
|
|
3
|
+
Version: 2.2.0
|
|
4
|
+
Summary: Run a shell-type command over every file of an input tree: a ChRIS DS plugin on pfdo_run
|
|
5
|
+
Home-page: https://github.com/FNNDSC/pl-shexec
|
|
6
|
+
Author: FNNDSC
|
|
7
|
+
Author-email: dev@babyMRI.org
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/FNNDSC/pl-shexec
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: POSIX
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Description-Content-Type: text/x-rst
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: chris_plugin>=0.4.0
|
|
21
|
+
Requires-Dist: pfdo>=6.2.0
|
|
22
|
+
Requires-Dist: pfdo_run>=4.2.0
|
|
23
|
+
Requires-Dist: hostlib>=1.2.0
|
|
24
|
+
Requires-Dist: pflog>=2.0.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
27
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
pl-shexec
|
|
31
|
+
=========
|
|
32
|
+
|
|
33
|
+
**Run a command over every file of a tree, as a ChRIS plugin; and the pattern for turning any command-line program into one.**
|
|
34
|
+
|
|
35
|
+
``pl-shexec`` is a `ChRIS <https://chrisproject.org/>`_ DS plugin that does two things. In a pipeline it is the plugin for the jobs too small to deserve one of their own: convert every image, zip every directory, rename, copy one file out of each subject, count lines. It takes a command template, runs it once per selected file of every directory in its input tree, and writes the results, and a record of each run, into an output tree that mirrors the input.
|
|
36
|
+
|
|
37
|
+
.. code-block:: bash
|
|
38
|
+
|
|
39
|
+
shexec --fileFilter 'glob:*.jpg' \
|
|
40
|
+
--exec "convert %{inputWorkingDir}/%{inputWorkingFile}
|
|
41
|
+
%{outputWorkingDir}/%{inputWorkingFile|rmext}.png" \
|
|
42
|
+
/incoming /outgoing
|
|
43
|
+
|
|
44
|
+
It is also the design for wrapping a large existing program as a plugin. Put the program in the image, set the default command, and the plugin is done: the program's whole command line stays available through the template rather than a hand-picked subset of flags, and the tree walk, the records, the parallelism and the exit status come from the stack. With for example FreeSurfer in the image, one ``recon-all`` per subject directory is:
|
|
45
|
+
|
|
46
|
+
.. code-block:: bash
|
|
47
|
+
|
|
48
|
+
shexec --analyzeFileIndex f --fileFilter 'glob:*.nii.gz' \
|
|
49
|
+
--exec "recon-all -all -i %{inputWorkingDir}/%{inputWorkingFile}
|
|
50
|
+
-s %{inputWorkingDir|basename} -sd %{outputWorkingDir}" \
|
|
51
|
+
/incoming /outgoing
|
|
52
|
+
|
|
53
|
+
pl-freesurfer is an example of the pattern: a plugin that depends on this one the way this one depends on pfdo_run, contributing the FreeSurfer image, that template as the default ``--exec``, and nothing else. See `Building a plugin on pl-shexec`_.
|
|
54
|
+
|
|
55
|
+
The engine is `pfdo_run <https://github.com/FNNDSC/pfdo_run>`_; the plugin is pfdo_run with its directories supplied by ChRIS and its own run timed.
|
|
56
|
+
|
|
57
|
+
The template
|
|
58
|
+
------------
|
|
59
|
+
|
|
60
|
+
``--exec`` is not a shell command. It is a template whose ``%{...}`` tags name the file and directories in hand, resolved once per file, and run directly without a shell, so a path with a space in it stays one argument and nothing is interpreted that was not written. Three tags describe the job:
|
|
61
|
+
|
|
62
|
+
========================== ==============================================
|
|
63
|
+
``%{inputWorkingDir}`` the input directory being processed
|
|
64
|
+
``%{outputWorkingDir}`` its counterpart in the output tree
|
|
65
|
+
``%{inputWorkingFile}`` the file being processed
|
|
66
|
+
========================== ==============================================
|
|
67
|
+
|
|
68
|
+
Functions derive one name from another: ``%{inputWorkingFile|rmext}.png`` turns ``scan.jpg`` into ``scan.png``; ``%{inputWorkingDir|basename}`` is the directory's own name. Every flag of the plugin is a tag too, and so are ``%{timestamp}``, ``%{hostname}`` and ``%{user}``. The grammar is `pftag <https://github.com/FNNDSC/pftag>`_'s; ``shexec --man`` lists it under TAGS. A template with no tag draws a warning, since the same command would then run for every file.
|
|
69
|
+
|
|
70
|
+
What each run leaves behind
|
|
71
|
+
---------------------------
|
|
72
|
+
|
|
73
|
+
Each directory of the output tree gets, beside its results, one file, ``jobs.jsonl``, with one line per job that ran there:
|
|
74
|
+
|
|
75
|
+
.. code-block:: text
|
|
76
|
+
|
|
77
|
+
scan.png
|
|
78
|
+
jobs.jsonl
|
|
79
|
+
|
|
80
|
+
The line is the whole record of the job as JSON: the command as executed, everything it printed and everything it complained, its exit status, the host it ran on, and when it started and ended. ``pflog read out/**/jobs.jsonl`` prints a day's jobs as a table, ``--failed`` keeps the ones that went wrong, and a log collector indexes the lines by field.
|
|
81
|
+
|
|
82
|
+
.. code-block:: text
|
|
83
|
+
|
|
84
|
+
STARTED DURATION STATUS NAME
|
|
85
|
+
2026-10-14T16:02:07-04:00 4.213 ok convert /incoming/a/scan.jpg /outgoing/a/scan.png
|
|
86
|
+
2026-10-14T16:02:12-04:00 0.004 failed convert /incoming/b/bad.jpg /outgoing/b/bad.png
|
|
87
|
+
|
|
88
|
+
``--jobFiles`` also writes each job as four files beside its result, ``scan.jpg-cmd``, ``-stdout``, ``-stderr`` and ``-returncode``, the stream files rendered as a terminal would have shown them, for the reader who opens them in an editor. ``--noJobLogging`` keeps no record at all. ``--json`` prints the whole run's record on standard output.
|
|
89
|
+
|
|
90
|
+
The plugin's own run is timed by `pflog <https://github.com/FNNDSC/pflog>`_: one line of JSON on standard error, when it began, when it ended, the seconds, and whether it succeeded. CUBE keeps that line with the rest of the plugin's output, and any log collector indexes it.
|
|
91
|
+
|
|
92
|
+
Selecting files and directories
|
|
93
|
+
-------------------------------
|
|
94
|
+
|
|
95
|
+
The filters are pftree's. ``--fileFilter`` and ``--dirFilter`` take substrings, or ``glob:`` and ``re:`` patterns, joined by ``--fileFilterLogic`` and ``--dirFilterLogic``; ``--maxDepth`` bounds the descent. ``--analyzeFileIndex`` chooses which of a directory's selected files get the command: ``a`` for all, ``f`` for the first, ``l`` for the last, ``m`` for the middle, or an index. ``f`` is how one command runs per directory, started on the first file, as for a DICOM series.
|
|
96
|
+
|
|
97
|
+
Directories run in parallel under ``--threads``. At ``--verbosity 2`` each command's output streams to the console as it runs.
|
|
98
|
+
|
|
99
|
+
Exit status
|
|
100
|
+
-----------
|
|
101
|
+
|
|
102
|
+
0 when at least one directory's jobs all succeeded; 1 when every directory failed or the run could not start; 2 when ``--exec`` is missing or a flag's value is unusable. A failed template, a command that cannot be found, or a non-zero exit fails its directory and the run continues; the record says why.
|
|
103
|
+
|
|
104
|
+
Wrapping a program
|
|
105
|
+
------------------
|
|
106
|
+
|
|
107
|
+
A plugin for a program with its own command line, FreeSurfer, ANTs, FSL, DCMTK, is pl-shexec with three changes and no new code:
|
|
108
|
+
|
|
109
|
+
- the image carries the program, with whatever licence or data it needs;
|
|
110
|
+
- the default ``--exec`` is the program's usual invocation as a template, so the common case needs no command at all and the unusual case overrides it;
|
|
111
|
+
- the default ``--analyzeFileIndex`` chooses the unit, ``f`` for one run per directory, as for a subject or a DICOM series, ``a`` for one run per file.
|
|
112
|
+
|
|
113
|
+
The user of such a plugin gets the program's entire interface, because anything the program accepts can be written into the template, and gets the same records, the same filters, the same threads and the same truthful exit status as every other plugin on the stack. This is the reason pl-shexec exists in the form it has.
|
|
114
|
+
|
|
115
|
+
Building a plugin on pl-shexec
|
|
116
|
+
------------------------------
|
|
117
|
+
|
|
118
|
+
pl-shexec is a library as well as a plugin, in the same way pfdo_run is a library to pl-shexec. A downstream plugin does not copy it; it depends on the ``shexec`` package and calls ``plugin_build`` with a spec of what differs:
|
|
119
|
+
|
|
120
|
+
.. code-block:: python
|
|
121
|
+
|
|
122
|
+
# freesurfer/__main__.py, the whole of pl-freesurfer's code
|
|
123
|
+
from shexec import PluginSpec, plugin_build
|
|
124
|
+
|
|
125
|
+
from freesurfer import __version__
|
|
126
|
+
|
|
127
|
+
RECON_ALL: str = (
|
|
128
|
+
"recon-all -all -i %{inputWorkingDir}/%{inputWorkingFile} "
|
|
129
|
+
"-s %{inputWorkingDir|basename} -sd %{outputWorkingDir}"
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
PLUGIN = plugin_build(PluginSpec(
|
|
133
|
+
name="freesurfer",
|
|
134
|
+
version=__version__,
|
|
135
|
+
title="FreeSurfer recon-all over every subject",
|
|
136
|
+
minMemory="8Gi",
|
|
137
|
+
minCpu="4000m",
|
|
138
|
+
defaults={"exec": RECON_ALL, "analyzeFileIndex": "f", "fileFilter": "glob:*.nii.gz"},
|
|
139
|
+
bannerTitle="FreeSurfer recon-all over an input tree",
|
|
140
|
+
description=DESCRIPTION,
|
|
141
|
+
examples=EXAMPLES,
|
|
142
|
+
))
|
|
143
|
+
main = PLUGIN.main
|
|
144
|
+
|
|
145
|
+
``PluginSpec`` holds everything a plugin changes: its name and version, which become its command, its span and its ``--version``; its title, category and resource limits for ChRIS; the defaults of its flags, above all ``--exec``, which ChRIS shows as the defaults and the user can override; and its words on the manual page, over the sections every plugin shares. ``plugin_build`` returns the parser with those defaults applied, the manual, and ``main`` already wrapped in pflog's ``timed`` and ``chris_plugin``, ready to be the console script. The engine, the flags, the records and the exit status are not in the spec, because they are the same for every plugin on the stack, and a fix to them reaches every plugin on its next build.
|
|
146
|
+
|
|
147
|
+
The rest of pl-freesurfer is a ``pyproject.toml`` naming ``chris_plugin`` and ``shexec`` as its dependencies, the first so that ``chris_plugin_info`` finds the plugin, a ``setup.cfg``, a Dockerfile that starts from the FreeSurfer image and installs the package, tests, and docs. pl-shexec itself is the first plugin built this way; its ``__main__`` is the same call with an empty default command. The pf* stack's ``docs/plugin.adoc`` has the template.
|
|
148
|
+
|
|
149
|
+
Running it
|
|
150
|
+
----------
|
|
151
|
+
|
|
152
|
+
From ChRIS, add the plugin to a pipeline and give it ``--exec`` and filters as parameters.
|
|
153
|
+
|
|
154
|
+
From the command line, with the image:
|
|
155
|
+
|
|
156
|
+
.. code-block:: bash
|
|
157
|
+
|
|
158
|
+
docker run --rm -v $PWD/in:/incoming:ro -v $PWD/out:/outgoing:rw \
|
|
159
|
+
fnndsc/pl-shexec shexec --fileFilter txt \
|
|
160
|
+
--exec "wc -l %{inputWorkingDir}/%{inputWorkingFile}" /incoming /outgoing
|
|
161
|
+
|
|
162
|
+
docker run --rm fnndsc/pl-shexec shexec --man /tmp /tmp
|
|
163
|
+
|
|
164
|
+
With Apptainer, ``apptainer exec docker://fnndsc/pl-shexec shexec ...``. The image carries the coreutils, zip and unzip, bc, perl, tar, ImageMagick and the DCMTK tools.
|
|
165
|
+
|
|
166
|
+
As a Python package, ``pip install .`` in this repository gives the ``shexec`` command and the ``shexec`` module, whose ``plugin_run(options, inputdir, outputdir)`` is the body of the plugin and whose ``shexec.models`` re-exports every record pfdo_run, pfdo, pftree, hostlib and pflog produce.
|
|
167
|
+
|
|
168
|
+
Development
|
|
169
|
+
-----------
|
|
170
|
+
|
|
171
|
+
.. code-block:: bash
|
|
172
|
+
|
|
173
|
+
uv venv --python /usr/bin/python3.14 .venv
|
|
174
|
+
uv pip install --python .venv/bin/python -e ".[dev]"
|
|
175
|
+
.venv/bin/ruff check . && .venv/bin/pytest -q
|
|
176
|
+
PATH=$PWD/.venv/bin:$PATH chris_plugin_info --dock-image docker.io/fnndsc/pl-shexec:2.1.0 shexec
|
|
177
|
+
|
|
178
|
+
The CI workflow lints and tests on Python 3.11 to 3.14, builds the image on every push to ``main``, pushes it to Docker Hub and ghcr.io, and on a ``v*`` tag uploads the plugin's description to cube.chrisproject.org and publishes the ``shexec`` package to PyPI, so that a plugin built on pl-shexec can depend on it. The tag must match ``__version__`` in ``shexec/__init__.py``.
|
|
179
|
+
|
|
180
|
+
Layout
|
|
181
|
+
------
|
|
182
|
+
|
|
183
|
+
.. code-block:: text
|
|
184
|
+
|
|
185
|
+
shexec/
|
|
186
|
+
├── __main__.py the plugin: parser, manual, @chris_plugin @timed main
|
|
187
|
+
├── shexec.py ShExec, options_build, plugin_run
|
|
188
|
+
└── models.py ShExecOptions, and every record below re-exported
|
|
189
|
+
|
|
190
|
+
``docs/purpose.adoc`` says why the plugin exists, ``docs/architecture.adoc`` how it is built, ``docs/history.adoc`` where it came from.
|
shexec-2.2.0/README.rst
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
pl-shexec
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
**Run a command over every file of a tree, as a ChRIS plugin; and the pattern for turning any command-line program into one.**
|
|
5
|
+
|
|
6
|
+
``pl-shexec`` is a `ChRIS <https://chrisproject.org/>`_ DS plugin that does two things. In a pipeline it is the plugin for the jobs too small to deserve one of their own: convert every image, zip every directory, rename, copy one file out of each subject, count lines. It takes a command template, runs it once per selected file of every directory in its input tree, and writes the results, and a record of each run, into an output tree that mirrors the input.
|
|
7
|
+
|
|
8
|
+
.. code-block:: bash
|
|
9
|
+
|
|
10
|
+
shexec --fileFilter 'glob:*.jpg' \
|
|
11
|
+
--exec "convert %{inputWorkingDir}/%{inputWorkingFile}
|
|
12
|
+
%{outputWorkingDir}/%{inputWorkingFile|rmext}.png" \
|
|
13
|
+
/incoming /outgoing
|
|
14
|
+
|
|
15
|
+
It is also the design for wrapping a large existing program as a plugin. Put the program in the image, set the default command, and the plugin is done: the program's whole command line stays available through the template rather than a hand-picked subset of flags, and the tree walk, the records, the parallelism and the exit status come from the stack. With for example FreeSurfer in the image, one ``recon-all`` per subject directory is:
|
|
16
|
+
|
|
17
|
+
.. code-block:: bash
|
|
18
|
+
|
|
19
|
+
shexec --analyzeFileIndex f --fileFilter 'glob:*.nii.gz' \
|
|
20
|
+
--exec "recon-all -all -i %{inputWorkingDir}/%{inputWorkingFile}
|
|
21
|
+
-s %{inputWorkingDir|basename} -sd %{outputWorkingDir}" \
|
|
22
|
+
/incoming /outgoing
|
|
23
|
+
|
|
24
|
+
pl-freesurfer is an example of the pattern: a plugin that depends on this one the way this one depends on pfdo_run, contributing the FreeSurfer image, that template as the default ``--exec``, and nothing else. See `Building a plugin on pl-shexec`_.
|
|
25
|
+
|
|
26
|
+
The engine is `pfdo_run <https://github.com/FNNDSC/pfdo_run>`_; the plugin is pfdo_run with its directories supplied by ChRIS and its own run timed.
|
|
27
|
+
|
|
28
|
+
The template
|
|
29
|
+
------------
|
|
30
|
+
|
|
31
|
+
``--exec`` is not a shell command. It is a template whose ``%{...}`` tags name the file and directories in hand, resolved once per file, and run directly without a shell, so a path with a space in it stays one argument and nothing is interpreted that was not written. Three tags describe the job:
|
|
32
|
+
|
|
33
|
+
========================== ==============================================
|
|
34
|
+
``%{inputWorkingDir}`` the input directory being processed
|
|
35
|
+
``%{outputWorkingDir}`` its counterpart in the output tree
|
|
36
|
+
``%{inputWorkingFile}`` the file being processed
|
|
37
|
+
========================== ==============================================
|
|
38
|
+
|
|
39
|
+
Functions derive one name from another: ``%{inputWorkingFile|rmext}.png`` turns ``scan.jpg`` into ``scan.png``; ``%{inputWorkingDir|basename}`` is the directory's own name. Every flag of the plugin is a tag too, and so are ``%{timestamp}``, ``%{hostname}`` and ``%{user}``. The grammar is `pftag <https://github.com/FNNDSC/pftag>`_'s; ``shexec --man`` lists it under TAGS. A template with no tag draws a warning, since the same command would then run for every file.
|
|
40
|
+
|
|
41
|
+
What each run leaves behind
|
|
42
|
+
---------------------------
|
|
43
|
+
|
|
44
|
+
Each directory of the output tree gets, beside its results, one file, ``jobs.jsonl``, with one line per job that ran there:
|
|
45
|
+
|
|
46
|
+
.. code-block:: text
|
|
47
|
+
|
|
48
|
+
scan.png
|
|
49
|
+
jobs.jsonl
|
|
50
|
+
|
|
51
|
+
The line is the whole record of the job as JSON: the command as executed, everything it printed and everything it complained, its exit status, the host it ran on, and when it started and ended. ``pflog read out/**/jobs.jsonl`` prints a day's jobs as a table, ``--failed`` keeps the ones that went wrong, and a log collector indexes the lines by field.
|
|
52
|
+
|
|
53
|
+
.. code-block:: text
|
|
54
|
+
|
|
55
|
+
STARTED DURATION STATUS NAME
|
|
56
|
+
2026-10-14T16:02:07-04:00 4.213 ok convert /incoming/a/scan.jpg /outgoing/a/scan.png
|
|
57
|
+
2026-10-14T16:02:12-04:00 0.004 failed convert /incoming/b/bad.jpg /outgoing/b/bad.png
|
|
58
|
+
|
|
59
|
+
``--jobFiles`` also writes each job as four files beside its result, ``scan.jpg-cmd``, ``-stdout``, ``-stderr`` and ``-returncode``, the stream files rendered as a terminal would have shown them, for the reader who opens them in an editor. ``--noJobLogging`` keeps no record at all. ``--json`` prints the whole run's record on standard output.
|
|
60
|
+
|
|
61
|
+
The plugin's own run is timed by `pflog <https://github.com/FNNDSC/pflog>`_: one line of JSON on standard error, when it began, when it ended, the seconds, and whether it succeeded. CUBE keeps that line with the rest of the plugin's output, and any log collector indexes it.
|
|
62
|
+
|
|
63
|
+
Selecting files and directories
|
|
64
|
+
-------------------------------
|
|
65
|
+
|
|
66
|
+
The filters are pftree's. ``--fileFilter`` and ``--dirFilter`` take substrings, or ``glob:`` and ``re:`` patterns, joined by ``--fileFilterLogic`` and ``--dirFilterLogic``; ``--maxDepth`` bounds the descent. ``--analyzeFileIndex`` chooses which of a directory's selected files get the command: ``a`` for all, ``f`` for the first, ``l`` for the last, ``m`` for the middle, or an index. ``f`` is how one command runs per directory, started on the first file, as for a DICOM series.
|
|
67
|
+
|
|
68
|
+
Directories run in parallel under ``--threads``. At ``--verbosity 2`` each command's output streams to the console as it runs.
|
|
69
|
+
|
|
70
|
+
Exit status
|
|
71
|
+
-----------
|
|
72
|
+
|
|
73
|
+
0 when at least one directory's jobs all succeeded; 1 when every directory failed or the run could not start; 2 when ``--exec`` is missing or a flag's value is unusable. A failed template, a command that cannot be found, or a non-zero exit fails its directory and the run continues; the record says why.
|
|
74
|
+
|
|
75
|
+
Wrapping a program
|
|
76
|
+
------------------
|
|
77
|
+
|
|
78
|
+
A plugin for a program with its own command line, FreeSurfer, ANTs, FSL, DCMTK, is pl-shexec with three changes and no new code:
|
|
79
|
+
|
|
80
|
+
- the image carries the program, with whatever licence or data it needs;
|
|
81
|
+
- the default ``--exec`` is the program's usual invocation as a template, so the common case needs no command at all and the unusual case overrides it;
|
|
82
|
+
- the default ``--analyzeFileIndex`` chooses the unit, ``f`` for one run per directory, as for a subject or a DICOM series, ``a`` for one run per file.
|
|
83
|
+
|
|
84
|
+
The user of such a plugin gets the program's entire interface, because anything the program accepts can be written into the template, and gets the same records, the same filters, the same threads and the same truthful exit status as every other plugin on the stack. This is the reason pl-shexec exists in the form it has.
|
|
85
|
+
|
|
86
|
+
Building a plugin on pl-shexec
|
|
87
|
+
------------------------------
|
|
88
|
+
|
|
89
|
+
pl-shexec is a library as well as a plugin, in the same way pfdo_run is a library to pl-shexec. A downstream plugin does not copy it; it depends on the ``shexec`` package and calls ``plugin_build`` with a spec of what differs:
|
|
90
|
+
|
|
91
|
+
.. code-block:: python
|
|
92
|
+
|
|
93
|
+
# freesurfer/__main__.py, the whole of pl-freesurfer's code
|
|
94
|
+
from shexec import PluginSpec, plugin_build
|
|
95
|
+
|
|
96
|
+
from freesurfer import __version__
|
|
97
|
+
|
|
98
|
+
RECON_ALL: str = (
|
|
99
|
+
"recon-all -all -i %{inputWorkingDir}/%{inputWorkingFile} "
|
|
100
|
+
"-s %{inputWorkingDir|basename} -sd %{outputWorkingDir}"
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
PLUGIN = plugin_build(PluginSpec(
|
|
104
|
+
name="freesurfer",
|
|
105
|
+
version=__version__,
|
|
106
|
+
title="FreeSurfer recon-all over every subject",
|
|
107
|
+
minMemory="8Gi",
|
|
108
|
+
minCpu="4000m",
|
|
109
|
+
defaults={"exec": RECON_ALL, "analyzeFileIndex": "f", "fileFilter": "glob:*.nii.gz"},
|
|
110
|
+
bannerTitle="FreeSurfer recon-all over an input tree",
|
|
111
|
+
description=DESCRIPTION,
|
|
112
|
+
examples=EXAMPLES,
|
|
113
|
+
))
|
|
114
|
+
main = PLUGIN.main
|
|
115
|
+
|
|
116
|
+
``PluginSpec`` holds everything a plugin changes: its name and version, which become its command, its span and its ``--version``; its title, category and resource limits for ChRIS; the defaults of its flags, above all ``--exec``, which ChRIS shows as the defaults and the user can override; and its words on the manual page, over the sections every plugin shares. ``plugin_build`` returns the parser with those defaults applied, the manual, and ``main`` already wrapped in pflog's ``timed`` and ``chris_plugin``, ready to be the console script. The engine, the flags, the records and the exit status are not in the spec, because they are the same for every plugin on the stack, and a fix to them reaches every plugin on its next build.
|
|
117
|
+
|
|
118
|
+
The rest of pl-freesurfer is a ``pyproject.toml`` naming ``chris_plugin`` and ``shexec`` as its dependencies, the first so that ``chris_plugin_info`` finds the plugin, a ``setup.cfg``, a Dockerfile that starts from the FreeSurfer image and installs the package, tests, and docs. pl-shexec itself is the first plugin built this way; its ``__main__`` is the same call with an empty default command. The pf* stack's ``docs/plugin.adoc`` has the template.
|
|
119
|
+
|
|
120
|
+
Running it
|
|
121
|
+
----------
|
|
122
|
+
|
|
123
|
+
From ChRIS, add the plugin to a pipeline and give it ``--exec`` and filters as parameters.
|
|
124
|
+
|
|
125
|
+
From the command line, with the image:
|
|
126
|
+
|
|
127
|
+
.. code-block:: bash
|
|
128
|
+
|
|
129
|
+
docker run --rm -v $PWD/in:/incoming:ro -v $PWD/out:/outgoing:rw \
|
|
130
|
+
fnndsc/pl-shexec shexec --fileFilter txt \
|
|
131
|
+
--exec "wc -l %{inputWorkingDir}/%{inputWorkingFile}" /incoming /outgoing
|
|
132
|
+
|
|
133
|
+
docker run --rm fnndsc/pl-shexec shexec --man /tmp /tmp
|
|
134
|
+
|
|
135
|
+
With Apptainer, ``apptainer exec docker://fnndsc/pl-shexec shexec ...``. The image carries the coreutils, zip and unzip, bc, perl, tar, ImageMagick and the DCMTK tools.
|
|
136
|
+
|
|
137
|
+
As a Python package, ``pip install .`` in this repository gives the ``shexec`` command and the ``shexec`` module, whose ``plugin_run(options, inputdir, outputdir)`` is the body of the plugin and whose ``shexec.models`` re-exports every record pfdo_run, pfdo, pftree, hostlib and pflog produce.
|
|
138
|
+
|
|
139
|
+
Development
|
|
140
|
+
-----------
|
|
141
|
+
|
|
142
|
+
.. code-block:: bash
|
|
143
|
+
|
|
144
|
+
uv venv --python /usr/bin/python3.14 .venv
|
|
145
|
+
uv pip install --python .venv/bin/python -e ".[dev]"
|
|
146
|
+
.venv/bin/ruff check . && .venv/bin/pytest -q
|
|
147
|
+
PATH=$PWD/.venv/bin:$PATH chris_plugin_info --dock-image docker.io/fnndsc/pl-shexec:2.1.0 shexec
|
|
148
|
+
|
|
149
|
+
The CI workflow lints and tests on Python 3.11 to 3.14, builds the image on every push to ``main``, pushes it to Docker Hub and ghcr.io, and on a ``v*`` tag uploads the plugin's description to cube.chrisproject.org and publishes the ``shexec`` package to PyPI, so that a plugin built on pl-shexec can depend on it. The tag must match ``__version__`` in ``shexec/__init__.py``.
|
|
150
|
+
|
|
151
|
+
Layout
|
|
152
|
+
------
|
|
153
|
+
|
|
154
|
+
.. code-block:: text
|
|
155
|
+
|
|
156
|
+
shexec/
|
|
157
|
+
├── __main__.py the plugin: parser, manual, @chris_plugin @timed main
|
|
158
|
+
├── shexec.py ShExec, options_build, plugin_run
|
|
159
|
+
└── models.py ShExecOptions, and every record below re-exported
|
|
160
|
+
|
|
161
|
+
``docs/purpose.adoc`` says why the plugin exists, ``docs/architecture.adoc`` how it is built, ``docs/history.adoc`` where it came from.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=69"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "shexec"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Run a shell-type command over every file of an input tree: a ChRIS DS plugin on pfdo_run"
|
|
9
|
+
readme = "README.rst"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
authors = [{ email = "dev@babyMRI.org" }]
|
|
12
|
+
# chris_plugin_info reads the legacy Author, Author-email and Home-page metadata
|
|
13
|
+
# fields, which the [project] table does not emit; setup.cfg supplies them, and the
|
|
14
|
+
# author name lives there alone so Author-email stays a bare address.
|
|
15
|
+
requires-python = ">=3.11"
|
|
16
|
+
dependencies = ["chris_plugin>=0.4.0", "pfdo>=6.2.0", "pfdo_run>=4.2.0", "hostlib>=1.2.0", "pflog>=2.0.0"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"License :: OSI Approved :: MIT License",
|
|
24
|
+
"Operating System :: POSIX",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/FNNDSC/pl-shexec"
|
|
29
|
+
|
|
30
|
+
[project.scripts]
|
|
31
|
+
shexec = "shexec.__main__:main"
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
dev = ["pytest>=8", "ruff>=0.6"]
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
include = ["shexec*"]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.dynamic]
|
|
40
|
+
version = { attr = "shexec.__version__" }
|
|
41
|
+
|
|
42
|
+
[tool.ruff]
|
|
43
|
+
target-version = "py311"
|
|
44
|
+
line-length = 110
|
|
45
|
+
|
|
46
|
+
[tool.ruff.lint]
|
|
47
|
+
select = ["E", "F", "W", "I", "UP", "B", "C4", "SIM", "RUF"]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
testpaths = ["tests"]
|
shexec-2.2.0/setup.cfg
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""shexec: a ChRIS DS plugin that runs a shell-type command over every file of a tree.
|
|
2
|
+
|
|
3
|
+
Key components:
|
|
4
|
+
- plugin_build, PluginSpec, Plugin: make a ChRIS plugin from a spec; what a
|
|
5
|
+
downstream plugin calls
|
|
6
|
+
- ShExec: pfdo_run's engine, built from the plugin's arguments
|
|
7
|
+
- ShExecOptions: the options
|
|
8
|
+
- plugin_run: the body of the plugin's main
|
|
9
|
+
- the records of pfdo_run and pflog, re-exported through shexec.models
|
|
10
|
+
|
|
11
|
+
Dependencies:
|
|
12
|
+
- chris_plugin, pfdo_run, pflog
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
__version__: str = "2.2.0"
|
|
18
|
+
|
|
19
|
+
from shexec.models import (
|
|
20
|
+
DirNode,
|
|
21
|
+
FileJob,
|
|
22
|
+
HostFacts,
|
|
23
|
+
JobResult,
|
|
24
|
+
JobSpec,
|
|
25
|
+
Plugin,
|
|
26
|
+
PluginSpec,
|
|
27
|
+
RunResult,
|
|
28
|
+
ShExecOptions,
|
|
29
|
+
Span,
|
|
30
|
+
StageResult,
|
|
31
|
+
)
|
|
32
|
+
from shexec.plugin import plugin_build
|
|
33
|
+
from shexec.shexec import ShExec, options_build, plugin_run
|
|
34
|
+
|
|
35
|
+
__all__: list[str] = [
|
|
36
|
+
"DirNode",
|
|
37
|
+
"FileJob",
|
|
38
|
+
"HostFacts",
|
|
39
|
+
"JobResult",
|
|
40
|
+
"JobSpec",
|
|
41
|
+
"Plugin",
|
|
42
|
+
"PluginSpec",
|
|
43
|
+
"RunResult",
|
|
44
|
+
"ShExec",
|
|
45
|
+
"ShExecOptions",
|
|
46
|
+
"Span",
|
|
47
|
+
"StageResult",
|
|
48
|
+
"__version__",
|
|
49
|
+
"options_build",
|
|
50
|
+
"plugin_build",
|
|
51
|
+
"plugin_run",
|
|
52
|
+
]
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""The ``shexec`` command: a ChRIS DS plugin that runs a command over every file of a tree.
|
|
2
|
+
|
|
3
|
+
This module is shexec's own call of ``plugin_build``: its name, its description for
|
|
4
|
+
ChRIS, no flag defaults, and its words on the manual page. A plugin built on shexec
|
|
5
|
+
has a module of the same shape with its own spec; see ``shexec.plugin`` and the
|
|
6
|
+
README's "Building a plugin on pl-shexec".
|
|
7
|
+
|
|
8
|
+
Key components:
|
|
9
|
+
- SPEC: what shexec says about itself
|
|
10
|
+
- PLUGIN: the built plugin
|
|
11
|
+
- parser, main: the plugin's flags and the console script's target
|
|
12
|
+
|
|
13
|
+
Typical usage:
|
|
14
|
+
shexec --fileFilter 'glob:*.jpg' --exec "convert %{inputWorkingDir}/%{inputWorkingFile}
|
|
15
|
+
%{outputWorkingDir}/%{inputWorkingFile|rmext}.png" /incoming /outgoing
|
|
16
|
+
|
|
17
|
+
Dependencies:
|
|
18
|
+
- shexec.plugin, shexec.models
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import argparse
|
|
24
|
+
from collections.abc import Callable
|
|
25
|
+
|
|
26
|
+
from pftree.manual import Manual
|
|
27
|
+
|
|
28
|
+
from shexec import __version__
|
|
29
|
+
from shexec.models import Plugin, PluginSpec
|
|
30
|
+
from shexec.plugin import plugin_build
|
|
31
|
+
|
|
32
|
+
TITLE: str = "Execute shell-type commands across input spaces"
|
|
33
|
+
CATEGORY: str = "Utility"
|
|
34
|
+
MIN_MEMORY: str = "2Gi"
|
|
35
|
+
MIN_CPU: str = "1000m"
|
|
36
|
+
|
|
37
|
+
BANNER_ART: str = r"""
|
|
38
|
+
_ _
|
|
39
|
+
| | | |
|
|
40
|
+
_ __ | |______ ___| |__ _____ _____ ___
|
|
41
|
+
| '_ \| |______/ __| '_ \ / _ \ \/ / _ \/ __|
|
|
42
|
+
| |_) | | \__ \ | | | __/> < __/ (__
|
|
43
|
+
| .__/|_| |___/_| |_|\___/_/\_\___|\___|
|
|
44
|
+
| |
|
|
45
|
+
|_|
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
BANNER_TITLE: str = "Run shell-type commands over an input tree"
|
|
49
|
+
|
|
50
|
+
BANNER_TEXT: str = """
|
|
51
|
+
Run shell-type commands over an input tree
|
|
52
|
+
|
|
53
|
+
-- version {version} --
|
|
54
|
+
|
|
55
|
+
A ChRIS DS plugin that runs one command for every selected file of
|
|
56
|
+
every directory in its input tree, the command written as a template
|
|
57
|
+
whose %{{...}} tags name the file and directories in hand. The output
|
|
58
|
+
tree mirrors the input tree; each command's record lands beside its
|
|
59
|
+
results. The engine is pfdo_run.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
DESCRIPTION: str = """
|
|
63
|
+
DESCRIPTION
|
|
64
|
+
|
|
65
|
+
``shexec`` is pfdo_run as a ChRIS plugin. ChRIS gives it an input and
|
|
66
|
+
an output directory; every other flag is pfdo_run's. The input tree is
|
|
67
|
+
walked, files are selected in each directory by the filters and
|
|
68
|
+
--analyzeFileIndex, and the --exec template is run once per selected
|
|
69
|
+
file with its tags resolved against that directory and file, without
|
|
70
|
+
a shell. Each job's record, the command, its output, its exit status,
|
|
71
|
+
the host and the timing, is one line of JSON in jobs.jsonl in its
|
|
72
|
+
output directory unless --noJobLogging is given; --jobFiles adds the
|
|
73
|
+
four files beside the result; at --verbosity 2 a job's output is also
|
|
74
|
+
streamed to the console as it runs.
|
|
75
|
+
|
|
76
|
+
The plugin's own run is timed: one line of JSON on standard error, when
|
|
77
|
+
it began, when it ended, the seconds, and whether it succeeded. CUBE
|
|
78
|
+
keeps that line with the rest of the plugin's output.
|
|
79
|
+
|
|
80
|
+
The exit status is 0 when at least one directory's jobs all succeeded,
|
|
81
|
+
1 when every directory failed or the run could not start, 2 when --exec
|
|
82
|
+
is missing. The commands available are those of the container image:
|
|
83
|
+
the coreutils, zip and unzip, bc, perl, tar, ImageMagick's convert,
|
|
84
|
+
and the DCMTK tools.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
EXAMPLES: str = """
|
|
88
|
+
|
|
89
|
+
EXAMPLES
|
|
90
|
+
|
|
91
|
+
Convert every jpg in the input tree to png in the output tree:
|
|
92
|
+
|
|
93
|
+
shexec \\
|
|
94
|
+
--fileFilter 'glob:*.jpg' \\
|
|
95
|
+
--exec "convert %{inputWorkingDir}/%{inputWorkingFile}
|
|
96
|
+
%{outputWorkingDir}/%{inputWorkingFile|rmext}.png" \\
|
|
97
|
+
--threads 4 /incoming /outgoing
|
|
98
|
+
|
|
99
|
+
Each conversion's command, stdout, stderr, return code and timing are one
|
|
100
|
+
line in the directory's jobs.jsonl; ``pflog read /outgoing/**/jobs.jsonl``
|
|
101
|
+
lists them.
|
|
102
|
+
|
|
103
|
+
Copy one file out of each directory whose path names a subject:
|
|
104
|
+
|
|
105
|
+
shexec \\
|
|
106
|
+
--dirFilter 100307 \\
|
|
107
|
+
--exec "cp %{inputWorkingDir}/brain.mgz %{outputWorkingDir}/brain.mgz" \\
|
|
108
|
+
--analyzeFileIndex f --noJobLogging /incoming /outgoing
|
|
109
|
+
|
|
110
|
+
Zip each directory's files into one archive named after the directory:
|
|
111
|
+
|
|
112
|
+
shexec \\
|
|
113
|
+
--analyzeFileIndex f \\
|
|
114
|
+
--exec "zip -j %{outputWorkingDir}/%{inputWorkingDir|basename}.zip
|
|
115
|
+
%{inputWorkingDir}/%{inputWorkingFile}" \\
|
|
116
|
+
/incoming /outgoing
|
|
117
|
+
|
|
118
|
+
From the image, with the directories mounted:
|
|
119
|
+
|
|
120
|
+
docker run --rm -v $PWD/in:/incoming:ro -v $PWD/out:/outgoing:rw \\
|
|
121
|
+
fnndsc/pl-shexec shexec --fileFilter txt \\
|
|
122
|
+
--exec "wc -l %{inputWorkingDir}/%{inputWorkingFile}" /incoming /outgoing
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
BRIEF_EXAMPLE: str = """
|
|
127
|
+
|
|
128
|
+
shexec --fileFilter 'glob:*.jpg' \\
|
|
129
|
+
--exec "convert %{inputWorkingDir}/%{inputWorkingFile}
|
|
130
|
+
%{outputWorkingDir}/%{inputWorkingFile|rmext}.png" \\
|
|
131
|
+
/incoming /outgoing
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
SPEC: PluginSpec = PluginSpec(
|
|
135
|
+
name="shexec",
|
|
136
|
+
version=__version__,
|
|
137
|
+
title=TITLE,
|
|
138
|
+
category=CATEGORY,
|
|
139
|
+
minMemory=MIN_MEMORY,
|
|
140
|
+
minCpu=MIN_CPU,
|
|
141
|
+
bannerArt=BANNER_ART,
|
|
142
|
+
bannerTitle=BANNER_TITLE,
|
|
143
|
+
bannerText=BANNER_TEXT,
|
|
144
|
+
briefExample=BRIEF_EXAMPLE,
|
|
145
|
+
description=DESCRIPTION,
|
|
146
|
+
examples=EXAMPLES,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
PLUGIN: Plugin = plugin_build(SPEC)
|
|
150
|
+
parser: argparse.ArgumentParser = PLUGIN.parser
|
|
151
|
+
main: Callable[..., int] = PLUGIN.main
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def manual_build() -> Manual:
|
|
155
|
+
"""The shexec manual page."""
|
|
156
|
+
return PLUGIN.manual
|