From: Artem Bityutskiy Date: Wed, 24 Oct 2012 07:44:18 +0000 (+0300) Subject: misc.py: make 'calc_hashes()' generic X-Git-Tag: 0.16~82^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=84ce3b5e4d2d438ef0fcaf5db300a7b21c6f1d98;p=platform%2Fupstream%2Fmic.git misc.py: make 'calc_hashes()' generic The 'calc_hashes()' function is generic so it does not make much sense to hide it in the 'RawImageCreator' class. Move it to 'misc.py' where miscellaneous stuff like this is kept. While moving, also rename the 'file_name' argument to 'file_path'. Also change the interface so that we pass a list of hash names to calculate, not a list of hash objects. Also do not import 'md5' from hashlib to the current name-spaces. Note, originally I have these things as a set of separate patches, but merged to satisfy review comments. Change-Id: I31d8ac35cf55447ddb5c56cfafa319a6fef85373 Signed-off-by: Artem Bityutskiy --- diff --git a/mic/imager/raw.py b/mic/imager/raw.py index e771c83..205c7c2 100644 --- a/mic/imager/raw.py +++ b/mic/imager/raw.py @@ -404,38 +404,6 @@ class RawImageCreator(BaseImageCreator): shutil.move(src,dst) self._write_image_xml() - def _calc_hashes(self, file_name, hashes, start = 0, end = None): - """ Calculate hashes for a file. The 'file_name' argument is the file - to calculate hash functions for, 'start' and 'end' are the starting and - ending file offset to calculate the has functions for. The 'hashes' - argument is a list of haslib hash functions to calculate. Returns the - the list of calculated hash values in the hexadecimal form in the same - order as 'hashes'. """ - - if end == None: - end = os.path.getsize(file_name) - - chunk_size = 65536 - to_read = end - start; - read = 0 - - with open(file_name, "rb") as f: - f.seek(start) - - while read < to_read: - if read + chunk_size > to_read: - chunk_size = to_read - read - chunk = f.read(chunk_size) - for hash_obj in hashes: - hash_obj.update(chunk) - read += chunk_size - - result = [] - for hash_obj in hashes: - result.append(hash_obj.hexdigest()) - - return result - def _write_image_xml(self): imgarch = "i686" if self.target_arch and self.target_arch.startswith("arm"): @@ -488,8 +456,7 @@ class RawImageCreator(BaseImageCreator): xml += " \n" \ % (full_name, self.__disk_format) - hashes = self._calc_hashes(diskpath, - (hashlib.sha1(), hashlib.sha256())) + hashes = misc.calc_hashes(diskpath, ('sha1', 'sha256')) xml += " %s\n" \ % hashes[0] @@ -645,9 +612,9 @@ class RawImageCreator(BaseImageCreator): mapped_cnt = 0 for first, last in self._get_ranges(f_image, blocks_cnt): mapped_cnt += last - first + 1 - sha1 = self._calc_hashes(image, (hashlib.sha1(),), - first * block_size, - (last + 1) * block_size) + sha1 = misc.calc_hashes(image, ('sha1', ), + first * block_size, + (last + 1) * block_size) f_bmap.write("\t\t %s-%s \n" \ % (sha1[0], first, last)) diff --git a/mic/utils/misc.py b/mic/utils/misc.py index ee018c3..d4ad1c0 100644 --- a/mic/utils/misc.py +++ b/mic/utils/misc.py @@ -25,7 +25,7 @@ import hashlib import subprocess import platform import rpmmisc -from hashlib import md5 +import hashlib import sqlite3 as sqlite from xml.etree import cElementTree @@ -225,17 +225,44 @@ def check_space_pre_cp(src, dst): raise CreatorError("space on %s(%s) is not enough for about %s files" % (dst, human_size(freesize), human_size(srcsize))) -def get_md5sum(fpath): - blksize = 65536 # should be optimized enough +def calc_hashes(file_path, hash_names, start = 0, end = None): + """ Calculate hashes for a file. The 'file_path' argument is the file + to calculate hash functions for, 'start' and 'end' are the starting and + ending file offset to calculate the has functions for. The 'hash_names' + argument is a list of hash names to calculate. Returns the the list + of calculated hash values in the hexadecimal form in the same order + as 'hash_names'. """ - md5sum = md5() - with open(fpath, 'rb') as f: - while True: - data = f.read(blksize) - if not data: - break - md5sum.update(data) - return md5sum.hexdigest() + if end == None: + end = os.path.getsize(file_path) + + chunk_size = 65536 + to_read = end - start; + read = 0 + + hashes = [] + for hash_name in hash_names: + hashes.append(hashlib.new(hash_name)) + + with open(file_path, "rb") as f: + f.seek(start) + + while read < to_read: + if read + chunk_size > to_read: + chunk_size = to_read - read + chunk = f.read(chunk_size) + for hash_obj in hashes: + hash_obj.update(chunk) + read += chunk_size + + result = [] + for hash_obj in hashes: + result.append(hash_obj.hexdigest()) + + return result + +def get_md5sum(fpath): + return calc_hashes(fpath, ('md5', ))[0] def normalize_ksfile(ksconf, release, arch): def _clrtempks():