Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/pip/_internal/commands/hash.py
811 views
1
# The following comment should be removed at some point in the future.
2
# mypy: disallow-untyped-defs=False
3
4
from __future__ import absolute_import
5
6
import hashlib
7
import logging
8
import sys
9
10
from pip._internal.cli.base_command import Command
11
from pip._internal.cli.status_codes import ERROR
12
from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES
13
from pip._internal.utils.misc import read_chunks, write_output
14
15
logger = logging.getLogger(__name__)
16
17
18
class HashCommand(Command):
19
"""
20
Compute a hash of a local package archive.
21
22
These can be used with --hash in a requirements file to do repeatable
23
installs.
24
"""
25
26
usage = '%prog [options] <file> ...'
27
ignore_require_venv = True
28
29
def __init__(self, *args, **kw):
30
super(HashCommand, self).__init__(*args, **kw)
31
self.cmd_opts.add_option(
32
'-a', '--algorithm',
33
dest='algorithm',
34
choices=STRONG_HASHES,
35
action='store',
36
default=FAVORITE_HASH,
37
help='The hash algorithm to use: one of {}'.format(
38
', '.join(STRONG_HASHES)))
39
self.parser.insert_option_group(0, self.cmd_opts)
40
41
def run(self, options, args):
42
if not args:
43
self.parser.print_usage(sys.stderr)
44
return ERROR
45
46
algorithm = options.algorithm
47
for path in args:
48
write_output('%s:\n--hash=%s:%s',
49
path, algorithm, _hash_of_file(path, algorithm))
50
51
52
def _hash_of_file(path, algorithm):
53
"""Return the hash digest of a file."""
54
with open(path, 'rb') as archive:
55
hash = hashlib.new(algorithm)
56
for chunk in read_chunks(archive):
57
hash.update(chunk)
58
return hash.hexdigest()
59
60