-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathindex.go
432 lines (391 loc) · 14.5 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package packageindex
import (
"encoding/json"
"fmt"
"slices"
"github.com/arduino/arduino-cli/internal/arduino/cores"
"github.com/arduino/arduino-cli/internal/arduino/resources"
"github.com/arduino/arduino-cli/internal/arduino/security"
"github.com/arduino/go-paths-helper"
easyjson "github.com/mailru/easyjson"
"github.com/sirupsen/logrus"
semver "go.bug.st/relaxed-semver"
)
// Index represents Cores and Tools struct as seen from package_index.json file.
//
//easyjson:json
type Index struct {
Packages []*indexPackage `json:"packages"`
IsTrusted bool
isInstalledJSON bool
}
// indexPackage represents a single entry from package_index.json file.
//
//easyjson:json
type indexPackage struct {
Name string `json:"name"`
Maintainer string `json:"maintainer"`
WebsiteURL string `json:"websiteUrl"`
URL string `json:"Url"`
Email string `json:"email"`
Platforms []*indexPlatformRelease `json:"platforms"`
Tools []*indexToolRelease `json:"tools"`
Help indexHelp `json:"help,omitempty"`
}
// indexPlatformRelease represents a single Core Platform from package_index.json file.
//
//easyjson:json
type indexPlatformRelease struct {
Name string `json:"name"`
Architecture string `json:"architecture"`
Version *semver.Version `json:"version"`
Deprecated bool `json:"deprecated"`
Category string `json:"category"`
URL string `json:"url"`
ArchiveFileName string `json:"archiveFileName"`
Checksum string `json:"checksum"`
Size json.Number `json:"size"`
Boards []indexBoard `json:"boards"`
Help indexHelp `json:"help,omitempty"`
ToolDependencies []indexToolDependency `json:"toolsDependencies"`
DiscoveryDependencies []indexDiscoveryDependency `json:"discoveryDependencies"`
MonitorDependencies []indexMonitorDependency `json:"monitorDependencies"`
}
// indexToolDependency represents a single dependency of a core from a tool.
//
//easyjson:json
type indexToolDependency struct {
Packager string `json:"packager"`
Name string `json:"name"`
Version *semver.RelaxedVersion `json:"version"`
}
// indexDiscoveryDependency represents a single dependency of a core from a pluggable discovery tool.
//
//easyjson:json
type indexDiscoveryDependency struct {
Packager string `json:"packager"`
Name string `json:"name"`
}
// indexMonitorDependency represents a single dependency of a core from a pluggable monitor tool.
//
//easyjson:json
type indexMonitorDependency struct {
Packager string `json:"packager"`
Name string `json:"name"`
}
// indexToolRelease represents a single Tool from package_index.json file.
//
//easyjson:json
type indexToolRelease struct {
Name string `json:"name"`
Version *semver.RelaxedVersion `json:"version"`
Systems []indexToolReleaseFlavour `json:"systems"`
}
// indexToolReleaseFlavour represents a single tool flavor in the package_index.json file.
//
//easyjson:json
type indexToolReleaseFlavour struct {
OS string `json:"host"`
URL string `json:"url"`
ArchiveFileName string `json:"archiveFileName"`
Size json.Number `json:"size"`
Checksum string `json:"checksum"`
}
// indexBoard represents a single Board as written in package_index.json file.
//
//easyjson:json
type indexBoard struct {
Name string `json:"name"`
ID []indexBoardID `json:"id,omitempty"`
}
// indexBoardID represents the ID of a single board. i.e. uno, yun, diecimila, micro and the likes
//
//easyjson:json
type indexBoardID struct {
USB string `json:"usb"`
}
// indexHelp represents the help URL
//
//easyjson:json
type indexHelp struct {
Online string `json:"online,omitempty"`
}
// MergeIntoPackages converts the Index data into a cores.Packages and merge them
// with the existing contents of the cores.Packages passed as parameter.
func (index Index) MergeIntoPackages(outPackages cores.Packages) {
for _, inPackage := range index.Packages {
inPackage.extractPackageIn(outPackages, index.IsTrusted, index.isInstalledJSON)
}
}
// IndexFromPlatformRelease creates an Index that contains a single indexPackage
// which in turn contains a single indexPlatformRelease converted from the one
// passed as argument
func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
boards := []indexBoard{}
for _, manifest := range pr.BoardsManifest {
board := indexBoard{
Name: manifest.Name,
}
for _, id := range manifest.ID {
if id.USB != "" {
board.ID = []indexBoardID{{USB: id.USB}}
}
}
boards = append(boards, board)
}
tools := []indexToolDependency{}
for _, t := range pr.ToolDependencies {
tools = append(tools, indexToolDependency{
Packager: t.ToolPackager,
Name: t.ToolName,
Version: t.ToolVersion,
})
}
discoveries := []indexDiscoveryDependency{}
for _, d := range pr.DiscoveryDependencies {
discoveries = append(discoveries, indexDiscoveryDependency{
Packager: d.Packager,
Name: d.Name,
})
}
monitors := []indexMonitorDependency{}
for _, m := range pr.MonitorDependencies {
monitors = append(monitors, indexMonitorDependency{
Packager: m.Packager,
Name: m.Name,
})
}
packageTools := []*indexToolRelease{}
for name, tool := range pr.Platform.Package.Tools {
for _, toolRelease := range tool.Releases {
flavours := []indexToolReleaseFlavour{}
for _, flavour := range toolRelease.Flavors {
flavours = append(flavours, indexToolReleaseFlavour{
OS: flavour.OS,
URL: flavour.Resource.URL,
ArchiveFileName: flavour.Resource.ArchiveFileName,
Size: json.Number(fmt.Sprintf("%d", flavour.Resource.Size)),
Checksum: flavour.Resource.Checksum,
})
}
packageTools = append(packageTools, &indexToolRelease{
Name: name,
Version: toolRelease.Version,
Systems: flavours,
})
}
}
return Index{
IsTrusted: pr.IsTrusted,
Packages: []*indexPackage{
{
Name: pr.Platform.Package.Name,
Maintainer: pr.Platform.Package.Maintainer,
WebsiteURL: pr.Platform.Package.WebsiteURL,
URL: pr.Platform.Package.URL,
Email: pr.Platform.Package.Email,
Platforms: []*indexPlatformRelease{{
Name: pr.Name,
Architecture: pr.Platform.Architecture,
Version: pr.Version,
Deprecated: pr.Deprecated,
Category: pr.Category,
URL: pr.Resource.URL,
ArchiveFileName: pr.Resource.ArchiveFileName,
Checksum: pr.Resource.Checksum,
Size: json.Number(fmt.Sprintf("%d", pr.Resource.Size)),
Boards: boards,
Help: indexHelp{Online: pr.Help.Online},
ToolDependencies: tools,
DiscoveryDependencies: discoveries,
MonitorDependencies: monitors,
}},
Tools: packageTools,
Help: indexHelp{Online: pr.Platform.Package.Help.Online},
},
},
}
}
func (inPackage indexPackage) extractPackageIn(outPackages cores.Packages, trusted bool, isInstallJSON bool) {
outPackage := outPackages.GetOrCreatePackage(inPackage.Name)
outPackage.Maintainer = inPackage.Maintainer
outPackage.WebsiteURL = inPackage.WebsiteURL
outPackage.URL = inPackage.URL
outPackage.Email = inPackage.Email
outPackage.Help = cores.PackageHelp{Online: inPackage.Help.Online}
for _, inTool := range inPackage.Tools {
inTool.extractToolIn(outPackage)
}
for _, inPlatform := range inPackage.Platforms {
inPlatform.extractPlatformIn(outPackage, trusted, isInstallJSON)
}
}
func (inPlatformRelease indexPlatformRelease) extractPlatformIn(outPackage *cores.Package, trusted bool, isInstallJSON bool) error {
outPlatform := outPackage.GetOrCreatePlatform(inPlatformRelease.Architecture)
// If the variable `isInstallJSON` is false it means that the index we're reading is coming from the additional-urls.
// Therefore, the `outPlatform.Indexed` will be set at `true`.
if !isInstallJSON {
outPlatform.Indexed = true
outPlatform.ManuallyInstalled = false
}
// If the latest platform release is deprecated, then deprecate the whole platform.
if outPlatform.Latest == nil || outPlatform.Latest.LessThan(inPlatformRelease.Version) {
outPlatform.Latest = inPlatformRelease.Version
outPlatform.Deprecated = inPlatformRelease.Deprecated
}
outPlatformRelease := outPlatform.GetOrCreateRelease(inPlatformRelease.Version)
outPlatformRelease.Name = inPlatformRelease.Name
outPlatformRelease.Category = inPlatformRelease.Category
outPlatformRelease.IsTrusted = trusted
size, err := inPlatformRelease.Size.Int64()
if err != nil {
logrus.Warningf("invalid platform %s archive size: %s", outPlatformRelease, err)
size = 0
}
outPlatformRelease.Resource = &resources.DownloadResource{
ArchiveFileName: inPlatformRelease.ArchiveFileName,
Checksum: inPlatformRelease.Checksum,
Size: size,
URL: inPlatformRelease.URL,
CachePath: "packages",
}
outPlatformRelease.Help = cores.PlatformReleaseHelp{Online: inPlatformRelease.Help.Online}
outPlatformRelease.BoardsManifest = inPlatformRelease.extractBoardsManifest()
outPlatformRelease.ToolDependencies = inPlatformRelease.extractToolDependencies()
outPlatformRelease.DiscoveryDependencies = inPlatformRelease.extractDiscoveryDependencies()
outPlatformRelease.MonitorDependencies = inPlatformRelease.extractMonitorDependencies()
outPlatformRelease.Deprecated = inPlatformRelease.Deprecated
return nil
}
func (inPlatformRelease indexPlatformRelease) extractToolDependencies() cores.ToolDependencies {
res := make(cores.ToolDependencies, len(inPlatformRelease.ToolDependencies))
for i, tool := range inPlatformRelease.ToolDependencies {
res[i] = &cores.ToolDependency{
ToolName: tool.Name,
ToolVersion: tool.Version,
ToolPackager: tool.Packager,
}
}
return res
}
func (inPlatformRelease indexPlatformRelease) extractDiscoveryDependencies() cores.DiscoveryDependencies {
res := make(cores.DiscoveryDependencies, len(inPlatformRelease.DiscoveryDependencies))
for i, discovery := range inPlatformRelease.DiscoveryDependencies {
res[i] = &cores.DiscoveryDependency{
Name: discovery.Name,
Packager: discovery.Packager,
}
}
return res
}
func (inPlatformRelease indexPlatformRelease) extractMonitorDependencies() cores.MonitorDependencies {
res := make(cores.MonitorDependencies, len(inPlatformRelease.MonitorDependencies))
for i, discovery := range inPlatformRelease.MonitorDependencies {
res[i] = &cores.MonitorDependency{
Name: discovery.Name,
Packager: discovery.Packager,
}
}
return res
}
func (inPlatformRelease indexPlatformRelease) extractBoardsManifest() []*cores.BoardManifest {
boards := make([]*cores.BoardManifest, len(inPlatformRelease.Boards))
for i, board := range inPlatformRelease.Boards {
manifest := &cores.BoardManifest{Name: board.Name}
for _, id := range board.ID {
if id.USB != "" {
manifest.ID = append(manifest.ID, &cores.BoardManifestID{USB: id.USB})
}
}
boards[i] = manifest
}
return boards
}
func (inToolRelease indexToolRelease) extractToolIn(outPackage *cores.Package) {
outTool := outPackage.GetOrCreateTool(inToolRelease.Name)
outToolRelease := outTool.GetOrCreateRelease(inToolRelease.Version)
outToolRelease.Flavors = inToolRelease.extractAndMergeFlavours(outToolRelease.Flavors)
}
// extractAndMergeFlavours extracts flavors objects from an indexToolRelease
// and adds them to the given flavors array if missing. It returns the updated array.
func (inToolRelease indexToolRelease) extractAndMergeFlavours(in []*cores.Flavor) []*cores.Flavor {
for _, flavour := range inToolRelease.Systems {
if slices.ContainsFunc(in, func(f *cores.Flavor) bool { return f.OS == flavour.OS }) {
continue
}
size, _ := flavour.Size.Int64()
in = append(in, &cores.Flavor{
OS: flavour.OS,
Resource: &resources.DownloadResource{
ArchiveFileName: flavour.ArchiveFileName,
Checksum: flavour.Checksum,
Size: size,
URL: flavour.URL,
CachePath: "packages",
},
})
}
return in
}
// LoadIndex reads a package_index.json from a file and returns the corresponding Index structure.
func LoadIndex(jsonIndexFile *paths.Path) (*Index, error) {
buff, err := jsonIndexFile.ReadFile()
if err != nil {
return nil, err
}
var index Index
err = easyjson.Unmarshal(buff, &index)
if err != nil {
return nil, err
}
jsonSignatureFile := jsonIndexFile.Parent().Join(jsonIndexFile.Base() + ".sig")
if jsonSignatureFile.Exist() {
trusted, _, err := security.VerifyArduinoDetachedSignature(jsonIndexFile, jsonSignatureFile)
if err != nil {
logrus.
WithField("index", jsonIndexFile).
WithField("signatureFile", jsonSignatureFile).
WithError(err).Warnf("Checking signature")
} else {
logrus.
WithField("index", jsonIndexFile).
WithField("signatureFile", jsonSignatureFile).
WithField("trusted", trusted).Infof("Checking signature")
index.IsTrusted = trusted
}
} else {
logrus.WithField("index", jsonIndexFile).Infof("Missing signature file")
}
if jsonIndexFile.Base() == "installed.json" {
index.isInstalledJSON = true
}
return &index, nil
}
// LoadIndexNoSign reads a package_index.json from a file and returns the corresponding Index structure.
func LoadIndexNoSign(jsonIndexFile *paths.Path) (*Index, error) {
buff, err := jsonIndexFile.ReadFile()
if err != nil {
return nil, err
}
var index Index
err = easyjson.Unmarshal(buff, &index)
if err != nil {
return nil, err
}
index.IsTrusted = true
return &index, nil
}