Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Evan Edmond
fslpy
Commits
7fd39ba4
Commit
7fd39ba4
authored
Jan 27, 2019
by
Paul McCarthy
🚵
Browse files
ENH: New function in path module - commonBase
parent
4e87cbf9
Changes
1
Hide whitespace changes
Inline
Side-by-side
fsl/utils/path.py
View file @
7fd39ba4
...
...
@@ -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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment