diff mbox series

[RFC,18/21] fetch: add gosum fetcher

Message ID 20241220112613.22647-19-stefan.herbrechtsmeier-oss@weidmueller.com
State New
Headers show
Series Concept for tightly coupled package manager (Node.js, Go, Rust) | expand

Commit Message

Stefan Herbrechtsmeier Dec. 20, 2024, 11:26 a.m. UTC
From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

Add gosum fetcher to fetch dependencies via a go.sum file. The fetcher
uses the dependency mixin and supports different types:

gosum
    The fetcher uses a local go.sum file to fetch dependencies.

    SRC_URI = "gosum://go.sum"

gosum+https
    The fetcher downloads a go.sum file or archive with a go.sum file in
    the root folder and uses the go.sum  to fetch dependencies.

    SRC_URI = "gosum+http://example.com/go.sum"
    SRC_URI = "gosum+http://example.com/${BP}.tar.gz;striplevel=1;subdir=${BP}"

gosum+git
    The fetcher checkouts a git repository with a go.sum file to fetch
    dependencies.

    SRC_URI = "gosum+git://example.com/${BPN}.git;protocol=https"

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
---

 lib/bb/fetch2/__init__.py |  2 ++
 lib/bb/fetch2/gosum.py    | 51 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 lib/bb/fetch2/gosum.py
diff mbox series

Patch

diff --git a/lib/bb/fetch2/__init__.py b/lib/bb/fetch2/__init__.py
index 5dbc0598d..10b4cf24b 100644
--- a/lib/bb/fetch2/__init__.py
+++ b/lib/bb/fetch2/__init__.py
@@ -2116,6 +2116,7 @@  from . import az
 from . import crate
 from . import gcp
 from . import gomod
+from . import gosum
 
 methods.append(local.Local())
 methods.append(wget.Wget())
@@ -2140,3 +2141,4 @@  methods.append(gcp.GCP())
 methods.append(gomod.GoMod())
 methods.append(gomod.GoModGit())
 methods.extend(npmsw.methods)
+methods.extend(gosum.methods)
diff --git a/lib/bb/fetch2/gosum.py b/lib/bb/fetch2/gosum.py
new file mode 100644
index 000000000..001ef3304
--- /dev/null
+++ b/lib/bb/fetch2/gosum.py
@@ -0,0 +1,51 @@ 
+# Copyright (C) 2024-2025 Weidmueller Interface GmbH & Co. KG
+# Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
+#
+# SPDX-License-Identifier: MIT
+#
+"""
+BitBake 'Fetch' implementation for go.sum
+
+The gosum, gosum+https and gosum+git fetchers are used to download
+Go mod dependencies via a go.sum file.
+"""
+
+import os
+import bb
+import base64
+from bb.fetch2 import ParameterError
+from bb.fetch2 import URI
+from bb.fetch2.dependency import create_methods
+
+class GoSumMixin:
+    def resolve_dependencies(self, ud, localpath, d):
+        urls = []
+
+        def resolve_dependency(module_path, version, hash):
+            uri = URI(f"gomod://{module_path}")
+            if version.endswith("/go.mod"):
+                uri.params["version"] = version[:-7]
+                uri.params["mod"] = "1"
+            else:
+                uri.params["version"] = version
+            if hash.startswith("h1:"):
+                uri.params["h1sum"] = base64.b64decode(hash[3:]).hex()
+            else:
+                raise ParameterError(f"Invalid hash: {hash}", ud.url)
+            urls.append(str(uri))
+
+        if os.path.isdir(localpath):
+            localpath = os.path.join(localpath, "go.sum")
+        try:
+            with open(localpath, "r") as f:
+                for line in f:
+                    fields = line.strip().split()
+                    if len(fields) != 3:
+                        raise ParameterError(f"Invalid go.sum line: {line}", ud.url)
+                    resolve_dependency(*fields)
+        except Exception as e:
+            raise ParameterError(f"Invalid go.sum file: {str(e)}", ud.url)
+
+        return urls
+
+methods = create_methods("gosum", GoSumMixin)