From: John Admanski Subject: Re: [Autotest] [PATCH] Fixing sha/md5 deprecation messages from autotest Date: Fri, 29 Jan 2010 07:27:54 -0800 Message-ID: <394b34481001290727m48b044ecx1ca0109f7ffdb6@mail.gmail.com> References: <1264743382-11198-1-git-send-email-lmr@redhat.com> <394b34481001290725v3deb6c36j95db3fd2dcc22335@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: autotest@test.kernel.org, kvm@vger.kernel.org To: Lucas Meneghel Rodrigues Return-path: Received: from smtp-out.google.com ([216.239.44.51]:21487 "EHLO smtp-out.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754831Ab0A2P2S convert rfc822-to-8bit (ORCPT ); Fri, 29 Jan 2010 10:28:18 -0500 Received: from spaceape24.eur.corp.google.com (spaceape24.eur.corp.google.com [172.28.16.76]) by smtp-out.google.com with ESMTP id o0TFSGLD031743 for ; Fri, 29 Jan 2010 07:28:16 -0800 Received: from pwi2 (pwi2.prod.google.com [10.241.219.2]) by spaceape24.eur.corp.google.com with ESMTP id o0TFSExw017803 for ; Fri, 29 Jan 2010 07:28:15 -0800 Received: by pwi2 with SMTP id 2so3481821pwi.37 for ; Fri, 29 Jan 2010 07:28:14 -0800 (PST) In-Reply-To: <394b34481001290725v3deb6c36j95db3fd2dcc22335@mail.gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: Assuming you do this, it also might make more sense to call them md5_digest and sha1_digest, or something a little less ambiguous than just md5 and sha1; clashing with the stdlib module name is probably a bad idea. -- John On Fri, Jan 29, 2010 at 7:25 AM, John Admanski w= rote: > Instead of wrapping all the calls in try-except blocks, maybe it woul= d > make more sense to add a couple of utility functions to the common_li= b > utils.py that do it for you? I'm just picturing something like: > > def md5(bytes): > =A0 =A0try: > =A0 =A0 =A0 =A0import hashlib > =A0 =A0 =A0 =A0return hashlib.md5(bytes).hexdigest() > =A0 =A0except ImportError: > =A0 =A0 =A0 =A0import md5 > =A0 =A0 =A0 =A0return md5.new(bytes).hexdigest() > > and then something similar for sha1. This would keep all the > complexity in one place. > > I'm just picturing a code review in the future where I have to explai= n > to someone that they have to wrap their import and digest in > try-excepts...it would be a lot easier to just tell them to use the > functions in utils.py instead. :) > > -- John > > On Thu, Jan 28, 2010 at 9:36 PM, Lucas Meneghel Rodrigues > wrote: >> When using python > 2.4, use the recommended hashlib. >> When using python 2.4, fall back to the md5 and sha >> modules. >> >> Signed-off-by: Lucas Meneghel Rodrigues >> --- >> =A0tko/models.py =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | =A0 12 ++= ++++++++-- >> =A0tko/parsers/version_1_unittest.py | =A0 16 +++++++++++++--- >> =A0utils/build_externals.py =A0 =A0 =A0 =A0 =A0| =A0 11 +++++++++-- >> =A03 files changed, 32 insertions(+), 7 deletions(-) >> >> diff --git a/tko/models.py b/tko/models.py >> index bc70074..2dc6724 100644 >> --- a/tko/models.py >> +++ b/tko/models.py >> @@ -1,4 +1,8 @@ >> -import os, md5 >> +import os >> +try: >> + =A0 =A0import hashlib >> +except ImportError: >> + =A0 =A0import md5 >> >> =A0from autotest_lib.client.common_lib import utils >> =A0from autotest_lib.tko import utils as tko_utils >> @@ -63,7 +67,11 @@ class kernel(object): >> =A0 =A0 @staticmethod >> =A0 =A0 def compute_hash(base, hashes): >> =A0 =A0 =A0 =A0 key_string =3D ','.join([base] + hashes) >> - =A0 =A0 =A0 =A0return md5.new(key_string).hexdigest() >> + =A0 =A0 =A0 =A0try: >> + =A0 =A0 =A0 =A0 =A0 =A0hash =3D hashlib.md5(key_string).hexdigest(= ) >> + =A0 =A0 =A0 =A0except NameError: >> + =A0 =A0 =A0 =A0 =A0 =A0hash =3D md5.new(key_string).hexdigest() >> + =A0 =A0 =A0 =A0return hash >> >> >> =A0class test(object): >> diff --git a/tko/parsers/version_1_unittest.py b/tko/parsers/version= _1_unittest.py >> index 5110fe8..a6e87e4 100755 >> --- a/tko/parsers/version_1_unittest.py >> +++ b/tko/parsers/version_1_unittest.py >> @@ -1,6 +1,10 @@ >> =A0#!/usr/bin/python >> >> -import unittest, datetime, time, md5 >> +import unittest, datetime, time >> +try: >> + =A0 =A0import hashlib >> +except ImportError: >> + =A0 =A0import md5 >> >> =A0import common >> =A0from autotest_lib.tko.parsers import version_1 >> @@ -163,7 +167,10 @@ class test_status_line(unittest.TestCase): >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 "patch0": "first_patch 0 0", >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 "patch1": "another_patch 0 0"}) >> =A0 =A0 =A0 =A0 kern =3D line.get_kernel() >> - =A0 =A0 =A0 =A0kernel_hash =3D md5.new("2.6.24-rc40,0,0").hexdiges= t() >> + =A0 =A0 =A0 =A0try: >> + =A0 =A0 =A0 =A0 =A0 =A0kernel_hash =3D hashlib.md5("2.6.24-rc40,0,= 0").hexdigest() >> + =A0 =A0 =A0 =A0except NameError: >> + =A0 =A0 =A0 =A0 =A0 =A0kernel_hash =3D md5.new("2.6.24-rc40,0,0").= hexdigest() >> =A0 =A0 =A0 =A0 self.assertEquals(kern.base, "2.6.24-rc40") >> =A0 =A0 =A0 =A0 self.assertEquals(kern.patches[0].spec, "first_patch= ") >> =A0 =A0 =A0 =A0 self.assertEquals(kern.patches[1].spec, "another_pat= ch") >> @@ -178,7 +185,10 @@ class test_status_line(unittest.TestCase): >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 "patch0": "first_patch 0 0", >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 "patch2": "another_patch 0 0"}) >> =A0 =A0 =A0 =A0 kern =3D line.get_kernel() >> - =A0 =A0 =A0 =A0kernel_hash =3D md5.new("2.6.24-rc40,0").hexdigest(= ) >> + =A0 =A0 =A0 =A0try: >> + =A0 =A0 =A0 =A0 =A0 =A0kernel_hash =3D hashlib.md5("2.6.24-rc40,0"= ).hexdigest() >> + =A0 =A0 =A0 =A0except: >> + =A0 =A0 =A0 =A0 =A0 =A0kernel_hash =3D md5.new("2.6.24-rc40,0").he= xdigest() >> =A0 =A0 =A0 =A0 self.assertEquals(kern.base, "2.6.24-rc40") >> =A0 =A0 =A0 =A0 self.assertEquals(kern.patches[0].spec, "first_patch= ") >> =A0 =A0 =A0 =A0 self.assertEquals(len(kern.patches), 1) >> diff --git a/utils/build_externals.py b/utils/build_externals.py >> index d58975e..8993249 100755 >> --- a/utils/build_externals.py >> +++ b/utils/build_externals.py >> @@ -12,7 +12,11 @@ Usage? =A0Just run it. >> =A0 =A0 utils/build_externals.py >> =A0""" >> >> -import compileall, logging, os, sha, shutil, sys, tempfile, time, u= rllib2 >> +import compileall, logging, os, shutil, sys, tempfile, time, urllib= 2 >> +try: >> + =A0 =A0import hashlib >> +except ImportError: >> + =A0 =A0import sha >> =A0import subprocess, re >> =A0import common >> =A0from autotest_lib.client.common_lib import logging_config, loggin= g_manager >> @@ -152,7 +156,10 @@ def _checksum_file(full_path): >> =A0 =A0 """@returns The hex checksum of a file given its pathname.""= " >> =A0 =A0 inputfile =3D open(full_path, 'rb') >> =A0 =A0 try: >> - =A0 =A0 =A0 =A0hex_sum =3D sha.sha(inputfile.read()).hexdigest() >> + =A0 =A0 =A0 =A0try: >> + =A0 =A0 =A0 =A0 =A0 =A0hex_sum =3D hashlib.sha1(inputfile.read()).= hexdigest() >> + =A0 =A0 =A0 =A0except NameError: >> + =A0 =A0 =A0 =A0 =A0 =A0hex_sum =3D sha.sha(inputfile.read()).hexdi= gest() >> =A0 =A0 finally: >> =A0 =A0 =A0 =A0 inputfile.close() >> =A0 =A0 return hex_sum >> -- >> 1.6.6 >> >> _______________________________________________ >> Autotest mailing list >> Autotest@test.kernel.org >> http://test.kernel.org/cgi-bin/mailman/listinfo/autotest >> >