Skip to contentSkip to conversation

Fixing sha/md5 deprecation messages from autotest

PATCH In review Lucas Meneghel Rodrigues 2 participants 4 messages Jan 29, 2010 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fixing sha/md5 deprecation messages from autotest

When using python > 2.4, use the recommended hashlib. When using python 2.4, fall back to the md5 and sha modules.

---
 tko/models.py                     |   12 ++++++++++--
 tko/parsers/version_1_unittest.py |   16 +++++++++++++---
 utils/build_externals.py          |   11 +++++++++--
 3 files changed, 32 insertions(+), 7 deletions(-)
tko/models.py +10 -2
index bc70074..2dc6724 100644
@@ -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):
tko/parsers/version_1_unittest.py +13 -3
index 5110fe8..a6e87e4 100755
@@ -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)
utils/build_externals.py +9 -2
index d58975e..8993249 100755
@@ -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
Signature · 1 line
1.6.6
#1 lore raw

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

Quoted 115 lines · Lucas Meneghel Rodrigues

When using python > 2.4, use the recommended hashlib. When using python 2.4, fall back to the md5 and sha modules.

---
 tko/models.py                     |   12 ++++++++++-- tko/parsers/version_1_unittest.py |   16 +++++++++++++--- utils/build_externals.py          |   11 +++++++++--

 3 files changed, 32 insertions(+), 7 deletions(-)
tko/models.py +5 -1
index bc70074..2dc6724 100644
@@ -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

tko/models.py +0 -0
@@ -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):

tko/parsers/version_1_unittest.py +5 -1
index 5110fe8..a6e87e4 100755
@@ -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

tko/parsers/version_1_unittest.py +8 -2
@@ -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)
utils/build_externals.py +0 -0
index d58975e..8993249 100755
@@ -12,7 +12,11 @@ Usage?  Just run it.

    utils/build_externals.py
 """

utils/build_externals.py +5 -1
-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

utils/build_externals.py +0 -0
@@ -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

#2 lore raw

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

Quoted 140 lines · John Admanski

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

Quoted 115 lines · Lucas Meneghel Rodrigues

When using python > 2.4, use the recommended hashlib. When using python 2.4, fall back to the md5 and sha modules.

---
 tko/models.py                     |   12 ++++++++++-- tko/parsers/version_1_unittest.py |   16 +++++++++++++--- utils/build_externals.py          |   11 +++++++++--

 3 files changed, 32 insertions(+), 7 deletions(-)
tko/models.py +5 -1
index bc70074..2dc6724 100644
@@ -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

tko/models.py +0 -0
@@ -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):

tko/parsers/version_1_unittest.py +5 -1
index 5110fe8..a6e87e4 100755
@@ -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

tko/parsers/version_1_unittest.py +8 -2
@@ -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)
utils/build_externals.py +0 -0
index d58975e..8993249 100755
@@ -12,7 +12,11 @@ Usage?  Just run it.

    utils/build_externals.py
 """

utils/build_externals.py +5 -1
-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

utils/build_externals.py +0 -0
@@ -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

#3 lore raw
Quoted 4 lines · John Admanski

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.

Yes, this makes sense, will do as you said!

Thanks,

Lucas