aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/mesonlib.py
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2017-02-02 19:43:03 +0200
committerGitHub <noreply@github.com>2017-02-02 19:43:03 +0200
commita9c4428c6925c921102e52aeaea1b92ed6914af1 (patch)
treebf1d3f780a28e732204e24de79caa2d3e8bcb67d /mesonbuild/mesonlib.py
parentccb0580f4f25b02df6acb8c445ebf60f2215dbc7 (diff)
parent00846ce26d87369043de59e071856668df698b2f (diff)
downloadmeson-a9c4428c6925c921102e52aeaea1b92ed6914af1.zip
meson-a9c4428c6925c921102e52aeaea1b92ed6914af1.tar.gz
meson-a9c4428c6925c921102e52aeaea1b92ed6914af1.tar.bz2
Merge pull request #1342 from centricular/commonpath-py34
Add our own implementation of os.path.commonpath
Diffstat (limited to 'mesonbuild/mesonlib.py')
-rw-r--r--mesonbuild/mesonlib.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py
index 2ad43c8..f0b20e1 100644
--- a/mesonbuild/mesonlib.py
+++ b/mesonbuild/mesonlib.py
@@ -496,3 +496,28 @@ def Popen_safe(args, write=None, stderr=subprocess.PIPE, **kwargs):
stderr=stderr, **kwargs)
o, e = p.communicate(write)
return p, o, e
+
+def commonpath(paths):
+ '''
+ For use on Python 3.4 where os.path.commonpath is not available.
+ We currently use it everywhere so this receives enough testing.
+ '''
+ # XXX: Replace me with os.path.commonpath when we start requiring Python 3.5
+ import pathlib
+ if not paths:
+ raise ValueError('arg is an empty sequence')
+ common = pathlib.PurePath(paths[0])
+ for path in paths[1:]:
+ new = []
+ path = pathlib.PurePath(path)
+ for c, p in zip(common.parts, path.parts):
+ if c != p:
+ break
+ new.append(c)
+ # Don't convert '' into '.'
+ if not new:
+ common = ''
+ break
+ new = os.path.join(*new)
+ common = pathlib.PurePath(new)
+ return str(common)