-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpm.go
297 lines (265 loc) · 8.17 KB
/
rpm.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
package db
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"time"
"github.com/cavaliergopher/rpm"
starlarkjson "go.starlark.net/lib/json"
"go.starlark.net/starlark"
)
type rpmPackage struct {
Type string `xml:"type,attr"`
Name string `xml:"name"`
Arch string `xml:"arch"`
Version struct {
Epoch string `xml:"epoch,attr"`
Ver string `xml:"ver,attr"`
Rel string `xml:"rel,attr"`
} `xml:"version"`
Checksum struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
Pkgid string `xml:"pkgid,attr"`
} `xml:"checksum"`
Summary string `xml:"summary"`
Description string `xml:"description"`
Packager string `xml:"packager"`
URL string `xml:"url"`
Time struct {
File int64 `xml:"file,attr"`
Build int64 `xml:"build,attr"`
} `xml:"time"`
Size struct {
Package int64 `xml:"package,attr"`
Installed int64 `xml:"installed,attr"`
Archive int64 `xml:"archive,attr"`
} `xml:"size"`
Location struct {
Text string `xml:",chardata"`
Href string `xml:"href,attr"`
} `xml:"location"`
Format struct {
License string `xml:"license"`
Vendor string `xml:"vendor"`
Group string `xml:"group"`
Buildhost string `xml:"buildhost"`
Sourcerpm string `xml:"sourcerpm"`
HeaderRange struct {
Start string `xml:"start,attr"`
End string `xml:"end,attr"`
} `xml:"header-range"`
Provides struct {
Entry []struct {
Name string `xml:"name,attr"`
Flags string `xml:"flags,attr"`
Epoch int64 `xml:"epoch,attr"`
Ver string `xml:"ver,attr"`
Rel string `xml:"rel,attr"`
} `xml:"entry"`
} `xml:"provides"`
Requires struct {
Entry []struct {
Name string `xml:"name,attr"`
Flags string `xml:"flags,attr"`
Pre string `xml:"pre,attr"`
Ver string `xml:"ver,attr"`
} `xml:"entry"`
} `xml:"requires"`
Conflicts struct {
Entry []struct {
Name string `xml:"name,attr"`
Flags string `xml:"flags,attr"`
Epoch int64 `xml:"epoch,attr"`
Ver string `xml:"ver,attr"`
} `xml:"entry"`
} `xml:"conflicts"`
Obsoletes struct {
Entry []struct {
Name string `xml:"name,attr"`
Flags string `xml:"flags,attr"`
Epoch int64 `xml:"epoch,attr"`
Ver string `xml:"ver,attr"`
} `xml:"entry"`
} `xml:"obsoletes"`
File []struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"file"`
} `xml:"format"`
}
type rpmRepoPrimaryIterator struct {
primary *rpmRepoPrimary
index int
}
// Done implements starlark.Iterator.
func (it *rpmRepoPrimaryIterator) Done() {
it.index = len(it.primary.Packages)
}
// Next implements starlark.Iterator.
func (it *rpmRepoPrimaryIterator) Next(p *starlark.Value) bool {
if it.index >= len(it.primary.Packages) {
return false
}
ent := it.primary.Packages[it.index]
bytes, err := json.Marshal(&ent)
if err != nil {
it.Done()
return false
}
val, err := starlark.Call(
&starlark.Thread{},
starlarkjson.Module.Members["decode"],
starlark.Tuple{starlark.String(bytes)},
[]starlark.Tuple{},
)
if err != nil {
it.Done()
return false
}
*p = val
it.index += 1
return true
}
type rpmRepoPrimary struct {
Packages []rpmPackage `xml:"package"`
}
// Iterate implements starlark.Iterable.
func (r *rpmRepoPrimary) Iterate() starlark.Iterator {
return &rpmRepoPrimaryIterator{primary: r}
}
func (*rpmRepoPrimary) String() string { return "rpmRepoPrimary" }
func (*rpmRepoPrimary) Type() string { return "rpmRepoPrimary" }
func (*rpmRepoPrimary) Hash() (uint32, error) { return 0, fmt.Errorf("rpmRepoPrimary is not hashable") }
func (*rpmRepoPrimary) Truth() starlark.Bool { return starlark.True }
func (*rpmRepoPrimary) Freeze() {}
var (
_ starlark.Value = &rpmRepoPrimary{}
_ starlark.Iterable = &rpmRepoPrimary{}
)
func rpmReadXml(thread *starlark.Thread, f io.Reader) (starlark.Value, error) {
dec := xml.NewDecoder(f)
var primary rpmRepoPrimary
if err := dec.Decode(&primary); err != nil {
return starlark.None, err
}
return &primary, nil
}
type starRpm struct {
pkg *rpm.Package
payloadReader io.ReadCloser
openedPayload bool
}
// Attr implements starlark.HasAttrs.
func (s *starRpm) Attr(name string) (starlark.Value, error) {
if name == "payload" {
return NewFile(nil, "", func() (io.ReadCloser, error) {
if s.openedPayload {
return nil, fmt.Errorf("payload has already been opened")
}
s.openedPayload = true
return s.payloadReader, nil
}, nil), nil
} else if name == "payload_compression" {
return starlark.String(s.pkg.PayloadCompression()), nil
} else if name == "metadata" {
var metadata = struct {
Name string
Version string
Release string
Epoch int
Summary string
Description string
BuildTime time.Time
BuildHost string
InstallTime time.Time
Size uint64
ArchiveSize uint64
Distribution string
Vendor string
License string
Packager string
Groups []string
ChangeLog []string
Source []string
Patch []string
URL string
OperatingSystem string
Architecture string
PreInstallScript string
PreInstallScriptProgram []string
PostInstallScript string
PostInstallScriptProgram []string
PreUninstallScript string
PostUninstallScript string
OldFilenames []string
SourceRPM string
RPMVersion string
Platform string
}{
Name: s.pkg.Name(),
Version: s.pkg.Version(),
Release: s.pkg.Release(),
Epoch: s.pkg.Epoch(),
Summary: s.pkg.Summary(),
Description: s.pkg.Description(),
BuildTime: s.pkg.BuildTime(),
BuildHost: s.pkg.BuildHost(),
InstallTime: s.pkg.InstallTime(),
Size: s.pkg.Size(),
ArchiveSize: s.pkg.ArchiveSize(),
Distribution: s.pkg.Distribution(),
Vendor: s.pkg.Vendor(),
License: s.pkg.License(),
Packager: s.pkg.Packager(),
Groups: s.pkg.Groups(),
ChangeLog: s.pkg.ChangeLog(),
Source: s.pkg.Source(),
Patch: s.pkg.Patch(),
URL: s.pkg.URL(),
OperatingSystem: s.pkg.OperatingSystem(),
Architecture: s.pkg.Architecture(),
PreInstallScript: s.pkg.PreInstallScript(),
PreInstallScriptProgram: s.pkg.Header.GetTag(1085).StringSlice(),
PostInstallScript: s.pkg.PostInstallScript(),
PostInstallScriptProgram: s.pkg.Header.GetTag(1086).StringSlice(),
PreUninstallScript: s.pkg.PreUninstallScript(),
PostUninstallScript: s.pkg.PostUninstallScript(),
OldFilenames: s.pkg.OldFilenames(),
SourceRPM: s.pkg.SourceRPM(),
RPMVersion: s.pkg.RPMVersion(),
Platform: s.pkg.Platform(),
}
bytes, err := json.Marshal(&metadata)
if err != nil {
return nil, err
}
return starlark.String(bytes), nil
}
return nil, nil
}
// AttrNames implements starlark.HasAttrs.
func (s *starRpm) AttrNames() []string {
return []string{"payload", "payload_compression", "metadata"}
}
func (*starRpm) String() string { return "starRpm" }
func (*starRpm) Type() string { return "starRpm" }
func (*starRpm) Hash() (uint32, error) { return 0, fmt.Errorf("starRpm is not hashable") }
func (*starRpm) Truth() starlark.Bool { return starlark.True }
func (*starRpm) Freeze() {}
var (
_ starlark.Value = &starRpm{}
_ starlark.HasAttrs = &starRpm{}
)
func parseRpm(fif FileIf) (starlark.Value, error) {
f, err := fif.Open()
if err != nil {
return starlark.None, err
}
pkg, err := rpm.Read(f)
if err != nil {
return starlark.None, err
}
return &starRpm{pkg: pkg, payloadReader: f}, nil
}