aboutsummaryrefslogtreecommitdiff
path: root/web/import.inc.php
blob: 7ff005431b1cef0c1855701154e03d471d61f4a7 (plain)
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
<?php
include(dirname(__FILE__) . "/../web/config.inc.php");
include(dirname(__FILE__) . "/../web/db.inc.php");

function parse_config($path) {
	$opts_set = array();
	$opts_unset = array();

	$configf = fopen($path, "r");
	while (!feof($configf)) {
		$line = trim(fgets($configf));
		if (!strncmp($line, "BR2_", strlen("BR2_"))) {
			preg_match("/(BR2_[a-zA-Z0-9_]*)=(.*)/", $line, $matches);
			$name = $matches[1];
			$value = str_replace('"', '', $matches[2]);
			$opts_set[$name] = $value;
		}
		else if (!strncmp($line, "# BR2_", strlen("# BR2_"))) {
			preg_match("/# (BR2_[a-zA-Z0-9_]*) is not set/", $line, $matches);
			$name = $matches[1];
			$opts_unset[$name] = "";
		}
	}

	fclose($configf);

	return array($opts_set, $opts_unset);
}

function insert_all_symbols($db, $opts)
{
	$sql = "insert ignore into config_symbol (name, value) values ";

	$opts_array = array_map(function($k, $v) {
			return "(\"" . $k . "\", \"" . $v . "\")";
		}, array_keys($opts), $opts);

	$sql .= join(",", $opts_array) . ";";

	$db->query($sql);
}

function get_all_symbols_id($db, $opts)
{
	$sql = "select id from config_symbol where ";

	$opts_array = array_map(function($k, $v) {
			return "(name=\"" . $k . "\" and value=\"" . $v . "\")";
		}, array_keys($opts), $opts);

	$sql .= join(" or ", $opts_array) . ";";

	$result = $db->query($sql);

	$symbolids = array();
	while($row = mysql_fetch_array($result)) {
		array_push($symbolids, $row["id"]);
	}

	return $symbolids;
}

function insert_symbol_result($db, $resultid, $symbolsid)
{
	$sql = "insert into symbol_per_result (result_id, symbol_id) values ";
	$symarray = array_map(function($v) use ($resultid) {
			return "(" . $resultid . "," . $v . ")";
		}, $symbolsid);
	$sql .= join(",", $symarray) . ";";
	$db->query($sql);
}

function insert_config($db, $resultid, $opts_set)
{
	/* Only keep the options with non-empty values */
	$opts = array_filter($opts_set, function($v) {
			return $v != "";
		});
	insert_all_symbols($db, $opts);
	$symbolsid = get_all_symbols_id($db, $opts);
	insert_symbol_result($db, $resultid, $symbolsid);
}

function get_duration($buildtimefile)
{
	$times = file($buildtimefile);

	$firstline = explode(":", $times[0]);
	$lastline  = explode(":", $times[count($times)-1]);

	$start = $firstline[0];
	$end   = $lastline[0];

	return $end - $start;
}

