]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_internal/commands/hash.py
4 from optparse
import Values
5 from typing
import List
7 from pip
._internal
.cli
.base_command
import Command
8 from pip
._internal
.cli
.status_codes
import ERROR
, SUCCESS
9 from pip
._internal
.utils
.hashes
import FAVORITE_HASH
, STRONG_HASHES
10 from pip
._internal
.utils
.misc
import read_chunks
, write_output
12 logger
= logging
.getLogger(__name__
)
15 class HashCommand(Command
):
17 Compute a hash of a local package archive.
19 These can be used with --hash in a requirements file to do repeatable
23 usage
= "%prog [options] <file> ..."
24 ignore_require_venv
= True
26 def add_options(self
) -> None:
27 self
.cmd_opts
.add_option(
31 choices
=STRONG_HASHES
,
33 default
=FAVORITE_HASH
,
34 help="The hash algorithm to use: one of {}".format(
35 ", ".join(STRONG_HASHES
)
38 self
.parser
.insert_option_group(0, self
.cmd_opts
)
40 def run(self
, options
: Values
, args
: List
[str]) -> int:
42 self
.parser
.print_usage(sys
.stderr
)
45 algorithm
= options
.algorithm
48 "%s:\n--hash=%s:%s", path
, algorithm
, _hash_of_file(path
, algorithm
)
53 def _hash_of_file(path
: str, algorithm
: str) -> str:
54 """Return the hash digest of a file."""
55 with open(path
, "rb") as archive
:
56 hash = hashlib
.new(algorithm
)
57 for chunk
in read_chunks(archive
):
59 return hash.hexdigest()