home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / PEAR / Packager.php < prev    next >
PHP Script  |  2001-11-13  |  7KB  |  183 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // |                                                                      |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Packager.php,v 1.9.2.3 2001/11/13 01:26:48 ssb Exp $
  22.  
  23. require_once 'PEAR/Common.php';
  24.  
  25. /**
  26.  * Administration class used to make a PEAR release tarball.
  27.  *
  28.  * TODO:
  29.  *  - add an extra param the dir where to place the created package
  30.  *  - finish and test Windows support
  31.  *
  32.  * @since PHP 4.0.2
  33.  * @author Stig Bakken <ssb@fast.no>
  34.  */
  35. class PEAR_Packager extends PEAR_Common
  36. {
  37.     // {{{ properties
  38.  
  39.     /** assoc with information about the package */
  40.     var $pkginfo = array();
  41.  
  42.     /** name of the package directory, for example Foo-1.0 */
  43.     var $pkgdir;
  44.  
  45.     /** directory where PHP code files go */
  46.     var $phpdir;
  47.  
  48.     /** directory where PHP extension files go */
  49.     var $extdir;
  50.  
  51.     /** directory where documentation goes */
  52.     var $docdir;
  53.  
  54.     /** directory where system state information goes */
  55.     var $statedir;
  56.  
  57.     /** debug mode (integer) */
  58.     var $debug = 0;
  59.  
  60.     /** temporary directory */
  61.     var $tmpdir;
  62.  
  63.     /** whether file list is currently being copied */
  64.     var $recordfilelist;
  65.  
  66.     /** temporary space for copying file list */
  67.     var $filelist;
  68.  
  69.     /** package name and version, for example "HTTP-1.0" */
  70.     var $pkgver;
  71.  
  72.     // }}}
  73.  
  74.     // {{{ constructor
  75.  
  76.     function PEAR_Packager($phpdir = PEAR_INSTALL_DIR,
  77.                            $extdir = PEAR_EXTENSION_DIR,
  78.                            $docdir = '')
  79.     {
  80.         $this->PEAR();
  81.         $this->phpdir = $phpdir;
  82.         $this->extdir = $extdir;
  83.         $this->docdir = $docdir;
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ destructor
  88.  
  89.     function _PEAR_Packager()
  90.     {
  91.         chdir($this->orig_pwd);
  92.         $this->_PEAR_Common();
  93.     }
  94.  
  95.     // }}}
  96.  
  97.     // {{{ package()
  98.  
  99.     function package($pkgfile = null)
  100.     {
  101.         $this->orig_pwd = getcwd();
  102.         if (empty($pkgfile)) {
  103.             $pkgfile = 'package.xml';
  104.         }
  105.         $pkginfo = $this->infoFromDescriptionFile($pkgfile);
  106.         if (PEAR::isError($pkginfo)) {
  107.             return $pkginfo;
  108.         }
  109.         // TMP DIR -------------------------------------------------
  110.         // We allow calls like "pear package /home/user/mypack/package.xml"
  111.         if (!@chdir(dirname($pkgfile))) {
  112.             return $this->raiseError('Couldn\'t chdir to package.xml dir',
  113.                               null, PEAR_ERROR_TRIGGER, E_USER_ERROR);
  114.         }
  115.         $pwd = getcwd();
  116.         $pkgfile = basename($pkgfile);
  117.         if (isset($pkginfo['release_state']) && $pkginfo['release_state'] == 'snapshot') {
  118.             $pkginfo['version'] = date('Ymd');
  119.         }
  120.         // don't want strange characters
  121.         $pkgname    = ereg_replace ('[^a-zA-Z0-9._]', '_', $pkginfo['package']);
  122.         $pkgversion = ereg_replace ('[^a-zA-Z0-9._\-]', '_', $pkginfo['version']);
  123.         $pkgver = $pkgname . '-' . $pkgversion;
  124.  
  125.         $tmpdir = $pwd . DIRECTORY_SEPARATOR . $pkgver;
  126.         if (file_exists($tmpdir)) {
  127.             return $this->raiseError('Tmpdir: ' . $tmpdir .' already exists',
  128.                               null, PEAR_ERROR_TRIGGER, E_USER_ERROR);
  129.         }
  130.         if (!mkdir($tmpdir, 0700)) {
  131.             return $this->raiseError("Unable to create temporary directory $tmpdir.",
  132.                               null, PEAR_ERROR_TRIGGER, E_USER_ERROR);
  133.         } else {
  134.             $this->log(2, "+ tmp dir created at: " . $tmpdir);
  135.         }
  136.         $this->addTempFile($tmpdir);
  137.  
  138.         // Copy files -----------------------------------------------
  139.         foreach ($pkginfo['filelist'] as $fname => $atts) {
  140.             $file = $tmpdir . DIRECTORY_SEPARATOR . $fname;
  141.             $dir = dirname($file);
  142.             if (!@is_dir($dir)) {
  143.                 if (!$this->mkDirHier($dir)) {
  144.                     return $this->raiseError("could not mkdir $dir");
  145.                 }
  146.             }
  147.             //Maintain original file perms
  148.             $orig_perms = @fileperms($fname);
  149.             if (!@copy($fname, $file)) {
  150.                 $this->log(0, "could not copy $fname to $file");
  151.             } else {
  152.                 $this->log(2, "+ copying $fname to $file");
  153.                 @chmod($file, $orig_perms);
  154.             }
  155.         }
  156.         // XXX TODO: Rebuild the package file as the old method did?
  157.  
  158.         // This allows build packages from different pear pack def files
  159.         $dest_pkgfile = $tmpdir . DIRECTORY_SEPARATOR . 'package.xml';
  160.         $this->log(2, "+ copying package $pkgfile to $dest_pkgfile");
  161.         if (!@copy($pkgfile, $dest_pkgfile)) {
  162.             return $this->raiseError("could not copy $pkgfile to $dest_pkgfile");
  163.         }
  164.         @chmod($dest_pkgfile, 0644);
  165.  
  166.         // TAR the Package -------------------------------------------
  167.         chdir(dirname($tmpdir));
  168.         $dest_package = $this->orig_pwd . DIRECTORY_SEPARATOR . "{$pkgver}.tgz";
  169.         $this->log(2, "Attempting to pack $tmpdir dir in $dest_package");
  170.         $tar = new Archive_Tar($dest_package, true);
  171.         $tar->setErrorHandling(PEAR_ERROR_PRINT);
  172.         if (!$tar->create($pkgver)) {
  173.             return $this->raiseError('an error ocurred during package creation');
  174.         }
  175.         $this->log(1, "Package $dest_package done");
  176.         return $dest_package;
  177.     }
  178.  
  179.     // }}}
  180. }
  181.  
  182. ?>
  183.