Rev 3738: Fix python2.6 deprecation warnings related to hashlib. in file:///v/home/vila/src/bzr/experimental/bzr-py26-compat/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Sep 26 08:09:53 BST 2008


At file:///v/home/vila/src/bzr/experimental/bzr-py26-compat/

------------------------------------------------------------
revno: 3738
revision-id: v.ladeuil+lp at free.fr-20080926070950-ux9kpjwt403ctegs
parent: v.ladeuil+lp at free.fr-20080925160322-omrugnbgkcwp9eo2
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: deprecations
timestamp: Fri 2008-09-26 09:09:50 +0200
message:
  Fix python2.6 deprecation warnings related to hashlib.
  
  * bzrlib/osutils.py: 
  Wrap md5 and sha imports to be compatible with python 2.4, 2.5 and
  above.  Replace all sha.new() calls by sha() calls they are
  reputedly faster (not profiled).
  
  * bzrlib/weave.py: 
  Update sha import, fix use.	
  
  * bzrlib/transport/http/_urllib2_wrappers.py: 
  Update md5 and sha imports, fix use.	
  
  * bzrlib/tests/test_testament.py:
  Update sha import, fix use.	
  
  * bzrlib/tests/test_knit.py:
  Update sha import, fix use.	
  
  * bzrlib/tests/test_hashcache.py: 
  Update sha import, fix use.	
  
  * bzrlib/tests/per_repository/test_check_reconcile.py:
  Update sha import, fix use.	
  
  * bzrlib/tests/http_utils.py: 
  Update md5 and sha imports, fix use.	
  
  * bzrlib/testament.py: 
  Update sha import, fix use.	
  
  * bzrlib/repofmt/pack_repo.py: 
  Update md5 import, fix use.	
  
  * bzrlib/hashcache.py: 
  Update sha import, fix use.	
  
  * bzrlib/btree_index.py: 
  Delete useless sha import.
-------------- next part --------------
=== modified file 'bzrlib/btree_index.py'
--- a/bzrlib/btree_index.py	2008-08-28 20:19:20 +0000
+++ b/bzrlib/btree_index.py	2008-09-26 07:09:50 +0000
@@ -22,7 +22,6 @@
 from bisect import bisect_right
 from copy import deepcopy
 import math
-import sha
 import struct
 import tempfile
 import zlib

=== modified file 'bzrlib/hashcache.py'
--- a/bzrlib/hashcache.py	2008-04-24 07:22:53 +0000
+++ b/bzrlib/hashcache.py	2008-09-26 07:09:50 +0000
@@ -30,9 +30,8 @@
 CACHE_HEADER = "### bzr hashcache v5\n"
 
 import os, stat, time
-import sha
 
-from bzrlib.osutils import sha_file, pathjoin, safe_unicode
+from bzrlib.osutils import sha_file, sha_string, pathjoin, safe_unicode
 from bzrlib.trace import mutter, warning
 from bzrlib.atomicfile import AtomicFile
 from bzrlib.errors import BzrError
@@ -164,7 +163,7 @@
         if stat.S_ISREG(mode):
             digest = self._really_sha1_file(abspath)
         elif stat.S_ISLNK(mode):
-            digest = sha.new(os.readlink(abspath)).hexdigest()
+            digest = sha_string(os.readlink(abspath))
         else:
             raise BzrError("file %r: unknown file stat mode: %o"%(abspath,mode))
 

=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2008-09-24 01:11:38 +0000
+++ b/bzrlib/osutils.py	2008-09-26 07:09:50 +0000
@@ -35,7 +35,6 @@
                     splitdrive as _nt_splitdrive,
                     )
 import posixpath
-import sha
 import shutil
 from shutil import (
     rmtree,
@@ -53,6 +52,19 @@
     )
 """)
 
+# sha and md5 modules are deprecated in python2.6 but hashlib is available as
+# of 2.5
+if sys.version_info < (2, 5):
+    import md5 as _md5
+    md5 = _md5.new
+    import sha as _sha
+    sha = _sha.new
+else:
+    from hashlib import (
+        md5,
+        sha1 as sha,
+        )
+
 
 import bzrlib
 from bzrlib import symbol_versioning
@@ -569,7 +581,7 @@
 
     The file cursor should be already at the start.
     """
