-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftbfs.go
286 lines (222 loc) · 5.94 KB
/
ftbfs.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
package main
import (
"os"
"fmt"
"http"
"flag"
"time"
"io/ioutil"
"strings"
"launchpad.net/gobson/bson"
"launchpad.net/mgo"
"github.com/janimo/lpad"
)
func check(err os.Error) {
if err != nil {
panic(err)
}
}
//Login to Launchpad
func login() lpad.Root {
root, err := lpad.Login(lpad.Production, &NoAuth{})
check(err)
return root
}
//Line in the buildlogs immediately following the error messages
const endOfLog = "FAILED [dpkg-buildpackage died]"
//Build error messages should only be looked for in the lastBytes of the log text
const lastBytes = 100 * 1024
//Retrieve the gist of the error logs pointed to by the URL
func getBuildLog(url string) string {
response, err := http.Get(url)
if url == "" {
return ""
}
defer response.Body.Close()
b, err := ioutil.ReadAll(response.Body)
check(err)
//only the last part of the log is relevant
e := len(b) - 1
s := 0
if e > lastBytes {
s = e - lastBytes
}
//drop anything after the error message
res := strings.SplitN(string(b[s:e]), endOfLog, 2)
//keep only last 200 lines
split := strings.Split(res[0], "\n")
e = len(split) - 1
s = 0
if e > 200 {
s = e - 200
}
return strings.Join(split[s:e], "\n")
}
//Has a value for all architectures we care about, currently only ARM
var archs = map[string]bool{"armel": true}
func process(b lpad.Build, state lpad.BuildState) {
//Ignore architectures we do not care about
if !archs[b.ArchTag()] {
return
}
//Ignore builds with no SPPH as they are not the most recent
spph, err := b.CurrentSourcePublicationLink()
if err != nil {
return
}
save(b, spph)
}
//Check if a build log is in the database
func stored(url string) bool {
q := collection.Find(bson.M{"url": url})
c, err := q.Count()
check(err)
return c > 0
}
//A set of hardcoded matches. Needs to allow user specified patterns to be more useful and generic
var errorPatterns = map[string]string{
"timeout": "Build killed with signal 15",
"segfault": "Segmentation fault",
"gcc-ice": "internal compiler error",
"cmake": "CMake Error",
"opengl": "error: '(GL|gl).* was not declared",
"qtopengl": "error: 'GLdouble' has a previous declaration",
"linker": "final link failed: Bad value",
"tests": "dh_auto_test: .* returned exit code 2",
"dependencies": "but it is not going to be installed",
"warnings": "some warnings being treated as errors",
}
type PatternCause struct {
Pattern string //string occuring in the error log
Cause string //name of the corresponding error cause
}
//Update the cause field for FTBFS records based on a patterns matching their error logs
func updateCauses() {
var c *PatternCause
q := causes.Find(nil)
q.For(&c, func() os.Error {
errorPatterns[c.Cause] = c.Pattern
return nil
})
for cause, p := range errorPatterns {
collection.UpdateAll(bson.M{"content": bson.RegEx{Pattern: p}}, bson.M{"$set": bson.M{"cause": cause}})
}
}
var ftbfsList map[string]bool
func save(b lpad.Build, spph lpad.SPPH) {
url := b.BuildLogURL()
ftbfsList[url] = false, false
if stored(url) {
return
}
fmt.Printf("Saving error log for %s %s %s\n", spph.PackageName(), spph.PackageVersion(), b.ArchTag())
content := getBuildLog(url)
collection.Upsert(bson.M{"package": spph.PackageName()}, bson.M{"package": spph.PackageName(), "version": spph.PackageVersion(), "url": url, "cause": "other", "content": content, "datecreated": b.DateCreated(), "component": spph.Component()})
}
type FTBFSEntry struct {
URL string
}
func loadFTBFSList() {
var entry *FTBFSEntry
if ftbfsList == nil {
ftbfsList = make(map[string]bool)
}
q := collection.Find(nil)
q.For(&entry, func() os.Error {
ftbfsList[entry.URL] = true
return nil
})
}
//Remove old FTBFS entries
func purgeFTBFSList() {
for url,_ := range(ftbfsList) {
collection.Remove(bson.M{"url":url})
}
}
//Find current FTBFS logs
func getFTBFS(root lpad.Root) {
ubuntu, _ := root.Distro("ubuntu")
series, _ := ubuntu.FocusSeries()
fmt.Println("Fetching FTBFS list for", series.FullSeriesName())
loadFTBFSList()
for _, build_state := range []lpad.BuildState{lpad.BSFailedToBuild} {
ftbfs, _ := series.GetBuildRecords(build_state, lpad.PocketRelease, "")
ftbfs.For(func(b lpad.Build) os.Error {
process(b, build_state)
return nil
})
}
purgeFTBFSList()
fmt.Println("Done fetching")
}
//nanoseconds in hour
const HOUR = 1e9*3600
func taskGetFTBFS(root lpad.Root) {
c := time.Tick(1 * HOUR)
for {
getFTBFS(root)
fillEntries()
<-c
}
}
//Struct into which Query.For() unmarshals
var result *struct{ URL string }
func queryFTBFS(cause string) {
q := collection.Find(bson.M{"cause": cause})
c, _ := q.Count()
fmt.Printf("A total of %d packages FTBFS with cause '%s'\n\n", c, cause)
q.For(&result, func() os.Error {
fmt.Println(result.URL)
return nil
})
}
const (
MONGO_URL = "localhost"
MONGO_DB = "FTBFS"
MONGO_COL_FTBFS = "ftbfs"
MONGO_COL_CAUSES = "causes"
)
var collection, causes mgo.Collection
func mongoConnect() {
session, err := mgo.Mongo(MONGO_URL)
check(err)
collection = session.DB(MONGO_DB).C(MONGO_COL_FTBFS)
causes = session.DB(MONGO_DB).C(MONGO_COL_CAUSES)
}
func main() {
var fetch, update, serve bool
var cause, port string
flag.BoolVar(&fetch, "f", false, "Fetch recent FTBFS data from Launhpad and store it to the database")
flag.BoolVar(&update, "u", false, "Update the FTBFS cause field on saved build records")
flag.BoolVar(&serve, "s", false, "Start the web server")
flag.StringVar(&port, "p", "9999", "Port to listen on")
flag.StringVar(&cause, "c", "", "List FTBFS for a given cause (i.e. timeout, opengl)")
flag.Parse()
mongoConnect()
root := login()
s := make(chan int)
if serve {
fillEntries()
go runServer(port, s)
}
if fetch {
if serve {
go taskGetFTBFS(root)
} else {
getFTBFS(root)
}
}
if update {
updateCauses()
}
if cause != "" {
queryFTBFS(cause)
}
if flag.NFlag() == 0 {
fmt.Println("Usage:")
flag.PrintDefaults()
}
if serve {
<-s
}
}