/
getTinderboxSummary.php
190 lines (173 loc) · 5.6 KB
/
getTinderboxSummary.php
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
<?php
/* -*- Mode: PHP; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=2 et tw=80 : */
if (!isset($_GET["tree"]) || !isset($_GET["id"]))
die("tree or id not set");
if (!preg_match('/^[a-zA-Z0-9\.-]+$/', $_GET["tree"]))
die("invalid tree");
if (!preg_match('/^\d+\.\d+\.\d+\.gz$/', $_GET["id"]))
die("invalid id");
if (isset($_GET["reftest"]) && $_GET["reftest"] == "true") {
$type = "reftest";
} elseif (isset($_GET["starred"]) && $_GET["starred"] == "true") {
$type = "starred";
} else {
$type = "notstarred";
}
header("Content-Type: text/plain,charset=utf-8");
header("Access-Control-Allow-Origin: *");
echo getSummary($_GET["tree"], $_GET["id"], $type);
function fgets_whole_line($fp) {
$line = '';
while (!feof($fp) && substr($line, -1, 1) != "\n")
$line .= fgets($fp);
return $line;
}
function getSummary($tree, $id, $type) {
if ($type != "starred" && $type != "notstarred" && $type != "reftest")
die("invalid type passed to getSummary");
$reftest = $type == "reftest";
$starred = $type == "starred";
$file = "../summaries/" . $tree . "_" . $id . "_" . $type;
if (file_exists($file))
return file_get_contents($file);
$host = "tinderbox.mozilla.org";
$page = "/showlog.cgi?log=" . $tree . "/" . $id; // . 1233853948.1233859186.27458.gz";
$fp = fsockopen($host, 80, $errno, $errdesc);
if (!$fp)
return "Couldn't connect to $host:\nError: $errno\nDesc: $errdesc\n";
$request = "GET $page HTTP/1.0\r\n";
$request .= "Host: $host\r\n";
$request .= "User-Agent: PHP test client\r\n\r\n";
$lines = array();
fputs ($fp, $request);
stream_set_timeout($fp, 20);
stream_set_blocking($fp, 0);
$foundSummaryStart = false;
$foundLogStart = false;
$fileExistedAfterAll = false;
$isStillRunning = false;
global $signature;
$signature = "";
while (!feof($fp)) {
if (file_exists($file)) {
$fileExistedAfterAll = true;
break;
}
$line = fgets_whole_line($fp);
if ($line != "") {
if (!$foundSummaryStart && !$foundLogStart) {
if (preg_match("/Build Error Summary.*Build Error Log.*No More Errors/i", $line)) {
$isStillRunning = true;
break;
}
if (preg_match("/Build Error Summary.*Build Error Log/i", $line)) {
// Summary is empty.
break;
}
}
if ($reftest) {
if (!$foundLogStart) {
if (preg_match("/Build Error Log.*<PRE>(.*)$/i", $line, $m)) {
$line = $m[0] . "\n";
$foundLogStart = true;
} else {
continue;
}
}
if (preg_match("/(REFTEST (?:TEST-UNEXPECTED| *IMAGE|number of).*)/", $line, $m)) {
$line = $m[0] . "\n";
$line = strip_tags($line);
$lines[] = $line;
}
} else {
if (!$foundSummaryStart) {
if (preg_match_all("/Build Error Summary.*<PRE>(.*)$/i", $line, $m)) {
$foundSummaryStart = true;
$line = $m[1][0] . "\n";
$line = strip_tags($line);
$lines[] = $line;
if (!$starred)
processLine($lines, $line);
}
if (strlen($signature) == 0 &&
preg_match("#<b>(.*\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})</b>#i", $line, $matches)) {
$signature = $matches[1];
}
} else {
if (preg_match("/Build Error Log/i", $line))
break;
$line = strip_tags($line);
$lines[] = $line;
if (!$starred)
processLine($lines, $line);
}
}
} else {
usleep(80 * 1000);
}
}
fclose($fp);
$summary = $fileExistedAfterAll ? file_get_contents($file) : implode($lines);
if (!file_exists($file) && !$isStillRunning)
file_put_contents($file, $summary);
return $summary;
}
function generateSuggestion($bug, $line) {
global $signature;
$bug->summary = htmlspecialchars($bug->summary);
$line = htmlspecialchars(strip_tags($line));
return "<span data-bugid=\"$bug->id\" " .
"data-summary=\"$bug->summary\" " .
"data-signature=\"$signature\" " .
"data-logline=\"$line\" " .
"data-status=\"$bug->status $bug->resolution\"" .
"></span>\n";
}
function processLine(&$lines, $line) {
$tokens = preg_split("/\s\\|\s/", $line);
if (count($tokens) < 3)
return;
// The middle path has the test file path.
$testPath = $tokens[1];
$parts = preg_split("/[\\/\\\\]/", $testPath);
if (count($parts) < 2 &&
preg_match('/^leaked/i', $tokens[2])) {
$lines[] = "<a href=\"leak-analysis/?id=" . $_GET["id"] .
"&tree=" . $_GET["tree"] . "\" target=\"_blank\">Analyze the leak.</a>";
return;
}
// Get the file name.
$fileName = end($parts);
$bugs = getBugsForTestFailure($fileName);
foreach ($bugs as $bug) {
$lines[] = generateSuggestion($bug, $line);
}
}
function parseJSON($json) {
require_once 'inc/JSON.php';
$engine = new Services_JSON();
return $engine->decode($json);
}
$bugsCache = array();
function getBugsForTestFailure($fileName) {
if ($fileName == '') {
return array();
}
global $bugsCache;
if (isset($bugsCache[$fileName]))
return array();
if ($fileName == 'automation.py') {
// This won't generate any useful suggestions, see bug 570174
return array();
}
$bugs_json = file_get_contents("https://api-dev.bugzilla.mozilla.org/latest/bug?whiteboard=orange&summary=" . urlencode($fileName));
if ($bugs_json !== false) {
$bugs = parseJSON($bugs_json);
if (isset($bugs->bugs)) {
$bugsCache[$fileName] = $bugs->bugs;
return $bugs->bugs;
}
}
return array();
}