Hashlib doesn't take unicode.

This commit is contained in:
Anthony Sottile
2014-06-18 07:36:11 -07:00
parent a34f3fc7c9
commit 5d1ffcba75
3 changed files with 14 additions and 11 deletions

View File

@@ -1,6 +1,5 @@
from __future__ import unicode_literals
import hashlib
import io
import logging
import os
@@ -11,6 +10,7 @@ from plumbum import local
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import clean_path_on_failure
from pre_commit.util import hex_md5
logger = logging.getLogger('pre_commit')
@@ -75,9 +75,7 @@ class Store(object):
self.require_created()
# Check if we already exist
sha_path = os.path.join(
self.directory, sha + '_' + hashlib.md5(url).hexdigest()
)
sha_path = os.path.join(self.directory, sha + '_' + hex_md5(url))
if os.path.exists(sha_path):
return os.readlink(sha_path)

View File

@@ -2,6 +2,7 @@ from __future__ import unicode_literals
import contextlib
import functools
import hashlib
import os
import os.path
import shutil
@@ -56,3 +57,11 @@ def noop_context():
def shell_escape(arg):
return "'" + arg.replace("'", "'\"'\"'".strip()) + "'"
def hex_md5(s):
"""Hexdigest an md5 of the string.
:param text s:
"""
return hashlib.md5(s.encode('utf-8')).hexdigest()

View File

@@ -1,7 +1,6 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import hashlib
import io
import mock
import os
@@ -14,6 +13,7 @@ from pre_commit import five
from pre_commit.store import _get_default_directory
from pre_commit.store import logger
from pre_commit.store import Store
from pre_commit.util import hex_md5
from testing.fixtures import git_dir
from testing.util import get_head_sha
@@ -105,9 +105,7 @@ def test_clone(store, tmpdir_factory, log_info_mock):
assert get_head_sha(ret) == sha
# Assert that we made a symlink from the sha to the repo
sha_path = os.path.join(
store.directory, sha + '_' + hashlib.md5(path).hexdigest(),
)
sha_path = os.path.join(store.directory, sha + '_' + hex_md5(path))
assert os.path.exists(sha_path)
assert os.path.islink(sha_path)
assert os.readlink(sha_path) == ret
@@ -141,9 +139,7 @@ def test_clone_when_repo_already_exists(store):
os.mkdir(repo_dir_path)
os.symlink(
repo_dir_path,
os.path.join(
store.directory, 'fake_sha' + '_' + hashlib.md5('url').hexdigest(),
),
os.path.join(store.directory, 'fake_sha' + '_' + hex_md5('url')),
)
ret = store.clone('url', 'fake_sha')