Skip to contentSkip to conversation

support/download: add blake2b-256 hash support

PATCH Superseded James Hilliard 2 participants 5 messages Jun 16 – Jun 17

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH 2/3 package/pkg-python: derive PyPI site from blake2b-256 hash

In reply to James Hilliard

Python packages downloaded from PyPI currently store the complete files.pythonhosted.org package path in their .mk file. That path is hash-based, so it changes for every version bump and adds churn to updates and backports.

When a Python package does not define _SITE, look for a blake2b-256 entry matching its source archive in the package .hash file and derive the files.pythonhosted.org directory from that digest before the generic package infrastructure validates _SITE.

This keeps the per-version digest in the .hash file and avoids carrying the same hash information as part of the package site URL. Update scanpypi to omit the generated _SITE assignment so newly generated PyPI packages can use the resolver.

---
 docs/manual/adding-packages-python.adoc |  5 +++++
 package/pkg-python.mk                   | 10 ++++++++++
 utils/scanpypi                          |  9 ---------
 3 files changed, 15 insertions(+), 9 deletions(-)
docs/manual/adding-packages-python.adoc +5 -0
index d6bae299a0..a66af1e7e2 100644
@@ -82,6 +82,11 @@ All the package metadata information variables that exist in the
 xref:generic-package-reference[generic package infrastructure] also
 exist in the Python infrastructure.
 
+For Python packages downloaded from PyPI, the +PYTHON_FOO_SITE+ variable
+can be omitted when the package +.hash+ file contains a +blake2b-256+
+hash for the source archive. Buildroot derives the hash-based
+files.pythonhosted.org URL from that hash.
+
 Note that:
 
  * It is not necessary to add +python+ or +host-python+ in the
package/pkg-python.mk +10 -0
index 32ace4aac1..0ba93d8fae 100644
@@ -454,6 +454,16 @@ endif
 
 endif # host / target
 
+ifndef $(2)_SITE
+ ifndef $(3)_SITE
+  $(2)_SITE = $$(shell \
+	awk -v filename="$$(notdir $$($(2)_SOURCE))" \
+		'$$$$1 == "blake2b-256" && $$$$3 == filename { h = $$$$2; printf "https://files.pythonhosted.org/packages/%s/%s/%s", substr(h, 1, 2), substr(h, 3, 2), substr(h, 5); exit }' \
+		$$(wildcard $$($(2)_HASH_FILES)) \
+		/dev/null)
+ endif
+endif
+
 # Call the generic package infrastructure to generate the necessary
 # make targets
 $(call inner-generic-package,$(1),$(2),$(3),$(4))
utils/scanpypi +0 -9
index de658328fe..63b8f8f4d0 100755
@@ -495,15 +495,6 @@ class BuildrootPackage():
                 filename=targz)
             lines.append(targz_line)
 
-        if self.filename not in self.url:
-            # Sometimes the filename is in the url, sometimes it's not
-            site_url = self.url
-        else:
-            site_url = self.url[:self.url.find(self.filename)]
-        site_line = '{name}_SITE = {url}'.format(name=self.mk_name,
-                                                 url=site_url)
-        site_line = site_line.rstrip('/') + '\n'
-        lines.append(site_line)
         return lines
 
     def __create_mk_setup(self):
Signature · 6 lines
2.53.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#0 lore raw

PATCH 1/3 support/download: add blake2b-256 hash support

PyPI exposes a blake2b_256 digest for distribution files, and that same digest is used in the hash-based files.pythonhosted.org download paths. Add support for storing those hashes in Buildroot .hash files.

Teach the download hash checker to validate blake2b-256 entries using b2sum -l 256, and update check-package so the new hash type and length are accepted. Also add the CycloneDX hash spelling so generated SBOMs can include blake2b-256 hashes from .hash files.

Update scanpypi to write the PyPI-provided blake2b_256 digest alongside the existing md5 and sha256 entries.

---
 docs/manual/adding-packages-directory.adoc             | 3 ++-
 support/download/check-hash                            | 7 ++++++-
 support/testing/tests/utils/test_generate_cyclonedx.py | 9 +++++++++
 utils/checkpackagelib/lib_hash.py                      | 2 +-
 utils/checkpackagelib/test_lib_hash.py                 | 4 ++++
 utils/generate-cyclonedx                               | 1 +
 utils/scanpypi                                         | 7 ++++++-
 7 files changed, 29 insertions(+), 4 deletions(-)
