Skip to content

Commit

Permalink
Unite and fix purl conversion code
Browse files Browse the repository at this point in the history
  • Loading branch information
another-rex committed Sep 5, 2024
1 parent c2b94e3 commit decae61
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Package pypipurl converts an inventory to a PyPI type PackageURL.

This comment has been minimized.

Copy link
@erikvarga

erikvarga Sep 6, 2024

Collaborator

Can you add a TODO to move this into the regular osv-scalibr/purl library? e.g. make a New() function that creates the PURL and applies this transformation for Python purls, and use that everywhere in SCALIBR instead of using &purl.PackageURL{...}
If you use an internal bug to track this you can use the syntax // TODO(b/...): ... With a link to the internal bug

This comment has been minimized.

Copy link
@another-rex

another-rex Sep 9, 2024

Author Collaborator

Done, made #173 and added a todo here. Copybara should create a internal bug as well.

package pypipurl

import (
"strings"

"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/purl"
)

// MakePackageURL returns a package URL following the purl PyPI spec:
// - Name is lowercased
// - Replaces _ with -
//
// See: https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#pypi
func MakePackageURL(i *extractor.Inventory) *purl.PackageURL {
return &purl.PackageURL{
Type: purl.TypePyPi,
Name: strings.ReplaceAll(strings.ToLower(i.Name), "_", "-"),
Version: i.Version,
}
}
7 changes: 2 additions & 5 deletions extractor/filesystem/language/python/pdmlock/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/extractor/filesystem/language/python/internal/pypipurl"
"github.com/google/osv-scalibr/extractor/filesystem/osv"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/purl"
Expand Down Expand Up @@ -98,11 +99,7 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]

// ToPURL converts an inventory created by this extractor into a PURL.
func (e Extractor) ToPURL(i *extractor.Inventory) (*purl.PackageURL, error) {
return &purl.PackageURL{
Type: purl.TypePyPi,
Name: i.Name,
Version: i.Version,
}, nil
return pypipurl.MakePackageURL(i), nil
}

// ToCPEs is not applicable as this extractor does not infer CPEs from the Inventory.
Expand Down
7 changes: 2 additions & 5 deletions extractor/filesystem/language/python/pipfilelock/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/extractor/filesystem/language/python/internal/pypipurl"
"github.com/google/osv-scalibr/extractor/filesystem/osv"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/purl"
Expand Down Expand Up @@ -100,11 +101,7 @@ func addPkgDetails(details map[string]*extractor.Inventory, packages map[string]

// ToPURL converts an inventory created by this extractor into a PURL.
func (e Extractor) ToPURL(i *extractor.Inventory) (*purl.PackageURL, error) {
return &purl.PackageURL{
Type: purl.TypePyPi,
Name: i.Name,
Version: i.Version,
}, nil
return pypipurl.MakePackageURL(i), nil
}

// ToCPEs is not applicable as this extractor does not infer CPEs from the Inventory.
Expand Down
7 changes: 2 additions & 5 deletions extractor/filesystem/language/python/poetrylock/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/extractor/filesystem/language/python/internal/pypipurl"
"github.com/google/osv-scalibr/extractor/filesystem/osv"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/purl"
Expand Down Expand Up @@ -91,11 +92,7 @@ func (e Extractor) Extract(ctx context.Context, input *filesystem.ScanInput) ([]

// ToPURL converts an inventory created by this extractor into a PURL.
func (e Extractor) ToPURL(i *extractor.Inventory) (*purl.PackageURL, error) {
return &purl.PackageURL{
Type: purl.TypePyPi,
Name: i.Name,
Version: i.Version,
}, nil
return pypipurl.MakePackageURL(i), nil
}

// ToCPEs is not applicable as this extractor does not infer CPEs from the Inventory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/google/osv-scalibr/extractor/filesystem"
scalibrfs "github.com/google/osv-scalibr/fs"
"github.com/google/osv-scalibr/log"
"github.com/google/osv-scalibr/extractor/filesystem/language/python/internal/pypipurl"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/purl"
"github.com/google/osv-scalibr/stats"
Expand Down Expand Up @@ -288,11 +289,7 @@ func splitPerRequirementOptions(s string) (string, []string) {

// ToPURL converts an inventory created by this extractor into a PURL.
func (e Extractor) ToPURL(i *extractor.Inventory) (*purl.PackageURL, error) {
return &purl.PackageURL{
Type: purl.TypePyPi,
Name: strings.ToLower(i.Name),
Version: i.Version,
}, nil
return pypipurl.MakePackageURL(i), nil
}

// ToCPEs is not applicable as this extractor does not infer CPEs from the Inventory.
Expand Down
7 changes: 2 additions & 5 deletions extractor/filesystem/language/python/wheelegg/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/extractor/filesystem/internal/units"
"github.com/google/osv-scalibr/extractor/filesystem/language/python/internal/pypipurl"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/purl"
"github.com/google/osv-scalibr/stats"
Expand Down Expand Up @@ -265,11 +266,7 @@ func parse(r io.Reader) (*extractor.Inventory, error) {

// ToPURL converts an inventory created by this extractor into a PURL.
func (e Extractor) ToPURL(i *extractor.Inventory) (*purl.PackageURL, error) {
return &purl.PackageURL{
Type: purl.TypePyPi,
Name: strings.ToLower(i.Name),
Version: i.Version,
}, nil
return pypipurl.MakePackageURL(i), nil
}

// ToCPEs is not applicable as this extractor does not infer CPEs from the Inventory.
Expand Down

0 comments on commit decae61

Please sign in to comment.