mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-05-08 15:31:29 +00:00
ubuntutools/misc: add verify_file_checksum function
This commit is contained in:
parent
9f428e471b
commit
8ebd086aac
@ -23,6 +23,7 @@
|
|||||||
# ##################################################################
|
# ##################################################################
|
||||||
|
|
||||||
# Modules.
|
# Modules.
|
||||||
|
import hashlib
|
||||||
import locale
|
import locale
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@ -32,6 +33,10 @@ import distro_info
|
|||||||
|
|
||||||
from ubuntutools.lp.udtexceptions import PocketDoesNotExistError
|
from ubuntutools.lp.udtexceptions import PocketDoesNotExistError
|
||||||
|
|
||||||
|
import logging
|
||||||
|
Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_POCKETS = ('Release', 'Security', 'Updates', 'Proposed')
|
DEFAULT_POCKETS = ('Release', 'Security', 'Updates', 'Proposed')
|
||||||
POCKETS = DEFAULT_POCKETS + ('Backports',)
|
POCKETS = DEFAULT_POCKETS + ('Backports',)
|
||||||
|
|
||||||
@ -188,3 +193,29 @@ def codename_to_distribution(codename):
|
|||||||
|
|
||||||
if info().valid(codename):
|
if info().valid(codename):
|
||||||
return distro
|
return distro
|
||||||
|
|
||||||
|
|
||||||
|
def verify_file_checksum(pathname, alg, checksum, size=0):
|
||||||
|
if not os.path.isfile(pathname):
|
||||||
|
Logger.error('File not found: %s', pathname)
|
||||||
|
return False
|
||||||
|
filename = os.path.basename(pathname)
|
||||||
|
if size and size != os.path.getsize(pathname):
|
||||||
|
Logger.error('File %s incorrect size, got %s expected %s',
|
||||||
|
filename, os.path.getsize(pathname), size)
|
||||||
|
return False
|
||||||
|
h = hashlib.new(alg)
|
||||||
|
with open(pathname, 'rb') as f:
|
||||||
|
while True:
|
||||||
|
block = f.read(h.block_size)
|
||||||
|
if len(block) == 0:
|
||||||
|
break
|
||||||
|
h.update(block)
|
||||||
|
match = h.hexdigest() == checksum
|
||||||
|
if match:
|
||||||
|
Logger.debug('File %s checksum (%s) verified: %s',
|
||||||
|
filename, alg, checksum)
|
||||||
|
else:
|
||||||
|
Logger.error('File %s checksum (%s) mismatch: got %s expected %s',
|
||||||
|
filename, alg, h.hexdigest(), checksum)
|
||||||
|
return match
|
||||||
|
Loading…
x
Reference in New Issue
Block a user