docs/manual/adding-packages-directory.adoc +2 -1
index 15de559deb..27f0fcd9a9 100644
@@ -464,7 +464,7 @@ The format of this file is one line for each file for which to check the
 hash, each line with the following three fields separated by two spaces:
 
 * the type of hash, one of:
-** +md5+, +sha1+, +sha224+, +sha256+, +sha384+, +sha512+
+** +md5+, +sha1+, +sha224+, +sha256+, +sha384+, +sha512+, +blake2b-256+
 * the hash of the file:
 ** for +md5+, 32 hexadecimal characters
 ** for +sha1+, 40 hexadecimal characters
@@ -472,6 +472,7 @@ hash, each line with the following three fields separated by two spaces:
 ** for +sha256+, 64 hexadecimal characters
 ** for +sha384+, 96 hexadecimal characters
 ** for +sha512+, 128 hexadecimal characters
+** for +blake2b-256+, 64 hexadecimal characters
 * the name of the file:
 ** for a source archive: the basename of the file, without any directory
    component,
support/download/check-hash +6 -1
index d18ec8b134..74079dac1a 100755
@@ -49,6 +49,7 @@ check_one_hash() {
     case "${_h}" in
         md5|sha1)                       ;;
         sha224|sha256|sha384|sha512)    ;;
+        blake2b-256)                    ;;
         *) # Unknown hash, exit with error
             printf "ERROR: unknown hash '%s' for '%s'\n"  \
                    "${_h}" "${base}" >&2
@@ -57,7 +58,11 @@ check_one_hash() {
     esac
 
     # Do the hashes match?
-    _hash="$( "${_h}sum" "${_file}" |cut -d ' ' -f 1 )"
+    if [ "${_h}" = "blake2b-256" ]; then
+        _hash="$( b2sum -l 256 "${_file}" |cut -d ' ' -f 1 )"
+    else
+        _hash="$( "${_h}sum" "${_file}" |cut -d ' ' -f 1 )"
+    fi
     if [ "${_hash}" = "${_known}" ]; then
         printf "%s: OK (%s: %s)\n" "${base}" "${_h}" "${_hash}"
         return 0
support/testing/tests/utils/test_generate_cyclonedx.py +9 -0
index e6640fbd0d..bb80a5ff23 100644
@@ -186,6 +186,7 @@ class TestGenerateCycloneDX(unittest.TestCase):
                 "# source archive checksums\n"
                 "sha256 1111111111111111111111111111111111111111111111111111111111111111 foo-1.2.tar.gz\n"
                 "sha1 2222222222222222222222222222222222222222 foo-1.2.tar.gz\n"
+                "blake2b-256 3333333333333333333333333333333333333333333333333333333333333333 foo-1.2.tar.gz\n"
                 "sha256 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa LICENSE\n"
             )
 
@@ -220,6 +221,10 @@ class TestGenerateCycloneDX(unittest.TestCase):
                             "alg": "SHA-1",
                             "content": "2222222222222222222222222222222222222222",
                         },
+                        {
+                            "alg": "BLAKE2b-256",
+                            "content": "3333333333333333333333333333333333333333333333333333333333333333",
+                        },
                     ]
                 },
                 {
@@ -234,6 +239,10 @@ class TestGenerateCycloneDX(unittest.TestCase):
                             "alg": "SHA-1",
                             "content": "2222222222222222222222222222222222222222",
                         },
+                        {
+                            "alg": "BLAKE2b-256",
+                            "content": "3333333333333333333333333333333333333333333333333333333333333333",
+                        },
                     ],
                 }
             ],
utils/checkpackagelib/lib_hash.py +1 -1
index 5968c809bf..3eafe7d9e0 100644
@@ -31,7 +31,7 @@ class HashNumberOfFields(_CheckFunction):
 
 class HashType(_CheckFunction):
     len_of_hash = {"md5": 32, "sha1": 40, "sha224": 56, "sha256": 64,
-                   "sha384": 96, "sha512": 128}
+                   "sha384": 96, "sha512": 128, "blake2b-256": 64}
 
     def check_line(self, lineno, text):
         if _empty_line_or_comment(text):
