hash.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import hashlib
  2. import logging
  3. import sys
  4. from optparse import Values
  5. from typing import List
  6. from pip._internal.cli.base_command import Command
  7. from pip._internal.cli.status_codes import ERROR, SUCCESS
  8. from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES
  9. from pip._internal.utils.misc import read_chunks, write_output
  10. logger = logging.getLogger(__name__)
  11. class HashCommand(Command):
  12. """
  13. Compute a hash of a local package archive.
  14. These can be used with --hash in a requirements file to do repeatable
  15. installs.
  16. """
  17. usage = '%prog [options] <file> ...'
  18. ignore_require_venv = True
  19. def add_options(self):
  20. # type: () -> None
  21. self.cmd_opts.add_option(
  22. '-a', '--algorithm',
  23. dest='algorithm',
  24. choices=STRONG_HASHES,
  25. action='store',
  26. default=FAVORITE_HASH,
  27. help='The hash algorithm to use: one of {}'.format(
  28. ', '.join(STRONG_HASHES)))
  29. self.parser.insert_option_group(0, self.cmd_opts)
  30. def run(self, options, args):
  31. # type: (Values, List[str]) -> int
  32. if not args:
  33. self.parser.print_usage(sys.stderr)
  34. return ERROR
  35. algorithm = options.algorithm
  36. for path in args:
  37. write_output('%s:\n--hash=%s:%s',
  38. path, algorithm, _hash_of_file(path, algorithm))
  39. return SUCCESS
  40. def _hash_of_file(path, algorithm):
  41. # type: (str, str) -> str
  42. """Return the hash digest of a file."""
  43. with open(path, 'rb') as archive:
  44. hash = hashlib.new(algorithm)
  45. for chunk in read_chunks(archive):
  46. hash.update(chunk)
  47. return hash.hexdigest()