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
|
<?php
function execute_command_with_all($description, $command, $vital=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 && $vital) {
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_vital_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);
debug('log_msg creating task... this is bad');
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, $vital=true) {
global $task;
if ($istask=!isset($task)) {
start_internal_task($msg);
} else {
log_msg($msg, false);
}
$status=is_string($cmd)?eval((strpos($cmd, 'return') === false?'return ':'').$cmd):$cmd;
if ($istask)
end_internal_task($status?0:1);
else
log_msg("... ".($status?color('[success]', 'green'):color('[failure]', 'red')));
if ($vital && $status === false)
throw new Exception('Failed');
return $status;
}
function makedirs() {
for ($i=0; $i<func_num_args(); $i++) {
$dir=func_get_arg($i);
if (is_array($dir)) {
call_user_func('makedirs', $dir);
} else {
makedir($dir);
}
}
}
function makedir($dir) {
global $workdir;
if (substr($dir, 0, 1) != '/')
$dir="$workdir/$dir";
if (!is_dir($dir))
log_status('Create '.$dir, mkdir($dir, 0700, true));
}
?>
|