Replace five with six

This commit is contained in:
Anthony Sottile 2017-03-08 13:06:48 -08:00
parent cb8dd335f4
commit c65a11ce3d
9 changed files with 27 additions and 57 deletions

View file

@ -3,10 +3,11 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import contextlib import contextlib
import io
import os.path import os.path
import traceback import traceback
import six
from pre_commit import five from pre_commit import five
from pre_commit import output from pre_commit import output
from pre_commit.errors import FatalError from pre_commit.errors import FatalError
@ -22,7 +23,7 @@ def _to_bytes(exc):
try: try:
return bytes(exc) return bytes(exc)
except Exception: except Exception:
return five.text(exc).encode('UTF-8') return six.text_type(exc).encode('UTF-8')
def _log_and_exit(msg, exc, formatted): def _log_and_exit(msg, exc, formatted):
@ -35,7 +36,7 @@ def _log_and_exit(msg, exc, formatted):
output.write_line('Check the log at ~/.pre-commit/pre-commit.log') output.write_line('Check the log at ~/.pre-commit/pre-commit.log')
store = Store() store = Store()
store.require_created() store.require_created()
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log: with open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
output.write(error_msg, stream=log) output.write(error_msg, stream=log)
output.write_line(formatted, stream=log) output.write_line(formatted, stream=log)
raise PreCommitSystemExit(1) raise PreCommitSystemExit(1)

View file

@ -1,42 +1,14 @@
from __future__ import unicode_literals from __future__ import unicode_literals
PY2 = str is bytes import six
PY3 = str is not bytes
if PY2: # pragma: no cover (PY2 only)
text = unicode # flake8: noqa
string_types = (text, bytes)
def n(s):
if isinstance(s, bytes):
return s
else:
return s.encode('UTF-8')
exec("""def reraise(tp, value, tb=None):
raise tp, value, tb
""")
else: # pragma: no cover (PY3 only)
text = str
string_types = (text,)
def n(s):
if isinstance(s, text):
return s
else:
return s.decode('UTF-8')
def reraise(tp, value, tb=None):
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
def to_text(s): def to_text(s):
return s if isinstance(s, text) else s.decode('UTF-8') return s if isinstance(s, six.text_type) else s.decode('UTF-8')
def to_bytes(s): def to_bytes(s):
return s if isinstance(s, bytes) else s.encode('UTF-8') return s if isinstance(s, bytes) else s.encode('UTF-8')
n = to_bytes if six.PY2 else to_text

View file

@ -5,7 +5,6 @@ from __future__ import unicode_literals
import os.path import os.path
import tarfile import tarfile
from pre_commit import five
from pre_commit import output from pre_commit import output
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
from pre_commit.util import cwd from pre_commit.util import cwd
@ -54,7 +53,7 @@ def make_archive(name, repo, ref, destdir):
# runtime # runtime
rmtree(os.path.join(tempdir, '.git')) rmtree(os.path.join(tempdir, '.git'))
with tarfile.open(five.n(output_path), 'w|gz') as tf: with tarfile.open(output_path, 'w|gz') as tf:
tf.add(tempdir, name) tf.add(tempdir, name)
return output_path return output_path

View file

@ -8,7 +8,7 @@ import os.path
import re import re
import sys import sys
from pre_commit import five import six
class ValidationError(ValueError): class ValidationError(ValueError):
@ -37,7 +37,7 @@ def validate_context(msg):
yield yield
except ValidationError as e: except ValidationError as e:
_, _, tb = sys.exc_info() _, _, tb = sys.exc_info()
five.reraise(ValidationError, ValidationError(e, ctx=msg), tb) six.reraise(ValidationError, ValidationError(e, ctx=msg), tb)
@contextlib.contextmanager @contextlib.contextmanager
@ -46,7 +46,7 @@ def reraise_as(tp):
yield yield
except ValidationError as e: except ValidationError as e:
_, _, tb = sys.exc_info() _, _, tb = sys.exc_info()
five.reraise(tp, tp(e), tb) six.reraise(tp, tp(e), tb)
def _dct_noop(self, dct): def _dct_noop(self, dct):
@ -218,7 +218,7 @@ def check_type(tp, typename=None):
check_bool = check_type(bool) check_bool = check_type(bool)
check_string = check_type(five.string_types, typename='string') check_string = check_type(six.string_types, typename='string')
def check_regex(v): def check_regex(v):

View file

