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.
This commit is contained in:
Ian Cordasco 2015-03-06 23:43:35 -06:00
parent 7ab30a747f
commit 0e2b873c38
3 changed files with 18 additions and 1 deletions

12
flake8/compat.py Normal file
View file

@ -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)

View file

@ -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)

View file

@ -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()