diff --git a/swh/objstorage/objstorage.py b/swh/objstorage/objstorage.py --- a/swh/objstorage/objstorage.py +++ b/swh/objstorage/objstorage.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2017 The Software Heritage developers +# Copyright (C) 2015-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -16,10 +16,20 @@ def compute_hash(content): - return hashutil.hash_data( + """Compute the content's hash. + + Args: + content (bytes): The raw content to hash + hash_name (str): Hash's name (default to ID_HASH_ALGO) + + Returns: + The ID_HASH_ALGO for the content + + """ + return hashutil.MultiHash.from_data( content, - algorithms=[ID_HASH_ALGO] - ).get(ID_HASH_ALGO) + hash_names=[ID_HASH_ALGO], + ).digest().get(ID_HASH_ALGO) class ObjStorage(metaclass=abc.ABCMeta): diff --git a/swh/objstorage/objstorage_pathslicing.py b/swh/objstorage/objstorage_pathslicing.py --- a/swh/objstorage/objstorage_pathslicing.py +++ b/swh/objstorage/objstorage_pathslicing.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2017 The Software Heritage developers +# Copyright (C) 2015-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -259,8 +259,8 @@ break f.rewind() - checksums = hashutil.hash_file(f, length, - algorithms=[ID_HASH_ALGO]) + checksums = hashutil.MultiHash.from_file( + f, hash_names=[ID_HASH_ALGO], length=length).digest() actual_obj_id = checksums[ID_HASH_ALGO] if obj_id != actual_obj_id: raise Error( diff --git a/swh/objstorage/tests/objstorage_testing.py b/swh/objstorage/tests/objstorage_testing.py --- a/swh/objstorage/tests/objstorage_testing.py +++ b/swh/objstorage/tests/objstorage_testing.py @@ -1,12 +1,12 @@ -# Copyright (C) 2015-2017 The Software Heritage developers +# Copyright (C) 2015-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import time -from swh.model import hashutil from swh.objstorage import exc +from swh.objstorage.objstorage import compute_hash class ObjStorageTestFixture(): @@ -15,7 +15,7 @@ super().setUp() def hash_content(self, content): - obj_id = hashutil.hash_data(content)['sha1'] + obj_id = compute_hash(content) return content, obj_id def assertContentMatch(self, obj_id, expected_content): # noqa diff --git a/swh/objstorage/tests/test_multiplexer_filter.py b/swh/objstorage/tests/test_multiplexer_filter.py --- a/swh/objstorage/tests/test_multiplexer_filter.py +++ b/swh/objstorage/tests/test_multiplexer_filter.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2017 The Software Heritage developers +# Copyright (C) 2015-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -13,6 +13,7 @@ from swh.objstorage import get_objstorage from swh.objstorage.exc import Error, ObjNotFoundError from swh.objstorage.multiplexer.filter import id_prefix, id_regex, read_only +from swh.objstorage.objstorage import compute_hash def get_random_content(): @@ -29,7 +30,7 @@ 'args': {'root': self.tmpdir, 'slicing': '0:5'}} base_storage = get_objstorage(**pstorage) - base_storage.id = lambda cont: hashutil.hash_data(cont)['sha1'] + base_storage.id = compute_hash self.storage = get_objstorage('filtered', {'storage_conf': pstorage, 'filters_conf': [read_only()]}) @@ -110,7 +111,7 @@ self.base_storage = storage self.storage = self.filter_storage(self.sconf) # Set the id calculators - storage.id = lambda cont: hashutil.hash_data(cont)['sha1'] + storage.id = compute_hash # Present content with valid id self.present_valid_content = self.ensure_valid(b'yroqdtotji') @@ -295,13 +296,13 @@ super().setUp() def ensure_valid(self, content): - obj_id = hashutil.hash_data(content)['sha1'] + obj_id = compute_hash(content) hex_obj_id = hashutil.hash_to_hex(obj_id) self.assertTrue(hex_obj_id.startswith(self.prefix)) return content def ensure_invalid(self, content): - obj_id = hashutil.hash_data(content)['sha1'] + obj_id = compute_hash(content) hex_obj_id = hashutil.hash_to_hex(obj_id) self.assertFalse(hex_obj_id.startswith(self.prefix)) return content