mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-13 08:01:09 +00:00
misc: add verify_file_checksums() function
Signed-off-by: Dan Streetman <ddstreet@canonical.com>
This commit is contained in:
parent
359cb18d8d
commit
e5a42b1ba1
@ -201,7 +201,20 @@ def codename_to_distribution(codename):
|
|||||||
return distro
|
return distro
|
||||||
|
|
||||||
|
|
||||||
def verify_file_checksum(pathname, alg, checksum, size=0):
|
def verify_file_checksums(pathname, checksums={}, size=0):
|
||||||
|
""" verify checksums of file
|
||||||
|
|
||||||
|
Any failure will log an error.
|
||||||
|
|
||||||
|
pathname: str
|
||||||
|
full path to file
|
||||||
|
checksums: dict
|
||||||
|
keys are alg name, values are expected checksum
|
||||||
|
size: int
|
||||||
|
size of file, if known
|
||||||
|
|
||||||
|
Returns True if all checks pass, False otherwise
|
||||||
|
"""
|
||||||
if not os.path.isfile(pathname):
|
if not os.path.isfile(pathname):
|
||||||
Logger.error('File not found: %s', pathname)
|
Logger.error('File not found: %s', pathname)
|
||||||
return False
|
return False
|
||||||
@ -210,21 +223,41 @@ def verify_file_checksum(pathname, alg, checksum, size=0):
|
|||||||
Logger.error('File %s incorrect size, got %s expected %s',
|
Logger.error('File %s incorrect size, got %s expected %s',
|
||||||
filename, os.path.getsize(pathname), size)
|
filename, os.path.getsize(pathname), size)
|
||||||
return False
|
return False
|
||||||
h = hashlib.new(alg)
|
|
||||||
with open(pathname, 'rb') as f:
|
for (alg, checksum) in checksums.items():
|
||||||
while True:
|
h = hashlib.new(alg)
|
||||||
block = f.read(h.block_size)
|
with open(pathname, 'rb') as f:
|
||||||
if len(block) == 0:
|
while True:
|
||||||
break
|
block = f.read(h.block_size)
|
||||||
h.update(block)
|
if len(block) == 0:
|
||||||
match = h.hexdigest() == checksum
|
break
|
||||||
if match:
|
h.update(block)
|
||||||
Logger.debug('File %s checksum (%s) verified: %s',
|
match = h.hexdigest() == checksum
|
||||||
filename, alg, checksum)
|
if match:
|
||||||
else:
|
Logger.debug('File %s checksum (%s) verified: %s',
|
||||||
Logger.error('File %s checksum (%s) mismatch: got %s expected %s',
|
filename, alg, checksum)
|
||||||
filename, alg, h.hexdigest(), checksum)
|
else:
|
||||||
return match
|
Logger.error('File %s checksum (%s) mismatch: got %s expected %s',
|
||||||
|
filename, alg, h.hexdigest(), checksum)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def verify_file_checksum(pathname, alg, checksum, size=0):
|
||||||
|
""" verify checksum of file
|
||||||
|
|
||||||
|
pathname: str
|
||||||
|
full path to file
|
||||||
|
alg: str
|
||||||
|
name of checksum alg
|
||||||
|
checksum: str
|
||||||
|
expected checksum
|
||||||
|
size: int
|
||||||
|
size of file, if known
|
||||||
|
|
||||||
|
Returns True if all checks pass, False otherwise
|
||||||
|
"""
|
||||||
|
return verify_file_checksums(pathname, {alg: checksum}, size)
|
||||||
|
|
||||||
|
|
||||||
def download(src, dst, size=0):
|
def download(src, dst, size=0):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user