mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-05-13 18:01:28 +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,6 +223,8 @@ 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
|
||||||
|
|
||||||
|
for (alg, checksum) in checksums.items():
|
||||||
h = hashlib.new(alg)
|
h = hashlib.new(alg)
|
||||||
with open(pathname, 'rb') as f:
|
with open(pathname, 'rb') as f:
|
||||||
while True:
|
while True:
|
||||||
@ -224,7 +239,25 @@ def verify_file_checksum(pathname, alg, checksum, size=0):
|
|||||||
else:
|
else:
|
||||||
Logger.error('File %s checksum (%s) mismatch: got %s expected %s',
|
Logger.error('File %s checksum (%s) mismatch: got %s expected %s',
|
||||||
filename, alg, h.hexdigest(), checksum)
|
filename, alg, h.hexdigest(), checksum)
|
||||||
return match
|
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