diff mbox

[2/3] Autotest: Porting all hash operations to use utils.hash

Message ID 1265079896-5337-2-git-send-email-lmr@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lucas Meneghel Rodrigues Feb. 2, 2010, 3:04 a.m. UTC
None
diff mbox

Patch

diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
index f8089f7..da7c543 100644
--- a/client/tests/kvm/kvm_utils.py
+++ b/client/tests/kvm/kvm_utils.py
@@ -4,7 +4,7 @@  KVM test utility functions.
 @copyright: 2008-2009 Red Hat Inc.
 """
 
-import md5, sha, thread, subprocess, time, string, random, socket, os, signal
+import thread, subprocess, time, string, random, socket, os, signal
 import select, re, logging, commands, cPickle, pty
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
@@ -788,13 +788,7 @@  def hash_file(filename, size=None, method="md5"):
         size = fsize
     f = open(filename, 'rb')
 
-    if method == "md5":
-        hash = md5.new()
-    elif method == "sha1":
-        hash = sha.new()
-    else:
-        logging.error("Unknown hash type %s, returning None" % method)
-        return None
+    hash = utils.hash(method)
 
     while size > 0:
         if chunksize > size:
@@ -851,11 +845,7 @@  def unmap_url_cache(cachedir, url, expected_hash, method="md5"):
     failure_counter = 0
     while not file_hash == expected_hash:
         if os.path.isfile(file_local_path):
-            if method == "md5":
-                file_hash = hash_file(file_local_path, method="md5")
-            elif method == "sha1":
-                file_hash = hash_file(file_local_path, method="sha1")
-
+            file_hash = hash_file(file_local_path, method)
             if file_hash == expected_hash:
                 # File is already at the expected position and ready to go
                 src = file_from_url
diff --git a/client/tests/kvm/ppm_utils.py b/client/tests/kvm/ppm_utils.py
index 8ff31da..90ff46d 100644
--- a/client/tests/kvm/ppm_utils.py
+++ b/client/tests/kvm/ppm_utils.py
@@ -4,8 +4,8 @@  Utility functions to deal with ppm (qemu screendump format) files.
 @copyright: Red Hat 2008-2009
 """
 
-import md5, os, struct, time, re
-
+import os, struct, time, re
+from autotest_lib.client.bin import utils
 
 # Some directory/filename utils, for consistency
 
@@ -120,9 +120,9 @@  def image_md5sum(width, height, data):
     @data: PPM file data
     """
     header = "P6\n%d %d\n255\n" % (width, height)
-    md5obj = md5.new(header)
-    md5obj.update(data)
-    return md5obj.hexdigest()
+    hash = utils.hash('md5', header)
+    hash.update(data)
+    return hash.hexdigest()
 
 
 def get_region_md5sum(width, height, data, x1, y1, dx, dy,
diff --git a/client/tests/kvm/tests/steps.py b/client/tests/kvm/tests/steps.py
index 456fb44..8ebe7c1 100644
--- a/client/tests/kvm/tests/steps.py
+++ b/client/tests/kvm/tests/steps.py
@@ -4,7 +4,7 @@  Utilities to perform automatic guest installation using step files.
 @copyright: Red Hat 2008-2009
 """
 
-import os, time, md5, re, shutil, logging
+import os, time, re, shutil, logging
 from autotest_lib.client.common_lib import utils, error
 import kvm_utils, ppm_utils, kvm_subprocess
 try:
diff --git a/frontend/afe/rpc_utils.py b/frontend/afe/rpc_utils.py
index 8b993a8..91b796d 100644
--- a/frontend/afe/rpc_utils.py
+++ b/frontend/afe/rpc_utils.py
@@ -624,18 +624,3 @@  def interleave_entries(queue_entries, special_tasks):
 
     return interleaved_entries
 
