-
Notifications
You must be signed in to change notification settings - Fork 1
/
indices.go
119 lines (104 loc) · 2.84 KB
/
indices.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
package sapt
import (
"bytes"
"compress/gzip"
"log"
"net/http"
"path/filepath"
"regexp"
"sync"
"text/template"
"github.com/goamz/goamz/s3"
)
const packagesTemplate = `{{range .}}Package: {{.Package}}
Version: {{.Version}}
Filename: {{.Filename}}
MD5sum: {{.MD5sum}}
SHA1: {{.SHA1}}
SHA256: {{.SHA256}}
Size: {{.Size}}
Installed-Size: {{.InstalledSize}}
{{ if .Architecture }}{{ printf "Architecture: %s\n" .Architecture }}{{ end -}}
{{ if .Depends }}{{ printf "Depends: %s\n" .Depends }}{{ end -}}
{{ if .Recommends }}{{ printf "Recommends: %s\n" .Recommends }}{{ end -}}
{{ if .Conflicts }}{{ printf "Conflicts: %s\n" .Conflicts }}{{ end -}}
{{ if .License }}{{ printf "License: %s\n" .License}}{{ end -}}
{{ if .Vendor }}{{ printf "Vendor: %s\n" .Vendor }}{{ end -}}
{{ if .Maintainer }}{{ printf "Maintainer: %s\n" .Maintainer }}{{ end -}}
{{ if .Section }}{{ printf "Section: %s\n" .Section }}{{ end -}}
{{ if .Priority }}{{ printf "Priority: %s\n" .Priority }}{{ end -}}
{{ if .Homepage }}{{ printf "Homepage: %s\n" .Homepage }}{{ end -}}
{{ if .Description }}{{ printf "Description: %s\n" .Description }}{{ end -}}
{{ printf "\n" }}{{end}}`
type Index struct {
Path string
Content []byte
}
func ScanBucketPackages(conn *S3) {
packages := []packageMetadata{}
contents := conn.getBucketContents()
packageList := getBucketPackages(contents)
var wg sync.WaitGroup
wg.Add(len(packageList))
headerChan := make(chan http.Header, 10)
for _, pkg := range packageList {
go func(pkg string) {
headerChan <- conn.getObjectHeaders(pkg)
}(pkg)
}
for i := 0; i < len(packageList); i++ {
go func() {
for {
headers, ok := <-headerChan
if !ok {
break
}
m := metadataFromHeaders(headers)
packages = append(packages, *m)
wg.Done()
}
}()
}
wg.Wait()
packageIndex := createPackageIndex(packages)
indices := getIndexPaths(contents)
if len(indices) == 0 {
indices = append(indices, Index{Path: "repo"})
}
for _, index := range indices {
index.Content = packageIndex
conn.uploadPackageIndex(&index)
}
}
func getIndexPaths(contents *map[string]s3.Key) []Index {
indices := []Index{}
pathRe := regexp.MustCompile(`^(.*)/Packages.gz$`)
for key := range *contents {
result := pathRe.FindStringSubmatch(key)
if result != nil {
index := Index{Path: result[1]}
indices = append(indices, index)
}
}
return indices
}
func getBucketPackages(contents *map[string]s3.Key) []string {
packages := []string{}
for key := range *contents {
if filepath.Ext(key) == ".deb" {
packages = append(packages, key)
}
}
return packages
}
func createPackageIndex(packages []packageMetadata) []byte {
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
t, err := template.New("Package Template").Parse(packagesTemplate)
if err != nil {
log.Fatal(err)
}
t.Execute(writer, packages)
writer.Close()
return buf.Bytes()
}