comparison planemo/lib/python3.7/site-packages/pip/_internal/commands/hash.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 from __future__ import absolute_import
2
3 import hashlib
4 import logging
5 import sys
6
7 from pip._internal.cli.base_command import Command
8 from pip._internal.cli.status_codes import ERROR
9 from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES
10 from pip._internal.utils.misc import read_chunks
11
12 logger = logging.getLogger(__name__)
13
14
15 class HashCommand(Command):
16 """
17 Compute a hash of a local package archive.
18
19 These can be used with --hash in a requirements file to do repeatable
20 installs.
21
22 """
23 name = 'hash'
24 usage = '%prog [options] <file> ...'
25 summary = 'Compute hashes of package archives.'
26 ignore_require_venv = True
27
28 def __init__(self, *args, **kw):
29 super(HashCommand, self).__init__(*args, **kw)
30 self.cmd_opts.add_option(
31 '-a', '--algorithm',
32 dest='algorithm',
33 choices=STRONG_HASHES,
34 action='store',
35 default=FAVORITE_HASH,
36 help='The hash algorithm to use: one of %s' %
37 ', '.join(STRONG_HASHES))
38 self.parser.insert_option_group(0, self.cmd_opts)
39
40 def run(self, options, args):
41 if not args:
42 self.parser.print_usage(sys.stderr)
43 return ERROR
44
45 algorithm = options.algorithm
46 for path in args:
47 logger.info('%s:\n--hash=%s:%s',
48 path, algorithm, _hash_of_file(path, algorithm))
49
50
51 def _hash_of_file(path, algorithm):
52 """Return the hash digest of a file."""
53 with open(path, 'rb') as archive:
54 hash = hashlib.new(algorithm)
55 for chunk in read_chunks(archive):
56 hash.update(chunk)
57 return hash.hexdigest()