imln.py 2.43 KB
Newer Older
Paul McCarthy's avatar
Paul McCarthy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
#
# imln.py - Create symbolic links to image files.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
"""This module defines the ``imln`` application, for creating sym-links
to NIFTI image files.

.. note:: When creating links to relative paths, ln requires that the path is
          relative to the link location, rather than the invocation
          location. This is *not* currently supported by imln, and possibly
          never will be.
"""


import os.path        as op
import                   os
import                   sys
import fsl.utils.path as fslpath


23
24
25
26
27
28
29
30
31
32
# The lists below are defined in the
# fsl.data.image class, but are duplicated
# here for performance (to avoid import of
# nibabel/numpy/etc).
exts = ['.nii.gz', '.nii',
        '.img',    '.hdr',
        '.img.gz', '.hdr.gz',
        '.mnc',    '.mnc.gz']
"""List of file extensions that are supported by ``imtest``.
"""
Paul McCarthy's avatar
Paul McCarthy committed
33

34
35
groups = [('.hdr', '.img'), ('.hdr.gz', '.img.gz')]
"""List of known image file groups (image/header file pairs). """
Paul McCarthy's avatar
Paul McCarthy committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


usage = """
Usage: imln <file1> <file2>
  Makes a link (called file2) to file1
  NB: filenames can be basenames or include an extension
""".strip()


def main(argv=None):
    """``imln`` - create sym-links to images. """

    if argv is None:
        argv = sys.argv[1:]

    if len(argv) != 2:
        print(usage)
        return 1

    target, linkbase = argv
56
57
    target           = fslpath.removeExt(target,   exts)
    linkbase         = fslpath.removeExt(linkbase, exts)
Paul McCarthy's avatar
Paul McCarthy committed
58
59
60
61
62
63
64

    # Target must exist, so we can
    # infer the correct extension(s).
    # Error on incomplete file groups
    # (e.g. a.img without a.hdr).
    try:
        targets = fslpath.getFileGroup(target,
65
66
                                       allowedExts=exts,
                                       fileGroups=groups,
Paul McCarthy's avatar
Paul McCarthy committed
67
68
69
70
71
72
73
74
75
                                       unambiguous=True)
    except Exception as e:
        print(f'Error: {e}')
        return 1

    for target in targets:
        if not op.exists(target):
            continue

76
        ext  = fslpath.getExt(target, exts)
Paul McCarthy's avatar
Paul McCarthy committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        link = f'{linkbase}{ext}'

        try:

            # emulate old imln behaviour - if
            # link already exists, it is removed
            if op.exists(link):
                os.remove(link)

            os.symlink(target, link)

        except Exception as e:
            print(f'Error: {e}')
            return 1

    return 0


if __name__ == '__main__':
    sys.exit(main())