mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 08:53:15 +01:00
Clarify python wheel parsing process (#281)
Signed-off-by: Dan Luhring <dan.luhring@anchore.com>
This commit is contained in:
parent
ae71b8832d
commit
65cbacd135
@ -23,29 +23,28 @@ func parseWheelOrEggMetadata(path string, reader io.Reader) (pkg.PythonPackageMe
|
|||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
line = strings.TrimRight(line, "\n")
|
line = strings.TrimRight(line, "\n")
|
||||||
|
|
||||||
// empty line indicates end of entry
|
// An empty line means we are done parsing (either because there's no more data,
|
||||||
|
// or because a description follows as specified in
|
||||||
|
// https://packaging.python.org/specifications/core-metadata/#description;
|
||||||
|
// and at this time, we're not interested in the description).
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
// if the entry has not started, keep parsing lines
|
if len(fields) > 0 {
|
||||||
if len(fields) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// however, if the field parsing has not started yet, keep scanning lines
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(line, " "):
|
case strings.HasPrefix(line, " "):
|
||||||
// a field-body continuation
|
// a field-body continuation
|
||||||
if len(key) == 0 {
|
updatedValue, err := handleFieldBodyContinuation(key, line, fields)
|
||||||
return pkg.PythonPackageMetadata{}, fmt.Errorf("no match for continuation: line: '%s'", line)
|
if err != nil {
|
||||||
|
return pkg.PythonPackageMetadata{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
val, ok := fields[key]
|
fields[key] = updatedValue
|
||||||
if !ok {
|
|
||||||
return pkg.PythonPackageMetadata{}, fmt.Errorf("no previous key exists, expecting: %s", key)
|
|
||||||
}
|
|
||||||
// concatenate onto previous value
|
|
||||||
val = fmt.Sprintf("%s\n %s", val, strings.TrimSpace(line))
|
|
||||||
fields[key] = val
|
|
||||||
default:
|
default:
|
||||||
// parse a new key (note, duplicate keys are overridden)
|
// parse a new key (note, duplicate keys are overridden)
|
||||||
if i := strings.Index(line, ":"); i > 0 {
|
if i := strings.Index(line, ":"); i > 0 {
|
||||||
@ -71,8 +70,23 @@ func parseWheelOrEggMetadata(path string, reader io.Reader) (pkg.PythonPackageMe
|
|||||||
|
|
||||||
// add additional metadata not stored in the egg/wheel metadata file
|
// add additional metadata not stored in the egg/wheel metadata file
|
||||||
|
|
||||||
sitePackagesRoot := filepath.Clean(filepath.Join(filepath.Dir(path), ".."))
|
metadata.SitePackagesRootPath = filepath.Clean(filepath.Join(filepath.Dir(path), ".."))
|
||||||
metadata.SitePackagesRootPath = sitePackagesRoot
|
|
||||||
|
|
||||||
return metadata, nil
|
return metadata, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
if len(key) == 0 {
|
||||||
|
return "", fmt.Errorf("no match for continuation: line: '%s'", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
val, ok := fields[key]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("no previous key exists, expecting: %s", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// concatenate onto previous value
|
||||||
|
return fmt.Sprintf("%s\n %s", val, strings.TrimSpace(line)), nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user