Index: drush =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/drush,v retrieving revision 1.9 diff -u -p -r1.9 drush --- drush 13 Oct 2009 04:36:25 -0000 1.9 +++ drush 9 Nov 2009 19:09:00 -0000 @@ -39,11 +39,11 @@ elif [ -f /Applications/xampp/xamppfiles /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 Index: drush.php =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/drush.php,v retrieving revision 1.76 diff -u -p -r1.76 drush.php --- drush.php 4 Nov 2009 22:29:52 -0000 1.76 +++ drush.php 9 Nov 2009 19:09:00 -0000 @@ -1,3 +1,4 @@ +#!/usr/bin/env php 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; } @@ -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); @@ -87,3 +91,17 @@ function drush_simpletest_format_results 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); +} \ No newline at end of file Index: includes/backend.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/backend.inc,v retrieving revision 1.20 diff -u -p -r1.20 backend.inc --- includes/backend.inc 4 Nov 2009 22:29:52 -0000 1.20 +++ includes/backend.inc 9 Nov 2009 19:09:00 -0000 @@ -352,12 +352,8 @@ function _drush_backend_generate_command 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(); Index: includes/environment.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/environment.inc,v retrieving revision 1.51 diff -u -p -r1.51 environment.inc --- includes/environment.inc 4 Nov 2009 22:29:52 -0000 1.51 +++ includes/environment.inc 9 Nov 2009 19:09:00 -0000 @@ -112,6 +112,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. * * For commands that need to iterate through the phases, such as help @@ -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; } /**