public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Uri Lublin <uril@redhat.com>
To: Lucas Meneghel Rodrigues <lmr@redhat.com>
Cc: kvm@vger.kernel.org
Subject: Re: [Autotest] [PATCH 1/3] KVM test: Add new utility functions to kvm_utils
Date: Thu, 22 Oct 2009 12:56:17 +0200	[thread overview]
Message-ID: <4AE03A51.8060007@redhat.com> (raw)
In-Reply-To: <1256160707-4333-1-git-send-email-lmr@redhat.com>

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]
> +


      parent reply	other threads:[~2009-10-22 10:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-21 21:31 [PATCH 1/3] KVM test: Add new utility functions to kvm_utils Lucas Meneghel Rodrigues
2009-10-21 21:31 ` [PATCH 2/3] KVM test: Daily DVD test control file Lucas Meneghel Rodrigues
2009-10-21 21:31   ` [PATCH 3/3] KVM test: Extend VM.create() method to support SHA1 check Lucas Meneghel Rodrigues
2009-10-22 10:58     ` [Autotest] " Uri Lublin
2009-10-22 10:56 ` Uri Lublin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4AE03A51.8060007@redhat.com \
    --to=uril@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=lmr@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox