Skip to content

Commit

Permalink
#586466 by anarcat. Drush.php is no longer directly executable
Browse files Browse the repository at this point in the history
  • Loading branch information
massgov-outsider committed Nov 10, 2009
1 parent 065f77c commit b6af786
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 28 deletions.
22 changes: 20 additions & 2 deletions commands/simpletest/simpletest.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ function simpletest_drush_command() {
'drupal dependencies' => array('simpletest'),
'core' => array('6','7'),
);
$items['test drush'] = array(
'callback' => 'drush_test_drush',
'description' => 'Run drush-specific tests',
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
);
return $items;
}

Expand All @@ -61,10 +66,9 @@ function drush_test_mail($recipients) {
return drush_set_error('DRUSH_SIMPLETEST_RUNTESTS_SH', dt('You must copy or symlink run-tests.sh into your /scripts directory beneath Drupal root.'));
}

$php = drush_find_php();
$extra = drush_get_option('extra');
$url = escapeshellarg(url('', array('absolute' => TRUE)));
$exec = $php . " $run_tests --php '" . $php . '\' --url ' . $url . " $extra";
$exec = "$run_tests --php '" . DRUSH_COMMAND . '\' --url ' . $url . " $extra";
drush_shell_exec($exec);
$output = implode("\n", drush_shell_exec_output());
$subject = 'Simpletest results - ' . drush_simpletest_format_results($output);
Expand All @@ -87,3 +91,17 @@ function drush_simpletest_format_results($output) {
return dt('Unknown.');
}
}

/**
* Simple drush self-test procedure
*
* This only tests self-execution for now.
*
* XXX: this needs to be adapted to a testing framework, see:
*
* http://drupal.org/node/483940
*/
function drush_test_drush() {
drush_log(dt("Invoking %drush help in a subprocess", array('%drush' => DRUSH_COMMAND)));
drush_backend_invoke('help', array(), 'GET', FALSE);
}
6 changes: 3 additions & 3 deletions drush
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ elif [ -f /Applications/xampp/xamppfiles/bin/php ]; then # XAMPP on OS X
/Applications/xampp/xamppfiles/bin/php $SCRIPT_PATH "$@"
else
# We check for a command line (cli) version of php, and if found use that.
/usr/bin/env php-cli -v &> /dev/null
which php-cli >/dev/null 2>&1
if [ "$?" = 0 ] ; then
/usr/bin/env php-cli $SCRIPT_PATH "$@"
php-cli $SCRIPT_PATH "$@"
else
# Alternatively we run with straight php, which works on most other systems.
/usr/bin/env php $SCRIPT_PATH "$@"
php $SCRIPT_PATH "$@"
fi
fi
4 changes: 3 additions & 1 deletion drush.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
// $Id$

Expand All @@ -20,7 +21,8 @@
}

define('DRUSH_BASE_PATH', dirname(__FILE__));
define('DRUSH_COMMAND', $GLOBALS['argv'][0]);


define('DRUSH_REQUEST_TIME', microtime(TRUE));

require_once DRUSH_BASE_PATH . '/includes/environment.inc';
Expand Down
6 changes: 1 addition & 5 deletions includes/backend.inc
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,8 @@ function _drush_backend_generate_command($command, $args, &$data, $method = 'GET
foreach ($args as $arg) {
$command .= ' ' . escapeshellarg($arg);
}
$php='';
if (".php" == substr($drush_path, strlen($drush_path) - strlen(".php"))) {
$php = drush_find_php() . ' ';
}
// @TODO: Implement proper multi platform / multi server support.
$cmd = $php . sprintf(escapeshellcmd(" %s %s %s --backend"), escapeshellcmd($drush_path), $option_str, $command);
$cmd = sprintf(escapeshellcmd(" %s %s %s --backend"), escapeshellcmd($drush_path), $option_str, $command);

if (!is_null($hostname)) {
$username = (!is_null($username)) ? $username : get_current_user();
Expand Down
81 changes: 64 additions & 17 deletions includes/environment.inc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ define('DRUSH_TABLE_VERSION', '1.1.3');
*/
define('DRUSH_TABLE_URL', 'http://cvs.php.net/viewvc.cgi/pear/Console_Table/Table.php?revision=1.28&view=co');

/**
*
*/
define('DRUSH_COMMAND', drush_find_drush());

/**
* Helper function listing phases.
*
Expand Down Expand Up @@ -915,24 +920,66 @@ function drush_valid_db_credentials() {
}
}

// Copied from run-tests.sh
function drush_find_php() {
// Determine location of php command automatically, unless a command line argument is supplied.
if (!$php = drush_get_option('php')) {
if (!empty($_ENV['_'])) {
// '_' is an environment variable set by the shell. It contains the command that was executed.
$php = $_ENV['_'];
}
elseif (!empty($_ENV['SUDO_COMMAND'])) {
// 'SUDO_COMMAND' is an environment variable set by the sudo program.
// Extract only the PHP interpreter, not the rest of the command.
list($php, ) = explode(' ', $_ENV['SUDO_COMMAND'], 2);
}
else {
drush_set_error('DRUSH_PHP_PATH_NOT_FOUND', dt('Unable to automatically determine the path to the PHP interpreter. Please supply the --php argument.'));
}
/**
* Determine a proper way to call drush again
*
* This check if we were called directly or as an argument to some
* wrapper command (php and sudo are checked now).
*
* Calling ./drush.php directly yields the following environment:
*
* _SERVER["argv"][0] => ./drush.php
* _SERVER["_"] => /home/anarcat/dist/drush/./drush.php
*
* Calling php ./drush.php yields the following:
*
* _SERVER["argv"][0] => drush.php
* _SERVER["_"] => /usr/bin/php
*
* Calling env php drush.php yields the following:
*
* _SERVER["argv"][0] => drush.php
* _SERVER["_"] => /usr/bin/env
*
* The latter is a problem as there is no way to find the PHP
* executable then. We consider it's the job of the shell script
* wrapper to do that fiddling around and we will therefore assume
* that $argv[0] is a proper PHP binary.
*
* Calling sudo php drush.php yields the following:
*
* _SERVER["argv"][0] => drush.php
* _SERVER["SUDO_COMMAND"] => /usr/bin/php drush.php [arguments]
*
* Notice how _ is missing when running in sudo.
*
* Therefore, we assume that if $_ finishes with $argv[0], we can call
* it directly again. Otherwise, we assume that $_ is the php binary
* and $argv[0] is the path to drush.php. We will use __FILE__,
* however, since we do not want to rely on (potentially) relative
* paths.
*
* The DRUSH_COMMAND constant is initialised to the value of this
* function when environment.inc is loaded.
*
* @see DRUSH_COMMAND
*/
function drush_find_drush() {
if (!empty($_ENV['SUDO_COMMAND']) && empty($_SERVER['_'])) {
// 'SUDO_COMMAND' is an environment variable set by the sudo program.
// Extract only the PHP interpreter, not the rest of the command.
list($_SERVER['_'], ) = explode(' ', $_ENV['SUDO_COMMAND'], 2);
}
if (substr($_SERVER['_'], -strlen($_SERVER['argv'][0])) == $_SERVER['argv'][0]) {
// $GLOBALS['_'] and $GLOBALS['argv'][0] both finish with the same,
// which means we were called directly
$drush = realpath($_SERVER['_']);
} else {
// not called directly, we were called as an argument to PHP, which
// should be in $GLOBALS['_']
$drush = $_SERVER['_'] . ' ' . realpath($_SERVER['argv'][0]);
}
return $php;
return $drush;
}

/**
Expand Down

0 comments on commit b6af786

Please sign in to comment.