utils/checkpackagelib/test_lib_hash.py +4 -0
index fdc6338189..cbccad647d 100644
@@ -124,6 +124,10 @@ HashType = [
      'sha512 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678'
      '9012345678\n',
      []),
+    ('blake2b-256',
+     'any',
+     'blake2b-256 1234567890123456789012345678901234567890123456789012345678901234\n',
+     []),
     ]
 
 
utils/generate-cyclonedx +1 -0
index bade018cd4..2f5981934f 100755
@@ -298,6 +298,7 @@ def cyclonedx_source_hashes(comp, source):
         "sha256": "SHA-256",
         "sha512": "SHA-512",
         "md5": "MD5",
+        "blake2b-256": "BLAKE2b-256",
     }
 
     hashes = []
utils/scanpypi +6 -1
index 61879e39d4..de658328fe 100755
@@ -672,7 +672,7 @@ class BuildrootPackage():
         print('Creating {filename}...'.format(filename=path_to_hash))
         lines = []
         if self.used_url['digests']['md5'] and self.used_url['digests']['sha256']:
-            hash_header = '# md5, sha256 from {url}\n'.format(
+            hash_header = '# md5, sha256, blake2b-256 from {url}\n'.format(
                 url=self.metadata_url)
             lines.append(hash_header)
             hash_line = '{method}  {digest}  {filename}\n'.format(
@@ -685,6 +685,11 @@ class BuildrootPackage():
                 digest=self.used_url['digests']['sha256'],
                 filename=self.filename)
             lines.append(hash_line)
+            hash_line = '{method}  {digest}  {filename}\n'.format(
+                method='blake2b-256',
+                digest=self.used_url['digests']['blake2b_256'],
+                filename=self.filename)
+            lines.append(hash_line)
 
         if self.license_files:
             lines.append('# Locally computed sha256 checksums\n')
Signature · 6 lines
2.53.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#4 lore raw

PATCH 3/3 package/python-async-timeout: use hash-based PyPI site

Add the blake2b-256 digest from the PyPI metadata and drop the explicit PYTHON_ASYNC_TIMEOUT_SITE assignment.

The Python package infrastructure can now derive the hash-based files.pythonhosted.org URL from the package .hash file, so the .mk file no longer needs to duplicate that hash-derived path.

---
 package/python-async-timeout/python-async-timeout.hash | 3 ++-
 package/python-async-timeout/python-async-timeout.mk   | 1 -
 2 files changed, 2 insertions(+), 2 deletions(-)
package/python-async-timeout/python-async-timeout.hash +2 -1
index 451c07de5d..c812b9c6c3 100644
@@ -1,5 +1,6 @@
-# md5, sha256 from https://pypi.org/pypi/async-timeout/json
+# md5, sha256, blake2b-256 from https://pypi.org/pypi/async-timeout/json
 md5  566a39011e87cb8044ee75369e69b327  async_timeout-5.0.1.tar.gz
 sha256  d9321a7a3d5a6a5e187e824d2fa0793ce379a202935782d555d6e9d2735677d3  async_timeout-5.0.1.tar.gz
+blake2b-256  a5ae136395dfbfe00dfc94da3f3e136d0b13f394cba8f4841120e34226265780  async_timeout-5.0.1.tar.gz
 # Locally computed sha256 checksums
 sha256  e18d7bb8f513e2c46bb585c94b585bd30720dd3ccb21ddb0786f72d16658f92c  LICENSE
package/python-async-timeout/python-async-timeout.mk +0 -1
index 2835368003..017ea548b5 100644
@@ -6,7 +6,6 @@
 
 PYTHON_ASYNC_TIMEOUT_VERSION = 5.0.1
 PYTHON_ASYNC_TIMEOUT_SOURCE = async_timeout-$(PYTHON_ASYNC_TIMEOUT_VERSION).tar.gz
-PYTHON_ASYNC_TIMEOUT_SITE = https://files.pythonhosted.org/packages/a5/ae/136395dfbfe00dfc94da3f3e136d0b13f394cba8f4841120e34226265780
 PYTHON_ASYNC_TIMEOUT_LICENSE = Apache-2.0
 PYTHON_ASYNC_TIMEOUT_LICENSE_FILES = LICENSE
 PYTHON_ASYNC_TIMEOUT_SETUP_TYPE = setuptools
Signature · 6 lines
2.53.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#2 lore raw

Re: [Buildroot] [PATCH 2/3] package/pkg-python: derive PyPI site from blake2b-256 hash

In reply to James Hilliard

Hi James,
thanks for this series. A few comments below.

Quoted 9 lines · James Hilliard

Python packages downloaded from PyPI currently store the complete files.pythonhosted.org package path in their .mk file. That path is hash-based, so it changes for every version bump and adds churn to updates and backports.

When a Python package does not define _SITE, look for a blake2b-256 entry matching its source archive in the package .hash file and derive the files.pythonhosted.org directory from that digest before the generic package infrastructure validates _SITE.

What do you think about going one step further and also calculating the SOURCE variable automatically if not defined?

The luarocks infrastructure has FOO_NAME_UPSTREAM for this, which is used to automatically calculate the FOO_SOURCE variable, for pypi it would be $(FOO_NAME_UPSTREAM)-$(FOO_VERSION).tar.gz if not overwritten, I think?)

