This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
359 lines (328 loc) · 7.69 KB
/
main.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
package main
import (
"archive/tar"
"compress/gzip"
"context"
"encoding/gob"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/meilisearch/meilisearch-go"
)
type Base struct {
Branch string
Repository string
Architecture string
RootPath string
IndexUID string
LastSet map[string]bool
NextSet map[string]bool
Content string
Packages []Package
}
var base Base
type Package struct {
CheckSum string `json:"id"` // for search and react list's key
Name string `json:"package"`
Version string `json:"version"`
Description string `json:"description"`
FileSize int `json:"file_size"`
InstalledSize int `json:"installed_size"`
ProjectURL string `json:"project"`
License string `json:"license"`
Origin string `json:"origin"`
Depends []string `json:"depends"`
Provides []string `json:"provides"`
Repository string `json:"repository"`
Commit string `json:"commit"`
BuildTime string `json:"build_time"`
Maintainer Maintainer `json:"maintainer"`
}
type Maintainer struct {
Name string `json:"name"`
Email string `json:"email"`
}
var (
key string
url string
path string
)
var (
authors = map[string]Maintainer{}
re_author = regexp.MustCompile(`(.+)\s<(.+)>`)
requires = map[string]string{}
re_require = regexp.MustCompile(`(?:.*:)?([^=<>]*)`)
)
func init() {
flag.StringVar(&url, "url", "http://localhost:7700", "meilisearch address")
flag.StringVar(&key, "key", "", "meilisearch master key")
flag.StringVar(&path, "path", "", "path of APKINDEX.tar.gz")
flag.Parse()
if key == "" || path == "" {
log.Panicln("KEY and PATH are required")
}
base.Init(path)
log.SetFlags(log.LstdFlags | log.Lmsgprefix)
log.SetPrefix(base.RootPath + ": ")
}
func main() {
log.Println("aindex start")
if err := base.LoadCache(); err != nil {
log.Panicln(err)
}
if err := base.Read(); err != nil {
log.Panicln(err)
}
count := base.Parse()
if err := base.SaveCache(); err != nil {
log.Panicln(err)
}
log.Printf("new %d old %d to update, sum %d\n",
count, len(base.LastSet), len(base.NextSet))
if count == 0 {
return
}
// connect meilisearch
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: url,
APIKey: key,
})
if _, err := client.GetKeys(nil); err != nil {
log.Panicln(err)
}
var (
task *meilisearch.TaskInfo
err error
)
// custom time out
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Minute*5)
defer cancelFunc()
option := meilisearch.WaitParams{
Context: ctx,
Interval: time.Millisecond * 100,
}
// delete old
delete_ids := make([]string, 0, len(base.LastSet))
for k := range base.LastSet {
delete_ids = append(delete_ids, k)
}
task, err = client.Index(base.IndexUID).DeleteDocumentsByFilter(
fmt.Sprintf(`id IN [%s]`, strings.Join(delete_ids, `,`)))
if err != nil {
log.Panicln(err)
}
if _, err := client.WaitForTask(task.TaskUID, option); err != nil {
log.Panicln(err)
}
// add new
task, err = client.Index(base.IndexUID).AddDocuments(base.Packages)
if err != nil {
log.Panicln(err)
}
if _, err := client.WaitForTask(task.TaskUID, option); err != nil {
log.Panicln(err)
}
log.Println("Done")
}
// empty when error
func (b *Base) Read() error {
// file
file, err := os.Open(b.RootPath + "/APKINDEX.tar.gz")
if err != nil {
return err
}
defer file.Close()
// gzip
gzip_reader, err := gzip.NewReader(file)
if err != nil {
return err
}
defer gzip_reader.Close()
// tar
tar_reader := tar.NewReader(gzip_reader)
for {
header, err := tar_reader.Next()
// if err == io.EOF {
// // come to ending
// fmt.Println("404 not found in tar")
// break
// }
if err != nil {
return err
}
if header.Name == "APKINDEX" {
buffer := new(strings.Builder)
buffer.Grow(int(header.Size))
_, err := io.Copy(buffer, tar_reader)
if err != nil {
return err
}
b.Content = buffer.String()
return nil
}
}
}
func (b *Base) Parse() int {
sections := strings.Split(strings.TrimRight(b.Content, "\n"), "\n\n")
out_pkgs := make([]Package, 0, len(sections))
for _, section := range sections {
pkg := parse_package(section)
if pkg == nil {
continue
}
out_pkgs = append(out_pkgs, *pkg)
}
b.Packages = out_pkgs
return len(out_pkgs)
}
func parse_package(str string) *Package {
pkg := Package{
Repository: base.Repository,
}
lines := strings.Split(str, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
log.Println("length of part is not two?")
return nil
}
key, value := parts[0], parts[1]
switch key {
case "C":
uuid, is_old := base.SetCache(value)
if is_old {
return nil
}
pkg.CheckSum = uuid
case "P":
pkg.Name = value
case "V":
pkg.Version = value
case "S":
pkg.FileSize, _ = strconv.Atoi(value)
case "I":
pkg.InstalledSize, _ = strconv.Atoi(value)
case "T":
pkg.Description = value
case "U":
pkg.ProjectURL = value
case "L":
pkg.License = value
case "o":
pkg.Origin = value
case "m":
pkg.Maintainer = get_author(value)
case "t":
pkg.BuildTime = value
case "c":
pkg.Commit = value
case "D":
pkg.Depends = get_require(value)
case "p":
pkg.Provides = get_require(value)
default:
// log.Println("parser key, continue:", key)
continue
}
}
return &pkg
}
func (b *Base) SetCache(check_sum string) (string, bool) {
uuid := strings.TrimRight(check_sum, "=")[2:]
uuid = strings.ReplaceAll(uuid, "+", "-")
uuid = strings.ReplaceAll(uuid, "/", "_")
b.NextSet[uuid] = true
// if this package is exists in cache
if b.LastSet[uuid] {
delete(b.LastSet, uuid) // remove it from last set
return uuid, true // it is old and we should skip
}
return uuid, false
}
func get_author(str string) Maintainer {
if value, ok := authors[str]; ok {
return value
}
match := re_author.FindStringSubmatch(str)
if len(match) != 3 {
return Maintainer{}
}
name := match[1]
email := match[2]
author := Maintainer{
Name: name,
Email: email,
}
authors[str] = author
return author
}
func get_require(str string) []string {
names := make(map[string]bool)
inputs := strings.Split(str, " ")
for _, input := range inputs {
if names[input] {
continue
}
if value, ok := requires[input]; ok {
names[value] = true
continue
}
match := re_require.FindStringSubmatch(input)
if len(match) != 2 {
log.Println("regexp require fail case", input)
continue
}
requires[input] = match[1]
names[match[1]] = true
}
outputs := make([]string, 0, len(names))
for name := range names {
outputs = append(outputs, name)
}
return outputs
}
func (b *Base) Init(path string) {
// root path, like /home/qaq/rsync/v3.19/main/aarch64
b.RootPath = filepath.Dir(path)
// branch, repository, architecture
patrs := strings.Split(b.RootPath, string(filepath.Separator))
length := len(patrs)
b.Architecture = patrs[length-1]
b.Repository = patrs[length-2]
b.Branch = patrs[length-3]
// search index name
b.IndexUID = fmt.Sprintf("%s_%s",
strings.ReplaceAll(b.Branch, ".", "_"), b.Architecture)
// NewSet for cache.gob
b.NextSet = make(map[string]bool)
b.LastSet = make(map[string]bool)
}
func (b *Base) LoadCache() error {
f, err := os.Open(b.RootPath + "/cache.gob")
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
defer f.Close()
decoder := gob.NewDecoder(f)
return decoder.Decode(&b.LastSet)
}
func (b *Base) SaveCache() error {
f, err := os.Create(b.RootPath + "/cache.gob")
if err != nil {
return err
}
defer f.Close()
encoder := gob.NewEncoder(f)
return encoder.Encode(b.NextSet)
}