Skip to contentSkip to conversation

upstream source tracing: base process

PATCH In review [email protected] 3 participants 8 messages Apr 21, 2023 – Apr 23, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 upstream source tracing: base process

From: Alberto Pianon <[email protected]>

License compliance, SBoM generation and CVE checking require to be able to trace each source file back to its corresponding upstream source. The current implementation of bb.fetch2 makes it difficult, especially when multiple sources are combined together.

This patch provides an interface to solve the issue by implementing a process that unpacks each SRC_URI element into a temporary directory, creates an entrypoint to collect relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata in a JSON file.

This patch contains required modifications to fetchers' code plus a TraceUnpackBase class that implements the above described process. Data collection logic should be separately implemented by subclassing TraceUnpackBase, implementing _collect_data() and _process_data() methods.

Splitting the above described solution in multiple patches aims at easing review and merge process, and also at decoupling the development of the data processing logic from the process that enables it.

---
 bin/bitbake-selftest        |   1 +
 lib/bb/fetch2/__init__.py   |  55 +++++++-
 lib/bb/fetch2/crate.py      |   2 +
 lib/bb/fetch2/gitsm.py      |  24 +++-
 lib/bb/fetch2/hg.py         |   1 +
 lib/bb/fetch2/npm.py        |   1 +
 lib/bb/fetch2/npmsw.py      |  26 +++-
 lib/bb/fetch2/trace_base.py | 254 ++++++++++++++++++++++++++++++++++++
 lib/bb/tests/trace_base.py  | 227 ++++++++++++++++++++++++++++++++
 9 files changed, 582 insertions(+), 9 deletions(-)
 create mode 100644 lib/bb/fetch2/trace_base.py
 create mode 100644 lib/bb/tests/trace_base.py
