From 183b8f79d0511c09dd87723682876f181903cd56 Mon Sep 17 00:00:00 2001 From: Dan Luhring Date: Tue, 22 Dec 2020 17:41:27 -0500 Subject: [PATCH] Handle site packages based on which egg file is parsed Signed-off-by: Dan Luhring --- .../python/parse_wheel_egg_metadata.go | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/syft/cataloger/python/parse_wheel_egg_metadata.go b/syft/cataloger/python/parse_wheel_egg_metadata.go index df58850df..66c899b66 100644 --- a/syft/cataloger/python/parse_wheel_egg_metadata.go +++ b/syft/cataloger/python/parse_wheel_egg_metadata.go @@ -3,6 +3,7 @@ package python import ( "bufio" "fmt" + "github.com/anchore/syft/internal/file" "io" "path/filepath" "strings" @@ -70,11 +71,28 @@ func parseWheelOrEggMetadata(path string, reader io.Reader) (pkg.PythonPackageMe // add additional metadata not stored in the egg/wheel metadata file - metadata.SitePackagesRootPath = filepath.Clean(filepath.Join(filepath.Dir(path), "..")) + metadata.SitePackagesRootPath = determineSitePackagesRootPath(path) return metadata, nil } +// isEggRegularFile determines if the specified path is the regular file variant +// of egg metadata (as opposed to a directory that contains more metadata +// files). +func isEggRegularFile(path string) bool { + return file.GlobMatch(eggFileMetadataGlob, path) +} + +// determineSitePackagesRootPath returns the path of the site packages root, +// given the egg metadata file or directory specified in the path. +func determineSitePackagesRootPath(path string) string { + if isEggRegularFile(path) { + return filepath.Clean(filepath.Dir(path)) + } + + return filepath.Clean(filepath.Dir(filepath.Dir(path))) +} + // handleFieldBodyContinuation returns the updated value for the specified field after processing the specified line. // If the continuation cannot be processed, it returns an error. func handleFieldBodyContinuation(key, line string, fields map[string]string) (string, error) {