Skip to content

Commit

Permalink
Move hash_file to utils.misc
Browse files Browse the repository at this point in the history
Why: Allows for better code usablity without introducing a
wheel->wheel_builder dependency.
  • Loading branch information
pradyunsg committed Nov 3, 2019
1 parent 226fea6 commit 9f10b82
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
15 changes: 15 additions & 0 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import contextlib
import errno
import getpass
import hashlib
import io
import logging
import os
Expand Down Expand Up @@ -868,3 +869,17 @@ def is_console_interactive():
"""Is this console interactive?
"""
return sys.stdin is not None and sys.stdin.isatty()


def hash_file(path, blocksize=1 << 20):
# type: (str, int) -> Tuple[Any, int]
"""Return (hash, length) for path using hashlib.sha256()
"""

h = hashlib.sha256()
length = 0
with open(path, 'rb') as f:
for block in read_chunks(f, size=blocksize):
length += len(block)
h.update(block)
return (h, length) # type: ignore
3 changes: 1 addition & 2 deletions src/pip/_internal/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
UnsupportedWheel,
)
from pip._internal.locations import get_major_minor_version
from pip._internal.utils.misc import captured_stdout, ensure_dir
from pip._internal.utils.misc import captured_stdout, ensure_dir, hash_file
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from pip._internal.wheel_builder import hash_file

if MYPY_CHECK_RUNNING:
from typing import (
Expand Down
16 changes: 2 additions & 14 deletions src/pip/_internal/wheel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pip._internal.models.link import Link
from pip._internal.utils.logging import indent_log
from pip._internal.utils.marker_files import has_delete_marker_file
from pip._internal.utils.misc import ensure_dir, read_chunks
from pip._internal.utils.misc import ensure_dir, hash_file
from pip._internal.utils.setuptools_build import (
make_setuptools_bdist_wheel_args,
make_setuptools_clean_args,
Expand All @@ -32,7 +32,7 @@

if MYPY_CHECK_RUNNING:
from typing import (
Any, Callable, Iterable, List, Optional, Pattern, Text, Tuple, Union,
Any, Callable, Iterable, List, Optional, Pattern, Text, Union,
)

from pip._internal.cache import WheelCache
Expand All @@ -46,18 +46,6 @@
logger = logging.getLogger(__name__)


def hash_file(path, blocksize=1 << 20):
# type: (str, int) -> Tuple[Any, int]
"""Return (hash, length) for path using hashlib.sha256()"""
h = hashlib.sha256()
length = 0
with open(path, 'rb') as f:
for block in read_chunks(f, size=blocksize):
length += len(block)
h.update(block)
return (h, length) # type: ignore


def replace_python_tag(wheelname, new_tag):
# type: (str, str) -> str
"""Replace the Python tag in a wheel file name with a new value."""
Expand Down

0 comments on commit 9f10b82

Please sign in to comment.