diff --git a/fsl/utils/path.py b/fsl/utils/path.py index 4190865a820dab8b52b8119ed305c0d17006a721..e9a577f20e69d69e3641c12f0674962ad3bd3c87 100644 --- a/fsl/utils/path.py +++ b/fsl/utils/path.py @@ -22,12 +22,14 @@ paths. getFileGroup removeDuplicates uniquePrefix + commonBase """ import os.path as op import os import glob +import operator class PathError(Exception): @@ -471,3 +473,27 @@ def uniquePrefix(path): hits = [h for h in hits if h.startswith(prefix)] return prefix + + +def commonBase(paths): + """Identifies the deepest common base directory shared by all files + in ``paths``. + + Raises a :exc:`PathError` if the paths have no common base. This will + never happen for absolute paths (as the base will be e.g. ``'/'``). + """ + + depths = [len(p.split(op.sep)) for p in paths] + base = max(zip(depths, paths), key=operator.itemgetter(0))[1] + + while True: + + base = op.split(base)[0] + + if len(base) == 0: + break + + if all([p.startswith(base) for p in paths]): + return base + + raise PathError('No common base')