mirror of
https://github.com/anchore/syft.git
synced 2026-11-05 00:46:38 +01:00
port python cataloger to new generic cataloger pattern (#1319)
Signed-off-by: Alex Goodman <alex.goodman@anchore.com> Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
@@ -2,27 +2,28 @@ package python
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/anchore/syft/internal/log"
|
||||
"github.com/anchore/syft/syft/artifact"
|
||||
"github.com/anchore/syft/syft/pkg"
|
||||
"github.com/anchore/syft/syft/pkg/cataloger/common"
|
||||
"github.com/anchore/syft/syft/pkg/cataloger/generic"
|
||||
"github.com/anchore/syft/syft/source"
|
||||
)
|
||||
|
||||
// integrity check
|
||||
var _ common.ParserFn = parseSetup
|
||||
var _ generic.Parser = parseSetup
|
||||
|
||||
// match examples:
|
||||
//
|
||||
// 'pathlib3==2.2.0;python_version<"3.6"' --> match(name=pathlib3 version=2.2.0)
|
||||
// "mypy==v0.770", --> match(name=mypy version=v0.770)
|
||||
// " mypy2 == v0.770", ' mypy3== v0.770', --> match(name=mypy2 version=v0.770), match(name=mypy3, version=v0.770)
|
||||
var pinnedDependency = regexp.MustCompile(`['"]\W?(\w+\W?==\W?[\w\.]*)`)
|
||||
var pinnedDependency = regexp.MustCompile(`['"]\W?(\w+\W?==\W?[\w.]*)`)
|
||||
|
||||
func parseSetup(_ string, reader io.Reader) ([]*pkg.Package, []artifact.Relationship, error) {
|
||||
packages := make([]*pkg.Package, 0)
|
||||
func parseSetup(_ source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
|
||||
var packages []pkg.Package
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
|
||||
@@ -37,14 +38,17 @@ func parseSetup(_ string, reader io.Reader) ([]*pkg.Package, []artifact.Relation
|
||||
}
|
||||
name := strings.Trim(parts[0], "'\"")
|
||||
name = strings.TrimSpace(name)
|
||||
name = strings.Trim(name, "'\"")
|
||||
|
||||
version := strings.TrimSpace(parts[len(parts)-1])
|
||||
packages = append(packages, &pkg.Package{
|
||||
Name: strings.Trim(name, "'\""),
|
||||
Version: strings.Trim(version, "'\""),
|
||||
Language: pkg.Python,
|
||||
Type: pkg.PythonPkg,
|
||||
})
|
||||
version = strings.Trim(version, "'\"")
|
||||
|
||||
if name == "" || version == "" {
|
||||
log.WithFields("path", reader.RealPath).Warnf("unable to parse package in setup.py line: %q", line)
|
||||
continue
|
||||
}
|
||||
|
||||
packages = append(packages, newPackageForIndex(name, version, reader.Location))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user