upstream source tracing: base process (patch 1/3)
PATCH 1/1 upstream source tracing: base process (patch 1/3)
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 series of patches provides a solution to the issue by implementing a process that unpacks each SRC_URI element into a temporary directory, collects relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata in a JSON file.
The proposed solution is split into a series of patches, with the first patch containing required modifications to fetchers' code and a TraceUnpackBase class that implements the process, and the second patch implementing the data collecting logic in a separate TraceUnpack subclass. The final patch includes test data and test cases to demonstrate the solution's efficacy.
--- bin/bitbake-selftest | 1 + lib/bb/fetch2/__init__.py | 57 +++++++- 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, 584 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
@@ -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 +54 -3
104 lines not on this page · raw message
lib/bb/fetch2/crate.py +2 -0
@@ -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
@@ -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 correspond 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
@@ -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
@@ -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
@@ -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
Hello Alberto,
thanks for your patches!
I'm not going into the details of the implementation but I have a few comments on formal aspects.
First, I would recommend to remove the "(patch 1/3)" from the subject, or it will be part of the final commit message when applying the patch using 'git am'. This is something that won't make sense later on, when one will look at the git history. 'git format-patch origin/master' will generate patches with an appropriate header for you, and for patch 3 which is special you can do a manual handling but still keep the standard format: "[PATCH 3/3] ...commit title..."
Quoted 12 lines · On Thu, 20 Apr 2023 08:20:24 +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 series of patches provides a solution to the issue by implementing a process that unpacks each
SRC_URIelement into a temporary directory, collects relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata in a JSON file.
Quoted 6 lines
The proposed solution is split into a series of patches, with the first patch containing required modifications to fetchers' code and a TraceUnpackBase class that implements the process, and the second patch implementing the data collecting logic in a separate TraceUnpack subclass. The final patch includes test data and test cases to demonstrate the solution's efficacy.
Your comment is identical for patches 1 and 2. Instead a commit message should describe what the single patch/commit does. Being this a pretty complex patch set you certainly can include a couple lines giving the big picture, but the bulk of the text should really be about _why_ we need the patch, and also about _what_ it does in case it is not obvious from the diff.
Quoted 33 lines
Signed-off-by Alberto Pianon <[email protected]>--- bin/bitbake-selftest | 1 + lib/bb/fetch2/__init__.py | 57 +++++++- 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, 584 insertions(+), 9 deletions(-) create mode 100644 lib/bb/fetch2/trace_base.py create mode 100644 lib/bb/tests/trace_base.pybin/bitbake-selftest +1
-0@@ -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 +0
-0
lib/bb/fetch2/__init__.py import bb.persist_data, bb.utils · line 31
Quoted 5 lines
lib/bb/fetch2/__init__.py +2
-0@@ -27,6 +27,10 @@ import bb.persist_data, bb.utils import bb.checksum import bb.process import bb.event +try: + from .trace import TraceUnpack
The trace module is introduced in a later patch, which is not very good practice as the git history would not be bisectable. Commits should first introduce basic components, and later commits should start using them, and in case there is a good reason to not do so, they should be merged in a single commit.
Best regards,
Luca
Signature · 3 lines
Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Hello Luca,
thanks for your suggestions, I will definitely try to follow them, re-submitting the patches. Best,
Alberto
Il 2023-04-20 10:56 Luca Ceresoli ha scritto:
Quoted 92 lines
Hello Alberto,
thanks for your patches!
I'm not going into the details of the implementation but I have a few comments on formal aspects.
First, I would recommend to remove the "(patch 1/3)" from the subject, or it will be part of the final commit message when applying the patch using 'git am'. This is something that won't make sense later on, when one will look at the git history. 'git format-patch origin/master' will generate patches with an appropriate header for you, and for patch 3 which is special you can do a manual handling but still keep the standard format: "[PATCH 3/3] ...commit title..."
Quoted 12 lines · On Thu, 20 Apr 2023 08:20:24 +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 series of patches provides a solution to the issue by implementing a process that unpacks each
SRC_URIelement into a temporary directory, collects relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata in a JSON file.Quoted 6 lines
The proposed solution is split into a series of patches, with the first patch containing required modifications to fetchers' code and a TraceUnpackBase class that implements the process, and the second patch implementing the data collecting logic in a separate TraceUnpack subclass. The final patch includes test data and test cases to demonstrate the solution's efficacy.
Your comment is identical for patches 1 and 2. Instead a commit message should describe what the single patch/commit does. Being this a pretty complex patch set you certainly can include a couple lines giving the big picture, but the bulk of the text should really be about _why_ we need the patch, and also about _what_ it does in case it is not obvious from the diff.
Quoted 34 lines
Signed-off-by Alberto Pianon <[email protected]>--- bin/bitbake-selftest | 1 + lib/bb/fetch2/__init__.py | 57 +++++++- 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, 584 insertions(+), 9 deletions(-) create mode 100644 lib/bb/fetch2/trace_base.py create mode 100644 lib/bb/tests/trace_base.pybin/bitbake-selftest +1
-0@@ -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 +0
-0lib/bb/fetch2/__init__.py import bb.persist_data, bb.utils · line 31
Quoted 5 lines
lib/bb/fetch2/__init__.py +2
-0@@ -27,6 +27,10 @@ import bb.persist_data, bb.utils import bb.checksum import bb.process import bb.event +try: + from .trace import TraceUnpackThe trace module is introduced in a later patch, which is not very good practice as the git history would not be bisectable. Commits should first introduce basic components, and later commits should start using them, and in case there is a good reason to not do so, they should be merged in a single commit.
Best regards,
Luca
Ciao Alberto,
Quoted 12 lines · On Thu, 20 Apr 2023 08:20:24 +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 series of patches provides a solution to the issue by implementing a process that unpacks each
SRC_URIelement into a temporary directory, collects relevant provenance metadata on each source file, moves everything to the recipe rootdir, and saves metadata in a JSON file.
Quoted 8 lines
The proposed solution is split into a series of patches, with the first patch containing required modifications to fetchers' code and a TraceUnpackBase class that implements the process, and the second patch implementing the data collecting logic in a separate TraceUnpack subclass. The final patch includes test data and test cases to demonstrate the solution's efficacy.
Signed-off-by Alberto Pianon <[email protected]>
Testing this patchset on the autobuilders triggered failures on the oe-selftest builds. Here are the logs:
https://autobuilder.yoctoproject.org/typhoon/#/builders/79/builds/5085/steps/14/logs/stdio
https://autobuilder.yoctoproject.org/typhoon/#/builders/80/builds/5034/steps/14/logs/stdio
https://autobuilder.yoctoproject.org/typhoon/#/builders/86/builds/5076/steps/14/logs/stdio
https://autobuilder.yoctoproject.org/typhoon/#/builders/87/builds/5114/steps/14/logs/stdio
https://autobuilder.yoctoproject.org/typhoon/#/builders/127/builds/1296/steps/15/logs/stdio
May you have a look at that?
Best regards,
Luca
Signature · 3 lines
Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com