Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
fslpy
Commits
7fd39ba4
Commit
7fd39ba4
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: New function in path module - commonBase
parent
4e87cbf9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/path.py
+26
-0
26 additions, 0 deletions
fsl/utils/path.py
with
26 additions
and
0 deletions
fsl/utils/path.py
+
26
−
0
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
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment