Skip to content

Commit

Permalink
sbomnix: Do not assume out output is always used
Browse files Browse the repository at this point in the history
Do not assume the derivation's `out` output should always be in the
`derivation.py:Derive` outputs list. Instead, only add the outputs of
the actually-used outputs.

This change fixes: #108.

Signed-off-by: Henri Rosten <henri.rosten@unikie.com>
  • Loading branch information
henrirosten committed Mar 13, 2024
1 parent d0e7119 commit 9df4fb3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/sbomnix/derivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
###############################################################################


def load(path):
def load(path, outpath):
"""Load derivation from path"""
d_obj = None
with open(path, encoding="utf-8") as f:
d_obj = eval(f.read(), {"__builtins__": {}, "Derive": Derive}, {})
d_obj.store_path = path
LOG.log(LOG_SPAM, "load derivation: %s", d_obj)
LOG.log(LOG_SPAM, "deivation attrs: %s", d_obj.to_dict())
d_obj.init(path, outpath)
LOG.log(LOG_SPAM, "load derivation: %s", d_obj)
LOG.log(LOG_SPAM, "derivation attrs: %s", d_obj.to_dict())
return d_obj


Expand All @@ -37,8 +38,6 @@ def destructure(env):
class Derive:
"""Nix derivation as found as .drv files in the Nix store."""

store_path = None

def __init__(
self,
_outputs=None,
Expand Down Expand Up @@ -74,7 +73,9 @@ def __init__(
self.version = envVars.get("version", "")
self.patches = patches or envVars.get("patches", "")
self.system = envVars.get("system", "")
self.outputs = [envVars.get("out", "")]
self.out = envVars.get("out", "")
self.outputs = []
self.store_path = None
# pname 'source' in Nix has special meaning - it is the default name
# for all fetchFromGitHub derivations. As such, it should not be used
# to construct cpe or purl, rather, cpe and purl should be empty
Expand All @@ -87,6 +88,14 @@ def __init__(
)
self.urls = envVars.get("urls", "")

def init(self, path, outpath):
"""Initialize self.store_path and self.outputs"""
assert self.store_path is None
LOG.log(LOG_SPAM, "path:%s, outpath:%s", path, outpath)
self.store_path = path
outpath = outpath if outpath and outpath != path else self.out
self.add_output_path(outpath)

def __repr__(self):
return f"<Derive({repr(self.name)})>"

Expand All @@ -97,7 +106,7 @@ def set_cpe(self, cpe_generator):

def add_output_path(self, path):
"""Add an output path to derivation"""
if path not in self.outputs and path != self.store_path:
if path and path not in self.outputs and path != self.store_path:
LOG.log(LOG_SPAM, "adding outpath to %s:%s", self, path)
bisect.insort(self.outputs, path)

Expand Down
2 changes: 1 addition & 1 deletion src/sbomnix/nix.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _update(self, drv_path, nixpath=None):
return
drv_obj = self._get_cached(drv_path)
if not drv_obj:
drv_obj = load(drv_path)
drv_obj = load(drv_path, nixpath)
drv_obj.set_cpe(self.cpe_generator)
self._add_cached(drv_path, drv=drv_obj)
assert drv_obj.store_path == drv_path, f"unexpected drv_path: {drv_path}"
Expand Down

0 comments on commit 9df4fb3

Please sign in to comment.