This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
repository.go
182 lines (146 loc) · 4.08 KB
/
repository.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
package debianupdate
import (
"gopkg.in/dedis/onet.v1/log"
"bufio"
"compress/gzip"
"io/ioutil"
"os"
"sort"
"strings"
//"sync"
"regexp"
)
/*
* Implement a Debian Repository containing packages
*/
type Repository struct {
Origin string
Suite string
Version string
Packages PackageSlice
SourceUrl string
//sync.Mutex
}
// NewRepository create a new repository from a release file, a packages file
// a keys file and a source url
func NewRepository(releaseFile string, packagesFile string,
sourceUrl string, dir string, maxPackages int) (*Repository, error) {
release, err := ioutil.ReadFile(dir + "/" + releaseFile)
log.ErrFatal(err)
repository := &Repository{SourceUrl: sourceUrl}
for _, line := range strings.Split(string(release), "\n") {
if strings.Contains(line, "Origin:") {
repository.Origin = strings.Replace(line, "Origin: ", "", 1)
} else if strings.Contains(line, "Archive:") {
repository.Suite = strings.Replace(line, "Archive: ", "", 1)
} else if strings.Contains(line, "Version:") {
repository.Version = strings.Replace(line, "Version: ", "", 1)
}
}
file_p, err := os.Open(dir + "/" + packagesFile)
if err != nil {
log.Fatal(err)
}
defer file_p.Close()
gr, err := gzip.NewReader(file_p)
if err != nil {
log.Fatal(err)
}
defer gr.Close()
scanner := bufio.NewScanner(gr)
log.ErrFatal(err)
packageString := ""
// only import the maxPackages first packages
i := 0
for scanner.Scan() {
if i >= maxPackages {
break
}
line := scanner.Text()
if line != " " && line != "" && line != "\n" {
packageString += line + "\n"
} else {
// TODO go repository.AddPackage(packageString) with chan instead of mutex
repository.AddPackage(packageString)
packageString = ""
i = i + 1
}
}
if len(packageString) != 0 {
repository.AddPackage(packageString)
packageString = ""
}
sort.Sort(repository.Packages)
/*for _, p := range repository.Packages {
log.Print(p)
}*/
return repository, nil
}
// NewRepository create a new repository from a release file, a packages file
// a keys file and a source url
func NewRepositoryPopularity(releaseFile, packagesFile, popularFile,
sourceUrl, dir string, maxPackages int) (*Repository, error) {
release, err := ioutil.ReadFile(dir + "/" + releaseFile)
log.ErrFatal(err)
repository := &Repository{SourceUrl: sourceUrl}
for _, line := range strings.Split(string(release), "\n") {
if strings.Contains(line, "Origin:") {
repository.Origin = strings.Replace(line, "Origin: ", "", 1)
} else if strings.Contains(line, "Archive:") {
repository.Suite = strings.Replace(line, "Archive: ", "", 1)
} else if strings.Contains(line, "Version:") {
repository.Version = strings.Replace(line, "Version: ", "", 1)
}
}
file_p, err := os.Open(dir + "/" + packagesFile)
log.ErrFatal(err)
defer file_p.Close()
gr, err := gzip.NewReader(file_p)
log.ErrFatal(err)
defer gr.Close()
scanner := bufio.NewScanner(gr)
log.ErrFatal(err)
packageString := ""
popular, err := ioutil.ReadFile(popularFile)
log.ErrFatal(err)
fetchPackages := make([]string, maxPackages)
re := regexp.MustCompile(".*? +(.*?) .*")
for i, s := range strings.Split(string(popular), "\n")[11 : maxPackages+11] {
fetchPackages[i] = re.ReplaceAllString(s, "$1")
}
// only import the maxPackages first packages
i := 0
for scanner.Scan() {
if i >= maxPackages {
break
}
line := scanner.Text()
if line != " " && line != "" && line != "\n" {
packageString += line + "\n"
} else {
// TODO go repository.AddPackage(packageString) with chan instead of mutex
repository.AddPackage(packageString)
packageString = ""
i = i + 1
}
}
if len(packageString) != 0 {
repository.AddPackage(packageString)
packageString = ""
}
sort.Sort(repository.Packages)
/*for _, p := range repository.Packages {
log.Print(p)
}*/
return repository, nil
}
func (r *Repository) AddPackage(packageString string) {
//r.Lock()
//defer r.Unlock()
p, err := NewPackage(packageString)
log.ErrFatal(err)
r.Packages = append(r.Packages, p)
}
func (r *Repository) GetName() string {
return r.Origin + "-" + r.Suite
}