From: Lucas Meneghel Rodrigues <lmr@redhat.com>
To: autotest@test.kernel.org
Cc: kvm@vger.kernel.org
Subject: [PATCH 1/3] Utils: Create a generic hash function for autotest
Date: Tue, 2 Feb 2010 01:04:54 -0200 [thread overview]
Message-ID: <1265079896-5337-1-git-send-email-lmr@redhat.com> (raw)
In order to get rid of md5/sha deprecation messages
when using python 2.6 and keeping 2.4 working, a
new function called utils.hash() was created: This
function abstracts the complexity of conditional
importing and can be used throughout autotest in
pretty much all the scenarios where an md5 or sha1
hash is needed.
The function returns a hash object, so if you want
the digest or hexdigest of the whole content you
need to explicitly call hexdigest(). This was made
because in some occasions we don't want to calculate
the digest straight away, rather we update the hash
with content until the point we can evaluate the
digest.
By the way, the name chosen does not clash with the
API of neither py 2.6 hashlib nor py 2.4 md5 or sha.
Signed-off-by: Lucas Meneghel Rodrigues <lmr@redhat.com>
---
client/common_lib/utils.py | 34 ++++++++++++++++++++++++++++++++++
1 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py
index bda72e2..a095e06 100644
--- a/client/common_lib/utils.py
+++ b/client/common_lib/utils.py
@@ -4,6 +4,10 @@
import os, pickle, random, re, resource, select, shutil, signal, StringIO
import socket, struct, subprocess, sys, time, textwrap, urlparse
import warnings, smtplib, logging, urllib2
+try:
+ import hashlib
+except ImportError:
+ import md5, sha
from autotest_lib.client.common_lib import error, barrier, logging_manager
def deprecated(func):
@@ -278,6 +282,36 @@ def urlretrieve(url, filename, data=None, timeout=300):
src_file.close()
+def hash(type, input=None):
+ """
+ Returns an hash object of type md5 or sha1. This function is implemented in
+ order to encapsulate hash objects in a way that is compatible with python
+ 2.4 and python 2.6 without warnings.
+
+ Note that even though python 2.6 hashlib supports hash types other than
+ md5 and sha1, we are artificially limiting the input values in order to
+ make the function to behave exactly the same among both python
+ implementations.
+
+ @param input: Optional input string that will be used to update the hash.
+ """
+ if type not in ['md5', 'sha1']:
+ raise ValueError("Unsupported hash type: %s" % type)
+
+ try:
+ hash = hashlib.new(type)
+ except NameError:
+ if type == 'md5':
+ hash = md5.new()
+ elif type == 'sha1':
+ hash = sha.new()
+
+ if input:
+ hash.update(input)
+
+ return hash
+
+
def get_file(src, dest, permissions=None):
"""Get a file from src, which can be local or a remote URL"""
if src == dest:
--
1.6.6
next reply other threads:[~2010-02-02 3:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-02 3:04 Lucas Meneghel Rodrigues [this message]
2010-02-02 3:04 ` [PATCH 2/3] Autotest: Porting all hash operations to use utils.hash Lucas Meneghel Rodrigues
2010-02-02 3:04 ` [PATCH 3/3] Client utils: Update utils.unmap_url_cache, added hash_file Lucas Meneghel Rodrigues
2010-02-02 15:21 ` [PATCH 2/3] Autotest: Porting all hash operations to use utils.hash John Admanski
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=1265079896-5337-1-git-send-email-lmr@redhat.com \
--to=lmr@redhat.com \
--cc=autotest@test.kernel.org \
--cc=kvm@vger.kernel.org \
/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