diff --git a/autobuild/autobuild_tool_edit.py b/autobuild/autobuild_tool_edit.py index 5e392ff..4def7f9 100644 --- a/autobuild/autobuild_tool_edit.py +++ b/autobuild/autobuild_tool_edit.py @@ -247,7 +247,7 @@ class Archive(InteractiveCommand): ARGUMENTS = ['format', 'hash_algorithm', 'platform'] ARG_DICT = {'format': {'help': 'Archive format (e.g zip or tbz2)'}, - 'hash_algorithm': {'help': 'The algorithm for computing the archive hash (e.g. md5)'}, + 'hash_algorithm': {'help': 'The algorithm for computing the archive hash (e.g. md5, blake2b)'}, 'platform': {'help': 'The name of the platform archive to be configured'} } diff --git a/autobuild/autobuild_tool_package.py b/autobuild/autobuild_tool_package.py index 72dc535..301896d 100644 --- a/autobuild/autobuild_tool_package.py +++ b/autobuild/autobuild_tool_package.py @@ -366,18 +366,15 @@ def _add_file_to_zip_archive(zip_file, unnormalized_file, archive_filename, adde def _print_hash(filename: str, results: dict): - fp = open(filename, 'rb') - m = hashlib.md5() - while True: - d = fp.read(65536) - if not d: - break - m.update(d) + md5 = common.compute_md5(filename) + blake2b = common.compute_blake2b(filename) + # printing unconditionally on stdout for backward compatibility # the Linden Lab build scripts no longer rely on this # (they use the --results-file option instead) - print("md5 %s" % m.hexdigest()) - results['autobuild_package_md5'] = m.hexdigest() + print("md5 %s" % md5) + results['autobuild_package_md5'] = md5 + results['autobuild_package_blake2b'] = blake2b # Not using logging, since this output should be produced unconditionally on stdout # Downstream build tools utilize this output diff --git a/autobuild/common.py b/autobuild/common.py index ec78749..9271947 100644 --- a/autobuild/common.py +++ b/autobuild/common.py @@ -338,6 +338,25 @@ def compute_md5(path): return hasher.hexdigest() +def compute_blake2b(path): + """ + Returns the blake2b sum for the given file. + """ + import hashlib + + try: + stream = open(path, 'rb') + except IOError as err: + raise AutobuildError("Can't compute blake2b for %s: %s" % (path, err)) + + try: + hasher = hashlib.blake2b(stream.read()) + finally: + stream.close() + + return hasher.hexdigest() + + def split_tarname(pathname): """ Given a tarfile pathname of the form: diff --git a/autobuild/hash_algorithms.py b/autobuild/hash_algorithms.py index 298df04..589b881 100644 --- a/autobuild/hash_algorithms.py +++ b/autobuild/hash_algorithms.py @@ -64,3 +64,8 @@ def verify_hash(hash_algorithm, pathname, hash): @hash_algorithm("md5") def _verify_md5(pathname, hash): return common.compute_md5(pathname) == hash + +@hash_algorithm("blake2b") +def _verify_blake2b(pathname, hash): + return common.compute_blake2b(pathname) == hash + diff --git a/tests/test_package.py b/tests/test_package.py index fa88449..8df5306 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -103,8 +103,9 @@ def test_results(self): autobuild_package_platform="%s" autobuild_package_filename="%s" autobuild_package_md5="%s" +autobuild_package_blake2b="%s" $''' % ('test1', re.escape(os.path.join(self.data_dir, "package-test", "autobuild-package.xml")), - "common", re.escape(self.tar_name), "[0-9a-f]{32}") + "common", re.escape(self.tar_name), "[0-9a-f]{32}", "[0-9a-f]{128}") expected=re.compile(expected_results_regex, flags=re.MULTILINE) assert os.path.exists(results_output), "results file not found: %s" % results_output actual_results = open(results_output,'r').read() @@ -125,6 +126,7 @@ def test_results_json(self): self.assertEqual(results["autobuild_package_platform"], "common") self.assertEqual(results["autobuild_package_filename"], self.tar_name) self.assertEqual(len(results["autobuild_package_md5"]), 32) + self.assertEqual(len(results["autobuild_package_blake2b"]), 128) def test_package_other_version(self): # read the existing metadata file and update stored package version