summaryrefslogtreecommitdiff
blob: f35e8c9d8b04ca8e7a8853e3d1cfce9c5b5792e5 (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
<?php
function task_get_order() {
	global $build;
	static $buildid=null;
	static $order=null;
	if ($build->id === $buildid) {
		$order++;
	} else {
		$buildid=$build->id;
		$order=0;
	}
	return $order;
}
function buildlog_entry_get_order() {
	global $build, $task;
	static $buildid, $taskorder, $order;
	if ($buildid === $build->id && $taskorder === $task->order) {
		$order++;
	} else {
		$buildid=$build->id;
		$taskorder=$task->order;
		$order=0;
	}
	return $order;
}
function execute_command_with_all($description, $command, $fatal=true, $path=null, $env=null) {
	global $build, $task;
	if (isset($task))
		end_internal_task();
	$default_env=array(
		'PATH' => $_ENV['PATH']
	);		
	$env=is_array($env)?array_merge($default_env, $env):$default_env;
	$task=new sql_task($build->id, task_get_order(), 'exec', $description, $command);
	$result=$task->execute($path, $env);
	unset($GLOBALS['task']);
	if ($result != 0 && $fatal) {
		if ($result > 0)
			throw_exception($command.' returned with exit status '.$result);
		elseif ($result == -128)
			throw_exception($command.' received an unknown signal');
		else
			throw_exception($command.' received signal '.-$result);
	}
	return $result;
}
function execute_command($desc, $cmd) {
	return execute_command_with_all($desc, $cmd, true, null, null);
}
function execute_command_with_env($desc, $cmd, $env) {
	return execute_command_with_all($desc, $cmd, true, null, $env);
}
function execute_command_with_path($desc, $cmd, $path) {
	return execute_command_with_all($desc, $cmd, true, $path, null);
}
function execute_non_fatal_command($desc, $cmd, $path=null, $env=null) {
	return execute_command_with_all($desc, $cmd, false, $path, $env);
}
function start_internal_task($desc) {
	global $build, $task;
	if (isset($task))
		end_internal_task();
	debug($desc);
	$task=new sql_task($build->id, task_get_order(), 'internal', $desc);
	$task->start=time();
	$task->write();
}
function end_internal_task($status=0) {
	global $task;
	if (isset($task)) {
		if (!isset($task->finish))
			$task->finish=time();
		if (!isset($task->exit))
			$task->exit=$status;
		$task->write();
		unset($GLOBALS['task']);
	}
}
function log_msg($msg, $nl=true) {
	global $build, $task;
	if (!isset($task)) {
		start_internal_task($msg);
		return;
	}
	$msg.=$nl?"\n":'';
	debug($msg);
	$entry=new sql_buildlog_entry($build->id, $task->order, buildlog_entry_get_order(), time(), 'system', $msg);
	$entry->write();
}
function log_status($msg, $cmd) {
	start_internal_task($msg);
	$status=is_string($cmd)?eval((strpos($cmd, 'return') === false?'return ':'').$cmd):$cmd;
	end_internal_task($status?0:1);
	debug("... ".($status?color('[success]', 'green'):color('[failure]', 'red')));
	return $status;
}
?>