From 57f7d212ff368e9d4f3b0f3246d4f031bec4c4cb Mon Sep 17 00:00:00 2001 From: boyska Date: Wed, 4 Jan 2017 14:08:01 +0100 Subject: [PATCH] Supports python 3.4 --- .travis.yml | 1 + larigira/tests/test_commonpath.py | 57 +++++++++++++++++++++++++++++++ larigira/unused.py | 19 ++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 larigira/tests/test_commonpath.py diff --git a/.travis.yml b/.travis.yml index 887529f..79b0e98 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: python sudo: false python: + - "3.4" - "3.5" install: diff --git a/larigira/tests/test_commonpath.py b/larigira/tests/test_commonpath.py new file mode 100644 index 0000000..b3eb105 --- /dev/null +++ b/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) diff --git a/larigira/unused.py b/larigira/unused.py index 1e592f0..3b7d2a4 100644 --- a/larigira/unused.py +++ b/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