forked from Open-CMSIS-Pack/vidx2pidx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpidx.go
109 lines (88 loc) · 2.26 KB
/
pidx.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
package main
import (
"encoding/xml"
"errors"
"fmt"
"sync"
)
var Pidx = new(PidxXML)
//
// This file contains all available packages from
// all vendors.
//
type PidxXML struct {
XMLName xml.Name `xml:"index"`
Timestamp string `xml:"timestamp"`
Pindex struct {
XMLName xml.Name `xml:"pindex"`
Pdscs []Pdsc `xml:"pdsc"`
} `xml:"pindex"`
}
type Pdsc struct {
XMLName xml.Name `xml:"pdsc"`
Vendor string `xml:"vendor,attr"`
URL string `xml:"url,attr"`
Name string `xml:"name,attr"`
Version string `xml:"version,attr"`
Timestamp string `xml:"timestamp,attr"`
}
func (p *PidxXML) addPdsc(pdsc Pdsc) error {
idx := p.findPdsc(pdsc)
if idx != -1 {
message := fmt.Sprintf("Package %s/%s/%s already exists!", pdsc.Vendor, pdsc.Name, pdsc.Version)
return errors.New(message)
}
p.Pindex.Pdscs = append(p.Pindex.Pdscs, pdsc)
return nil
}
func (p *PidxXML) findPdsc(targetPdsc Pdsc) int {
// Use map for this
return -1
}
func (p *PidxXML) ListPdsc() []Pdsc {
fmt.Println("D: Listing available packages")
return p.Pindex.Pdscs
}
func updatePdscListTask(id int, vendorPidx VendorPidx, pidx *PidxXML, wg *sync.WaitGroup, err *error) {
defer wg.Done()
url := fmt.Sprintf("%s%s.pidx", vendorPidx.URL, vendorPidx.Vendor)
fmt.Printf("I: [%d] Fetching packages list from %s\n", id, url)
incomingPidx := new(PidxXML)
if *err = ReadXML(url, &incomingPidx); *err != nil {
return
}
if vendorPidx.Timestamp == incomingPidx.Timestamp {
// Nothing changed, avoid extra work
return
}
for _, pdsc := range incomingPidx.ListPdsc() {
if *err = pidx.addPdsc(pdsc); *err != nil {
return
}
}
}
func (p *PidxXML) Update() error {
fmt.Println("I: Updating list of packages")
var wg sync.WaitGroup
var err error
var errs []error
// Process package index first
errs = make([]error, Vidx.PidxLength())
for i, vendorPidx := range Vidx.ListPidx() {
wg.Add(1)
go updatePdscListTask(i, vendorPidx, p, &wg, &errs[i])
}
wg.Wait()
if err = AnyErr(errs); err != nil {
return err
}
// Now process package descriptors (vendors without pidx files)
errs = make([]error, Vidx.PdscLength())
for i, pdsc := range Vidx.ListPdsc() {
errs[i] = Pidx.addPdsc(pdsc)
}
if err = AnyErr(errs); err != nil {
return err
}
return nil
}