function import_result($buildid, $filename)
{
    global $maindir;
    $buildresultdir = $maindir . "/results/";
    $tmpbuildresultdir = $buildresultdir . "tmp/";

    echo "Importing $buildid from $filename\n";

    $finalbuildresultdir = $buildresultdir . "/" . substr($buildid, 0, 3) . "/";

    /* Check that we don't have a build result with the same SHA1 */
    if (file_exists($finalbuildresultdir . $buildid)) {
      echo "We already have a build result with the same SHA1, sorry.\n";
      return;
    }

    /* Create the temporary directory where the tarball will be
       extracted */
    $thisbuildtmpdir = $tmpbuildresultdir . $buildid . "/";
    if (! mkdir($thisbuildtmpdir)) {
      echo "Cannot create temporary directory.\n";
      return;
    }

    /* Extract the tarball into the temporary directory */
    $tarcmd = "tar -C " . $thisbuildtmpdir . " --strip-components=1 -xf " . $filename;
    system($tarcmd, $retval);
    if ($retval != 0) {
      echo "Unable to uncompress build report file\n";
      return;
    }

    /* Perform some tests */
    if (! file_exists($thisbuildtmpdir . "status") ||
	! file_exists($thisbuildtmpdir . "gitid")  ||
	! file_exists($thisbuildtmpdir . "build-end.log") ||
	! file_exists($thisbuildtmpdir . "config") ||
	! file_exists($thisbuildtmpdir . "build-time.log") ||
	! file_exists($thisbuildtmpdir . "submitter")) {
      system("rm -rf " . $thisbuildtmpdir);
      echo "Invalid contents of the build report file\n";
      return;
    }

    system("grep -q 'fork: Cannot allocate memory' " . $thisbuildtmpdir . "build-end.log",
	   $retval);
    if ($retval == 0) {
      echo "Reject build result, there was a memory allocation problem\n";
      return;
    }

    /* Remove the build.log.bz2 file if it's in there */
    system("rm -f " . $thisbuildtmpdir . "build.log.bz2", $retval);

    /* Create the 'results/xyz/' directory if it doesn't already
       exists */
    if (! file_exists($finalbuildresultdir)) {
      if (! mkdir($finalbuildresultdir)) {
	system("rm -rf " . $thisbuildtmpdir);
	echo "Cannot create final output directory.\n";
	return;
      }
    }

    /* Move to the final location */
    echo "mv " . $thisbuildtmpdir . " " . $finalbuildresultdir . "\n";
    system("mv " . $thisbuildtmpdir . " " . $finalbuildresultdir, $retval);
    if ($retval != 0) {
      system("rm -rf " . $thisbuildtmpdir);
      echo "Unable to move build results to the final location";
      return;
    }

    $thisbuildfinaldir = $finalbuildresultdir . "/" . $buildid . "/";

    /* Get the status */
    $status_str = trim(file_get_contents($thisbuildfinaldir . "status", "r"));
    if ($status_str == "OK")
      $status = 0;
    else if ($status_str == "NOK")
      $status = 1;
    else if ($status_str == "TIMEOUT")
      $status = 2;

    /* Get the build date (use the mtime of the status file */
    $status_stat = stat($thisbuildfinaldir . "status");
    $builddate = strftime("%Y-%m-%d %H:%M:%S", $status_stat['mtime']);

    /* Get submitter, commitid, duration */
    $submitter  = trim(file_get_contents($thisbuildfinaldir . "submitter", "r"));
    $commitid  = trim(file_get_contents($thisbuildfinaldir . "gitid", "r"));
    $duration = get_duration($thisbuildfinaldir . "build-time.log");

    list($opts_set, $opts_unset) = parse_config($thisbuildfinaldir . "config");

    $arch = $opts_set["BR2_ARCH"];
    $subarch = "";

    if (array_key_exists("BR2_GCC_TARGET_CPU", $opts_set))
	    $subarch = $opts_set["BR2_GCC_TARGET_CPU"];
    else if (array_key_exists("BR2_GCC_TARGET_ARCH", $opts_set))
	    $subarch = $opts_set["BR2_GCC_TARGET_ARCH"];

    $found_libc = "";
    foreach (array("glibc", "uclibc", "musl") as $libc) {
	    if (array_key_exists("BR2_TOOLCHAIN_USES_" . strtoupper($libc), $opts_set)) {
		    $found_libc = $libc;
		    break;
	    }
    }

    $static = 0;
    if (array_key_exists("BR2_STATIC_LIBS", $opts_set))
	    $static = 1;

    if ($status == 0)
      $reason = "none";
    else {
	$tmp = Array();
	exec("tail -3 " . $thisbuildfinaldir . "build-end.log | grep -v '\[_all\]' | grep 'make.*: \*\*\*' | sed 's,.*\[\([^\]*\)\] Error.*,\\1,' | sed 's,.*/build/\([^/]*\)/.*,\\1,'", $tmp);
	if (trim($tmp[0]))
	  $reason = $tmp[0];
	else
	  $reason = "unknown";
    }

    $db = new db();

    /* Insert into the database */
    $sql = "insert into results (status, builddate, submitter, commitid, identifier, arch, reason, libc, static, subarch, duration) values (" .
      $db->quote_smart($status) . "," .
      $db->quote_smart($builddate) . "," .
      $db->quote_smart($submitter) . "," .
      $db->quote_smart($commitid) . "," .
      $db->quote_smart($buildid) . "," .
      $db->quote_smart($arch) . "," .
      $db->quote_smart($reason) . "," .
      $db->quote_smart($found_libc) . "," .
      $db->quote_smart($static) . "," .
      $db->quote_smart($subarch) . "," .
      $db->quote_smart($duration) .
    ")";

    $ret = $db->query($sql);
    if ($ret == FALSE) {
      echo "Couldn't register result in DB\n";
      system("rm -rf " . $thisbuildfinaldir);
      return;
    }

    $resultdbid = $db->insertid();

    insert_config($db, $resultdbid, $opts_set);

    echo "Build result accepted. Thanks!";
}

?>