And then FOO_NAME_UPSTREAM can also be set to package name minus the python prefix, unless set explicitly? We only have a handful of packages that diverge from this pattern, mainly if they are called python-something upstream or if we packages them as a generic tool instead of a python package (i.e. scons). There's also (I think) two packages which got renamed upstream and IMO should just be renamed in buildroot as well at some point. But anyway, this will cover like 98& of our python packages, so I think it's worth having and documenting this as a default?

Quoted 34 lines

This keeps the per-version digest in the .hash file and avoids carrying the same hash information as part of the package site URL. Update scanpypi to omit the generated _SITE assignment so newly generated PyPI packages can use the resolver.

---
  docs/manual/adding-packages-python.adoc |  5 +++++
  package/pkg-python.mk                   | 10 ++++++++++
  utils/scanpypi                          |  9 ---------
  3 files changed, 15 insertions(+), 9 deletions(-)
docs/manual/adding-packages-python.adoc +5 -0
index d6bae299a0..a66af1e7e2 100644
@@ -82,6 +82,11 @@ All the package metadata information variables that exist in the
  xref:generic-package-reference[generic package infrastructure] also
  exist in the Python infrastructure.
 
+For Python packages downloaded from PyPI, the +PYTHON_FOO_SITE+ variable
+can be omitted when the package +.hash+ file contains a +blake2b-256+
+hash for the source archive. Buildroot derives the hash-based
+files.pythonhosted.org URL from that hash.
+
  Note that:
 
   * It is not necessary to add +python+ or +host-python+ in the
package/pkg-python.mk +0 -0
index 32ace4aac1..0ba93d8fae 100644

package/pkg-python.mk endif · line 458

Quoted 5 lines
package/pkg-python.mk +2 -0
@@ -454,6 +454,16 @@ endif
 
  endif # host / target
 
+ifndef $(2)_SITE
+ ifndef $(3)_SITE

The usual pattern here is doing:

ifndef $(2)_SITE
ifdef $(3)_SITE
$(2)_SITE = $$($(3)_SITE)
else
[...]

Is there any reason to not follow this here?

package/pkg-python.mk endif · line 461

Quoted 3 lines
package/pkg-python.mk +3 -0
+  $(2)_SITE = $$(shell \
+	awk -v filename="$$(notdir $$($(2)_SOURCE))" \
+		'$$$$1 == "blake2b-256" && $$$$3 == filename { h = $$$$2; printf "https://files.pythonhosted.org/packages/%s/%s/%s", substr(h, 1, 2), substr(h, 3, 2), substr(h, 5); exit }' \

This is easier than what I came up with, nice. :)

package/pkg-python.mk endif · line 463

Quoted 2 lines
package/pkg-python.mk +2 -0
+		$$(wildcard $$($(2)_HASH_FILES)) \
+		/dev/null)

Not a huge fan of passing /dev/null to awk here, can we add a condition instead that guards against there being no hash files and then errors out?

