From: John Admanski Subject: Re: [Autotest] [PATCH] Fixing sha/md5 deprecation messages from autotest Date: Fri, 29 Jan 2010 07:25:16 -0800 Message-ID: <394b34481001290725v3deb6c36j95db3fd2dcc22335@mail.gmail.com> References: <1264743382-11198-1-git-send-email-lmr@redhat.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]:20738 "EHLO smtp-out.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755925Ab0A2PZt convert rfc822-to-8bit (ORCPT ); Fri, 29 Jan 2010 10:25:49 -0500 Received: from kpbe18.cbf.corp.google.com (kpbe18.cbf.corp.google.com [172.25.105.82]) by smtp-out.google.com with ESMTP id o0TFPlGj028446 for ; Fri, 29 Jan 2010 07:25:47 -0800 Received: from pxi38 (pxi38.prod.google.com [10.243.27.38]) by kpbe18.cbf.corp.google.com with ESMTP id o0TFPaar019829 for ; Fri, 29 Jan 2010 07:25:46 -0800 Received: by pxi38 with SMTP id 38so1568008pxi.28 for ; Fri, 29 Jan 2010 07:25:36 -0800 (PST) In-Reply-To: <1264743382-11198-1-git-send-email-lmr@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Instead of wrapping all the calls in try-except blocks, maybe it would make more sense to add a couple of utility functions to the common_lib utils.py that do it for you? I'm just picturing something like: def md5(bytes): try: import hashlib return hashlib.md5(bytes).hexdigest() except ImportError: import md5 return 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 explain 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").hexdigest= () > + =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").h= exdigest() > =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_patc= h") > @@ -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")= =2Ehexdigest() > + =A0 =A0 =A0 =A0except: > + =A0 =A0 =A0 =A0 =A0 =A0kernel_hash =3D md5.new("2.6.24-rc40,0").hex= digest() > =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, ur= llib2 > +import compileall, logging, os, shutil, sys, tempfile, time, urllib2 > +try: > + =A0 =A0import hashlib > +except ImportError: > + =A0 =A0import sha > =A0import subprocess, re > =A0import common > =A0from autotest_lib.client.common_lib import logging_config, logging= _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()).h= exdigest() > + =A0 =A0 =A0 =A0except NameError: > + =A0 =A0 =A0 =A0 =A0 =A0hex_sum =3D sha.sha(inputfile.read()).hexdig= est() > =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 >