-
Notifications
You must be signed in to change notification settings - Fork 856
Port python cataloger to new generic cataloger pattern #1319
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package python | ||
|
|
||
| import ( | ||
| "github.com/anchore/syft/syft/pkg/cataloger/generic" | ||
| ) | ||
|
|
||
| const ( | ||
| eggMetadataGlob = "**/*egg-info/PKG-INFO" | ||
| eggFileMetadataGlob = "**/*.egg-info" | ||
| wheelMetadataGlob = "**/*dist-info/METADATA" | ||
| ) | ||
|
|
||
| // NewPythonIndexCataloger returns a new cataloger for python packages referenced from poetry lock files, requirements.txt files, and setup.py files. | ||
| func NewPythonIndexCataloger() *generic.Cataloger { | ||
| return generic.NewCataloger("python-index-cataloger"). | ||
| WithParserByGlobs(parseRequirementsTxt, "**/*requirements*.txt"). | ||
| WithParserByGlobs(parsePoetryLock, "**/poetry.lock"). | ||
| WithParserByGlobs(parsePipfileLock, "**/Pipfile.lock"). | ||
| WithParserByGlobs(parseSetup, "**/setup.py") | ||
| } | ||
|
|
||
| // NewPythonPackageCataloger returns a new cataloger for python packages within egg or wheel installation directories. | ||
| func NewPythonPackageCataloger() *generic.Cataloger { | ||
| return generic.NewCataloger("python-package-cataloger"). | ||
| WithParserByGlobs(parseWheelOrEgg, eggMetadataGlob, eggFileMetadataGlob, wheelMetadataGlob) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package python | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/anchore/packageurl-go" | ||
| "github.com/anchore/syft/syft/pkg" | ||
| "github.com/anchore/syft/syft/source" | ||
| ) | ||
|
|
||
| func newPackageForIndex(name, version string, locations ...source.Location) pkg.Package { | ||
| p := pkg.Package{ | ||
| Name: name, | ||
| Version: version, | ||
| Locations: source.NewLocationSet(locations...), | ||
| PURL: packageURL(name, version, nil), | ||
| Language: pkg.Python, | ||
| Type: pkg.PythonPkg, | ||
| } | ||
|
|
||
| p.SetID() | ||
|
|
||
| return p | ||
| } | ||
|
|
||
| func newPackageForPackage(m pkg.PythonPackageMetadata, sources ...source.Location) pkg.Package { | ||
| var licenses []string | ||
| if m.License != "" { | ||
| licenses = []string{m.License} | ||
| } | ||
|
|
||
| p := pkg.Package{ | ||
| Name: m.Name, | ||
| Version: m.Version, | ||
| PURL: packageURL(m.Name, m.Version, &m), | ||
| Locations: source.NewLocationSet(sources...), | ||
| Licenses: licenses, | ||
| Language: pkg.Python, | ||
| Type: pkg.PythonPkg, | ||
| MetadataType: pkg.PythonPackageMetadataType, | ||
| Metadata: m, | ||
| } | ||
|
|
||
| p.SetID() | ||
| return p | ||
| } | ||
|
|
||
| func packageURL(name, version string, m *pkg.PythonPackageMetadata) string { | ||
| // generate a purl from the package data | ||
| pURL := packageurl.NewPackageURL( | ||
| packageurl.TypePyPi, | ||
| "", | ||
| name, | ||
| version, | ||
| purlQualifiersForPackage(m), | ||
| "") | ||
|
|
||
| return pURL.ToString() | ||
| } | ||
|
|
||
| func purlQualifiersForPackage(m *pkg.PythonPackageMetadata) packageurl.Qualifiers { | ||
| q := packageurl.Qualifiers{} | ||
| if m == nil { | ||
| return q | ||
| } | ||
| if m.DirectURLOrigin != nil { | ||
| q = append(q, vcsURLQualifierForPackage(m.DirectURLOrigin)...) | ||
| } | ||
| return q | ||
| } | ||
|
|
||
| func vcsURLQualifierForPackage(p *pkg.PythonDirectURLOriginInfo) packageurl.Qualifiers { | ||
| if p == nil || p.VCS == "" { | ||
| return nil | ||
| } | ||
| // Taken from https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst#known-qualifiers-keyvalue-pairs | ||
| // packageurl-go still doesn't support all qualifier names | ||
| return packageurl.Qualifiers{ | ||
| {Key: pkg.PURLQualifierVCSURL, Value: fmt.Sprintf("%s+%s@%s", p.VCS, p.URL, p.CommitID)}, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package python | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/anchore/syft/syft/pkg" | ||
| ) | ||
|
|
||
| func Test_packageURL(t *testing.T) { | ||
| tests := []struct { | ||
| testName string | ||
| name string | ||
| version string | ||
| metadata *pkg.PythonPackageMetadata | ||
| want string | ||
| }{ | ||
| { | ||
| testName: "without metadata", | ||
| name: "name", | ||
| version: "v0.1.0", | ||
| want: "pkg:pypi/name@v0.1.0", | ||
| }, | ||
| { | ||
| testName: "with vcs info", | ||
| name: "name", | ||
| version: "v0.1.0", | ||
| metadata: &pkg.PythonPackageMetadata{ | ||
| Name: "bogus", // note: ignored | ||
| Version: "v0.2.0", // note: ignored | ||
| DirectURLOrigin: &pkg.PythonDirectURLOriginInfo{ | ||
| VCS: "git", | ||
| URL: "https://github.com/test/test.git", | ||
| CommitID: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
| }, | ||
| }, | ||
| want: "pkg:pypi/name@v0.1.0?vcs_url=git+https://github.com/test/test.git%40aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.testName, func(t *testing.T) { | ||
| assert.Equal(t, tt.want, packageURL(tt.name, tt.version, tt.metadata)) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun name :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I couldn't think of another 👎