1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Consolidate hash constants in pip.utils.hashing.

This commit is contained in:
Erik Rose 2015-10-12 16:29:08 -04:00
parent 7c5e5039ad
commit e23f59673e
4 changed files with 19 additions and 22 deletions

View file

@ -17,6 +17,7 @@ from pip.index import (
PyPI, FormatControl, fmt_ctl_handle_mutual_exclude, fmt_ctl_no_binary,
fmt_ctl_no_use_wheel)
from pip.locations import CA_BUNDLE_PATH, USER_CACHE_DIR, src_prefix
from pip.utils.hashes import STRONG_HASHES
def make_option_group(group, parser):
@ -523,14 +524,6 @@ always_unzip = partial(
)
def strong_hashes():
"""Return names of hashlib algorithms allowed by the --hash option.
Currently, those are the ones at least as collision-resistant as sha256.
"""
return ['sha256', 'sha384', 'sha512']
def _merge_hash(option, opt_str, value, parser):
"""Given a value spelled "algo:digest", append the digest to a list
pointed to in a dict by the algo name."""
@ -542,10 +535,9 @@ def _merge_hash(option, opt_str, value, parser):
parser.error('Arguments to %s must be a hash name '
'followed by a value, like --hash=sha256:abcde...' %
opt_str)
strongs = strong_hashes()
if algo not in strongs:
if algo not in STRONG_HASHES:
parser.error('Allowed hash algorithms for %s are %s.' %
(opt_str, ', '.join(strongs)))
(opt_str, ', '.join(STRONG_HASHES)))
parser.values.hashes.setdefault(algo, []).append(digest)

View file

@ -5,10 +5,9 @@ import logging
import sys
from pip.basecommand import Command
from pip.cmdoptions import strong_hashes
from pip.exceptions import FAVORITE_HASH
from pip.status_codes import ERROR
from pip.utils import read_chunks
from pip.utils.hashes import FAVORITE_HASH, STRONG_HASHES
logger = logging.getLogger(__name__)
@ -31,11 +30,11 @@ class HashCommand(Command):
self.cmd_opts.add_option(
'-a', '--algorithm',
dest='algorithm',
choices=strong_hashes(),
choices=STRONG_HASHES,
action='store',
default=FAVORITE_HASH,
help='The hash algorithm to use: one of %s' %
', '.join(strong_hashes()))
', '.join(STRONG_HASHES))
self.parser.insert_option_group(0, self.cmd_opts)
def run(self, options, args):

View file

@ -51,11 +51,6 @@ class UnsupportedWheel(InstallationError):
"""Unsupported wheel."""
# The recommended hash algo of the moment. Change this whenever the state of
# the art changes; it won't hurt backward compatibility.
FAVORITE_HASH = 'sha256'
class HashErrors(InstallationError):
"""Multiple HashError instances rolled into one for reporting"""
@ -163,6 +158,8 @@ class HashMissing(HashError):
self.gotten_hash = gotten_hash
def body(self):
from pip.utils.hashes import FAVORITE_HASH # Dodge circular import.
package_name = (self.req.req if self.req and
# In case someone feeds something
# downright stupid to

View file

@ -2,12 +2,21 @@ from __future__ import absolute_import
import hashlib
from pip.exceptions import (HashMismatch, HashMissing, InstallationError,
FAVORITE_HASH)
from pip.exceptions import HashMismatch, HashMissing, InstallationError
from pip.utils import read_chunks
from pip._vendor.six import iteritems, iterkeys, itervalues
# The recommended hash algo of the moment. Change this whenever the state of
# the art changes; it won't hurt backward compatibility.
FAVORITE_HASH = 'sha256'
# Names of hashlib algorithms allowed by the --hash option and ``pip hash``
# Currently, those are the ones at least as collision-resistant as sha256.
STRONG_HASHES = ['sha256', 'sha384', 'sha512']
class Hashes(object):
"""A wrapper that builds multiple hashes at once and checks them against
known-good values