Browse Source

Supports python 3.4

boyska 7 years ago
parent
commit
57f7d212ff
3 changed files with 76 additions and 1 deletions
  1. 1 0
      .travis.yml
  2. 57 0
      larigira/tests/test_commonpath.py
  3. 18 1
      larigira/unused.py

+ 1 - 0
.travis.yml

@@ -1,6 +1,7 @@
 language: python
 sudo: false
 python:
+    - "3.4"
     - "3.5"
 
 install:

+ 57 - 0
larigira/tests/test_commonpath.py

@@ -0,0 +1,57 @@
+import pytest
+
+from larigira.unused import old_commonpath
+
+
+def test_same():
+    assert old_commonpath(['/foo/bar', '/foo/bar/']) == '/foo/bar'
+
+
+def test_prefix():
+    assert old_commonpath(['/foo/bar', '/foo/zap/']) == '/foo'
+    assert old_commonpath(['/foo/bar/', '/foo/zap/']) == '/foo'
+    assert old_commonpath(['/foo/bar/', '/foo/zap']) == '/foo'
+    assert old_commonpath(['/foo/bar', '/foo/zap']) == '/foo'
+
+
+try:
+    from os.path import commonpath
+except ImportError:
+    pass  # TODO: log the fact and clarify
+else:
+    # these tests are only available on python >= 3.5. That's fine though, as
+    # our CI will perform validation of those cases to see if they match python
+    # behavior
+    @pytest.fixture(params=['a', 'a/', 'a/b', 'a/b/', 'a/b/c', ])
+    def relpath(request):
+        return request.param
+
+    @pytest.fixture
+    def abspath(relpath):
+        return '/' + relpath
+
+    @pytest.fixture(params=['', '/'])
+    def slashed_abspath(abspath, request):
+        return '%s%s' % (abspath, request.param)
+    slashed_abspath_b = slashed_abspath
+
+    @pytest.fixture
+    def abspath_couple(slashed_abspath, slashed_abspath_b):
+        return (slashed_abspath, slashed_abspath_b)
+
+    def test_abspath_match(abspath_couple):
+        assert commonpath(abspath_couple) == old_commonpath(abspath_couple)
+
+    @pytest.fixture(params=['', '/'])
+    def slashed_relpath(relpath, request):
+        s = '%s%s' % (relpath, request.param)
+        if s:
+            return s
+    slashed_relpath_b = slashed_relpath
+
+    @pytest.fixture
+    def relpath_couple(slashed_relpath, slashed_relpath_b):
+        return (slashed_relpath, slashed_relpath_b)
+
+    def test_relpath_match(relpath_couple):
+        assert commonpath(relpath_couple) == old_commonpath(relpath_couple)

+ 18 - 1
larigira/unused.py

@@ -5,11 +5,28 @@ This component will look for files to be removed. There are some assumptions:
     * MPD URIs are parsed, and only file:/// is supported
 '''
 import os
-from os.path import commonpath, normpath
+from os.path import normpath
 import logging
 import mpd
 
 
+def old_commonpath(directories):
+    if any(p for p in directories if p.startswith('/')) and \
+       any(p for p in directories if not p.startswith('/')):
+        raise ValueError("Can't mix absolute and relative paths")
+    norm_paths = [normpath(p) + os.path.sep for p in directories]
+    ret = os.path.dirname(os.path.commonprefix(norm_paths))
+    if len(ret) > 0 and ret == '/' * len(ret):
+        return '/'
+    return ret
+
+
+try:
+    from os.path import commonpath
+except ImportError:
+    commonpath = old_commonpath
+
+
 class UnusedCleaner:
     def __init__(self, conf):
         self.conf = conf