From 07bec0375f9c6c0d730f9a9e5056169a5bb262e3 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Thu, 17 Nov 2016 19:25:34 +0000
Subject: [PATCH] Paths passed to removeDuplicates must exist. Immv/cp scripts
 raise error when called programmatically.

---
 fsl/scripts/imcp.py | 14 +++++++-------
 fsl/scripts/immv.py | 13 ++++++-------
 fsl/utils/path.py   | 20 ++++++++++++++++----
 3 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/fsl/scripts/imcp.py b/fsl/scripts/imcp.py
index 71af0fd86..4ec9887ab 100755
--- a/fsl/scripts/imcp.py
+++ b/fsl/scripts/imcp.py
@@ -58,13 +58,13 @@ def main(argv=None):
                                     fileGroups=fslimage.FILE_GROUPS)
 
     for src in srcs:
-        try:
-            imcp.imcp(src, dest, useDefaultExt=True) 
-            
-        except Exception as e:
-            print(e)
-            break
+        imcp.imcp(src, dest, useDefaultExt=True) 
 
 
 if __name__ == '__main__':
-    main()
+
+    try:
+        main()
+    except Exception as e:
+        print(e)
+        sys.exit(1) 
diff --git a/fsl/scripts/immv.py b/fsl/scripts/immv.py
index 66f57bd49..f5a4843a6 100755
--- a/fsl/scripts/immv.py
+++ b/fsl/scripts/immv.py
@@ -59,13 +59,12 @@ def main(argv=None):
                                     fileGroups=fslimage.FILE_GROUPS) 
 
     for src in srcs:
-        try:
-            imcp.immv(src, dest, useDefaultExt=True)
-            
-        except Exception as e:
-            print(e)
-            break
+        imcp.immv(src, dest, useDefaultExt=True)
 
 
 if __name__ == '__main__':
-    main()
+    try:
+        main()
+    except Exception as e:
+        print(e)
+        sys.exit(1)
diff --git a/fsl/utils/path.py b/fsl/utils/path.py
index cbd9fd8bd..32a676cc3 100644
--- a/fsl/utils/path.py
+++ b/fsl/utils/path.py
@@ -246,7 +246,10 @@ def removeDuplicates(paths, allowedExts=None, fileGroups=None):
 
          ['001.img', '002.img', '003.img']
 
-    :arg paths:       List of paths to reduce.
+    A :exc:`PathError` is raised if any of the paths do not exist.
+
+    :arg paths:       List of paths to reduce. If ``allowedExts`` is not
+                      ``None``, may be incomplete.
 
     :arg allowedExts: Allowed/recognised file extensions.
 
@@ -257,10 +260,19 @@ def removeDuplicates(paths, allowedExts=None, fileGroups=None):
 
     for path in paths:
 
+        path = addExt(path,
+                      mustExist=True,
+                      allowedExts=allowedExts,
+                      fileGroups=fileGroups)
+
         groupFiles = getFileGroup(path, allowedExts, fileGroups)
 
-        if not any([g in unique for g in groupFiles]):
-            unique.append(path)
+        if len(groupFiles) == 0:
+            if path not in unique:
+                unique.append(path)
+                
+        elif not any([p in unique for p in groupFiles]):
+            unique.append(groupFiles[0])
 
     return unique
 
@@ -320,7 +332,7 @@ def getFileGroup(path, allowedExts=None, fileGroups=None, fullPaths=True):
 
     for group in fileGroups:
 
-        if ext not in group:
+        if ext != '' and ext not in group:
             continue
 
         groupFiles = [base + s for s in group]
-- 
GitLab