]> granicus.if.org Git - php/commitdiff
* implemented role="script" and <replace> substitution during install
authorStig Bakken <ssb@php.net>
Tue, 9 Apr 2002 08:10:12 +0000 (08:10 +0000)
committerStig Bakken <ssb@php.net>
Tue, 9 Apr 2002 08:10:12 +0000 (08:10 +0000)
pear/PEAR/Command/Install.php
pear/PEAR/Common.php
pear/PEAR/Config.php
pear/PEAR/Installer.php
pear/package-pear.xml
pear/package.dtd

index 471ed03b61052075bcfcc45cbb33d63d6c5a7599..e037c0a1bb8699d94e30938281b5f0ad0cb702f1 100644 (file)
@@ -93,9 +93,7 @@ class PEAR_Command_Install extends PEAR_Command_Common
 
     function run($command, $options, $params)
     {
-        $installer = &new PEAR_Installer($this->config->get('php_dir'),
-                                         $this->config->get('ext_dir'),
-                                         $this->config->get('doc_dir'));
+        $installer = &new PEAR_Installer($this->config);
         $installer->setFrontend($this->ui);
         $installer->debug = $this->config->get('verbose');
 
index 9d846ceb1f0ed692da221817e0afb926d2bcb8a9..f522d12fae122dcb4dffc3f37a27027ac22e2183 100644 (file)
@@ -84,7 +84,13 @@ class PEAR_Common extends PEAR
      * Valid file roles
      * @var array
      */
-    var $file_roles  = array('php','ext','test','doc','data','extsrc');
+    var $file_roles  = array('php','ext','test','doc','data','extsrc','script');
+
+    /**
+     * Valid replacement types
+     * @var array
+     */
+    var $replacement_types  = array('php-const', 'pear-config');
 
     /**
      * User Interface object (PEAR_Frontend_* class).  If null,
@@ -93,6 +99,8 @@ class PEAR_Common extends PEAR
      */
     var $ui = null;
 
+    var $current_path = null;
+
     // }}}
 
     // {{{ constructor
@@ -322,6 +330,34 @@ class PEAR_Common extends PEAR
                     $this->dir_role = $attribs['role'];
                 }
                 break;
+            case 'file':
+                if (isset($attribs['name'])) {
+                    $path = '';
+                    if (count($this->dir_names)) {
+                        foreach ($this->dir_names as $dir) {
+                            $path .= $dir . DIRECTORY_SEPARATOR;
+                        }
+                    }
+                    $path .= $attribs['name'];
+                    unset($attribs['name']);
+                    $this->current_path = $path;
+                    $this->filelist[$path] = $attribs;
+                    // Set the baseinstalldir only if the file don't have this attrib
+                    if (!isset($this->filelist[$path]['baseinstalldir']) &&
+                        isset($this->dir_install))
+                    {
+                        $this->filelist[$path]['baseinstalldir'] = $this->dir_install;
+                    }
+                    // Set the Role
+                    if (!isset($this->filelist[$path]['role']) && isset($this->dir_role)) {
+                        $this->filelist[$path]['role'] = $this->dir_role;
+                    }
+                }
+                break;
+            case 'replace':
+                $this->filelist[$this->current_path]['replacements'][] = $attribs;
+                break;
+
             case 'libfile':
                 $this->lib_atts = $attribs;
                 $this->lib_atts['role'] = 'extsrc';
@@ -452,24 +488,25 @@ class PEAR_Common extends PEAR
                 array_pop($this->dir_names);
                 break;
             case 'file':
