This commit is contained in:
Ambulance Clerc
2023-06-01 08:59:37 +02:00
parent 1fe8228d1b
commit 796746d175
346 changed files with 18799 additions and 44645 deletions

View File

@@ -20,18 +20,21 @@ class HashCommand(Command):
installs.
"""
usage = '%prog [options] <file> ...'
usage = "%prog [options] <file> ..."
ignore_require_venv = True
def add_options(self) -> None:
self.cmd_opts.add_option(
'-a', '--algorithm',
dest='algorithm',
"-a",
"--algorithm",
dest="algorithm",
choices=STRONG_HASHES,
action='store',
action="store",
default=FAVORITE_HASH,
help='The hash algorithm to use: one of {}'.format(
', '.join(STRONG_HASHES)))
help="The hash algorithm to use: one of {}".format(
", ".join(STRONG_HASHES)
),
)
self.parser.insert_option_group(0, self.cmd_opts)
def run(self, options: Values, args: List[str]) -> int:
@@ -41,14 +44,15 @@ class HashCommand(Command):
algorithm = options.algorithm
for path in args:
write_output('%s:\n--hash=%s:%s',
path, algorithm, _hash_of_file(path, algorithm))
write_output(
"%s:\n--hash=%s:%s", path, algorithm, _hash_of_file(path, algorithm)
)
return SUCCESS
def _hash_of_file(path: str, algorithm: str) -> str:
"""Return the hash digest of a file."""
with open(path, 'rb') as archive:
with open(path, "rb") as archive:
hash = hashlib.new(algorithm)
for chunk in read_chunks(archive):
hash.update(chunk)