cvs: pear-core /PEAR Installer.php /PEAR/Command Install.php /PEAR/Frontend CLI.php /PEAR/PackageFile v1.php v2.php /PEAR/Task Common.php
Postinstallscript.php

From: Date: Sun, 06 Feb 2005 21:29:44 +0000
Subject: cvs: pear-core /PEAR Installer.php /PEAR/Command Install.php /PEAR/Frontend CLI.php /PEAR/PackageFile v1.php v2.php /PEAR/Task Common.php
Postinstallscript.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-cvs+get-29158@lists.php.net to get a copy of this message
cellog Sun Feb 6 16:29:44 2005 EDT Modified files: /pear-core/PEAR/Command Install.php /pear-core/PEAR/Frontend CLI.php /pear-core/PEAR Installer.php /pear-core/PEAR/PackageFile v1.php v2.php /pear-core/PEAR/Task Common.php Postinstallscript.php Log: re-implement post-install scripts as "pear run-scripts" command

http://cvs.php.net/diff.php/pear-core/PEAR/Command/Install.php?r1=1.82&r2=1.83&ty=u Index: pear-core/PEAR/Command/Install.php diff -u pear-core/PEAR/Command/Install.php:1.82 pear-core/PEAR/Command/Install.php:1.83 --- pear-core/PEAR/Command/Install.php:1.82 Tue Feb 1 23:57:23 2005 +++ pear-core/PEAR/Command/Install.php Sun Feb 6 16:29:40 2005 @@ -16,11 +16,12 @@ // | Author: Stig S?ther Bakken <ssb@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Install.php,v 1.82 2005/02/02 04:57:23 cellog Exp $ +// $Id: Install.php,v 1.83 2005/02/06 21:29:40 cellog Exp $ require_once "PEAR/Command/Common.php"; require_once "PEAR/Installer.php"; require_once 'PEAR/Downloader.php'; +require_once 'PEAR/PackageFile/v2.php'; /** * PEAR commands for installation or deinstallation/upgrading of @@ -279,6 +280,15 @@ Unpacks a Pecl Package into the selected location. It will download the package if needed. '), + 'run-scripts' => array( + 'summary' => 'Run Post-Install Scripts bundled with a package', + 'function' => 'doRunScripts', + 'shortcut' => 'rs', + 'options' => array( + ), + 'doc' => '<package> +Run post-installation scripts in package <package>, if any exist. +'), ); // }}} @@ -601,5 +611,27 @@ // }}} + function doRunScripts($command, $options, $params) + { + if (!isset($params[0])) { + return $this->raiseError('run-scripts expects 1 parameter: a package name'); + } + $reg = &$this->config->getRegistry(); + PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); + $parsed = $reg->parsePackageName($params[0]); + PEAR::staticPopErrorHandling(); + if (PEAR::isError($parsed)) { + return $this->raiseError($parsed); + } + $package = &$reg->getPackage($parsed['package'], $parsed['channel']); + $package->setConfig($this->config); + if (is_object($package)) { + $package->runPostinstallScripts(); + } else { + return $this->raiseError('Could not retrieve package "' . $params[0] . '" from registry'); + } + $this->ui->outputData('Install scripts complete', $command); + return true; + } } ?> http://cvs.php.net/diff.php/pear-core/PEAR/Frontend/CLI.php?r1=1.45&r2=1.46&ty=u Index: pear-core/PEAR/Frontend/CLI.php diff -u pear-core/PEAR/Frontend/CLI.php:1.45 pear-core/PEAR/Frontend/CLI.php:1.46 --- pear-core/PEAR/Frontend/CLI.php:1.45 Sun Jan 23 22:10:10 2005 +++ pear-core/PEAR/Frontend/CLI.php Sun Feb 6 16:29:42 2005 @@ -16,7 +16,7 @@ | Author: Stig S?ther Bakken <ssb@php.net> | +----------------------------------------------------------------------+ - $Id: CLI.php,v 1.45 2005/01/24 03:10:10 cellog Exp $ + $Id: CLI.php,v 1.46 2005/02/06 21:29:42 cellog Exp $ */ require_once "PEAR/Frontend.php"; @@ -150,15 +150,23 @@ } // }}} + + function runPostinstallScripts(&$scripts) + { + foreach ($scripts as $i => $script) { + $this->runInstallScript($scripts[$i]->_params, $scripts[$i]->_obj); + } + } + /** * @param array $xml contents of postinstallscript tag * @param object $script post-installation script * @param string install|upgrade */ - function runInstallScript($xml, &$script, $installtype) + function runInstallScript($xml, &$script) { if (!is_array($xml) || !isset($xml['paramgroup'])) { - $script->run(array(), '_default', $installtype); + $script->run(array(), '_default'); } else { $completedPhases = array(); if (!isset($xml['paramgroup'][0])) { @@ -205,12 +213,12 @@ $answers = $this->confirmDialog($group['param']); if ($answers) { array_unshift($completedPhases, $group['id']); - if (!$script->run($answers, $group['id'], $installtype)) { - $script->run($completedPhases, '_undoOnError', $installtype); + if (!$script->run($answers, $group['id'])) { + $script->run($completedPhases, '_undoOnError'); return; } } else { - $script->run($completedPhases, '_undoOnError', $installtype); + $script->run($completedPhases, '_undoOnError'); return; } } http://cvs.php.net/diff.php/pear-core/PEAR/Installer.php?r1=1.187&r2=1.188&ty=u Index: pear-core/PEAR/Installer.php diff -u pear-core/PEAR/Installer.php:1.187 pear-core/PEAR/Installer.php:1.188 --- pear-core/PEAR/Installer.php:1.187 Tue Feb 1 23:57:23 2005 +++ pear-core/PEAR/Installer.php Sun Feb 6 16:29:42 2005 @@ -19,7 +19,7 @@ // | Greg Beaver <cellog@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Installer.php,v 1.187 2005/02/02 04:57:23 cellog Exp $ +// $Id: Installer.php,v 1.188 2005/02/06 21:29:42 cellog Exp $ require_once "OS/Guess.php"; require_once 'PEAR/Downloader.php'; @@ -489,15 +489,17 @@ $tag = str_replace($pkg->getTasksNs() . ':', '', $tag); $task = "PEAR_Task_$tag"; $task = &new $task($this->config, $this, PEAR_TASK_INSTALL); - $task->init($raw, $attribs); - $res = $task->startSession($pkg, $contents, $final_dest_file); - if (!$res) { - continue; // skip this file - } - if (PEAR::isError($res)) { - return $res; + if (!$task->isScript()) { // scripts are only handled after installation + $task->init($raw, $attribs); + $res = $task->startSession($pkg, $contents, $final_dest_file); + if (!$res) { + continue; // skip this file + } + if (PEAR::isError($res)) { + return $res; + } + $contents = $res; // save changes } - $contents = $res; // save changes $wp = @fopen($dest_file, "wb"); if (!is_resource($wp)) { return $this->raiseError("failed to create $dest_file: $php_errormsg", @@ -1094,9 +1096,6 @@ $pkg->installedFile($file, $atts); } } - if (PEAR_Task_Common::hasPrecommitTasks()) { - PEAR_Task_Common::runPrecommitTasks(); - } // }}} // {{{ compile and install source files @@ -1121,10 +1120,12 @@ $ret = false; $installphase = 'install'; + $oldversion = false; // {{{ Register that the package is installed ----------------------- if (empty($options['upgrade'])) { // if 'force' is used, replace the info in registry if (!empty($options['force']) && $this->_registry->packageExists($pkgname, $channel)) { + $oldversion = $this->_registry->packageInfo($pkgname, 'version', $channel); $this->_registry->deletePackage($pkgname, $channel); } $ret = $this->_registry->addPackage2($pkg); http://cvs.php.net/diff.php/pear-core/PEAR/PackageFile/v1.php?r1=1.45&r2=1.46&ty=u Index: pear-core/PEAR/PackageFile/v1.php diff -u pear-core/PEAR/PackageFile/v1.php:1.45 pear-core/PEAR/PackageFile/v1.php:1.46 --- pear-core/PEAR/PackageFile/v1.php:1.45 Tue Feb 1 23:57:23 2005 +++ pear-core/PEAR/PackageFile/v1.php Sun Feb 6 16:29:43 2005 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: v1.php,v 1.45 2005/02/02 04:57:23 cellog Exp $ +// $Id: v1.php,v 1.46 2005/02/06 21:29:43 cellog Exp $ require_once 'PEAR/PackageFile/Generator/v1.php'; require_once 'PEAR/ErrorStack.php'; require_once 'Archive/Tar.php'; @@ -339,6 +339,33 @@ // placeholder } + /** + * For saving in the registry. + * + * Set the last version that was installed + * @param string + */ + function setLastInstalledVersion($version) + { + $this->_packageInfo['_lastversion'] = $version; + } + + /** + * @return string|false + */ + function getLastInstalledVersion() + { + if (isset($this->_packageInfo['_lastversion'])) { + return $this->_packageInfo['_lastversion']; + } + return false; + } + + function initPostinstallScripts() + { + return false; + } + function setLogger(&$logger) { if ($logger && (!is_object($logger) || !method_exists($logger, 'log'))) { http://cvs.php.net/diff.php/pear-core/PEAR/PackageFile/v2.php?r1=1.83&r2=1.84&ty=u Index: pear-core/PEAR/PackageFile/v2.php diff -u pear-core/PEAR/PackageFile/v2.php:1.83 pear-core/PEAR/PackageFile/v2.php:1.84 --- pear-core/PEAR/PackageFile/v2.php:1.83 Tue Feb 1 23:57:23 2005 +++ pear-core/PEAR/PackageFile/v2.php Sun Feb 6 16:29:43 2005 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: v2.php,v 1.83 2005/02/02 04:57:23 cellog Exp $ +// $Id: v2.php,v 1.84 2005/02/06 21:29:43 cellog Exp $ require_once 'PEAR/ErrorStack.php'; require_once 'PEAR/Validate.php'; require_once 'PEAR/Dependency2.php'; @@ -334,6 +334,92 @@ } /** + * For saving in the registry. + * + * Set the last version that was installed + * @param string + */ + function setLastInstalledVersion($version) + { + $this->_packageInfo['_lastversion'] = $version; + } + + /** + * @return string|false + */ + function getLastInstalledVersion() + { + if (isset($this->_packageInfo['_lastversion'])) { + return $this->_packageInfo['_lastversion']; + } + return false; + } + + /** + * Initialize post-install scripts for running + * + * This method can be used to detect post-install scripts, as the return value + * indicates whether any exist + * @return bool + */ + function initPostinstallScripts() + { + $filelist = $this->getFilelist(); + $contents = $this->getContents(); + $contents = $contents['dir']['file']; + if (!is_array($contents) || !isset($contents[0])) { + $contents = array($contents); + } + $taskfiles = array(); + foreach ($contents as $file) { + $atts = $file['attribs']; + unset($file['attribs']); + if (count($file)) { + $taskfiles[$atts['name']] = $file; + } + } + $common = new PEAR_Common; + $common->debug = $this->_config->get('verbose'); + $this->_scripts = array(); + foreach ($taskfiles as $name => $tasks) { + $atts = $filelist[$name]; + foreach ($tasks as $tag => $raw) { + $task = $this->getTask($tag); + $task = &new $task($this->_config, $common, PEAR_TASK_INSTALL); + if (!$task->isScript()) { + continue; // scripts are only handled after installation + } + $lastversion = isset($this->_packageInfo['_lastversion']) ? + $this->_packageInfo['_lastversion'] : null; + $task->init($raw, $atts, $lastversion); + $res = $task->startSession($this, $atts['installed_as']); + if (!$res) { + continue; // skip this file + } + if (PEAR::isError($res)) { + return $res; + } + $this->_scripts[] = &$task; + } + } + if (count($this->_scripts)) { + return true; + } + return false; + } + + function runPostinstallScripts() + { + if ($this->initPostinstallScripts()) { + $ui = &PEAR_Frontend::singleton(); + if ($ui) { + $ui->runPostinstallScripts($this->_scripts, $this); + } + } + } + + + /** * Convert a recursive set of <dir> and <file> tags into a single <dir> tag with * <file> tags. */ http://cvs.php.net/diff.php/pear-core/PEAR/Task/Common.php?r1=1.6&r2=1.7&ty=u Index: pear-core/PEAR/Task/Common.php diff -u pear-core/PEAR/Task/Common.php:1.6 pear-core/PEAR/Task/Common.php:1.7 --- pear-core/PEAR/Task/Common.php:1.6 Sun Jan 23 22:10:10 2005 +++ pear-core/PEAR/Task/Common.php Sun Feb 6 16:29:43 2005 @@ -64,18 +64,15 @@ var $installphase; /** * @param PEAR_Config - * @param PEAR_Installer + * @param PEAR_Common */ - function PEAR_Task_Common(&$config, &$installer, $phase) + function PEAR_Task_Common(&$config, &$logger, $phase) { $this->config = &$config; $this->registry = &$config->getRegistry(); - $this->installer = &$installer; + $this->logger = &$logger; $this->installphase = $phase; if ($this->type == 'multiple') { - $GLOBALS['_PEAR_TASK_PREINSTANCES'][get_class($this)][] = &$this; - } - if ($this->type == 'postinstall-multiple') { $GLOBALS['_PEAR_TASK_POSTINSTANCES'][get_class($this)][] = &$this; } } @@ -142,51 +139,18 @@ * @static * @final */ - function hasPrecommitTasks() - { - return isset($GLOBALS['_PEAR_TASK_PREINSTANCES']); - } - - /** - * @static - * @final - */ - function runPrecommitTasks() - { - foreach ($GLOBALS['_PEAR_TASK_PREINSTANCES'] as $class => $tasks) { - $err = call_user_func(array($class, 'run'), - $GLOBALS['_PEAR_TASK_PREINSTANCES'][$class]); - if ($err) { - return PEAR_Task_Common::throwError($err); - } - } - unset($GLOBALS['_PEAR_TASK_PREINSTANCES']); - } - - /** - * @static - * @final - */ function hasPostinstallTasks() { return isset($GLOBALS['_PEAR_TASK_POSTINSTANCES']); } /** - * @param string install or upgrade - * @static - * @final + * Determines whether a role is a script + * @return bool */ - function runPostinstallTasks($installphase) + function isScript() { - foreach ($GLOBALS['_PEAR_TASK_POSTINSTANCES'] as $class => $tasks) { - $err = call_user_func(array($class, 'run'), - $GLOBALS['_PEAR_TASK_POSTINSTANCES'][$class], $installphase); - if ($err) { - return PEAR_Task_Common::throwError($err); - } - } - unset($GLOBALS['_PEAR_TASK_POSTINSTANCES']); + return $this->type == 'script'; } function throwError($msg, $code = -1) http://cvs.php.net/diff.php/pear-core/PEAR/Task/Postinstallscript.php?r1=1.6&r2=1.7&ty=u Index: pear-core/PEAR/Task/Postinstallscript.php diff -u pear-core/PEAR/Task/Postinstallscript.php:1.6 pear-core/PEAR/Task/Postinstallscript.php:1.7 --- pear-core/PEAR/Task/Postinstallscript.php:1.6 Mon Jan 31 00:13:09 2005 +++ pear-core/PEAR/Task/Postinstallscript.php Sun Feb 6 16:29:43 2005 @@ -1,13 +1,16 @@ <?php require_once 'PEAR/Task/Common.php'; /** - * Implements the postinstallscript file task + * Implements the postinstallscript file task. + * + * Note that post-install scripts are handled separately from installation, by the + * "pear run-scripts" command * @package PEAR * @author Greg Beaver <cellog@php.net> */ class PEAR_Task_Postinstallscript extends PEAR_Task_Common { - var $type = 'postinstall-multiple'; + var $type = 'script'; var $_class; var $_obj; var $_pkg; @@ -157,64 +160,62 @@ * Initialize a task instance with the parameters * @param array raw, parsed xml * @param array attributes from the <file> tag containing this task + * @param string|null last installed version of this package, if any (useful for upgrades) */ - function init($xml, $fileattribs) + function init($xml, $fileattribs, $lastversion) { $this->_class = str_replace('/', '_', $fileattribs['name']); $this->_filename = $fileattribs['name']; $this->_class = str_replace ('.php', '', $this->_class) . '_postinstall'; $this->_params = $xml; + $this->_lastversion = $lastversion; } /** + * Unlike other tasks, the installed file name is passed in instead of the file contents, + * because this task is handled post-installation * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2 - * @param string file contents - * @param string the eventual final file location (informational only) + * @param string file name * @return bool|PEAR_Error false to skip this file, PEAR_Error to fail * (use $this->throwError) */ - function startSession($pkg, $contents, $dest) + function startSession($pkg, $contents) { - $orig = $contents; - $contents = str_replace(array('<?php', '?>'), array('', ''), $contents); - $this->installer->log(0, 'Including external post-installation script "' . - $dest . '" - any fatal errors are in this script'); - eval($contents); + $this->logger->log(0, 'Including external post-installation script "' . + $contents . '" - any errors are in this script'); + include_once $contents; if (class_exists($this->_class)) { - $this->installer->log(0, 'Inclusion succeeded'); + $this->logger->log(0, 'Inclusion succeeded'); } else { return $this->throwError('init of post-install script class "' . $this->_class . '" failed'); } $this->_obj = new $this->_class; - $this->installer->log(1, 'running post-install script "' . $this->_class . '->init()"'); + $this->logger->log(1, 'running post-install script "' . $this->_class . '->init()"'); PEAR::pushErrorHandling(PEAR_ERROR_RETURN); - $res = $this->_obj->init($this->config, $this->installer->ui, $pkg); + $res = $this->_obj->init($this->config, $pkg); PEAR::popErrorHandling(); if ($res) { - $this->installer->log(0, 'init succeeded'); + $this->logger->log(0, 'init succeeded'); } else { return $this->throwError('init of post-install script "' . $this->_class . '->init()" failed'); } - $this->_contents = $dest; + $this->_contents = $contents; $this->_pkg = $pkg; - return $orig; // unchanged + return true; } /** - * Run the post-installation script + * No longer used + * @see PEAR_PackageFile_v2::runPostinstallScripts() * @param array an array of tasks * @param string install or upgrade * @access protected * @static */ - function run($tasks, $installphase) + function run() { - foreach ($tasks as $i => $task) { - $tasks[$i]->installer->ui->runInstallScript($tasks[$i]->_params, $tasks[$i]->_obj, - $installphase, $tasks[$i]->_pkg, $tasks[$i]->_contents); - } } } ?> \ No newline at end of file
« previous php.pear.cvs (#29158) next »