Relatedly, we also need to guard against no blake2b hash being in the hash file and thus no SITE being extracted (when none is defined manually. I think this should raise an explicit error as well.

Quoted 1 line

[...]

Marcus
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#3 lore raw

Re: [Buildroot] [PATCH 2/3] package/pkg-python: derive PyPI site from blake2b-256 hash

Quoted 23 lines · Marcus Hoffmann

Hi James,
thanks for this series. A few comments below.

Quoted 9 lines · James Hilliard

Python packages downloaded from PyPI currently store the complete files.pythonhosted.org package path in their .mk file. That path is hash-based, so it changes for every version bump and adds churn to updates and backports.

When a Python package does not define _SITE, look for a blake2b-256 entry matching its source archive in the package .hash file and derive the files.pythonhosted.org directory from that digest before the generic package infrastructure validates _SITE.

What do you think about going one step further and also calculating the SOURCE variable automatically if not defined?

The luarocks infrastructure has FOO_NAME_UPSTREAM for this, which is used to automatically calculate the FOO_SOURCE variable, for pypi it would be $(FOO_NAME_UPSTREAM)-$(FOO_VERSION).tar.gz if not overwritten, I think?)

Quoted 9 lines

And then FOO_NAME_UPSTREAM can also be set to package name minus the python prefix, unless set explicitly? We only have a handful of packages that diverge from this pattern, mainly if they are called python-something upstream or if we packages them as a generic tool instead of a python package (i.e. scons). There's also (I think) two packages which got renamed upstream and IMO should just be renamed in buildroot as well at some point. But anyway, this will cover like 98& of our python packages, so I think it's worth having and documenting this as a default?

Probably makes sense, but I think best handle that separately since we will have to deal with packages that diverge from the normal pattern.

Quoted 60 lines
Quoted 35 lines

This keeps the per-version digest in the .hash file and avoids carrying the same hash information as part of the package site URL. Update scanpypi to omit the generated _SITE assignment so newly generated PyPI packages can use the resolver.

---
  docs/manual/adding-packages-python.adoc |  5 +++++
  package/pkg-python.mk                   | 10 ++++++++++
  utils/scanpypi                          |  9 ---------
  3 files changed, 15 insertions(+), 9 deletions(-)
docs/manual/adding-packages-python.adoc +5 -0
index d6bae299a0..a66af1e7e2 100644
@@ -82,6 +82,11 @@ All the package metadata information variables that exist in the
  xref:generic-package-reference[generic package infrastructure] also
  exist in the Python infrastructure.
 
+For Python packages downloaded from PyPI, the +PYTHON_FOO_SITE+ variable
+can be omitted when the package +.hash+ file contains a +blake2b-256+
+hash for the source archive. Buildroot derives the hash-based
+files.pythonhosted.org URL from that hash.
+
  Note that:
 
   * It is not necessary to add +python+ or +host-python+ in the
package/pkg-python.mk +0 -0
index 32ace4aac1..0ba93d8fae 100644

package/pkg-python.mk endif · line 458

Quoted 5 lines
package/pkg-python.mk +2 -0
@@ -454,6 +454,16 @@ endif
 
  endif # host / target
 
+ifndef $(2)_SITE
+ ifndef $(3)_SITE

The usual pattern here is doing:

ifndef $(2)_SITE
ifdef $(3)_SITE

    $(2)_SITE = $$($(3)_SITE)

else
[...]

Is there any reason to not follow this here?

package/pkg-python.mk endif · line 461

Quoted 3 lines
package/pkg-python.mk +3 -0
+  $(2)_SITE = $$(shell \
+     awk -v filename="$$(notdir $$($(2)_SOURCE))" \
+             '$$$$1 == "blake2b-256" && $$$$3 == filename { h = $$$$2; printf "https://files.pythonhosted.org/packages/%s/%s/%s", substr(h, 1, 2), substr(h, 3, 2), substr(h, 5); exit }' \

This is easier than what I came up with, nice. :)

package/pkg-python.mk endif · line 463

Quoted 2 lines
package/pkg-python.mk +2 -0
+             $$(wildcard $$($(2)_HASH_FILES)) \
+             /dev/null)
Quoted 6 lines

Not a huge fan of passing /dev/null to awk here, can we add a condition instead that guards against there being no hash files and then errors out?

Relatedly, we also need to guard against no blake2b hash being in the hash file and thus no SITE being extracted (when none is defined manually. I think this should raise an explicit error as well.

Applied remaining suggestions in v2:
https://lore.kernel.org/buildroot/[email protected]/T/

Quoted 4 lines
Quoted 2 lines

[...]

Marcus

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot