github.com/anchore/syft@v1.38.2/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  		ProvidesRequires: dependency.ProvidesRequires{
    42  			Provides: provides,
    43  			Requires: requires,
    44  		},
    45  	}
    46  }
    47  
    48  func stripVersionSpecifier(s string) string {
    49  	// examples:
    50  	// libgmp10 (>= 2:6.2.1+dfsg1)         -->  libgmp10
    51  	// libgmp10                            -->  libgmp10
    52  	// foo [i386]                          -->  foo
    53  	// default-mta | mail-transport-agent  -->  default-mta | mail-transport-agent
    54  	// kernel-headers-2.2.10 [!hurd-i386]  -->  kernel-headers-2.2.10
    55  
    56  	return strings.TrimSpace(internal.SplitAny(s, "[(<>=")[0])
    57  }
    58  
    59  func splitPackageChoice(s string) (ret []string) {
    60  	fields := strings.Split(s, "|")
    61  	for _, field := range fields {
    62  		field = strings.TrimSpace(field)
    63  		if field != "" {
    64  			ret = append(ret, stripVersionSpecifier(field))
    65  		}
    66  	}
    67  	return ret
    68  }