-
Notifications
You must be signed in to change notification settings - Fork 59
/
parse_package.go
223 lines (211 loc) · 7.29 KB
/
parse_package.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package parser2v2
import (
"fmt"
"strings"
"github.com/spdx/tools-golang/spdx"
)
func (parser *tvParser2_2) parsePairFromPackage2_2(tag string, value string) error {
// expire pkgExtRef for anything other than a comment
// (we'll actually handle the comment further below)
if tag != "ExternalRefComment" {
parser.pkgExtRef = nil
}
switch tag {
case "PackageName":
// if package already has a name, create and go on to a new package
if parser.pkg == nil || parser.pkg.PackageName != "" {
// check if the previous package contained an spdx Id or not
if parser.pkg != nil && parser.pkg.PackageSPDXIdentifier == nullSpdxElementId2_2 {
return fmt.Errorf("package with PackageName %s does not have SPDX identifier", parser.pkg.PackageName)
}
parser.pkg = &spdx.Package2_2{
FilesAnalyzed: true,
IsFilesAnalyzedTagPresent: false,
}
}
parser.pkg.PackageName = value
// tag for going on to file section
case "FileName":
parser.st = psFile2_2
return parser.parsePairFromFile2_2(tag, value)
// tag for going on to other license section
case "LicenseID":
parser.st = psOtherLicense2_2
return parser.parsePairFromOtherLicense2_2(tag, value)
case "SPDXID":
eID, err := extractElementID(value)
if err != nil {
return err
}
parser.pkg.PackageSPDXIdentifier = eID
if parser.doc.Packages == nil {
parser.doc.Packages = []*spdx.Package2_2{}
}
parser.doc.Packages = append(parser.doc.Packages, parser.pkg)
case "PackageVersion":
parser.pkg.PackageVersion = value
case "PackageFileName":
parser.pkg.PackageFileName = value
case "PackageSupplier":
supplier := &spdx.Supplier{Supplier: value}
if value == "NOASSERTION" {
parser.pkg.PackageSupplier = supplier
break
}
subkey, subvalue, err := extractSubs(value)
if err != nil {
return err
}
switch subkey {
case "Person", "Organization":
supplier.Supplier = subvalue
supplier.SupplierType = subkey
default:
return fmt.Errorf("unrecognized PackageSupplier type %v", subkey)
}
parser.pkg.PackageSupplier = supplier
case "PackageOriginator":
originator := &spdx.Originator{Originator: value}
if value == "NOASSERTION" {
parser.pkg.PackageOriginator = originator
break
}
subkey, subvalue, err := extractSubs(value)
if err != nil {
return err
}
switch subkey {
case "Person", "Organization":
originator.Originator = subvalue
originator.OriginatorType = subkey
default:
return fmt.Errorf("unrecognized PackageOriginator type %v", subkey)
}
parser.pkg.PackageOriginator = originator
case "PackageDownloadLocation":
parser.pkg.PackageDownloadLocation = value
case "FilesAnalyzed":
parser.pkg.IsFilesAnalyzedTagPresent = true
if value == "false" {
parser.pkg.FilesAnalyzed = false
} else if value == "true" {
parser.pkg.FilesAnalyzed = true
}
case "PackageVerificationCode":
parser.pkg.PackageVerificationCode = extractCodeAndExcludes(value)
case "PackageChecksum":
subkey, subvalue, err := extractSubs(value)
if err != nil {
return err
}
if parser.pkg.PackageChecksums == nil {
parser.pkg.PackageChecksums = []spdx.Checksum{}
}
switch spdx.ChecksumAlgorithm(subkey) {
case spdx.SHA1, spdx.SHA256, spdx.MD5:
algorithm := spdx.ChecksumAlgorithm(subkey)
parser.pkg.PackageChecksums = append(parser.pkg.PackageChecksums, spdx.Checksum{Algorithm: algorithm, Value: subvalue})
default:
return fmt.Errorf("got unknown checksum type %s", subkey)
}
case "PackageHomePage":
parser.pkg.PackageHomePage = value
case "PackageSourceInfo":
parser.pkg.PackageSourceInfo = value
case "PackageLicenseConcluded":
parser.pkg.PackageLicenseConcluded = value
case "PackageLicenseInfoFromFiles":
parser.pkg.PackageLicenseInfoFromFiles = append(parser.pkg.PackageLicenseInfoFromFiles, value)
case "PackageLicenseDeclared":
parser.pkg.PackageLicenseDeclared = value
case "PackageLicenseComments":
parser.pkg.PackageLicenseComments = value
case "PackageCopyrightText":
parser.pkg.PackageCopyrightText = value
case "PackageSummary":
parser.pkg.PackageSummary = value
case "PackageDescription":
parser.pkg.PackageDescription = value
case "PackageComment":
parser.pkg.PackageComment = value
case "PackageAttributionText":
parser.pkg.PackageAttributionTexts = append(parser.pkg.PackageAttributionTexts, value)
case "ExternalRef":
parser.pkgExtRef = &spdx.PackageExternalReference2_2{}
parser.pkg.PackageExternalReferences = append(parser.pkg.PackageExternalReferences, parser.pkgExtRef)
category, refType, locator, err := extractPackageExternalReference(value)
if err != nil {
return err
}
parser.pkgExtRef.Category = category
parser.pkgExtRef.RefType = refType
parser.pkgExtRef.Locator = locator
case "ExternalRefComment":
if parser.pkgExtRef == nil {
return fmt.Errorf("no current ExternalRef found")
}
parser.pkgExtRef.ExternalRefComment = value
// now, expire pkgExtRef anyway because it can have at most one comment
parser.pkgExtRef = nil
// for relationship tags, pass along but don't change state
case "Relationship":
parser.rln = &spdx.Relationship2_2{}
parser.doc.Relationships = append(parser.doc.Relationships, parser.rln)
return parser.parsePairForRelationship2_2(tag, value)
case "RelationshipComment":
return parser.parsePairForRelationship2_2(tag, value)
// for annotation tags, pass along but don't change state
case "Annotator":
parser.ann = &spdx.Annotation2_2{}
parser.doc.Annotations = append(parser.doc.Annotations, parser.ann)
return parser.parsePairForAnnotation2_2(tag, value)
case "AnnotationDate":
return parser.parsePairForAnnotation2_2(tag, value)
case "AnnotationType":
return parser.parsePairForAnnotation2_2(tag, value)
case "SPDXREF":
return parser.parsePairForAnnotation2_2(tag, value)
case "AnnotationComment":
return parser.parsePairForAnnotation2_2(tag, value)
// tag for going on to review section (DEPRECATED)
case "Reviewer":
parser.st = psReview2_2
return parser.parsePairFromReview2_2(tag, value)
default:
return fmt.Errorf("received unknown tag %v in Package section", tag)
}
return nil
}
// ===== Helper functions =====
func extractCodeAndExcludes(value string) spdx.PackageVerificationCode {
// FIXME this should probably be done using regular expressions instead
// split by paren + word "excludes:"
sp := strings.SplitN(value, "(excludes:", 2)
if len(sp) < 2 {
// not found; return the whole string as just the code
return spdx.PackageVerificationCode{Value: value, ExcludedFiles: []string{}}
}
// if we're here, code is in first part and excludes filename is in
// second part, with trailing paren
code := strings.TrimSpace(sp[0])
parsedSp := strings.SplitN(sp[1], ")", 2)
fileName := strings.TrimSpace(parsedSp[0])
return spdx.PackageVerificationCode{Value: code, ExcludedFiles: []string{fileName}}
}
func extractPackageExternalReference(value string) (string, string, string, error) {
sp := strings.Split(value, " ")
// remove any that are just whitespace
keepSp := []string{}
for _, s := range sp {
ss := strings.TrimSpace(s)
if ss != "" {
keepSp = append(keepSp, ss)
}
}
// now, should have 3 items and should be able to map them
if len(keepSp) != 3 {
return "", "", "", fmt.Errorf("expected 3 elements, got %d", len(keepSp))
}
return keepSp[0], keepSp[1], keepSp[2], nil
}