forked from stepcode/stepcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize-appveyor-log.go
More file actions
330 lines (308 loc) · 8.54 KB
/
summarize-appveyor-log.go
File metadata and controls
330 lines (308 loc) · 8.54 KB
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
//summarize MSVC errors from an appveyor log
// compile with 'go build summarize-appveyor-log.go'
// takes 0 or 1 args; with 0, gets log from latest
// build. with 1, uses that file as raw json-like log
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"sort"
"strings"
)
const (
headerKey = "Authorization"
headerVal = "Bearer %s"
projUrl = "https://ci.appveyor.com/api/projects/mpictor/stepcode"
//"https://ci.appveyor.com/api/buildjobs/2rjxdv1rnb8jcg8y/log"
logUrl = "https://ci.appveyor.com/api/buildjobs/%s/log"
consoleUrl = "https://ci.appveyor.com/api/buildjobs/%s/console"
)
func main() {
var rawlog io.ReadCloser
var build string
var err error
if len(os.Args) == 2 {
rawlog, build, err = processArgv()
} else {
rawlog, build, err = getLog()
}
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return
}
defer rawlog.Close()
log := decodeConsole(rawlog)
warns, errs := countMessages(log)
fi, err := os.Create(fmt.Sprintf("appveyor-%s.smy", build))
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return
}
printMessages("error", errs, fi)
printMessages("warning", warns, fi)
fmt.Printf("done\n")
}
/* categorizes warnings and errors based upon the MSVC message number (i.e. C4244)
* the regex will match lines like
c:\projects\stepcode\src\base\sc_benchmark.h(45): warning C4251: 'benchmark::descr' : class 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' needs to have dll-interface to be used by clients of class 'benchmark' [C:\projects\STEPcode\build\src\base\base.vcxproj]
[00:03:48] C:\projects\STEPcode\src\base\sc_benchmark.cc(61): warning C4244: '=' : conversion from 'SIZE_T' to 'long', possible loss of data [C:\projects\STEPcode\build\src\base\base.vcxproj]*
*/
func countMessages(log []string) (warns, errs map[string][]string) {
warns = make(map[string][]string)
errs = make(map[string][]string)
fname := " *(.*)" // $1
fline := `(?:\((\d+)\)| ): ` // $2 - either line number in parenthesis or a space, followed by a colon
msgNr := `([A-Z]+\d+): ` // $3 - C4251, LNK2005, etc
msgTxt := `([^\[]*) ` // $4
tail := `\[[^\[\]]*\]`
warnRe := regexp.MustCompile(fname + fline + `warning ` + msgNr + msgTxt + tail)
errRe := regexp.MustCompile(fname + fline + `(?:fatal )?error ` + msgNr + msgTxt + tail)
for _, line := range log {
if warnRe.MatchString(line) {
key := warnRe.ReplaceAllString(line, "$3")
path := strings.ToLower(warnRe.ReplaceAllString(line, "$1:$2"))
arr := warns[key]
if arr == nil {
arr = make([]string, 5)
//detailed text as first string in array
text := warnRe.ReplaceAllString(line, "$4")
arr[0] = fmt.Sprintf("%s", text)
}
//eliminate duplicates
match := false
for _, l := range arr {
if l == path {
match = true
}
}
if !match {
warns[key] = append(arr, path)
}
} else if errRe.MatchString(line) {
key := errRe.ReplaceAllString(line, "$3")
path := strings.ToLower(errRe.ReplaceAllString(line, "$1:$2"))
arr := errs[key]
if arr == nil {
arr = make([]string, 5)
//detailed text as first string in array
text := errRe.ReplaceAllString(line, "$4")
arr[0] = fmt.Sprintf("%s", text)
}
//eliminate duplicates
match := false
for _, l := range arr {
if l == path {
match = true
}
}
if !match {
errs[key] = append(arr, path)
}
}
}
return
}
func printMessages(typ string, m map[string][]string, w io.Writer) {
//sort keys
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
for _, k := range keys {
for i, l := range m[k] {
//first string is an example, not a location
if i == 0 {
fmt.Fprintf(w, "%s %s (i.e. \"%s\")\n", typ, k, l)
} else if len(l) > 1 { //not sure where blank lines are coming from...
fmt.Fprintf(w, " >> %s\n", l)
}
}
}
}
//structs from http://json2struct.mervine.net/
//{"values":[{"i":0,"t":"Specify a project or solution file. The directory does not contain a project or solution file.\r\n","dt":"00:00:04","bg":12,"fg":15}]}
type AppVeyorConsoleLines struct {
Values []struct {
I int `json:"i"`
Text string `json:"t"`
DateTime string `json:"dt"`
BgColor int `json:"bg"`
FgColor int `json:"fg"`
}
}
type AppVeyorBuild struct {
Build struct {
/*BuildNumber int `json:"buildNumber"`*/
Version string `json:"version"`
Jobs []struct {
JobID string `json:"jobId"`
} `json:"jobs"`
} `json:"build"`
}
func splitAppend(log *[]string, blob string) {
//blob = strings.Replace(blob,"\r\n", "\n",-1)
blob = strings.Replace(blob, "\\", "/", -1)
r := strings.NewReader(blob)
unwrapScanner := bufio.NewScanner(r)
for unwrapScanner.Scan() {
txt := unwrapScanner.Text()
//fmt.Printf("%s\n", txt)
*log = append(*log, txt)
}
}
//calculate length of string without escape chars
// func escapeLen(s string)(l int) {
// //s = strings.Replace(s,"\\\\", "/",-1)
// s = strings.Replace(s,"\\\"", "",-1)
// s = strings.Replace(s,"\r\n", "RN",-1)
// return len(s)
// }
//decode the almost-JSON console data from appveyor
func decodeConsole(r io.Reader) (log []string) {
wrapper := Wrap(r)
dec := json.NewDecoder(wrapper)
var consoleLines AppVeyorConsoleLines
var err error
var txtBlob string
err = dec.Decode(&consoleLines)
if err == io.EOF {
err = nil
}
if err == nil {
for _, l := range consoleLines.Values {
txtBlob += l.Text
//el := escapeLen(l.Text)
//something inserts newlines at 229 chars (+\n\r == 231) (found in CMake output)
lenTwoThreeOne := len(l.Text) == 231
if lenTwoThreeOne {
txtBlob = strings.TrimSuffix(txtBlob, "\r\n")
}
//something else starts new log lines at 1024 chars without inserting newlines (found in CTest error output)
if len(l.Text) != 1024 && !lenTwoThreeOne {
//fmt.Printf("sa for l %d, el %d\n", len(l.Text),el)
splitAppend(&log, txtBlob)
txtBlob = ""
}
}
} else {
fmt.Printf("decode err %s\n", err)
}
if len(txtBlob) > 0 {
splitAppend(&log, txtBlob)
}
return
}
func processArgv() (log io.ReadCloser, build string, err error) {
fname := os.Args[1]
if len(fname) < 14 {
err = fmt.Errorf("Name arg '%s' too short. Run as '%s appveyor-NNN.log'", fname, os.Args[0])
return
}
buildRe := regexp.MustCompile(`appveyor-(.+).log`)
build = buildRe.ReplaceAllString(fname, "$1")
if len(build) == 0 {
err = fmt.Errorf("No build id in %s", fname)
return
}
log, err = os.Open(fname)
return
}
func getLog() (log io.ReadCloser, build string, err error) {
client := &http.Client{}
req, err := http.NewRequest("GET", projUrl, nil)
if err != nil {
return
}
apikey := os.Getenv("APPVEYOR_API_KEY")
//api key isn't necessary for read-only queries on public projects
if len(apikey) > 0 {
req.Header.Add(headerKey, fmt.Sprintf(headerVal, apikey))
} //else {
// fmt.Printf("Env var APPVEYOR_API_KEY is not set.")
//}
resp, err := client.Do(req)
if err != nil {
return
}
build, job := decodeProjInfo(resp.Body)
fmt.Printf("build #%s, jobId %s\n", build, job)
resp, err = http.Get(fmt.Sprintf(consoleUrl, job))
if err != nil {
return
}
logName := fmt.Sprintf("appveyor-%s.log", build)
fi, err := os.Create(logName)
if err != nil {
return
}
_, err = io.Copy(fi, resp.Body)
if err != nil {
return
}
log, err = os.Open(logName)
if err != nil {
log = nil
}
return
}
func decodeProjInfo(r io.Reader) (vers string, job string) {
dec := json.NewDecoder(r)
var av AppVeyorBuild
err := dec.Decode(&av)
if err != io.EOF && err != nil {
fmt.Printf("err %s\n", err)
return
}
if len(av.Build.Jobs) != 1 {
return
}
vers = av.Build.Version
job = av.Build.Jobs[0].JobID
return
}
//wrap a reader, modifying content to make the json decoder happy
//only tested with data from appveyor console
type jsonWrapper struct {
source io.Reader
begin bool
end bool
}
func Wrap(r io.Reader) *jsonWrapper {
return &jsonWrapper{
source: r,
begin: true,
}
}
// func nonNeg(n int) (int) {
// if n < 0 {
// return 0
// }
// return n
// }
func (w *jsonWrapper) Read(p []byte) (n int, err error) {
if w.end {
return 0, io.EOF
}
if w.begin {
w.begin = false
n = copy(p, []byte(`{"values":[`))
}
m, err := w.source.Read(p[n:])
n += m
if err == io.EOF {
w.end = true
if n < len(p) {
n = copy(p, []byte(`{"dummy":"data"}]}`))
} else {
err = fmt.Errorf("No room to terminate JSON struct with '}'\n")
}
}
return
}
// kate: indent-width 8; space-indent off; replace-tabs off; replace-tabs-save off; replace-trailing-space-save on; remove-trailing-space on; tab-intent on; tab-width 8; show-tabs off;