/
ExecTask.php
600 lines (537 loc) · 15.1 KB
/
ExecTask.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
/**
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/Task.php';
/**
* Executes a command on the shell.
*
* @author Andreas Aderhold <andi@binarycloud.com>
* @author Hans Lellelid <hans@xmpl.org>
* @author Christian Weiske <cweiske@cweiske.de>
* @version $Id$
* @package phing.tasks.system
*/
class ExecTask extends Task
{
const INVALID = PHP_INT_MAX;
private $exitValue = self::INVALID;
/**
* Command to be executed
* @var string
*/
protected $realCommand;
/**
* Given command
* @var string
*/
protected $command;
/**
* Commandline managing object
*
* @var Commandline
*/
protected $commandline;
/**
* Working directory.
* @var PhingFile
*/
protected $dir;
protected $currdir;
/**
* Operating system.
* @var string
*/
protected $os;
/**
* Whether to escape shell command using escapeshellcmd().
* @var boolean
*/
protected $escape = false;
/**
* Where to direct output.
* @var PhingFile
*/
protected $output;
/**
* Whether to use PHP's passthru() function instead of exec()
* @var boolean
*/
protected $passthru = false;
/**
* Whether to log returned output as MSG_INFO instead of MSG_VERBOSE
* @var boolean
*/
protected $logOutput = false;
/**
* Logging level for status messages
* @var integer
*/
protected $logLevel = Project::MSG_VERBOSE;
/**
* Where to direct error output.
* @var PhingFile
*/
protected $error;
/**
* If spawn is set then [unix] programs will redirect stdout and add '&'.
* @var boolean
*/
protected $spawn = false;
/**
* Property name to set with return value from exec call.
*
* @var string
*/
protected $returnProperty;
/**
* Property name to set with output value from exec call.
*
* @var string
*/
protected $outputProperty;
/**
* Whether to check the return code.
* @var boolean
*/
protected $checkreturn = false;
private $osFamily;
/**
*
*/
public function __construct()
{
$this->commandline = new Commandline();
}
/**
* Main method: wraps execute() command.
*
* @return void
*/
public function main()
{
if (!$this->isValidOs()) {
return;
}
$this->prepare();
$this->buildCommand();
list($return, $output) = $this->executeCommand();
$this->cleanup($return, $output);
}
/**
* Prepares the command building and execution, i.e.
* changes to the specified directory.
*
* @throws BuildException
* @return void
*/
protected function prepare()
{
if ($this->dir === null) {
return;
}
// expand any symbolic links first
try {
if (!$this->dir->getCanonicalFile()->isDirectory()) {
throw new BuildException(
"'" . (string) $this->dir . "' is not a valid directory"
);
}
} catch (IOException $e) {
throw new BuildException(
"'" . (string) $this->dir . "' is not a readable directory"
);
}
$this->currdir = getcwd();
@chdir($this->dir->getPath());
}
/**
* Builds the full command to execute and stores it in $command.
*
* @throws BuildException
* @return void
* @uses $command
*/
protected function buildCommand()
{
if ($this->command === null && $this->commandline->getExecutable() === null) {
throw new BuildException(
'ExecTask: Please provide "command" OR "executable"'
);
} else {
if ($this->command === null) {
$this->realCommand = Commandline::toString($this->commandline->getCommandline(), $this->escape);
} else {
if ($this->commandline->getExecutable() === null) {
$this->realCommand = $this->command;
//we need to escape the command only if it's specified directly
// commandline takes care of "executable" already
if ($this->escape == true) {
$this->realCommand = escapeshellcmd($this->realCommand);
}
} else {
throw new BuildException(
'ExecTask: Either use "command" OR "executable"'
);
}
}
}
if ($this->error !== null) {
$this->realCommand .= ' 2> ' . escapeshellarg($this->error->getPath());
$this->log(
"Writing error output to: " . $this->error->getPath(),
$this->logLevel
);
}
if ($this->output !== null) {
$this->realCommand .= ' 1> ' . escapeshellarg($this->output->getPath());
$this->log(
"Writing standard output to: " . $this->output->getPath(),
$this->logLevel
);
} elseif ($this->spawn) {
$this->realCommand .= ' 1>/dev/null';
$this->log("Sending output to /dev/null", $this->logLevel);
}
// If neither output nor error are being written to file
// then we'll redirect error to stdout so that we can dump
// it to screen below.
if ($this->output === null && $this->error === null && $this->passthru === false) {
$this->realCommand .= ' 2>&1';
}
// we ignore the spawn boolean for windows
if ($this->spawn) {
$this->realCommand .= ' &';
}
}
/**
* Executes the command and returns return code and output.
*
* @return array array(return code, array with output)
*/
protected function executeCommand()
{
$this->log("Executing command: " . $this->realCommand, $this->logLevel);
$output = [];
$return = null;
if ($this->passthru) {
passthru($this->realCommand, $return);
} else {
exec($this->realCommand, $output, $return);
}
return [$return, $output];
}
/**
* Runs all tasks after command execution:
* - change working directory back
* - log output
* - verify return value
*
* @param integer $return Return code
* @param array $output Array with command output
*
* @throws BuildException
* @return void
*/
protected function cleanup($return, $output)
{
if ($this->dir !== null) {
@chdir($this->currdir);
}
$outloglevel = $this->logOutput ? Project::MSG_INFO : Project::MSG_VERBOSE;
foreach ($output as $line) {
$this->log($line, $outloglevel);
}
if ($this->returnProperty) {
$this->project->setProperty($this->returnProperty, $return);
}
if ($this->outputProperty) {
$this->project->setProperty(
$this->outputProperty,
implode("\n", $output)
);
}
$this->setExitValue($return);
if ($return != 0 && $this->checkreturn) {
throw new BuildException("Task exited with code $return");
}
}
/**
* Set the exit value.
*
* @param int $value exit value of the process.
*/
protected function setExitValue($value)
{
$this->exitValue = $value;
}
/**
* Query the exit value of the process.
*
* @return int the exit value or self::INVALID if no exit value has
* been received.
*/
public function getExitValue()
{
return $this->exitValue;
}
/**
* The command to use.
*
* @param mixed $command String or string-compatible (e.g. w/ __toString()).
*
* @return void
*/
public function setCommand($command)
{
$this->command = "" . $command;
}
/**
* The executable to use.
*
* @param mixed $executable String or string-compatible (e.g. w/ __toString()).
*
* @return void
*/
public function setExecutable($executable)
{
$this->commandline->setExecutable((string) $executable);
}
/**
* Whether to use escapeshellcmd() to escape command.
*
* @param boolean $escape If the command shall be escaped or not
*
* @return void
*/
public function setEscape($escape)
{
$this->escape = (bool) $escape;
}
/**
* Specify the working directory for executing this command.
*
* @param PhingFile $dir Working directory
*
* @return void
*/
public function setDir(PhingFile $dir)
{
$this->dir = $dir;
}
/**
* Specify OS (or multiple OS) that must match in order to execute this command.
*
* @param string $os Operating system string (e.g. "Linux")
*
* @return void
*/
public function setOs($os)
{
$this->os = (string) $os;
}
/**
* List of operating systems on which the command may be executed.
*/
public function getOs()
{
return $this->os;
}
/**
* Restrict this execution to a single OS Family
* @param string $osFamily the family to restrict to.
*/
public function setOsFamily($osFamily)
{
$this->osFamily = strtolower($osFamily);
}
/**
* Restrict this execution to a single OS Family
*/
public function getOsFamily()
{
return $this->osFamily;
}
/**
* File to which output should be written.
*
* @param PhingFile $f Output log file
*
* @return void
*/
public function setOutput(PhingFile $f)
{
$this->output = $f;
}
/**
* File to which error output should be written.
*
* @param PhingFile $f Error log file
*
* @return void
*/
public function setError(PhingFile $f)
{
$this->error = $f;
}
/**
* Whether to use PHP's passthru() function instead of exec()
*
* @param boolean $passthru If passthru shall be used
*
* @return void
*/
public function setPassthru($passthru)
{
$this->passthru = $passthru;
}
/**
* Whether to log returned output as MSG_INFO instead of MSG_VERBOSE
*
* @param boolean $logOutput If output shall be logged visibly
*
* @return void
*/
public function setLogoutput($logOutput)
{
$this->logOutput = $logOutput;
}
/**
* Whether to suppress all output and run in the background.
*
* @param boolean $spawn If the command is to be run in the background
*
* @return void
*/
public function setSpawn($spawn)
{
$this->spawn = $spawn;
}
/**
* Whether to check the return code.
*
* @param boolean $checkreturn If the return code shall be checked
*
* @return void
*/
public function setCheckreturn($checkreturn)
{
$this->checkreturn = $checkreturn;
}
/**
* The name of property to set to return value from exec() call.
*
* @param string $prop Property name
*
* @return void
*/
public function setReturnProperty($prop)
{
$this->returnProperty = $prop;
}
/**
* The name of property to set to output value from exec() call.
*
* @param string $prop Property name
*
* @return void
*/
public function setOutputProperty($prop)
{
$this->outputProperty = $prop;
}
/**
* Set level of log messages generated (default = verbose)
*
* @param string $level Log level
*
* @throws BuildException
* @return void
*/
public function setLevel($level)
{
switch ($level) {
case 'error':
$this->logLevel = Project::MSG_ERR;
break;
case 'warning':
$this->logLevel = Project::MSG_WARN;
break;
case 'info':
$this->logLevel = Project::MSG_INFO;
break;
case 'verbose':
$this->logLevel = Project::MSG_VERBOSE;
break;
case 'debug':
$this->logLevel = Project::MSG_DEBUG;
break;
default:
throw new BuildException(
sprintf('Unknown log level "%s"', $level)
);
}
}
/**
* Creates a nested <arg> tag.
*
* @return CommandlineArgument Argument object
*/
public function createArg()
{
return $this->commandline->createArgument();
}
/**
* Is this the OS the user wanted?
* @return boolean.
* <ul>
* <li>
* <li><code>true</code> if the os and osfamily attributes are null.</li>
* <li><code>true</code> if osfamily is set, and the os family and must match
* that of the current OS, according to the logic of
* {@link Os#isOs(String, String, String, String)}, and the result of the
* <code>os</code> attribute must also evaluate true.
* </li>
* <li>
* <code>true</code> if os is set, and the system.property os.name
* is found in the os attribute,</li>
* <li><code>false</code> otherwise.</li>
* </ul>
*/
protected function isValidOs()
{
//hand osfamily off to Os class, if set
if ($this->osFamily !== null && !OsCondition::isFamily($this->osFamily)) {
return false;
}
//the Exec OS check is different from Os.isOs(), which
//probes for a specific OS. Instead it searches the os field
//for the current os.name
$myos = Phing::getProperty("os.name");
$this->log("Current OS is " . $myos, Project::MSG_VERBOSE);
if (($this->os !== null) && (strpos($this->os, $myos) === false)) {
// this command will be executed only on the specified OS
$this->log("This OS, " . $myos
. " was not found in the specified list of valid OSes: " . $this->os,
Project::MSG_VERBOSE);
return false;
}
return true;
}
}