forked from anchore/syft
/
parse_wheel_egg_record.go
97 lines (81 loc) · 2.04 KB
/
parse_wheel_egg_record.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package python
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/gsoc2/syft/internal/log"
"github.com/gsoc2/syft/syft/pkg"
)
// parseWheelOrEggMetadata takes a Python Egg or Wheel (which share the same format and values for our purposes),
// returning all Python packages listed.
func parseWheelOrEggRecord(reader io.Reader) []pkg.PythonFileRecord {
var records []pkg.PythonFileRecord
r := csv.NewReader(reader)
for {
recordList, err := r.Read()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
log.Warnf("unable to read python record file: %w", err)
continue
}
if len(recordList) != 3 {
log.Warnf("python record an unexpected length=%d: %q", len(recordList), recordList)
continue
}
var record pkg.PythonFileRecord
for idx, item := range recordList {
switch idx {
case 0:
record.Path = item
case 1:
if item == "" {
continue
}
fields := strings.SplitN(item, "=", 2)
if len(fields) != 2 {
log.Warnf("unexpected python record digest: %q", item)
continue
}
record.Digest = &pkg.PythonFileDigest{
Algorithm: fields[0],
Value: fields[1],
}
case 2:
record.Size = item
}
}
records = append(records, record)
}
return records
}
func parseInstalledFiles(reader io.Reader, location, sitePackagesRootPath string) ([]pkg.PythonFileRecord, error) {
var installedFiles []pkg.PythonFileRecord
r := bufio.NewReader(reader)
for {
line, err := r.ReadString('\n')
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, fmt.Errorf("unable to read python installed-files file: %w", err)
}
if location != "" && sitePackagesRootPath != "" {
joinedPath := filepath.Join(filepath.Dir(location), line)
line, err = filepath.Rel(sitePackagesRootPath, joinedPath)
if err != nil {
return nil, err
}
}
installedFile := pkg.PythonFileRecord{
Path: strings.ReplaceAll(line, "\n", ""),
}
installedFiles = append(installedFiles, installedFile)
}
return installedFiles, nil
}