Commit 90466b63 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

New function removeDuplicates, so immv/imcp scripts can reduce redundant

copies/overwrite errors. Made imcp overwrite check more robust.
parent ce2d5b52
...@@ -56,6 +56,10 @@ def main(): ...@@ -56,6 +56,10 @@ def main():
print(usage) print(usage)
sys.exit(1) sys.exit(1)
srcs = fslpath.removeDuplicates(srcs,
allowedExts=SUPPORTED_EXTENSIONS,
fileGroups=FILE_GROUPS)
for src in srcs: for src in srcs:
try: try:
fslpath.imcp(src, fslpath.imcp(src,
......
...@@ -57,6 +57,10 @@ def main(): ...@@ -57,6 +57,10 @@ def main():
print(usage) print(usage)
sys.exit(1) sys.exit(1)
srcs = fslpath.removeDuplicates(srcs,
allowedExts=SUPPORTED_EXTENSIONS,
fileGroups=FILE_GROUPS)
for src in srcs: for src in srcs:
try: try:
fslpath.immv(src, fslpath.immv(src,
......
...@@ -221,6 +221,53 @@ def splitExt(filename, allowedExts=None): ...@@ -221,6 +221,53 @@ def splitExt(filename, allowedExts=None):
return filename[:-extLen], filename[-extLen:] return filename[:-extLen], filename[-extLen:]
def removeDuplicates(paths, allowedExts=None, fileGroups=None):
"""Reduces the list of ``paths`` down to those which are unique with
respect to the specified ``fileGroups``.
For example, if you have a directory containing::
001.hdr
001.img
002.hdr
002.img
003.img
003.img
And you call ``removeDuplicates`` like so::
paths = ['001.img', '001.hdr',
'002.img', '002.hdr',
'003.img', '003.hdr']
allowedExts = ['.img', '.hdr']
fileGroups = [('.img', '.hdr')]
removeDuplicates(paths, allowedExts, fileGroups)
The returned list will be::
['001.img', '003.img', '003.img']
:arg paths: List of paths to reduce.
:arg allowedExts: Allowed/recognised file extensions.
:arg fileGroups: Recognised file groups - see :func:`getFileGroup`.
"""
unique = []
for path in paths:
groupFiles = getFileGroup(path, allowedExts, fileGroups)
if not any([g in unique for g in groupFiles]):
unique.append(path)
return unique
def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True): def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
"""If the given ``path`` is part of a ``fileGroup``, returns a list """If the given ``path`` is part of a ``fileGroup``, returns a list
containing the paths to all other files in the group (including the containing the paths to all other files in the group (including the
...@@ -386,15 +433,16 @@ def imcp(src, ...@@ -386,15 +433,16 @@ def imcp(src,
# to re-combine the source paths # to re-combine the source paths
copySrcs[i] = base + ext copySrcs[i] = base + ext
if destIsDir: copyDests.append(dest) basename = op.basename(base)
if destIsDir: copyDests.append(op.join(dest, basename + ext))
else: copyDests.append(dest + ext) else: copyDests.append(dest + ext)
# Fail if any of the destination # Fail if any of the destination
# paths already exist # paths already exist
if not overwrite: if not overwrite and any([op.exists(d) for d in copyDests]):
if not destIsDir and any([op.exists(d) for d in copyDests]): raise PathError('imcp error - a destination path already '
raise PathError('imcp error - a destination path already ' 'exists ({})'.format(', '.join(copyDests)))
'exists ({})'.format(', '.join(copyDests)))
# Do the copy/move # Do the copy/move
for src, dest in zip(copySrcs, copyDests): for src, dest in zip(copySrcs, copyDests):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment