Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Python licenses from LicenseExpression entry in the Wheel Metadata #2431

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions syft/pkg/cataloger/python/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ func newPackageForRequirementsWithMetadata(name, version string, metadata pkg.Py

func newPackageForPackage(resolver file.Resolver, m parsedData, sources ...file.Location) pkg.Package {
var licenseSet pkg.LicenseSet
if m.Licenses != "" {

switch {
case m.LicenseExpression != "":
licenseSet = pkg.NewLicenseSet(pkg.NewLicensesFromLocation(m.LicenseLocation, m.LicenseExpression)...)
case m.Licenses != "":
licenseSet = pkg.NewLicenseSet(pkg.NewLicensesFromLocation(m.LicenseLocation, m.Licenses)...)
} else if m.LicenseLocation.Path() != "" {
case m.LicenseLocation.Path() != "":
// If we have a license file then resolve and parse it
found, err := resolver.FilesByPath(m.LicenseLocation.Path())
if err != nil {
Expand All @@ -82,6 +86,7 @@ func newPackageForPackage(resolver file.Resolver, m parsedData, sources ...file.
}
}
}

p := pkg.Package{
Name: m.Name,
Version: m.Version,
Expand Down
3 changes: 2 additions & 1 deletion syft/pkg/cataloger/python/parse_wheel_egg_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type parsedData struct {
Licenses string `mapstructure:"License"`
LicenseFile string `mapstructure:"LicenseFile"`
LicenseExpression string `mapstructure:"LicenseExpression"`
LicenseLocation file.Location
pkg.PythonPackage `mapstructure:",squash"`
}
Expand Down Expand Up @@ -81,7 +82,7 @@ func parseWheelOrEggMetadata(path string, reader io.Reader) (parsedData, error)
// add additional metadata not stored in the egg/wheel metadata file

pd.SitePackagesRootPath = determineSitePackagesRootPath(path)
if pd.Licenses != "" {
if pd.Licenses != "" || pd.LicenseExpression != "" {
pd.LicenseLocation = file.NewLocation(path)
} else if pd.LicenseFile != "" {
pd.LicenseLocation = file.NewLocation(filepath.Join(filepath.Dir(path), pd.LicenseFile))
Expand Down
3 changes: 3 additions & 0 deletions syft/pkg/cataloger/python/parse_wheel_egg_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestParseWheelEggMetadata(t *testing.T) {
ExpectedMetadata: parsedData{
"Apache 2.0",
"",
"",
file.NewLocation("test-fixtures/egg-info/PKG-INFO"),
pkg.PythonPackage{
Name: "requests",
Expand All @@ -36,6 +37,7 @@ func TestParseWheelEggMetadata(t *testing.T) {
ExpectedMetadata: parsedData{
"BSD License",
"",
"",
file.NewLocation("test-fixtures/dist-info/METADATA"),
pkg.PythonPackage{
Name: "Pygments",
Expand Down Expand Up @@ -136,6 +138,7 @@ func TestParseWheelEggMetadataInvalid(t *testing.T) {
{
Fixture: "test-fixtures/egg-info/PKG-INFO-INVALID",
ExpectedMetadata: parsedData{
"",
"",
"",
file.Location{},
Expand Down
Loading