-                $this->current_file = $data;
-                $path = '';
-                if (count($this->dir_names)) {
-                    foreach ($this->dir_names as $dir) {
-                        $path .= $dir . DIRECTORY_SEPARATOR;
+                if ($data) {
+                    $path = '';
+                    if (count($this->dir_names)) {
+                        foreach ($this->dir_names as $dir) {
+                            $path .= $dir . DIRECTORY_SEPARATOR;
+                        }
+                    }
+                    $path .= $data;
+                    $this->filelist[$path] = $this->current_attributes;
+                    // Set the baseinstalldir only if the file don't have this attrib
+                    if (!isset($this->filelist[$path]['baseinstalldir']) &&
+                        isset($this->dir_install))
+                    {
+                        $this->filelist[$path]['baseinstalldir'] = $this->dir_install;
+                    }
+                    // Set the Role
+                    if (!isset($this->filelist[$path]['role']) && isset($this->dir_role)) {
+                        $this->filelist[$path]['role'] = $this->dir_role;
                     }
-                }
-                $path .= $this->current_file;
-                $this->filelist[$path] = $this->current_attributes;
-                // Set the baseinstalldir only if the file don't have this attrib
-                if (!isset($this->filelist[$path]['baseinstalldir']) &&
-                    isset($this->dir_install))
-                {
-                    $this->filelist[$path]['baseinstalldir'] = $this->dir_install;
-                }
-                // Set the Role
-                if (!isset($this->filelist[$path]['role']) && isset($this->dir_role)) {
-                    $this->filelist[$path]['role'] = $this->dir_role;
                 }
                 break;
             case 'libfile':
@@ -769,20 +806,41 @@ class PEAR_Common extends PEAR
         if (isset($pkginfo['filelist'])) {
             $ret .= "$indent    <filelist>\n";
             foreach ($pkginfo['filelist'] as $file => $fa) {
-                if ($fa['role'] == 'extsrc') {
+                if (@$fa['role'] == 'extsrc') {
                     $ret .= "$indent      <libfile>\n";
                     $ret .= "$indent        <libname>$file</libname>\n";
                     $ret .= "$indent        <sources>$fa[sources]</sources>\n";
                     $ret .= "$indent      </libfile>\n";
                 } else {
-                    $ret .= "$indent      <file role=\"$fa[role]\"";
+                    @$ret .= "$indent      <file role=\"$fa[role]\"";
                     if (isset($fa['baseinstalldir'])) {
-                        $ret .= " baseinstalldir=\"$fa[baseinstalldir]\"";
+                        $ret .= ' baseinstalldir="' .
+                             htmlspecialchars($fa['baseinstalldir']) . '"';
                     }
                     if (isset($fa['md5sum'])) {
                         $ret .= " md5sum=\"$fa[md5sum]\"";
                     }
-                    $ret .= ">$file</file>\n";
+                    if (!empty($fa['replace'])) {
+                        $ret .= ' replace="yes"';
+                    }
+                    if (!empty($fa['install-as'])) {
+                        $ret .= ' install-as="' .
+                             htmlspecialchars($fa['install-as']) . '"';
+                    }
+                    $ret .= ' name="' . htmlspecialchars($file) . '"';
+                    if (empty($fa['replacements'])) {
+                        $ret .= "/>\n";
+                    } else {
+                        $ret .= ">\n";
+                        foreach ($fa['replacements'] as $r) {
+                            $ret .= "$indent        <replace";
+                            foreach ($r as $k => $v) {
+                                $ret .= " $k=\"" . htmlspecialchars($v) .'"';
+                            }
+                            $ret .= "/>\n";
+                        }
+                        @$ret .= "$indent      </file>\n";
+                    }
                 }
             }
             $ret .= "$indent    </filelist>\n";
index c9283046dea67a42aef0349b1682c655cc3178c2..3efe11323597dba0b780a55515b58c32da0422d2 100644 (file)
@@ -27,7 +27,7 @@ require_once 'PEAR.php';
 $GLOBALS['_PEAR_Config_instance'] = null;
 
 define('PEAR_CONFIG_DEFAULT_DOCDIR',
-       PHP_DATADIR.DIRECTORY_SEPARATOR.'pear'.DIRECTORY_SEPARATOR.'doc');
+       PHP_DATADIR.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'pear');
 
 // in case a --without-pear PHP installation is used
 if (!defined('PEAR_INSTALL_DIR')) {
@@ -38,21 +38,8 @@ if (!defined('PEAR_EXTENSION_DIR')) {
 }
 
 /**
- * This is a class for storing simple configuration values keeping
- * track of which are system-defined, user-defined or defaulted.  By
- * default, only user-defined settings are stored back to the user's
- * configuration file.
- *
- * Configuration member is a simple associative array.  Used keys:
- *
- *  master_server      which server to query for mirror lists etc.
- *  server             which server/mirror we're currently using
- *  username           PEAR username
- *  password           PEAR password (stored base64-encoded)
- *  php_dir            Where to install .php files
- *  ext_dir            Directory to install compiled libs in
- *  doc_dir            Directory to install documentation in
- *
+ * This is a class for storing configuration data, keeping track of
+ * which are system-defined, user-defined or defaulted.
  */
 class PEAR_Config extends PEAR
 {
@@ -114,7 +101,12 @@ class PEAR_Config extends PEAR
         'doc_dir' => array(
             'type' => 'directory',
             'default' => PEAR_CONFIG_DEFAULT_DOCDIR,
-            'doc' => 'directory where documentation is located',
+            'doc' => 'directory where documentation is installed',
+            ),
+        'bin_dir' => array(
+            'type' => 'directory',
+            'default' => PHP_BINDIR,
+            'doc' => 'directory where executables are installed',
             ),
         'username' => array(
             'type' => 'string',
@@ -775,18 +767,21 @@ when installing packages without a version or state specified',
     }
 
     // }}}
+    // {{{ getLayers()
 
     /**
-    * Returns the layers defined (except the 'default' one)
-    *
-    * @return array of the defined layers
-    */
+     * Returns the layers defined (except the 'default' one)
+     *
+     * @return array of the defined layers
+     */
     function getLayers()
     {
         $cf = $this->configuration;
         unset($cf['default']);
         return array_keys($cf);
     }
+
+    // }}}
 }
 
 ?>
\ No newline at end of file
index 1ca292943184a4d056dd0795f67edf86f87d9ded..6f536c31e174475a8378f930631bc14a57e00942 100644 (file)
@@ -38,48 +38,60 @@ class PEAR_Installer extends PEAR_Common
 {
     // {{{ properties
 
-    /** name of the package directory, for example Foo-1.0 */
+    /** name of the package directory, for example Foo-1.0
+     * @var string
+     */
     var $pkgdir;
 
-    /** directory where PHP code files go */
+    /** directory where PHP code files go
+     * @var string
+     */
     var $phpdir;
 
-    /** directory where PHP extension files go */
+    /** directory where PHP extension files go
+     * @var string
+     */
     var $extdir;
 
-    /** directory where documentation goes */
+    /** directory where documentation goes
+     * @var string
+     */
     var $docdir;
 
     /** directory where the package wants to put files, relative
-     *  to one of the three previous dirs
+     *  to one of the previous dirs
+     * @var string
      */
     var $destdir = '';
 
-    /** debug level (integer) */
+    /** debug level
+     * @var int
+     */
     var $debug = 1;
 
-    /** temporary directory */
+    /** temporary directory
+     * @var string
+     */
     var $tmpdir;
 
-    /** PEAR_Registry object used by the installer */
+    /** PEAR_Registry object used by the installer
+     * @var object
+     */
     var $registry;
 
+    /** PEAR_Config object used by the installer
+     * @var object
+     */
+    var $config;
+
     // }}}
 
     // {{{ constructor
 
-    function PEAR_Installer($phpdir = PEAR_INSTALL_DIR,
-                            $extdir = PEAR_EXTENSION_DIR,
-                            $docdir = null)
+    function PEAR_Installer(&$config)
     {
         $this->PEAR();
-        $this->phpdir = $phpdir;
-        $this->extdir = $extdir;
-        if ($docdir === null) {
-            $docdir = PHP_DATADIR . DIRECTORY_SEPARATOR . 'pear' .
-                      DIRECTORY_SEPARATOR . 'doc';
-        }
-        $this->docdir = $docdir;
+        $this->config = &$config;
     }
 
     // }}}
@@ -108,30 +120,40 @@ class PEAR_Installer extends PEAR_Common
 
     function _installFile($file, $atts, $tmp_path)
     {
-        $type = strtolower($atts['role']);
-        switch ($type) {
-            case 'test':
+        switch ($atts['role']) {
+            case 'test': case 'data': case 'ext':
                 // don't install test files for now
-                $this->log(2, "+ Test file $file won't be installed yet");
+                $this->log(2, "+ $file: $atts[role] file not installed yet");
                 return true;
-                break;
             case 'doc':
-                $dest_dir = $this->docdir . DIRECTORY_SEPARATOR .
-                            $this->pkginfo['package'];
+                $dest_dir = $this->config->get('doc_dir') .
+                     DIRECTORY_SEPARATOR . $this->pkginfo['package'];
                 break;
+            case 'extsrc':
+                // don't install test files for now
+                $this->log(2, "+ $file: no support for building extensions yet");
+                return true;
             case 'php':
-            default: {
-                $dest_dir = $this->phpdir;
-                if (isset($atts['baseinstalldir'])) {
-                    $dest_dir .= DIRECTORY_SEPARATOR . $atts['baseinstalldir'];
-                }
-                if (dirname($file) != '.') {
-                    $dest_dir .= DIRECTORY_SEPARATOR . dirname($file);
-                }
+                $dest_dir = $this->config->get('php_dir');
+                break;
+            case 'script': {
+                $dest_dir = $this->config->get('bin_dir');
                 break;
             }
+            default:
+                break;
+        }
+        if (isset($atts['baseinstalldir'])) {
+            $dest_dir .= DIRECTORY_SEPARATOR . $atts['baseinstalldir'];
+        }
+        if (dirname($file) != '.') {
+            $dest_dir .= DIRECTORY_SEPARATOR . dirname($file);
+        }
+        if (empty($atts['install-as'])) {
+            $dest_file = $dest_dir . DIRECTORY_SEPARATOR . basename($file);
+        } else {
+            $dest_file = $dest_dir . DIRECTORY_SEPARATOR . $atts['install-as'];
         }
-        $dest_file = $dest_dir . DIRECTORY_SEPARATOR . basename($file);
         if (!@is_dir($dest_dir)) {
             if (!$this->mkDirHier($dest_dir)) {
                 $this->log(0, "failed to mkdir $dest_dir");
@@ -140,13 +162,55 @@ class PEAR_Installer extends PEAR_Common
             $this->log(2, "+ created dir $dest_dir");
         }
         $orig_file = $tmp_path . DIRECTORY_SEPARATOR . $file;
-        $orig_perms = fileperms($orig_file);
-        if (!@copy($orig_file, $dest_file)) {
-            $this->log(0, "failed to copy $orig_file to $dest_file");
-            return false;
+        if (empty($atts['replacements'])) {
+            if (!@copy($orig_file, $dest_file)) {
+                $this->log(0, "failed to copy $orig_file to $dest_file");
+                return false;
+            }
+            $this->log(2, "+ copy $orig_file to $dest_file");
+        } else {
+            $fp = fopen($orig_file, "r");
+            $contents = fread($fp, filesize($orig_file));
+            fclose($fp);
+            $subst_from = $subst_to = array();
+            foreach ($atts['replacements'] as $a) {
+                $to = '';
+                if ($a['type'] == 'php-const') {
+                    if (preg_match('/^[a-z0-9_]+$/i', $a['to'])) {
+                        eval("\$to = $a[to];");
+                    } else {
+                        $this->log(0, "invalid php-const replacement: $a[to]");
+                        continue;
+                    }
+                } elseif ($a['type'] == 'pear-config') {
+                    $to = $this->config->get($a['to']);
+                }
+                if ($to) {
+                    $subst_from = $a['from'];
+                    $subst_to = $to;
+                }
+            }
+            if (sizeof($subst_from)) {
+                $contents = str_replace($subst_from, $subst_to, $contents);
+            }
+            $wp = @fopen($dest_file, "w");
+            if (!is_resource($wp)) {
+                $this->log(0, "failed to create $dest_file");
+                return false;
+            }
+            fwrite($wp, $contents);
+            fclose($wp);
+        }
+        if (!OS_WINDOWS) {
+            if ($atts['role'] == 'script') {
+                $mode = 0755;
+            } else {
+                $mode = 0644;
+            }
+            if (!@chmod($dest_file, $mode)) {
+                $this->log(0, "failed to change mode of $dest_file");
+            }
         }
-        chmod($dest_file, $orig_perms);
-        $this->log(2, "+ copy $orig_file to $dest_file");
 
         // Store the full path where the file was installed for easy unistall
         $this->pkginfo['filelist'][$file]['installed_as'] = $dest_file;
@@ -174,7 +238,7 @@ class PEAR_Installer extends PEAR_Common
         // - soft          : fail silently
         //
         if (empty($this->registry)) {
-            $this->registry = new PEAR_Registry($this->phpdir);
+            $this->registry = new PEAR_Registry($this->config->get('php_dir'));
         }
         $oldcwd = getcwd();
         $need_download = false;
@@ -332,7 +396,7 @@ class PEAR_Installer extends PEAR_Common
         // info from the package it self we want to access from _installFile
         $this->pkginfo = $pkginfo;
         if (empty($options['register_only'])) {
-            if (!is_dir($this->phpdir)) {
+            if (!is_dir($this->config->get('php_dir'))) {
                 chdir($oldcwd);
                 return $this->raiseError("no script destination directory\n",
                                          null, PEAR_ERROR_DIE);
@@ -374,7 +438,7 @@ class PEAR_Installer extends PEAR_Common
     function uninstall($package)
     {
         if (empty($this->registry)) {
-            $this->registry = new PEAR_Registry($this->phpdir);
+            $this->registry = new PEAR_Registry($this->config->get('php_dir'));
         }
 
         // Delete the files
index 62f23d36eadbbaa936cd9407d8bdca1d6c7619e1..90e27d542c18fd4f93bb1342dbdda9d7338478bc 100644 (file)
@@ -48,32 +48,32 @@ First package release.  Commands implemented:
    logout
 </notes>
     <filelist>
-      <file type="php" name="PEAR.php"/>
+      <file role="php" name="PEAR.php"/>
       <dir name="PEAR">
-       <file type="php" name="Autoloader.php"/>
-       <file type="php" name="Command.php"/>
+       <file role="php" name="Autoloader.php"/>
+       <file role="php" name="Command.php"/>
        <dir name="Command">
-         <file type="php" name="Auth.php"/>
-         <file type="php" name="Common.php"/>
-         <file type="php" name="Config.php"/>
-         <file type="php" name="Install.php"/>
-         <file type="php" name="Package.php"/>
-         <file type="php" name="Registry.php"/>
-         <file type="php" name="Remote.php"/>
+         <file role="php" name="Auth.php"/>
+         <file role="php" name="Common.php"/>
+         <file role="php" name="Config.php"/>
+         <file role="php" name="Install.php"/>
+         <file role="php" name="Package.php"/>
+         <file role="php" name="Registry.php"/>
+         <file role="php" name="Remote.php"/>
        </dir>
-       <file type="php" name="Common.php"/>
-       <file type="php" name="Config.php"/>
-       <file type="php" name="Dependency.php"/>
+       <file role="php" name="Common.php"/>
+       <file role="php" name="Config.php"/>
+       <file role="php" name="Dependency.php"/>
        <dir name="Frontend">
-         <file type="php" name="CLI.php"/>
+         <file role="php" name="CLI.php"/>
        </dir>
-       <file type="php" name="Installer.php"/>
-       <file type="php" name="Packager.php"/>
-       <file type="php" name="Registry.php"/>
-       <file type="php" name="Remote.php"/>
+       <file role="php" name="Installer.php"/>
+       <file role="php" name="Packager.php"/>
+       <file role="php" name="Registry.php"/>
+       <file role="php" name="Remote.php"/>
       </dir>
       <dir name="scripts">
-       <file baseinstalldir="/" type="script" install-as="pear" name="pear.in" replace="yes">
+       <file baseinstalldir="/" role="script" install-as="pear" name="pear.in" replace="yes">
           <replace from="@prefix@" to="PHP_PREFIX" type="php-const"/>
         </file>
       </dir>
index 95fec3988501453e8c5af64745bcfe20b80beddd..f80e766c13ecae6b420500d8802f837f4fa60edc 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-     $Id: package.dtd,v 1.21 2002-04-09 00:03:18 ssb Exp $
+     $Id: package.dtd,v 1.22 2002-04-09 08:10:08 ssb Exp $
 
      This is the PEAR package description, version 1.0b6.
      It should be used with the informal public identifier:
@@ -68,7 +68,6 @@
                platform       CDATA              #IMPLIED
                md5sum         CDATA              #IMPLIED
                name           CDATA              #REQUIRED
-               replace        (yes|no)           'no'
                install-as     CDATA              #IMPLIED>
 
 <!ELEMENT replace EMPTY>