Add a utility to generate a hashsum of a file

This commit is contained in:
Jesse Keating 2008-07-14 23:26:52 -04:00
parent 0517ef0782
commit 38bd19ea9a

View File

@ -16,6 +16,7 @@ import subprocess
import os
import shutil
import sys
import hashlib
def _doRunCommand(command, logger, rundir='/tmp', output=subprocess.PIPE, error=subprocess.PIPE, env=None):
"""Run a command and log the output. Error out if we get something on stderr"""
@ -86,3 +87,29 @@ def _ensuredir(target, logger, force=False, clean=False):
else:
sys.stderr(message)
sys.exit(1)
def _doCheckSum(path, hash, logger, binary=True):
"""Generate a checksum hash from a provided path.
Set binary to false if the file is not binary.
Return the hash"""
try:
func = getattr(hashlib, hash)
except AttributeError:
logger.error("Invalid hash type: %s" % hash)
return False
if binary:
flags = 'rb'
else:
flags = 'r'
try:
myfile = open(path, 'rb')
except IOError, e:
logger.error("Could not open file %s: %s" % (path, e))
return False
sum = func(myfile.read()).hexdigest()
return sum