hash.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import hashlib
  2. import logging
  3. import sys
  4. from optparse import Values
  5. from pip._internal.cli.base_command import Command
  6. from pip._internal.cli.status_codes import ERROR, SUCCESS
  7. from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES
  8. from pip._internal.utils.misc import read_chunks, write_output
  9. logger = logging.getLogger(__name__)
  10. class HashCommand(Command):
  11. """
  12. Compute a hash of a local package archive.
  13. These can be used with --hash in a requirements file to do repeatable
  14. installs.
  15. """
  16. usage = "%prog [options] <file> ..."
  17. ignore_require_venv = True
  18. def add_options(self) -> None:
  19. self.cmd_opts.add_option(
  20. "-a",
  21. "--algorithm",
  22. dest="algorithm",
  23. choices=STRONG_HASHES,
  24. action="store",
  25. default=FAVORITE_HASH,
  26. help="The hash algorithm to use: one of {}".format(
  27. ", ".join(STRONG_HASHES)
  28. ),
  29. )
  30. self.parser.insert_option_group(0, self.cmd_opts)
  31. def run(self, options: Values, args: 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(
  38. "%s:\n--hash=%s:%s", path, algorithm, _hash_of_file(path, algorithm)
  39. )
  40. return SUCCESS
  41. def _hash_of_file(path: str, algorithm: 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()