Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/pip/_internal/commands/hash.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import hashlib | |
2 import logging | |
3 import sys | |
4 | |
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 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
10 | |
11 if MYPY_CHECK_RUNNING: | |
12 from optparse import Values | |
13 from typing import List | |
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 add_options(self): | |
30 # type: () -> None | |
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 # type: (Values, List[str]) -> int | |
43 if not args: | |
44 self.parser.print_usage(sys.stderr) | |
45 return ERROR | |
46 | |
47 algorithm = options.algorithm | |
48 for path in args: | |
49 write_output('%s:\n--hash=%s:%s', | |
50 path, algorithm, _hash_of_file(path, algorithm)) | |
51 return SUCCESS | |
52 | |
53 | |
54 def _hash_of_file(path, algorithm): | |
55 # type: (str, str) -> str | |
56 """Return the hash digest of a file.""" | |
57 with open(path, 'rb') as archive: | |
58 hash = hashlib.new(algorithm) | |
59 for chunk in read_chunks(archive): | |
60 hash.update(chunk) | |
61 return hash.hexdigest() |