From 0e2b873c38321941572ea887f7708710602fe809 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Fri, 6 Mar 2015 23:43:35 -0600 Subject: [PATCH] Add compatibility layer for Python 2.6 os.path.relpath on Python 2.6 returns slightly different output than it does on 2.7. Rather than try to write tests around the behaviour, it makes sense to have a common relpath function that behaves the same on every version. --- flake8/compat.py | 12 ++++++++++++ flake8/hooks.py | 3 ++- flake8/tests/test_hooks.py | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 flake8/compat.py diff --git a/flake8/compat.py b/flake8/compat.py new file mode 100644 index 0000000..9bd00a7 --- /dev/null +++ b/flake8/compat.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +"""Compatibility shims for Flake8.""" +import os.path +import sys + + +def relpath(path, start='.'): + """Wallpaper over the differences between 2.6 and newer versions.""" + if sys.version_info < (2, 7) and path.startswith(start): + return path[len(start):] + else: + return os.path.relpath(path, start=start) diff --git a/flake8/hooks.py b/flake8/hooks.py index e938632..659e5d4 100644 --- a/flake8/hooks.py +++ b/flake8/hooks.py @@ -12,6 +12,7 @@ try: except ImportError: # Python 2 from ConfigParser import ConfigParser +from flake8 import compat from flake8.engine import get_parser, get_style_guide from flake8.main import DEFAULT_CONFIG @@ -65,7 +66,7 @@ def git_hook(complexity=-1, strict=False, ignore=None, lazy=False): # avoid overwriting files with the same name dirname, filename = os.path.split(os.path.abspath(file_)) prefix = os.path.commonprefix([dirname, tmpdir]) - dirname = os.path.relpath(dirname, start=prefix) + dirname = compat.relpath(dirname, start=prefix) dirname = os.path.join(tmpdir, dirname) if not os.path.isdir(dirname): os.makedirs(dirname) diff --git a/flake8/tests/test_hooks.py b/flake8/tests/test_hooks.py index 31fcd81..42293ed 100644 --- a/flake8/tests/test_hooks.py +++ b/flake8/tests/test_hooks.py @@ -35,3 +35,7 @@ class TestGitHook(unittest.TestCase): tmpdir = os.path.join('/fake/tmp', dirname[1:]) tmpfile = os.path.join(tmpdir, 'bfile.py') style_guide.check_files.assert_called_once_with([tmpfile]) + + +if __name__ == '__main__': + unittest.main()