forked from quay/claircore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpurl.go
More file actions
48 lines (42 loc) · 1.28 KB
/
Copy pathpurl.go
File metadata and controls
48 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package python
import (
"context"
"fmt"
"github.com/package-url/packageurl-go"
"github.com/quay/claircore"
"github.com/quay/claircore/pkg/pep440"
"github.com/quay/claircore/toolkit/types"
)
const (
// PURLType is the type of package URL for Python packages.
PURLType = "pypi"
)
// GeneratePURL generates a PyPI PURL for a given [claircore.IndexRecord].
// Example: pkg:pypi/django@1.11.1
func GeneratePURL(ctx context.Context, ir *claircore.IndexRecord) (packageurl.PackageURL, error) {
return packageurl.PackageURL{
Type: PURLType,
Name: ir.Package.Name,
Version: ir.Package.Version,
}, nil
}
// ParsePURL parses a PyPI PURL into a list of [claircore.IndexRecord]s.
// The matcher needs the NormalizedVersion to be set, and it to be pep440.
func ParsePURL(ctx context.Context, purl packageurl.PackageURL) ([]*claircore.IndexRecord, error) {
v, err := pep440.Parse(purl.Version)
if err != nil {
return nil, fmt.Errorf("python: unable to parse version: %w", err)
}
return []*claircore.IndexRecord{
{
Package: &claircore.Package{
Name: purl.Name,
Version: v.String(),
NormalizedVersion: v.Version(),
Kind: types.BinaryPackage,
Source: &claircore.Package{},
},
Repository: &Repository,
},
}, nil
}