On 10/21/2009 11:31 PM, Lucas Meneghel Rodrigues wrote:
Some distributors ship CD and DVD files with SHA1 hash sums instead of MD5 hash sums, so let's extend the kvm_utils functions to evaluate and compare SHA1 hashes: * sha1sum_file(): Calculate SHA1 sum for file +def sha1sum_file(filename, size=None): + """ + Calculate the sha1sum of filename. + If size is not None, limit to first size bytes. + Throw exception if something is wrong with filename. + Can be also implemented with bash one-liner (assuming size%1024==0): + dd if=filename bs=1024 count=size/1024 | sha1sum - + + @param filename: Path of the file that will have its sha1sum calculated. + @param returns: sha1sum of the file. + """ + chunksize = 4096 + fsize = os.path.getsize(filename) + if not size or size>fsize: + size = fsize + f = open(filename, 'rb') + o = sha.new() + while size> 0: + if chunksize> size: + chunksize = size + data = f.read(chunksize) + if len(data) == 0: + logging.debug("Nothing left to read but size=%d" % size) + break + o.update(data) + size -= len(data) + f.close() + return o.hexdigest() +
This code and md5sum_file can share code. Just pass o (or hash_new func: sha.new/md5.new) as a parameter to the function.
+ + +def get_hash_from_file(sha_path, dvd_basename):
For consistency, replace sha_path with hash_path (as in the comment below).
+ """ + Get the a hash from a given DVD image from a hash file + (Hash files are usually named MD5SUM or SHA1SUM and are located inside the + download directories of the DVDs) + + @param hash_path: Local path to a hash file. + @param cd_image: Basename of a CD image + """ + hash_file = open(sha_path, 'r') + for line in hash_file.readlines(): + if dvd_basename in line: + return line.split()[0] +
-- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html