aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/mesonlib.py
diff options
context:
space:
mode:
authorNirbheek Chauhan <nirbheek@centricular.com>2017-01-30 02:53:35 +0530
committerNirbheek Chauhan <nirbheek@centricular.com>2017-01-30 03:19:31 +0530
commitbb491735a980d9124078ac032a5281e648027de7 (patch)
tree12891bd7f7427089fee1a392c9940a98a54bdfde /mesonbuild/mesonlib.py
parent66fbcde96e4dd3782d5289b4faf2479335bdf7b1 (diff)
downloadmeson-bb491735a980d9124078ac032a5281e648027de7.zip
meson-bb491735a980d9124078ac032a5281e648027de7.tar.gz
meson-bb491735a980d9124078ac032a5281e648027de7.tar.bz2
coredata: Use our own implementation of commonpath
os.path.commonpath was added in Python 3.5, so just write our own for now. pathlib was added in Python 3.4, so this should be ok. We need to use that instead of doing str.split() etc because Windows path handling has a lot of exceptions and pathlib handles all that for us. Also adds a unit test for this.
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)