-
-def get_sha1_hash(source):
-    """Gets the SHA-1 hash of the source string
-
-    @param source The string to hash
-    """
-    if sys.version_info < (2,5):
-        import sha
-        digest = sha.new()
-    else:
-        import hashlib
-        digest = hashlib.sha1()
-
-    digest.update(source)
-    return digest.hexdigest()
diff --git a/frontend/planner/models.py b/frontend/planner/models.py
index c4d8988..7f62471 100644
--- a/frontend/planner/models.py
+++ b/frontend/planner/models.py
@@ -3,7 +3,7 @@  import common
 from autotest_lib.frontend.afe import models as afe_models
 from autotest_lib.frontend.afe import model_logic, rpc_utils
 from autotest_lib.frontend.tko import models as tko_models
-from autotest_lib.client.common_lib import enum
+from autotest_lib.client.common_lib import enum, utils
 
 
 class Plan(dbmodels.Model):
@@ -102,7 +102,7 @@  class ControlFile(model_logic.ModelWithHash):
 
     @classmethod
     def _compute_hash(cls, **kwargs):
-        return rpc_utils.get_sha1_hash(kwargs['contents'])
+        return utils.hash('sha1', kwargs['contents']).hexdigest()
 
 
     def __unicode__(self):
@@ -322,8 +322,8 @@  class KeyVal(model_logic.ModelWithHash):
 
     @classmethod
     def _compute_hash(cls, **kwargs):
-        round1 = rpc_utils.get_sha1_hash(kwargs['key'])
-        return rpc_utils.get_sha1_hash(round1 + kwargs['value'])
+        round1 = utils.hash('sha1', kwargs['key']).hexdigest()
+        return utils.hash('sha1', round1 + kwargs['value']).hexdigest()
 
 
     def __unicode__(self):
diff --git a/tko/models.py b/tko/models.py
index bc70074..8f06049 100644
--- a/tko/models.py
+++ b/tko/models.py
@@ -1,4 +1,4 @@ 
-import os, md5
+import os
 
 from autotest_lib.client.common_lib import utils
 from autotest_lib.tko import utils as tko_utils
@@ -63,7 +63,7 @@  class kernel(object):
     @staticmethod
     def compute_hash(base, hashes):
         key_string = ','.join([base] + hashes)
-        return md5.new(key_string).hexdigest()
+        return utils.hash('md5', key_string).hexdigest()
 
 
 class test(object):
diff --git a/tko/parsers/version_1_unittest.py b/tko/parsers/version_1_unittest.py
index 5110fe8..f0ccf55 100755
--- a/tko/parsers/version_1_unittest.py
+++ b/tko/parsers/version_1_unittest.py
@@ -1,8 +1,9 @@ 
 #!/usr/bin/python
 
-import unittest, datetime, time, md5
+import unittest, datetime, time
 
 import common
+from autotest_lib.client.common_lib import utils
 from autotest_lib.tko.parsers import version_1
 
 
@@ -163,7 +164,7 @@  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()
+        kernel_hash = utils.hash("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 +179,7 @@  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()
+        kernel_hash = utils.hash("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..a5a797d 100755
--- a/utils/build_externals.py
+++ b/utils/build_externals.py
@@ -12,10 +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
 import subprocess, re
 import common
 from autotest_lib.client.common_lib import logging_config, logging_manager
+from autotest_lib.client.common_lib import utils
 
 # Where package source be fetched to relative to the top of the autotest tree.
 PACKAGE_DIR = 'ExternalSource'
@@ -152,7 +153,7 @@  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()
+        hex_sum = utils.hash('sha1', inputfile.read()).hexdigest()
     finally:
         inputfile.close()
     return hex_sum
@@ -532,7 +533,7 @@  class ExternalPackage(object):
                 raise FetchError('%s from %s fails Content-Length %d '
                                  'sanity check.' % (self.name, url,
                                                     data_length))
-            checksum = sha.sha()
+            checksum = utils.hash('sha1')
             total_read = 0
             output = open(local_path, 'wb')
             try: