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
<[email protected]> 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 <[email protected]>
> ---
> tko/models.py | 12 ++++++++++--
> tko/parsers/version_1_unittest.py | 16 +++++++++++++---
> utils/build_externals.py | 11 +++++++++--
> 3 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:
> + import hashlib
> +except ImportError:
> + import md5
>
> from autotest_lib.client.common_lib import utils
> from autotest_lib.tko import utils as tko_utils
> @@ -63,7 +67,11 @@ class kernel(object):
> @staticmethod
> def compute_hash(base, hashes):
> key_string = ','.join([base] + hashes)
> - return md5.new(key_string).hexdigest()
> + try:
> + hash = hashlib.md5(key_string).hexdigest()
> + except NameError:
> + hash = md5.new(key_string).hexdigest()
> + return hash
>
>
> class 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 @@
> #!/usr/bin/python
>
> -import unittest, datetime, time, md5
> +import unittest, datetime, time
> +try:
> + import hashlib
> +except ImportError:
> + import md5
>
> import common
> from autotest_lib.tko.parsers import version_1
> @@ -163,7 +167,10 @@ class test_status_line(unittest.TestCase):
> "patch0": "first_patch 0 0",
> "patch1": "another_patch 0 0"})
> kern = line.get_kernel()
> - kernel_hash = md5.new("2.6.24-rc40,0,0").hexdigest()
> + try:
> + kernel_hash = hashlib.md5("2.6.24-rc40,0,0").hexdigest()
> + except NameError:
> + kernel_hash = md5.new("2.6.24-rc40,0,0").hexdigest()
> self.assertEquals(kern.base, "2.6.24-rc40")
> self.assertEquals(kern.patches[0].spec, "first_patch")
> self.assertEquals(kern.patches[1].spec, "another_patch")
> @@ -178,7 +185,10 @@ class test_status_line(unittest.TestCase):
> "patch0": "first_patch 0 0",
> "patch2": "another_patch 0 0"})
> kern = line.get_kernel()
> - kernel_hash = md5.new("2.6.24-rc40,0").hexdigest()
> + try:
> + kernel_hash = hashlib.md5("2.6.24-rc40,0").hexdigest()
> + except:
> + kernel_hash = md5.new("2.6.24-rc40,0").hexdigest()
> self.assertEquals(kern.base, "2.6.24-rc40")
> self.assertEquals(kern.patches[0].spec, "first_patch")
> 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? Just run it.
> utils/build_externals.py
> """
>
> -import compileall, logging, os, sha, shutil, sys, tempfile, time, urllib2
> +import compileall, logging, os, shutil, sys, tempfile, time, urllib2
> +try:
> + import hashlib
> +except ImportError:
> + import sha
> import subprocess, re
> import common
> from autotest_lib.client.common_lib import logging_config, logging_manager
> @@ -152,7 +156,10 @@ def _checksum_file(full_path):
> """@returns The hex checksum of a file given its pathname."""
> inputfile = open(full_path, 'rb')
> try:
> - hex_sum = sha.sha(inputfile.read()).hexdigest()
> + try:
> + hex_sum = hashlib.sha1(inputfile.read()).hexdigest()
> + except NameError:
> + hex_sum = sha.sha(inputfile.read()).hexdigest()
> finally:
> inputfile.close()
> return hex_sum
> --
> 1.6.6
>
> _______________________________________________
> Autotest mailing list
> [email protected]
> http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
>
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html