-
Notifications
You must be signed in to change notification settings - Fork 331
/
parse-conan-lock.go
189 lines (160 loc) · 4.87 KB
/
parse-conan-lock.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
package lockfile
import (
"encoding/json"
"fmt"
"os"
"strings"
)
type ConanReference struct {
Name string
Version string
Username string
Channel string
RecipeRevision string
PackageID string
PackageRevision string
TimeStamp string
}
type ConanGraphNode struct {
Pref string `json:"pref"`
Ref string `json:"ref"`
Options string `json:"options"`
PackageID string `json:"package_id"`
Prev string `json:"prev"`
Path string `json:"path"`
Context string `json:"context"`
}
type ConanGraphLock struct {
Nodes map[string]ConanGraphNode `json:"nodes"`
}
type ConanLockFile struct {
Version string `json:"version"`
// conan v0.4- lockfiles use "graph_lock", "profile_host" and "profile_build"
GraphLock ConanGraphLock `json:"graph_lock,omitempty"`
ProfileHost string `json:"profile_host,omitempty"`
ProfileBuild string `json:"profile_build,omitempty"`
// conan v0.5+ lockfiles use "requires", "build_requires" and "python_requires"
Requires []string `json:"requires,omitempty"`
BuildRequires []string `json:"build_requires,omitempty"`
PythonRequires []string `json:"python_requires,omitempty"`
}
// TODO this is tentative and subject to change depending on the OSV schema
const ConanEcosystem Ecosystem = "ConanCenter"
func parseConanRenference(ref string) ConanReference {
// very flexible format name/version[@username[/channel]][#rrev][:pkgid[#prev]][%timestamp]
var reference ConanReference
parts := strings.SplitN(ref, "%", 2)
if len(parts) == 2 {
ref = parts[0]
reference.TimeStamp = parts[1]
}
parts = strings.SplitN(ref, ":", 2)
if len(parts) == 2 {
ref = parts[0]
parts = strings.SplitN(parts[1], "#", 2)
reference.PackageID = parts[0]
if len(parts) == 2 {
reference.PackageRevision = parts[1]
}
}
parts = strings.SplitN(ref, "#", 2)
if len(parts) == 2 {
ref = parts[0]
reference.RecipeRevision = parts[1]
}
parts = strings.SplitN(ref, "@", 2)
if len(parts) == 2 {
ref = parts[0]
UsernameChannel := parts[1]
parts = strings.SplitN(UsernameChannel, "/", 2)
reference.Username = parts[0]
if len(parts) == 2 {
reference.Channel = parts[1]
}
}
parts = strings.SplitN(ref, "/", 2)
if len(parts) == 2 {
reference.Name = parts[0]
reference.Version = parts[1]
} else {
// consumer conanfile.txt or conanfile.py might not have a name
reference.Name = ""
reference.Version = ref
}
return reference
}
func parseConanV1Lock(lockfile ConanLockFile) []PackageDetails {
var reference ConanReference
packages := make([]PackageDetails, 0, len(lockfile.GraphLock.Nodes))
for _, node := range lockfile.GraphLock.Nodes {
if node.Path != "" {
// a local "conanfile.txt", skip
continue
}
if node.Pref != "" {
// old format 0.3 (conan 1.27-) lockfiles use "pref" instead of "ref"
reference = parseConanRenference(node.Pref)
} else if node.Ref != "" {
reference = parseConanRenference(node.Ref)
} else {
continue
}
// skip entries with no name, they are most likely consumer's conanfiles
// and not dependencies to be searched in a database anyway
if reference.Name == "" {
continue
}
packages = append(packages, PackageDetails{
Name: reference.Name,
Version: reference.Version,
Ecosystem: ConanEcosystem,
CompareAs: ConanEcosystem,
})
}
return packages
}
func parseConanRequires(packages *[]PackageDetails, requires []string) {
for _, ref := range requires {
reference := parseConanRenference(ref)
// skip entries with no name, they are most likely consumer's conanfiles
// and not dependencies to be searched in a database anyway
if reference.Name == "" {
continue
}
*packages = append(*packages, PackageDetails{
Name: reference.Name,
Version: reference.Version,
Ecosystem: ConanEcosystem,
CompareAs: ConanEcosystem,
})
}
}
func parseConanV2Lock(lockfile ConanLockFile) []PackageDetails {
packages := make(
[]PackageDetails,
0,
uint64(len(lockfile.Requires))+uint64(len(lockfile.BuildRequires))+uint64(len(lockfile.PythonRequires)),
)
parseConanRequires(&packages, lockfile.Requires)
parseConanRequires(&packages, lockfile.BuildRequires)
parseConanRequires(&packages, lockfile.PythonRequires)
return packages
}
func parseConanLock(lockfile ConanLockFile) []PackageDetails {
if lockfile.GraphLock.Nodes != nil {
return parseConanV1Lock(lockfile)
}
return parseConanV2Lock(lockfile)
}
func ParseConanLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *ConanLockFile
lockfileContents, err := os.ReadFile(pathToLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
}
err = json.Unmarshal(lockfileContents, &parsedLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err)
}
return parseConanLock(*parsedLockfile), nil
}