From mboxrd@z Thu Jan 1 00:00:00 1970 From: Uri Lublin Subject: Re: [Autotest] [PATCH 1/3] KVM test: Add new utility functions to kvm_utils Date: Thu, 22 Oct 2009 12:56:17 +0200 Message-ID: <4AE03A51.8060007@redhat.com> References: <1256160707-4333-1-git-send-email-lmr@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org To: Lucas Meneghel Rodrigues Return-path: Received: from mx1.redhat.com ([209.132.183.28]:56386 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752813AbZJVK4c (ORCPT ); Thu, 22 Oct 2009 06:56:32 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n9MAub2I025159 for ; Thu, 22 Oct 2009 06:56:37 -0400 In-Reply-To: <1256160707-4333-1-git-send-email-lmr@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: 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] > +