diff --git a/src/sbomnix/derivation.py b/src/sbomnix/derivation.py index 2d1e213..652d8ab 100644 --- a/src/sbomnix/derivation.py +++ b/src/sbomnix/derivation.py @@ -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 @@ -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, @@ -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 @@ -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"" @@ -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) diff --git a/src/sbomnix/nix.py b/src/sbomnix/nix.py index b100455..44834d2 100644 --- a/src/sbomnix/nix.py +++ b/src/sbomnix/nix.py @@ -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}"