From 5d644d61140a245d044213346ecdaac9dde8271f Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Mon, 14 Nov 2016 18:27:08 +0000 Subject: [PATCH] Wrapper scripts around immv/imcp functions. --- bin/imcp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/immv | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 5 ++++ 3 files changed, 150 insertions(+) create mode 100755 bin/imcp create mode 100755 bin/immv diff --git a/bin/imcp b/bin/imcp new file mode 100755 index 000000000..bef56cc74 --- /dev/null +++ b/bin/imcp @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# +# immv.py - Copy image files. +# +# Author: Paul McCarthy <paulmc@fmrib.ox.ac.uk> +# + + +from __future__ import print_function + +import os.path as op +import sys +import fsl.utils.path as fslpath + + +SUPPORTED_EXTENSIONS = [ + '.nii', '.nii.gz', + '.hdr', '.hdr.gz', + '.img', '.img.gz', + '.mnc', '.mnc.gz' +] + + +FILE_GROUPS = [ + ('.img', '.hdr'), + ('.img.gz', '.hdr.gz') +] + + +usage = """Usage: + imcp <file1> <file2> + imcp <file1> <directory> + imcp <file1> <file2> ... <fileN> <directory> + +Copy images from <file1> to <file2>, or copy all <file>s to <directory> + +NB: filenames can be basenames or include an extension. + +Recognised file extensions: {} +""".format(', '.join(SUPPORTED_EXTENSIONS)) + + +def main(): + """Parses CLI arguments (see the usage string), and calls the + fsl.utils.path.imcp function on each input. + """ + + if len(sys.argv) < 2: + print(usage) + sys.exit(1) + + srcs = sys.argv[1:-1] + dest = sys.argv[ -1] + + if len(srcs) > 1 and not op.isdir(dest): + print(usage) + sys.exit(1) + + for src in srcs: + try: + fslpath.imcp(src, + dest, + allowedExts=SUPPORTED_EXTENSIONS, + fileGroups=FILE_GROUPS) + + except Exception as e: + print(e) + break + + +if __name__ == '__main__': + main() diff --git a/bin/immv b/bin/immv new file mode 100755 index 000000000..6054693e4 --- /dev/null +++ b/bin/immv @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# +# immv.py - Move image files. +# +# Author: Paul McCarthy <paulmc@fmrib.ox.ac.uk> +# + + +from __future__ import print_function + +import os.path as op +import sys +import fsl.utils.path as fslpath + + +SUPPORTED_EXTENSIONS = [ + '.nii', '.nii.gz', + '.hdr', '.hdr.gz', + '.img', '.img.gz', + '.mnc', '.mnc.gz' +] + + + +FILE_GROUPS = [ + ('.img', '.hdr'), + ('.img.gz', '.hdr.gz') +] + + +usage = """Usage: + immv <file1> <file2> + immv <file1> <directory> + immv <file1> <file2> ... <fileN> <directory> + +Moves images from <file1> to <file2>, or move all <file>s to <directory> + +NB: filenames can be basenames or include an extension. + +Recognised file extensions: {} +""".format(', '.join(SUPPORTED_EXTENSIONS)) + + +def main(): + """Parses CLI arguments (see the usage string), and calls the + fsl.utils.path.immv function on each input. + """ + + if len(sys.argv) < 2: + print(usage) + sys.exit(1) + + srcs = sys.argv[1:-1] + dest = sys.argv[ -1] + + if len(srcs) > 1 and not op.isdir(dest): + print(usage) + sys.exit(1) + + for src in srcs: + try: + fslpath.immv(src, + dest, + allowedExts=SUPPORTED_EXTENSIONS, + fileGroups=FILE_GROUPS) + + except Exception as e: + print(e) + break + + +if __name__ == '__main__': + main() diff --git a/setup.py b/setup.py index 932946572..0b8573d8c 100644 --- a/setup.py +++ b/setup.py @@ -59,4 +59,9 @@ setup( setup_requires=['pytest-runner'], tests_require=['pytest', 'pytest-runner'], test_suite='tests', + + scripts=[ + op.join('bin', 'imcp'), + op.join('bin', 'immv'), + ] ) -- GitLab