-    s = sha.new()
+    s = sha()
     BUFSIZE = 128<<10
     while True:
         b = f.read(BUFSIZE)
@@ -581,7 +593,7 @@
 
 def sha_file_by_name(fname):
     """Calculate the SHA1 of a file by reading the full text"""
-    s = sha.new()
+    s = sha()
     f = os.open(fname, os.O_RDONLY | O_BINARY)
     try:
         while True:
@@ -593,21 +605,21 @@
         os.close(f)
 
 
-def sha_strings(strings, _factory=sha.new):
+def sha_strings(strings, _factory=sha):
     """Return the sha-1 of concatenation of strings"""
     s = _factory()
     map(s.update, strings)
     return s.hexdigest()
 
 
-def sha_string(f, _factory=sha.new):
+def sha_string(f, _factory=sha):
     return _factory(f).hexdigest()
 
 
 def fingerprint_file(f):
     b = f.read()
     return {'size': len(b),
-            'sha1': sha.new(b).hexdigest()}
+            'sha1': sha(b).hexdigest()}
 
 
 def compare_files(a, b):

=== modified file 'bzrlib/repofmt/pack_repo.py'
--- a/bzrlib/repofmt/pack_repo.py	2008-09-02 03:17:08 +0000
+++ b/bzrlib/repofmt/pack_repo.py	2008-09-26 07:09:50 +0000
@@ -17,22 +17,22 @@
 from bzrlib.lazy_import import lazy_import
 lazy_import(globals(), """
 from itertools import izip
-import md5
 import time
 
 from bzrlib import (
     debug,
     graph,
+    osutils,
     pack,
     transactions,
     ui,
     )
 from bzrlib.index import (
+    CombinedGraphIndex,
     GraphIndex,
     GraphIndexBuilder,
+    GraphIndexPrefixAdapter,
     InMemoryGraphIndex,
-    CombinedGraphIndex,
-    GraphIndexPrefixAdapter,
     )
 from bzrlib.knit import (
     KnitPlainFactory,
@@ -40,7 +40,6 @@
     _KnitGraphIndex,
     _DirectPackAccess,
     )
-from bzrlib.osutils import rand_chars, split_lines
 from bzrlib import tsort
 """)
 from bzrlib import (
@@ -256,7 +255,7 @@
         # What file mode to upload the pack and indices with.
         self._file_mode = file_mode
         # tracks the content written to the .pack file.
-        self._hash = md5.new()
+        self._hash = osutils.md5()
         # a four-tuple with the length in bytes of the indices, once the pack
         # is finalised. (rev, inv, text, sigs)
         self.index_sizes = None
@@ -266,7 +265,7 @@
         # under creation.
         self._cache_limit = 0
         # the temporary pack file name.
-        self.random_name = rand_chars(20) + upload_suffix
+        self.random_name = osutils.rand_chars(20) + upload_suffix
         # when was this pack started ?
         self.start_time = time.time()
         # open an output stream for the data added to the pack.
@@ -1072,7 +1071,7 @@
                     raise errors.BzrError('Mismatched key parent %r:%r' %
                         (key, parent_keys))
                 parents.append(parent_key[1])
-            text_lines = split_lines(repo.texts.get_record_stream(
+            text_lines = osutils.split_lines(repo.texts.get_record_stream(
                 [key], 'unordered', True).next().get_bytes_as('fulltext'))
             output_texts.add_lines(key, parent_keys, text_lines,
                 random_id=True, check_content=False)

=== modified file 'bzrlib/testament.py'
--- a/bzrlib/testament.py	2008-04-24 07:22:53 +0000
+++ b/bzrlib/testament.py	2008-09-26 07:09:50 +0000
@@ -70,9 +70,12 @@
 # revisions can be serialized.
 
 from copy import copy
-from sha import sha
 
-from bzrlib.osutils import contains_whitespace, contains_linebreaks
+from bzrlib.osutils import (
+    contains_whitespace,
+    contains_linebreaks,
+    sha,
+    )
 
 
 class Testament(object):

=== modified file 'bzrlib/tests/http_utils.py'
--- a/bzrlib/tests/http_utils.py	2008-08-14 02:15:31 +0000
+++ b/bzrlib/tests/http_utils.py	2008-09-26 07:09:50 +0000
@@ -16,17 +16,17 @@
 
 from cStringIO import StringIO
 import errno
-import md5
 import re
-import sha
 import socket
 import threading
 import time
 import urllib2
 import urlparse
 
+
 from bzrlib import (
     errors,
+    osutils,
     tests,
     )
 from bzrlib.smart import medium, protocol
@@ -397,7 +397,7 @@
         A1 = '%s:%s:%s' % (user, realm, password)
         A2 = '%s:%s' % (command, auth['uri'])
 
-        H = lambda x: md5.new(x).hexdigest()
+        H = lambda x: osutils.md5(x).hexdigest()
         KD = lambda secret, data: H("%s:%s" % (secret, data))
 
         nonce_count = int(auth['nc'], 16)

=== modified file 'bzrlib/tests/per_repository/test_check_reconcile.py'
--- a/bzrlib/tests/per_repository/test_check_reconcile.py	2008-09-04 20:32:04 +0000
+++ b/bzrlib/tests/per_repository/test_check_reconcile.py	2008-09-26 07:09:50 +0000
@@ -19,8 +19,7 @@
 That is, tests for reconcile and check.
 """
 
-
-import sha
+from bzrlib import osutils
 
 from bzrlib.inventory import Inventory, InventoryFile
 from bzrlib.revision import Revision
@@ -98,7 +97,7 @@
         entry.text_size = 0
         if file_contents is None:
             file_contents = '%sline\n' % entry.revision
-        entry.text_sha1 = sha.sha(file_contents).hexdigest()
+        entry.text_sha1 = osutils.sha(file_contents).hexdigest()
         inv.add(entry)
         if make_file_version:
             repo.texts.add_lines((file_id, revision),

=== modified file 'bzrlib/tests/test_hashcache.py'
--- a/bzrlib/tests/test_hashcache.py	2007-11-02 05:12:17 +0000
+++ b/bzrlib/tests/test_hashcache.py	2008-09-26 07:09:50 +0000
@@ -15,18 +15,18 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import os
-import sha
 import stat
 import sys
 import time
 
+from bzrlib import osutils
 from bzrlib.errors import BzrError
 from bzrlib.hashcache import HashCache
 from bzrlib.tests import OsFifoFeature, TestCaseInTempDir, TestCase
 
 
 def sha1(t):
-    return sha.new(t).hexdigest()
+    return osutils.sha(t).hexdigest()
 
 
 def pause():

=== modified file 'bzrlib/tests/test_knit.py'
--- a/bzrlib/tests/test_knit.py	2008-07-14 07:39:30 +0000
+++ b/bzrlib/tests/test_knit.py	2008-09-26 07:09:50 +0000
@@ -19,7 +19,6 @@
 from cStringIO import StringIO
 import difflib
 import gzip
-import sha
 import sys
 
 from bzrlib import (
@@ -27,6 +26,7 @@
     generate_ids,
     knit,
     multiparent,
+    osutils,
     pack,
     )
 from bzrlib.errors import (
@@ -48,8 +48,6 @@
     _KnitKeyAccess,
     make_file_factory,
     )
-from bzrlib.osutils import split_lines
-from bzrlib.symbol_versioning import one_four
 from bzrlib.tests import (
     Feature,
     KnownFailure,
@@ -375,7 +373,7 @@
         return sio.getvalue()
 
     def test_valid_knit_data(self):
-        sha1sum = sha.new('foo\nbar\n').hexdigest()
+        sha1sum = osutils.sha('foo\nbar\n').hexdigest()
         gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
                                         'foo\n'
                                         'bar\n'
@@ -394,7 +392,7 @@
         self.assertEqual([(('rev-id-1',), gz_txt, sha1sum)], raw_contents)
 
     def test_not_enough_lines(self):
-        sha1sum = sha.new('foo\n').hexdigest()
+        sha1sum = osutils.sha('foo\n').hexdigest()
         # record says 2 lines data says 1
         gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
                                         'foo\n'
@@ -412,7 +410,7 @@
         self.assertEqual([(('rev-id-1',),  gz_txt, sha1sum)], raw_contents)
 
     def test_too_many_lines(self):
-        sha1sum = sha.new('foo\nbar\n').hexdigest()
+        sha1sum = osutils.sha('foo\nbar\n').hexdigest()
         # record says 1 lines data says 2
         gz_txt = self.create_gz_content('version rev-id-1 1 %s\n'
                                         'foo\n'
@@ -431,7 +429,7 @@
         self.assertEqual([(('rev-id-1',), gz_txt, sha1sum)], raw_contents)
 
     def test_mismatched_version_id(self):
-        sha1sum = sha.new('foo\nbar\n').hexdigest()
+        sha1sum = osutils.sha('foo\nbar\n').hexdigest()
         gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
                                         'foo\n'
                                         'bar\n'
@@ -450,7 +448,7 @@
             knit._read_records_iter_raw(records))
 
     def test_uncompressed_data(self):
-        sha1sum = sha.new('foo\nbar\n').hexdigest()
+        sha1sum = osutils.sha('foo\nbar\n').hexdigest()
         txt = ('version rev-id-1 2 %s\n'
                'foo\n'
                'bar\n'
@@ -470,7 +468,7 @@
             knit._read_records_iter_raw(records))
 
     def test_corrupted_data(self):
-        sha1sum = sha.new('foo\nbar\n').hexdigest()
+        sha1sum = osutils.sha('foo\nbar\n').hexdigest()
         gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
                                         'foo\n'
                                         'bar\n'
@@ -1641,7 +1639,7 @@
         key_basis = ('bar',)
         key_missing = ('missing',)
         test.add_lines(key, (), ['foo\n'])
-        key_sha1sum = sha.new('foo\n').hexdigest()
+        key_sha1sum = osutils.sha('foo\n').hexdigest()
         sha1s = test.get_sha1s([key])
         self.assertEqual({key: key_sha1sum}, sha1s)
         self.assertEqual([], basis.calls)
@@ -1649,7 +1647,7 @@
         # directly (rather than via text reconstruction) so that remote servers
         # etc don't have to answer with full content.
         basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
-        basis_sha1sum = sha.new('foo\nbar\n').hexdigest()
+        basis_sha1sum = osutils.sha('foo\nbar\n').hexdigest()
         basis.calls = []
         sha1s = test.get_sha1s([key, key_missing, key_basis])
         self.assertEqual({key: key_sha1sum,

=== modified file 'bzrlib/tests/test_repository.py'
--- a/bzrlib/tests/test_repository.py	2008-09-04 21:10:07 +0000
+++ b/bzrlib/tests/test_repository.py	2008-09-26 07:09:50 +0000
@@ -22,7 +22,6 @@
 also see this file.
 """
 
-import md5
 from stat import S_ISDIR
 from StringIO import StringIO
 
@@ -52,6 +51,7 @@
     bzrdir,
     errors,
     inventory,
+    osutils,
     progress,
     repository,
     revision as _mod_revision,
@@ -974,7 +974,7 @@
             pack_transport)
         self.assertIsInstance(pack.revision_index, InMemoryGraphIndex)
         self.assertIsInstance(pack.inventory_index, InMemoryGraphIndex)
-        self.assertIsInstance(pack._hash, type(md5.new()))
+        self.assertIsInstance(pack._hash, type(osutils.md5()))
         self.assertTrue(pack.upload_transport is upload_transport)
         self.assertTrue(pack.index_transport is index_transport)
         self.assertTrue(pack.pack_transport is pack_transport)

=== modified file 'bzrlib/tests/test_testament.py'
--- a/bzrlib/tests/test_testament.py	2007-11-01 09:52:45 +0000
+++ b/bzrlib/tests/test_testament.py	2008-09-26 07:09:50 +0000
@@ -19,8 +19,8 @@
 # TODO: Testaments with x-bits
 
 import os
-from sha import sha
 
+from bzrlib import osutils
 from bzrlib.tests import SymlinkFeature, TestCaseWithTransport
 from bzrlib.testament import Testament, StrictTestament, StrictTestament3
 from bzrlib.transform import TreeTransform
@@ -211,21 +211,21 @@
 bazaar-ng testament short form 1
 revision-id: test at user-1
 sha1: %s
-""" % sha(REV_1_TESTAMENT).hexdigest()
+""" % osutils.sha(REV_1_TESTAMENT).hexdigest()
 
 
 REV_1_SHORT_STRICT = """\
 bazaar-ng testament short form 2.1
 revision-id: test at user-1
 sha1: %s
-""" % sha(REV_1_STRICT_TESTAMENT).hexdigest()
+""" % osutils.sha(REV_1_STRICT_TESTAMENT).hexdigest()
 
 
 REV_1_SHORT_STRICT3 = """\
 bazaar testament short form 3 strict
 revision-id: test at user-1
 sha1: %s
-""" % sha(REV_1_STRICT_TESTAMENT3).hexdigest()
+""" % osutils.sha(REV_1_STRICT_TESTAMENT3).hexdigest()
 
 
 REV_2_TESTAMENT = """\
@@ -293,21 +293,21 @@
 bazaar-ng testament short form 1
 revision-id: test at user-2
 sha1: %s
-""" % sha(REV_2_TESTAMENT).hexdigest()
+""" % osutils.sha(REV_2_TESTAMENT).hexdigest()
 
 
 REV_2_SHORT_STRICT = """\
 bazaar-ng testament short form 2.1
 revision-id: test at user-2
 sha1: %s
-""" % sha(REV_2_STRICT_TESTAMENT).hexdigest()
+""" % osutils.sha(REV_2_STRICT_TESTAMENT).hexdigest()
 
 
 REV_2_SHORT_STRICT3 = """\
 bazaar testament short form 3 strict
 revision-id: test at user-2
 sha1: %s
-""" % sha(REV_2_STRICT_TESTAMENT3).hexdigest()
+""" % osutils.sha(REV_2_STRICT_TESTAMENT3).hexdigest()
 
 
 REV_PROPS_TESTAMENT = """\

=== modified file 'bzrlib/transport/http/_urllib2_wrappers.py'
--- a/bzrlib/transport/http/_urllib2_wrappers.py	2008-05-19 10:30:37 +0000
+++ b/bzrlib/transport/http/_urllib2_wrappers.py	2008-09-26 07:09:50 +0000
@@ -47,8 +47,6 @@
 # ensure that.
 
 import httplib
-import md5
-import sha
 import socket
 import urllib
 import urllib2
@@ -62,6 +60,7 @@
     config,
     debug,
     errors,
+    osutils,
     trace,
     transport,
     ui,
@@ -1123,9 +1122,9 @@
     H = None
     KD = None
     if algorithm == 'MD5':
-        H = lambda x: md5.new(x).hexdigest()
+        H = lambda x: osutils.md5(x).hexdigest()
     elif algorithm == 'SHA':
-        H = lambda x: sha.new(x).hexdigest()
+        H = lambda x: osutils.sha(x).hexdigest()
     if H is not None:
         KD = lambda secret, data: H("%s:%s" % (secret, data))
     return H, KD
@@ -1134,7 +1133,7 @@
 def get_new_cnonce(nonce, nonce_count):
     raw = '%s:%d:%s:%s' % (nonce, nonce_count, time.ctime(),
                            urllib2.randombytes(8))
-    return sha.new(raw).hexdigest()[:16]
+    return osutils.sha(raw).hexdigest()[:16]
 
 
 class DigestAuthHandler(AbstractAuthHandler):

=== modified file 'bzrlib/weave.py'
--- a/bzrlib/weave.py	2008-09-25 16:02:34 +0000
+++ b/bzrlib/weave.py	2008-09-26 07:09:50 +0000
@@ -71,7 +71,6 @@
 from copy import copy
 from cStringIO import StringIO
 import os
-import sha
 import time
 import warnings
 
@@ -86,7 +85,7 @@
         WeaveRevisionNotPresent,
         )
 import bzrlib.errors as errors
-from bzrlib.osutils import dirname, sha_strings, split_lines
+from bzrlib.osutils import dirname, sha, sha_strings, split_lines
 import bzrlib.patiencediff
 from bzrlib.revision import NULL_REVISION
 from bzrlib.symbol_versioning import *
@@ -799,7 +798,7 @@
             # For creating the ancestry, IntSet is much faster (3.7s vs 0.17s)
             # The problem is that set membership is much more expensive
             name = self._idx_to_name(i)
-            sha1s[name] = sha.new()
+            sha1s[name] = sha()
             texts[name] = []
             new_inc = set([name])
             for p in self._parents[i]:



More information about the bazaar-commits mailing list