bin/bitbake-selftest +1 -0
index f25f23b1..6d60a5d2 100755
@@ -31,6 +31,7 @@ tests = ["bb.tests.codeparser",
          "bb.tests.runqueue",
          "bb.tests.siggen",
          "bb.tests.utils",
+         "bb.tests.trace_base",
          "bb.tests.compression",
          "hashserv.tests",
          "layerindexlib.tests.layerindexobj",
lib/bb/fetch2/__init__.py +52 -3

102 lines not on this page · raw message

lib/bb/fetch2/crate.py +2 -0
index a7021e5b..93666de0 100644
@@ -101,8 +101,10 @@ class Crate(Wget):
         pn = d.getVar('BPN')
         if pn == ud.parm.get('name'):
             cmd = "tar -xz --no-same-owner -f %s" % thefile
+            ud.destdir = rootdir
         else:
             cargo_bitbake = self._cargo_bitbake_path(rootdir)
+            ud.destdir = cargo_bitbake
 
             cmd = "tar -xz --no-same-owner -f %s -C %s" % (thefile, cargo_bitbake)
 
lib/bb/fetch2/gitsm.py +23 -1
index f8e239bc..c161d1f3 100644
@@ -34,6 +34,11 @@ class GitSM(Git):
         """
         return ud.type in ['gitsm']
 
+    def urldata_init(self, ud, d):
+        super(GitSM, self).urldata_init(ud, d)
+        ud.module_data = []
+        ud.checkout_destdir = None
+
     def process_submodules(self, ud, workdir, function, d):
         """
         Iterate over all of the submodules in this repository and execute
@@ -138,6 +143,15 @@ class GitSM(Git):
 
             function(ud, url, module, paths[module], workdir, ld)
 
+            if function.__name__ == "unpack_submodules":
+                destdir = os.path.join(ud.checkout_destdir, paths[module])
+                ud.module_data.append({
+                    "url": url,
+                    "destdir": destdir.rstrip("/"),
+                    "parent_destdir": ud.checkout_destdir.rstrip("/"),
+                    "revision": subrevision[module]
+                })
+
         return submodules != []
 
     def need_update(self, ud, d):
@@ -209,9 +223,13 @@ class GitSM(Git):
             else:
                 repo_conf = os.path.join(ud.destdir, '.git')
 
+            checkout_destdir = os.path.join(ud.checkout_destdir, modpath)
+
             try:
                 newfetch = Fetch([url], d, cache=False)
-                newfetch.unpack(root=os.path.dirname(os.path.join(repo_conf, 'modules', module)))
+                newfetch.unpack(root=os.path.dirname(os.path.join(repo_conf, 'modules', module)), is_module=True, checkout_destdir=checkout_destdir)
+                # add nested submodules' data
+                ud.module_data += newfetch.ud[url].module_data
             except Exception as e:
                 logger.error('gitsm: submodule unpack failed: %s %s' % (type(e).__name__, str(e)))
                 raise
@@ -233,6 +251,10 @@ class GitSM(Git):
 
         Git.unpack(self, ud, destdir, d)
 
+        if not ud.checkout_destdir:
+            # for main git repo, checkout destdir corresponds with unpack destdir
+            ud.checkout_destdir = ud.destdir
+
         ret = self.process_submodules(ud, ud.destdir, unpack_submodules, d)
 
         if not ud.bareclone and ret:
lib/bb/fetch2/hg.py +1 -0
index 063e1300..0fd69db7 100644
@@ -242,6 +242,7 @@ class Hg(FetchMethod):
         revflag = "-r %s" % ud.revision
         subdir = ud.parm.get("destsuffix", ud.module)
         codir = "%s/%s" % (destdir, subdir)
+        ud.destdir = codir
 
         scmdata = ud.parm.get("scmdata", "")
         if scmdata != "nokeep":
lib/bb/fetch2/npm.py +1 -0
index 8a179a33..34e1f276 100644
@@ -294,6 +294,7 @@ class Npm(FetchMethod):
         destsuffix = ud.parm.get("destsuffix", "npm")
         destdir = os.path.join(rootdir, destsuffix)
         npm_unpack(ud.localpath, destdir, d)
+        ud.destdir = destdir
 
     def clean(self, ud, d):
         """Clean any existing full or partial download"""
lib/bb/fetch2/npmsw.py +21 -5
index 36fcbfba..79c369dc 100644
@@ -66,6 +66,9 @@ class NpmShrinkWrap(FetchMethod):
     def urldata_init(self, ud, d):
         """Init npmsw specific variables within url data"""
 
+        # initialize module_data (for module source tracing)
+        ud.module_data = []
+
         # Get the 'shrinkwrap' parameter
         ud.shrinkwrap_file = re.sub(r"^npmsw://", "", ud.url.split(";")[0])
 
@@ -250,20 +253,33 @@ class NpmShrinkWrap(FetchMethod):
 
     def unpack(self, ud, rootdir, d):
         """Unpack the downloaded dependencies"""
-        destdir = d.getVar("S")
-        destsuffix = ud.parm.get("destsuffix")
-        if destsuffix:
-            destdir = os.path.join(rootdir, destsuffix)
+        # rootdir param is a temporary dir. The real rootdir, where sources are
+        # moved after being traced, is stored in ud.rootdir.
+        destsuffix = ud.parm.get("destsuffix") or os.path.relpath(d.getVar("S"), ud.rootdir)
+        destdir = os.path.join(rootdir, destsuffix)
+        ud.destdir = destdir
 
         bb.utils.mkdirhier(destdir)
         bb.utils.copyfile(ud.shrinkwrap_file,
                           os.path.join(destdir, "npm-shrinkwrap.json"))
 
+        for dep in ud.deps:
+            dep_destdir = os.path.join(destdir, dep["destsuffix"])
+            # to get parent destdir, we get rid of the last 2 path elements
+            # (node_modules/<module_name>)
+            dep_parent_destdir = "/".join(dep_destdir.split("/")[:-2])
+            ud.module_data.append({
+                "url": dep["url"] or dep["localpath"],
+                "destdir": dep_destdir.rstrip("/"),
+                "parent_destdir": dep_parent_destdir.rstrip("/"),
+                "revision": None
+            })
+
         auto = [dep["url"] for dep in ud.deps if not dep["localpath"]]
         manual = [dep for dep in ud.deps if dep["localpath"]]
 
         if auto:
-            ud.proxy.unpack(destdir, auto)
+            ud.proxy.unpack(destdir, auto, is_module=True)
 
         for dep in manual:
             depdestdir = os.path.join(destdir, dep["destsuffix"])
lib/bb/fetch2/trace_base.py new file +254 -0

255 lines not on this page · raw message

lib/bb/tests/trace_base.py new file +227 -0

227 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 2/3 upstream source tracing: metadata collection

From: Alberto Pianon <[email protected]>

This patch subclasses TraceUnpackBase by implementing _collect_data() and _process_data() methods, in order to implement upstream metadata collection logic for bb.fetch2.

The final output is a compressed json file, stored in rootdir/temp. Data format is described in the help text of the trace module.

---
 lib/bb/fetch2/__init__.py |   2 +-
 lib/bb/fetch2/trace.py    | 534 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 535 insertions(+), 1 deletion(-)
 create mode 100644 lib/bb/fetch2/trace.py
lib/bb/fetch2/__init__.py +1 -1
index c4915208..d241ae0f 100644
@@ -28,7 +28,7 @@ import bb.checksum
 import bb.process
 import bb.event
 
-from .trace_base import TraceUnpackBase as TraceUnpack
+from .trace import TraceUnpack
 
 __version__ = "2"
 _checksum_cache = bb.checksum.FileChecksumCache()
lib/bb/fetch2/trace.py new file +534 -0

534 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 3/3 upstream source tracing: tests

In reply to [email protected]

Hi,

as agreed in the last weekly meeting I submit this third patch by just sending the link to the commit, since the patch contains some big compressed JSON files that would be too large for an email message. From reading tests and test data, you may get what is the final goal/result of this series of patches.

http://cgit.openembedded.org/bitbake-contrib/commit/?h=alpianon/srctrace2&id=f1aaf9fe1233ad9340bddd129331d33aa6c951d4

Cheers,

Alberto

#3 lore raw
In reply to [email protected]

Hello Alberto,

Quoted 19 lines · On Fri, 21 Apr 2023 09:05:23 +0200 "Alberto Pianon" <[email protected]> wrote:

From: Alberto Pianon <[email protected]>

License compliance, SBoM generation and CVE checking require to be able to trace each source file back to its corresponding upstream source. The current implementation of bb.fetch2 makes it difficult, especially when multiple sources are combined together.

This patch provides an interface to solve the issue by implementing a process that unpacks each SRC_URI element into a temporary directory, creates an entrypoint to collect relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata in a JSON file.

This patch contains required modifications to fetchers' code plus a TraceUnpackBase class that implements the above described process. Data collection logic should be separately implemented by subclassing TraceUnpackBase, implementing _collect_data() and _process_data() methods.

Quoted 6 lines

Splitting the above described solution in multiple patches aims at easing review and merge process, and also at decoupling the development of the data processing logic from the process that enables it.

Thank you for having fixed the subject! Now I can apply your patches with a clean git commit.

Do you think the build failures I reported on the v1 series are fixed by this v2? If you think so, then I can test v2 on the autobuilders.

Also, I see this patch set is not bisectable as well as v1: you define class TraceUnpack in patch 2 and use it in patch 1.

Best regards,
Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Il 2023-04-21 09:28 Luca Ceresoli ha scritto:

Quoted 29 lines

Hello Alberto,

Quoted 24 lines · On Fri, 21 Apr 2023 09:05:23 +0200 "Alberto Pianon" <[email protected]> wrote:

From: Alberto Pianon <[email protected]>

License compliance, SBoM generation and CVE checking require to be able to trace each source file back to its corresponding upstream source. The current implementation of bb.fetch2 makes it difficult, especially when multiple sources are combined together.

This patch provides an interface to solve the issue by implementing a process that unpacks each SRC_URI element into a temporary directory, creates an entrypoint to collect relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata in a JSON file.

This patch contains required modifications to fetchers' code plus a TraceUnpackBase class that implements the above described process. Data collection logic should be separately implemented by subclassing TraceUnpackBase, implementing _collect_data() and _process_data() methods.

Splitting the above described solution in multiple patches aims at easing review and merge process, and also at decoupling the development of the data processing logic from the process that enables it.

Quoted 7 lines
Quoted 1 line

Thank you for having fixed the subject! Now I can apply your patches with a clean git commit.

Do you think the build failures I reported on the v1 series are fixed by this v2? If you think so, then I can test v2 on the autobuilders.

No but I'm working on it. Actually, I should have found the problem:
it's just that in some tests WORKDIR/temp is not created so I added a check to create it if it's not present. I'm testing it, but oe-selftest is taking forever in my build machine. So far, no error in the logs...

Quoted 4 lines

Also, I see this patch set is not bisectable as well as v1: you define class TraceUnpack in patch 2 and use it in patch 1.

do you mean that:

import TraceUnpackBase as TraceUnpack

in patch 1 is not good, and that I should use just TraceUnpackBase as a class name/alias in patch 1?

#5 lore raw
Quoted 63 lines

-----Original Message-----
From: [email protected] <bitbake- [email protected]> On Behalf Of Alberto Pianon
Sent: den 21 april 2023 14:53
To: Luca Ceresoli <[email protected]>
Cc: [email protected]
Subject: Re: [bitbake-devel] [PATCH v2 1/3] upstream source tracing: base process

Il 2023-04-21 09:28 Luca Ceresoli ha scritto:

Quoted 34 lines

Hello Alberto,

Quoted 29 lines · On Fri, 21 Apr 2023 09:05:23 +0200 "Alberto Pianon" <[email protected]> wrote:

From: Alberto Pianon <[email protected]>

License compliance, SBoM generation and CVE checking require to be able
to trace each source file back to its corresponding upstream source. The
current implementation of bb.fetch2 makes it difficult, especially when
multiple sources are combined together.

This patch provides an interface to solve the issue by implementing a process that unpacks each SRC_URI element into a temporary directory, creates an entrypoint to collect relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata
in a JSON file.

This patch contains required modifications to fetchers' code plus a TraceUnpackBase class that implements the above described process. Data
collection logic should be separately implemented by subclassing TraceUnpackBase, implementing _collect_data() and _process_data() methods.

Splitting the above described solution in multiple patches aims at easing review and merge process, and also at decoupling the development of the data processing logic from the process that enables it.

Quoted 7 lines
Quoted 1 line

Thank you for having fixed the subject! Now I can apply your patches with a clean git commit.

Do you think the build failures I reported on the v1 series are fixed by this v2? If you think so, then I can test v2 on the autobuilders.

No but I'm working on it. Actually, I should have found the problem:
it's just that in some tests WORKDIR/temp is not created so I added a check to create it if it's not present. I'm testing it, but oe-selftest is taking forever in my build machine. So far, no error in the logs...

Quoted 4 lines

Also, I see this patch set is not bisectable as well as v1: you define class TraceUnpack in patch 2 and use it in patch 1.

Quoted 6 lines

do you mean that:

import TraceUnpackBase as TraceUnpack

in patch 1 is not good, and that I should use just TraceUnpackBase as a class name/alias in patch 1?

My guess is that Luca missed the import ... as line. I believe you should be fine as you are. However, one thing I would point out is the commit subjects. For changes to the fetcher, the subject should typically start with "fetch2: " so that one can clearly see with, e.g., git log --oneline where you are making changes. If it was up to me, I would change the subjects for the three commits to:

fetch2: Add support for upstream source tracing
fetch2: Add metadata collection for upstream source tracing
fetch2: Add tests for upstream source tracing

//Peter

#6 lore raw

Hello Peter, Alberto,

Quoted 7 lines · On Fri, 21 Apr 2023 13:50:25 +0000 "Peter Kjellerstedt"…
Quoted 7 lines
Quoted 4 lines

Also, I see this patch set is not bisectable as well as v1: you define class TraceUnpack in patch 2 and use it in patch 1.

do you mean that:

Quoted 7 lines
Quoted 4 lines

import TraceUnpackBase as TraceUnpack

in patch 1 is not good, and that I should use just TraceUnpackBase as a class name/alias in patch 1?

My guess is that Luca missed the import ... as line. I believe you should be fine as you are.

You're right Peter, my review was too quick. Indeed Alberto's patches are OK in that respect. Sorry for the noise.

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#7 lore raw

I tested all patches, they should be OK now (hopefully). I also changed subjects following Peter's suggestion. I just sent them to the ML
Cheers,
Alberto

Il 2023-04-21 18:15 Luca Ceresoli via lists.openembedded.org ha scritto:

Quoted 33 lines

Hello Peter, Alberto,

Quoted 7 lines · On Fri, 21 Apr 2023 13:50:25 +0000 "Peter Kjellerstedt"…
Quoted 7 lines
Quoted 4 lines

Also, I see this patch set is not bisectable as well as v1: you define class TraceUnpack in patch 2 and use it in patch 1.

do you mean that:

Quoted 7 lines
Quoted 4 lines

import TraceUnpackBase as TraceUnpack

in patch 1 is not good, and that I should use just TraceUnpackBase as a class name/alias in patch 1?

My guess is that Luca missed the import ... as line. I believe you should be fine as you are.

You're right Peter, my review was too quick. Indeed Alberto's patches are OK in that respect. Sorry for the noise.

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#14734): https://lists.openembedded.org/g/bitbake-devel/message/14734
Mute This Topic: https://lists.openembedded.org/mt/98405736/1160266
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-