From patchwork Tue Feb 2 03:04:54 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Meneghel Rodrigues X-Patchwork-Id: 76207 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o1235JPr010274 for ; Tue, 2 Feb 2010 03:05:19 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753834Ab0BBDFI (ORCPT ); Mon, 1 Feb 2010 22:05:08 -0500 Received: from mx1.redhat.com ([209.132.183.28]:16511 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753327Ab0BBDFH (ORCPT ); Mon, 1 Feb 2010 22:05:07 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o123528A000380 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 1 Feb 2010 22:05:02 -0500 Received: from localhost.localdomain (vpn-9-172.rdu.redhat.com [10.11.9.172]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o1234x9Q002186; Mon, 1 Feb 2010 22:05:00 -0500 From: Lucas Meneghel Rodrigues To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, jadmanski@google.com, Lucas Meneghel Rodrigues Subject: [PATCH 1/3] Utils: Create a generic hash function for autotest Date: Tue, 2 Feb 2010 01:04:54 -0200 Message-Id: <1265079896-5337-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Tue, 02 Feb 2010 03:05:19 +0000 (UTC) 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: