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
|
<?php
/**
* $Id: 2c92cd320562d21560dff67ae0b5697bff865de8 $
*
* 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: 2c92cd320562d21560dff67ae0b5697bff865de8 $
* @package phing.tasks.system
*/
class ExecTask extends Task
{
/**
* 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;
/**
* Operating system.
* @var string
*/
protected $os;
/**
* Whether to escape shell command using escapeshellcmd().
* @var boolean
*/
protected $escape = false;
/**
* Where to direct output.
* @var File
*/
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 File
*/
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;
public function __construct()
{
$this->commandline = new Commandline();
}
/**
* Main method: wraps execute() command.
*
* @return void
*/
public function main()
{
if (!$this->isApplicable()) {
return;
}
$this->prepare();
$this->buildCommand();
list($return, $output) = $this->executeCommand();
$this->cleanup($return, $output);
}
/**
* Checks whether the command shall be executed
*
* @return boolean False if the exec command shall not be run
*/
protected function isApplicable()
{
if ($this->os === null) {
return true;
}
$myos = Phing::getProperty('os.name');
$this->log('Myos = ' . $myos, Project::MSG_VERBOSE);
if (strpos($this->os, $myos) !== false) {
// this command will be executed only on the specified OS
// OS matches
return true;
}
$this->log(
sprintf(
'Operating system %s not found in %s',
$myos, $this->os
),
Project::MSG_VERBOSE
);
return false;
}
/**
* Prepares the command building and execution, i.e.
* changes to the specified directory.
*
* @return void
*/
protected function prepare()
{
if ($this->dir === null) {
return;
}
// expand any symbolic links first
if (!$this->dir->getCanonicalFile()->isDirectory()) {
throw new BuildException(
"'" . (string) $this->dir . "' is not a valid directory"
);
}
$this->currdir = getcwd();
@chdir($this->dir->getPath());
}
/**
* Builds the full command to execute and stores it in $command.
*
* @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 = array();
$return = null;
if ($this->passthru) {
passthru($this->realCommand, $return);
} else {
exec($this->realCommand, $output, $return);
}
return array($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
*
* @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)
);
}
if ($return != 0 && $this->checkreturn) {
throw new BuildException("Task exited with code $return");
}
}
/**
* 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 muliple 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;
}
/**
* 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 = (bool) $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 = (bool) $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 = (bool) $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 = (bool) $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
*
* @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();
}
}
|