@ -10,6 +10,7 @@ import subprocess
import tempfile import tempfile
import pkg_resources import pkg_resources
import six
from pre_commit import five from pre_commit import five
from pre_commit import parse_shebang from pre_commit import parse_shebang
@ -143,12 +144,12 @@ class CalledProcessError(RuntimeError):
def to_text(self): def to_text(self):
return self.to_bytes().decode('UTF-8') return self.to_bytes().decode('UTF-8')
if five.PY3: # pragma: no cover (py3) if six.PY2: # pragma: no cover (py2)
__bytes__ = to_bytes
__str__ = to_text
else: # pragma: no cover (py2)
__str__ = to_bytes __str__ = to_bytes
__unicode__ = to_text __unicode__ = to_text
else: # pragma: no cover (py3)
__bytes__ = to_bytes
__str__ = to_text
def cmd_output(*cmd, **kwargs): def cmd_output(*cmd, **kwargs):

View file

@ -43,6 +43,7 @@ setup(
'cached-property', 'cached-property',
'nodeenv>=0.11.1', 'nodeenv>=0.11.1',
'pyyaml', 'pyyaml',
'six',
'virtualenv', 'virtualenv',
], ],
entry_points={ entry_points={

View file

@ -8,9 +8,9 @@ import os.path
import mock import mock
import pytest import pytest
import six
import pre_commit.constants as C import pre_commit.constants as C
from pre_commit import five
from pre_commit import output from pre_commit import output
from pre_commit.logging_handler import add_logging_handler from pre_commit.logging_handler import add_logging_handler
from pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.prefixed_command_runner import PrefixedCommandRunner
@ -29,7 +29,7 @@ def tempdir_factory(tmpdir):
self.tmpdir_count = 0 self.tmpdir_count = 0
def get(self): def get(self):
path = tmpdir.join(five.text(self.tmpdir_count)).strpath path = tmpdir.join(six.text_type(self.tmpdir_count)).strpath
self.tmpdir_count += 1 self.tmpdir_count += 1
os.mkdir(path) os.mkdir(path)
return path return path

View file

@ -6,7 +6,6 @@ import subprocess
import mock import mock
import pytest import pytest
from pre_commit import five
from pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import CalledProcessError from pre_commit.util import CalledProcessError
@ -17,10 +16,7 @@ def norm_slash(input_tup):
def test_CalledProcessError_str(): def test_CalledProcessError_str():
error = CalledProcessError( error = CalledProcessError(
1, 1, [str('git'), str('status')], 0, (str('stdout'), str('stderr')),
[five.n('git'), five.n('status')],
0,
(five.n('stdout'), five.n('stderr')),
) )
assert str(error) == ( assert str(error) == (
"Command: ['git', 'status']\n" "Command: ['git', 'status']\n"
@ -35,7 +31,7 @@ def test_CalledProcessError_str():
def test_CalledProcessError_str_nooutput(): def test_CalledProcessError_str_nooutput():
error = CalledProcessError( error = CalledProcessError(
1, [five.n('git'), five.n('status')], 0, (five.n(''), five.n('')) 1, [str('git'), str('status')], 0, (str(''), str(''))
) )
assert str(error) == ( assert str(error) == (
"Command: ['git', 'status']\n" "Command: ['git', 'status']\n"
@ -78,7 +74,7 @@ def test_run_substitutes_prefix(popen_mock, makedirs_mock):
) )
ret = instance.run(['{prefix}bar', 'baz'], retcode=None) ret = instance.run(['{prefix}bar', 'baz'], retcode=None)
popen_mock.assert_called_once_with( popen_mock.assert_called_once_with(
(five.n(os.path.join('prefix', 'bar')), five.n('baz')), (str(os.path.join('prefix', 'bar')), str('baz')),
env=None, env=None,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,

View file

@ -7,8 +7,8 @@ import sqlite3
import mock import mock
import pytest import pytest
import six
from pre_commit import five
from pre_commit.store import _get_default_directory from pre_commit.store import _get_default_directory
from pre_commit.store import Store from pre_commit.store import Store
from pre_commit.util import cmd_output from pre_commit.util import cmd_output
@ -116,7 +116,7 @@ def test_clone_cleans_up_on_checkout_failure(store):
# doesn't exist! # doesn't exist!
store.clone('/i_dont_exist_lol', 'fake_sha') store.clone('/i_dont_exist_lol', 'fake_sha')
except Exception as e: except Exception as e:
assert '/i_dont_exist_lol' in five.text(e) assert '/i_dont_exist_lol' in six.text_type(e)
things_starting_with_repo = [ things_starting_with_repo = [
thing for thing in os.listdir(store.directory) thing for thing in os.listdir(store.directory)