-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.php
361 lines (308 loc) · 8.1 KB
/
helpers.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/*
* This file is part of the Eventum (Issue Tracking System) package.
*
* @copyright (c) Eventum Team
* @license GNU General Public License, version 2 or later (GPL-2+)
*
* For the full copyright and license information,
* please see the COPYING and AUTHORS files
* that were distributed with this source code.
*/
/**
* Execute command
*
* @param string $command
* @return array command output each line as array element
* @throw RuntimeException throw exception if exits with non-zero
*/
function execx($command)
{
exec($command, $output, $rc);
if ($rc) {
throw new RuntimeException("$command exited with $rc");
}
return $output;
}
/**
* Execute command, returning first line from it
*
* @param string $command
* @return string
*/
function execl($command)
{
$output = execx($command);
return current($output);
}
/**
* Submit SCM commit data to Eventum.
*
* @param array $params
*/
function scm_ping($params)
{
global $PROGRAM, $eventum_url;
$ping_url = $eventum_url . 'scm_ping.php?scm=' . $params['scm'];
$status = json_post($ping_url, $params, 1);
if ($status['code']) {
throw new RuntimeException($status['message'], $status['code']);
}
$message = trim($status['message']);
if (!$message) {
return;
}
// prefix response with our name
foreach (explode("\n", $message) as $line) {
echo "$PROGRAM: $line\n";
}
}
/**
* Extract dir and file name from abspath
*
* @param string $abspath
* @return array file dirname and basename
*/
function fileparts($abspath)
{
// special for "dirname/" case, pathinfo would set dir to '.' and filename to 'dirname'
$length = strlen($abspath);
if ($abspath[$length - 1] === '/') {
return array(rtrim($abspath, '/'), '');
}
$fi = pathinfo($abspath);
return array($fi['dirname'], $fi['basename']);
}
/**
* parse the commit message and get all issue numbers we can find
*
* @param string $commit_msg
* @return array
*/
function match_issues($commit_msg)
{
preg_match_all('/(?:issue|bug) ?:? ?#?(\d+)/i', $commit_msg, $matches);
if (count($matches[1]) > 0) {
return $matches[1];
}
return null;
}
/**
* Fetch $url, return response and optionally unparsed headers array.
*
* @param string $url URL to request
* @param array $params QueryString parameters to URL
* @param bool $headers = false
* @return mixed
* @return array|string
* @author Elan Ruusamäe <[email protected]>
*/
function wget($url, $params, $headers = true)
{
$url .= '?' . http_build_query($params, null, '&');
// see if we can fopen
$flag = ini_get('allow_url_fopen');
if (!$flag) {
throw new RuntimeException('allow_url_fopen is disabled');
}
// see if https is supported
$scheme = parse_url($url, PHP_URL_SCHEME);
if (!in_array($scheme, stream_get_wrappers(), true)) {
throw new RuntimeException("$scheme:// scheme not supported. Load openssl php extension?");
}
$fp = @fopen($url, 'r');
if (!$fp) {
$error = error_get_last();
throw new RuntimeException($error['message']);
}
if ($headers) {
$meta = stream_get_meta_data($fp);
}
$data = '';
while (!feof($fp)) {
$data .= fread($fp, 4096);
}
fclose($fp);
if ($headers) {
return array($meta['wrapper_data'], $data);
}
return $data;
}
/**
* POST json encoded data to $url
*
* @param string $url
* @param array $data
* @param bool $assoc
* @return array|stdClass result with extra 'meta' key
* @author Elan Ruusamäe <[email protected]>
*/
function json_post($url, $data, $assoc = false)
{
// see if schema in url is supported
$scheme = parse_url($url, PHP_URL_SCHEME);
if (!in_array($scheme, stream_get_wrappers())) {
throw new RuntimeException("$scheme:// scheme not supported. Load openssl php extension?");
}
$body = json_encode($data);
$headers = array(
'Expect: ',
'Content-Type: application/json',
'Accept: application/json',
'Content-Length: ' . strlen($body),
);
$options = array(
'method' => 'POST',
'content' => $body,
'header' => implode("\r\n", $headers),
'timeout' => 5.0,
);
$options = array(
// this needs to be 'http', regardless if we post to https://
'http' => $options,
);
$context = stream_context_create($options);
$stream = @fopen($url, 'r', false, $context);
if (!$stream) {
$error = error_get_last();
throw new RuntimeException($error['message']);
}
$result = stream_get_contents($stream);
$meta = stream_get_meta_data($stream);
fclose($stream);
$response = json_decode($result, $assoc);
if (!$response) {
throw new InvalidArgumentException("Unable to decode: $result");
}
if ($assoc) {
$response['meta'] = $meta;
$response['raw'] = $result;
} else {
$response->raw = $result;
$response->meta = $meta;
}
return $response;
}
/**
* Sane getopt() ajusted from this post:
* http://php.net/getopt#100573
*/
function _getopt($parameters)
{
global $argv, $argc;
$options = getopt($parameters);
$pruneargv = array();
foreach ($options as $option => $value) {
foreach ($argv as $key => $chunk) {
$regex = '/^' . (isset($option[1]) ? '--' : '-') . $option . '/';
if (($chunk == $value && $argv[$key - 1][0] == '-') || preg_match($regex, $chunk)) {
$pruneargv[] = $key;
}
}
}
while ($key = array_pop($pruneargv)) {
unset($argv[$key]);
}
// renumber $argv to be continuous
$argv = array_values($argv);
// reset $argc to be correct
$argc = count($argv);
return $options;
}
/**
* Static version to get STDIN more than once even for older PHP engines
*/
function getInput()
{
static $stdin;
if ($stdin === null) {
$stdin = stream_get_contents(STDIN);
}
return $stdin;
}
/**
* Retrieve environment variables
* As $_ENV is not reliable (variables_order may not contain E), we use phpinfo() call
*/
function get_all_env()
{
if (PHP_VERSION_ID >= 70100) {
return getenv();
}
ob_start();
phpinfo(INFO_ENVIRONMENT);
$buffer = ob_get_clean();
# parse output like: "CVS_PID => 27518"
preg_match_all('/^(?P<name>[^=]+)\s=>\s+/m', $buffer, $m);
# we use getenv() to get "raw" value of env
$env = array();
foreach ($m['name'] as $key) {
$value = getenv($key);
if ($value !== false) {
$env[$key] = $value;
}
}
return $env;
}
/**
* Create execution environment context
*
* @param array $argv
* @return array
*/
function create_context(array $argv)
{
return array(
'time' => microtime(true),
'php_version' => PHP_VERSION,
'argv' => $argv,
'cwd' => getcwd(),
'stdin' => getInput(),
'env' => get_all_env(),
);
}
/**
* Store execution environment details to temp file so the failed command could be repeated
*
* @param array $context
* @return bool|string
*/
function store_environment(array $context)
{
global $PROGRAM;
$tmpfile = tempnam(sys_get_temp_dir(), $PROGRAM);
file_put_contents($tmpfile, serialize($context));
return $tmpfile;
}
/**
* @param array $argv
* @return string
*/
function save_environment($argv)
{
return store_environment(create_context($argv));
}
/**
* Load context from file, and adjust environment accordingly.
*
* @param string $dump_file
* @return array
*/
function load_context($dump_file)
{
$contents = file_get_contents($dump_file);
if ($contents === false) {
throw new RuntimeException("Unable to load $dump_file");
}
/** @noinspection UnserializeExploitsInspection */
$context = unserialize($contents);
// backward compatible
if (!isset($context['argv']) && isset($context['command'])) {
$context['argv'] = $context['command'];
}
// backward compatible
if (!isset($context['program'])) {
$context['program'] = basename($context['argv'][0], '.php');
}
return $context;
}