Skip to content

Commit 7871563

Browse files
committed
Reuse existing code for hasher_from_chunks #3941 #4556
* Move test Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent eb13ccb commit 7871563

4 files changed

Lines changed: 23 additions & 21 deletions

File tree

src/commoncode/hash.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,24 @@ def checksum(location, name, base64=False):
229229
return checksum_from_chunks(chunks=chunks, total_length=total_length, name=name, base64=base64)
230230

231231

232+
def hasher_from_chunks(chunks, name, total_length=0):
233+
"""
234+
Return a hasher of ``name`` checksum algorithm of contains the contents of
235+
the iterator of byte strings ``chunks`` of length ``total_length``.
236+
"""
237+
hasher = get_hasher_instance_by_name(name=name, total_length=total_length)
238+
for chunk in chunks:
239+
hasher.update(chunk)
240+
return hasher
241+
242+
232243
def checksum_from_chunks(chunks, name, total_length=0, base64=False):
233244
"""
234245
Return a checksum from the content of the iterator of byte strings ``chunks`` with a
235246
``total_length`` combined length using the ``name`` checksum algorithm. The returned checksum is
236247
a string as a hexdigest or is base64-encoded is ``base64`` is True.
237248
"""
238-
hasher = get_hasher_instance_by_name(name=name, total_length=total_length)
239-
for chunk in chunks:
240-
hasher.update(chunk)
249+
hasher = hasher_from_chunks(chunks=chunks, name=name, total_length=total_length)
241250
if base64:
242251
return hasher.b64digest()
243252
return hasher.hexdigest()

src/scancode/results_cache.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from commoncode.fileutils import create_dir
1515
from commoncode.hash import binary_chunks
16+
from commoncode.hash import hasher_from_chunks
1617
from scancode_config import results_cache_dir
1718

1819

@@ -38,22 +39,13 @@
3839
After scanning, the cache is updated.
3940
"""
4041

41-
def hasher_from_chunks(chunks):
42-
"""
43-
Return a sha256 hasher loaded with `chunks`.
44-
"""
45-
hasher = hashlib.sha256()
46-
for chunk in chunks:
47-
hasher.update(chunk)
48-
return hasher
49-
5042

5143
def compute_results_cache_index(location, filename):
5244
"""
5345
Compute results_cache_index value for a Resource at `location`.
5446
"""
5547
chunks = binary_chunks(location=location)
56-
sha256_hasher = hasher_from_chunks(chunks=chunks)
48+
sha256_hasher = hasher_from_chunks(chunks=chunks, name='sha256')
5749
sha256_hasher.update(filename.encode('utf-8', 'surrogateescape'))
5850
return sha256_hasher.hexdigest()
5951

tests/commoncode/test_hash.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import os
1212

1313
from commoncode.hash import b64sha1
14+
from commoncode.hash import binary_chunks
1415
from commoncode.hash import checksum
1516
from commoncode.hash import checksum_from_chunks
1617
from commoncode.hash import get_hasher
18+
from commoncode.hash import hasher_from_chunks
1719
from commoncode.hash import md5
1820
from commoncode.hash import multi_checksums
1921
from commoncode.hash import sha1
@@ -200,3 +202,10 @@ def test_checksum_empty_file(self):
200202
test_file = self.get_test_loc("hash/empty")
201203
checksums = multi_checksums(location=test_file, checksum_names=("sha1",))
202204
assert checksums == {"sha1": None}
205+
206+
def test_hasher_from_chunks(self):
207+
test_file = self.get_test_loc("hash/dir1/a.png")
208+
chunks = binary_chunks(location=test_file)
209+
hasher = hasher_from_chunks(chunks=chunks, name='sha256')
210+
expected = "1b598db6fee8f1ec7bb919c0adf68956f3d20af8c9934a9cf2db52e1347efd35"
211+
assert hasher.hexdigest() == expected

tests/scancode/test_results_cache.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from os.path import dirname
1212
from os.path import join
1313

14-
from commoncode.hash import binary_chunks
1514
from commoncode.testcase import FileDrivenTesting
1615
from scancode import results_cache
1716

@@ -21,13 +20,6 @@ class TestResultsCache(FileDrivenTesting):
2120
test_results_cache = join(test_data_dir, 'results_cache/results')
2221
test_results_cache_index = "b0e3dd13b9b5980bb1ca7aab89e5490cb136952374489a755947ac004309b035"
2322

24-
def test_hasher_from_chunks(self):
25-
test_file_loc = self.get_test_loc('results_cache/package.json')
26-
chunks = binary_chunks(location=test_file_loc)
27-
hasher = results_cache.hasher_from_chunks(chunks=chunks)
28-
expected = "cb6f5a82e473620da4d1aecf82dd4d4fa9ada393a7679b28a42cac86f0a83c92"
29-
assert hasher.hexdigest() == expected
30-
3123
def test_compute_results_cache_index(self):
3224
test_file_loc = self.get_test_loc('results_cache/package.json')
3325
results_cache_index = results_cache.compute_results_cache_index(location=test_file_loc, filename='package.json')

0 commit comments

Comments
 (0)