-
Notifications
You must be signed in to change notification settings - Fork 3
/
packagetypes.go
321 lines (288 loc) · 7.58 KB
/
packagetypes.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
package dpkg
import (
"fmt"
"path"
"strconv"
"strings"
)
type BinaryPackage struct {
Package string `json:"package"`
Source string `json:"source"`
SourceVersion string `json:"source_version"`
Version string `json:"version"`
InstalledSize int `json:"installed_size"`
Size int `json:"size"`
Architectures Architectures `json:"architectures"`
Description string `json:"description"`
Filename string `json:"filename"`
Tag string `json:"tag"`
Homepage string `json:"homepage"`
SHA256 string `json:"sha256"`
Maintainer string `json:"maintainer"`
Priority string `json:"priority"`
conflicts string
depends string
preDepends string
}
type PackageListItem struct {
Name string
Ptype string
Section string
Priority string
Archs []string
Profile string
Essional bool
}
func (item PackageListItem) Support(arch string) bool {
if arch == "any" {
panic("It's wrong to query package by architecture of any.")
}
for _, i := range item.Archs {
switch i {
case "any", "linux-any", "all":
return true
case arch, "any-" + arch:
return true
}
}
return false
}
type SourcePackage struct {
Package string `json:"package"`
Version string `json:"version"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Architecture []string `json:"architecture"`
Maintainer string `json:"maintainer"`
Format string `json:"format"`
Binary []string `json:"binary"`
PackageList []PackageListItem `json:"package_list"`
Section string `json:"section"`
Priority string `json:"priority"`
directory string
files256 []string
buildDepends string
buildDependsArch string
buildDependsIndep string
}
type SourcefileInfo struct {
Path string
Size int
SHA256 string
}
func (sp SourcePackage) Files(prefix string) []SourcefileInfo {
var ret []SourcefileInfo
for _, f := range sp.files256 {
fs := getArrayString(f, " ")
if len(fs) != 3 {
DebugPrintf("Unknown deb-src-files: %q", f)
continue
}
s, _ := strconv.Atoi(fs[1])
ret = append(ret, SourcefileInfo{
Path: path.Join(prefix, sp.directory, fs[2]),
Size: s,
SHA256: fs[0],
})
}
return ret
}
type Architecture string
type Architectures []string
func (as Architectures) Len() int {
return len(as)
}
func (as Architectures) Swap(i, j int) {
as[i], as[j] = as[j], as[i]
}
func (as Architectures) Less(i, j int) bool {
return strings.Contains(string(as[i]), "64")
}
func (t BinaryPackage) valid() error {
if len(t.Package) < 2 {
return fmt.Errorf("W: pacakge name must be at least two characters long and start with an alphanumeric character: %q", t.Package)
}
if t.Filename == "" {
return fmt.Errorf("W: BinaryPackage %q hasn't a filename field", t.Package)
}
if len(t.SHA256) != 64 {
return fmt.Errorf("W: Wrong SHA256 length %s %d", t.Package, len(t.SHA256))
}
return nil
}
func (cf ControlFile) ToBinary() (BinaryPackage, error) {
t := BinaryPackage{}
t.Package = cf.Get("package")
t.Version = cf.Get("version")
t.Source, t.SourceVersion = parseSourceLine(cf.Get("source"), t.Package, t.Version)
t.InstalledSize, _ = strconv.Atoi(cf.Get("installed-size"))
t.Size, _ = strconv.Atoi(cf.Get("size"))
for _, arch := range cf.GetArray("architecture", " ") {
t.Architectures = append(t.Architectures, arch)
}
t.Description = cf.Get("description")
t.Filename = cf.Get("filename")
t.Tag = cf.Get("tag")
t.Homepage = cf.Get("homepage")
t.SHA256 = cf.Get("sha256")
t.Maintainer = cf.Get("maintainer")
t.Priority = cf.Get("priority")
t.conflicts = cf.Get("conflicts")
t.depends = cf.Get("depends")
t.preDepends = cf.Get("pre-depends")
return t, t.valid()
}
func parseProvides(str string) []string {
if str == "" {
return nil
}
depInfo, err := ParseDepInfo(str)
if err != nil {
panic(err)
}
var ret []string
for depInfo.And != nil {
ret = append(ret, depInfo.Name)
depInfo = depInfo.And
}
ret = append(ret, depInfo.Name)
return ret
}
func (cf SourcePackage) valid() error { return nil }
func (cf ControlFile) ToSource() (SourcePackage, error) {
t := SourcePackage{}
t.Package = cf.Get("package")
t.Version = cf.Get("version")
t.Description = cf.Get("description")
t.Homepage = cf.Get("homepage")
t.Format = cf.Get("format")
t.Binary = cf.GetArray("binary", ",")
if len(t.Binary) == 0 {
t.Binary = []string{t.Package}
}
t.directory = cf.Get("directory")
t.files256 = cf.GetMultiline("Checksums-Sha256")
t.Architecture = cf.GetArray("architecture", " ")
t.Maintainer = cf.Get("maintainer")
t.Section = cf.Get("section")
t.Priority = cf.Get("priority")
t.buildDepends = cf.Get("build-depends")
t.buildDependsArch = cf.Get("build-depends-arch")
t.buildDependsIndep = cf.Get("build-depends-indep")
plist := cf.GetMultiline("package-list")
if len(plist) > 0 {
for _, line := range plist {
i, err := buildPackageListItem(line, t.Format)
if err != nil {
return t, FormatError{"SourcePackage", t.Package + t.Format, err}
}
if len(i.Archs) == 0 {
i.Archs = t.Architecture
}
t.PackageList = append(t.PackageList, i)
}
} else {
// from binary field to build
for _, b := range t.Binary {
t.PackageList = append(t.PackageList, PackageListItem{
Name: b,
Ptype: "deb",
Archs: t.Architecture,
})
}
}
return t, t.valid()
}
func (cf SourcePackage) GetBinary(arch string) []string {
var ret []string
for _, bp := range cf.PackageList {
if bp.Ptype != "deb" {
continue
}
if !bp.Support(arch) {
continue
}
ret = append(ret, bp.Name)
}
if len(ret) == 0 {
panic(fmt.Sprintf("GetBinary(%q) of %q failed. %d %+v\n", arch, cf.Package, len(cf.PackageList), cf.PackageList))
}
return ret
}
func (cf SourcePackage) BuildDepends(arch string, profile string) (*DepInfo, error) {
AssertNoUseAny(arch)
var deps []string
switch arch {
case "linux-all", "all":
if len(cf.buildDependsIndep) > 0 {
deps = append(deps, cf.buildDependsIndep)
}
default:
if len(cf.buildDependsArch) > 0 {
deps = append(deps, cf.buildDependsArch)
}
}
var info *DepInfo
var err error
if len(deps) == 0 {
info, err = ParseDepInfo(cf.buildDepends)
} else {
info, err = ParseDepInfo(strings.Join(deps, ","))
}
if err != nil || info == nil {
return info, err
}
return info.Filter(arch, profile), err
}
func (cf BinaryPackage) deprecatedConflict() []string {
info, _ := ParseDepInfo(cf.conflicts)
if info == nil {
return nil
}
return info.SimpleDeps()
}
func (cf BinaryPackage) Depends(arch string, profile string) (*DepInfo, error) {
AssertNoUseAny(arch)
var deps []string
if cf.depends != "" {
deps = append(deps, cf.depends)
}
if cf.preDepends != "" {
deps = append(deps, cf.preDepends)
}
info, err := ParseDepInfo(strings.Join(deps, ","))
if err != nil || info == nil {
return info, err
}
return info.Filter(arch, profile), nil
}
func buildPackageListItem(line string, format string) (PackageListItem, error) {
var r PackageListItem
fields := getArrayString(line, " ")
n := len(fields)
if n < 4 || n > 7 {
return r, FormatError{"PackageList", line, nil}
}
for i, v := range fields {
switch i {
case 0:
r.Name = v
case 1:
r.Ptype = v
case 2:
r.Section = v
case 3:
r.Priority = v
case 4:
if !strings.HasPrefix(v, "arch=") {
return r, FormatError{"PackageList", line, nil}
}
r.Archs = getArrayString(v[len("arch="):], ",")
case 5:
r.Profile = v
case 6:
r.Essional = v == "essential=yes"
}
}
return r, nil
}