github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/debian/dependency.go (about)

     1  package debian
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/anchore/syft/internal"
     7  	"github.com/anchore/syft/internal/log"
     8  	"github.com/anchore/syft/syft/pkg"
     9  	"github.com/anchore/syft/syft/pkg/cataloger/internal/dependency"
    10  )
    11  
    12  var _ dependency.Specifier = dbEntryDependencySpecifier
    13  
    14  func dbEntryDependencySpecifier(p pkg.Package) dependency.Specification {
    15  	meta, ok := p.Metadata.(pkg.DpkgDBEntry)
    16  	if !ok {
    17  		log.Tracef("cataloger failed to extract dpkg metadata for package %+v", p.Name)
    18  		return dependency.Specification{}
    19  	}
    20  	provides := []string{p.Name}
    21  	for _, key := range meta.Provides {
    22  		if key == "" {
    23  			continue
    24  		}
    25  		provides = append(provides, stripVersionSpecifier(key))
    26  	}
    27  
    28  	var allDeps []string
    29  	allDeps = append(allDeps, meta.Depends...)
    30  	allDeps = append(allDeps, meta.PreDepends...)
    31  
    32  	var requires []string
    33  	for _, depSpecifier := range allDeps {
    34  		if depSpecifier == "" {
    35  			continue
    36  		}
    37  		requires = append(requires, splitPackageChoice(depSpecifier)...)
    38  	}
    39  
    40  	return dependency.Specification{
    41  		Provides: provides,
    42  		Requires: requires,
    43  	}
    44  }
    45  
    46  func stripVersionSpecifier(s string) string {
    47  	// examples:
    48  	// libgmp10 (>= 2:6.2.1+dfsg1)         -->  libgmp10
    49  	// libgmp10                            -->  libgmp10
    50  	// foo [i386]                          -->  foo
    51  	// default-mta | mail-transport-agent  -->  default-mta | mail-transport-agent
    52  	// kernel-headers-2.2.10 [!hurd-i386]  -->  kernel-headers-2.2.10
    53  
    54  	return strings.TrimSpace(internal.SplitAny(s, "[(<>=")[0])
    55  }
    56  
    57  func splitPackageChoice(s string) (ret []string) {
    58  	fields := strings.Split(s, "|")
    59  	for _, field := range fields {
    60  		field = strings.TrimSpace(field)
    61  		if field != "" {
    62  			ret = append(ret, stripVersionSpecifier(field))
    63  		}
    64  	}
    65  	return ret
    66  }