Packager.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.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@php.net> |
  17. // | Tomas V.V.Cox <cox@idecnet.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Packager.php,v 1.53 2004/06/13 14:06:01 pajoye Exp $
  21. require_once 'PEAR/Common.php';
  22. require_once 'System.php';
  23. /**
  24. * Administration class used to make a PEAR release tarball.
  25. *
  26. * TODO:
  27. * - add an extra param the dir where to place the created package
  28. *
  29. * @since PHP 4.0.2
  30. * @author Stig Bakken <ssb@php.net>
  31. */
  32. class PEAR_Packager extends PEAR_Common
  33. {
  34. // {{{ constructor
  35. function PEAR_Packager()
  36. {
  37. parent::PEAR_Common();
  38. }
  39. // }}}
  40. // {{{ destructor
  41. function _PEAR_Packager()
  42. {
  43. parent::_PEAR_Common();
  44. }
  45. // }}}
  46. // {{{ package()
  47. function package($pkgfile = null, $compress = true)
  48. {
  49. // {{{ validate supplied package.xml file
  50. if (empty($pkgfile)) {
  51. $pkgfile = 'package.xml';
  52. }
  53. // $this->pkginfo gets populated inside
  54. $pkginfo = $this->infoFromDescriptionFile($pkgfile);
  55. if (PEAR::isError($pkginfo)) {
  56. return $this->raiseError($pkginfo);
  57. }
  58. $pkgdir = dirname(realpath($pkgfile));
  59. $pkgfile = basename($pkgfile);
  60. $errors = $warnings = array();
  61. $this->validatePackageInfo($pkginfo, $errors, $warnings, $pkgdir);
  62. foreach ($warnings as $w) {
  63. $this->log(1, "Warning: $w");
  64. }
  65. foreach ($errors as $e) {
  66. $this->log(0, "Error: $e");
  67. }
  68. if (sizeof($errors) > 0) {
  69. return $this->raiseError('Errors in package');
  70. }
  71. // }}}
  72. $pkgver = $pkginfo['package'] . '-' . $pkginfo['version'];
  73. // {{{ Create the package file list
  74. $filelist = array();
  75. $i = 0;
  76. foreach ($pkginfo['filelist'] as $fname => $atts) {
  77. $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
  78. if (!file_exists($file)) {
  79. return $this->raiseError("File does not exist: $fname");
  80. } else {
  81. $filelist[$i++] = $file;
  82. if (empty($pkginfo['filelist'][$fname]['md5sum'])) {
  83. $md5sum = md5_file($file);
  84. $pkginfo['filelist'][$fname]['md5sum'] = $md5sum;
  85. }
  86. $this->log(2, "Adding file $fname");
  87. }
  88. }
  89. // }}}
  90. // {{{ regenerate package.xml
  91. $new_xml = $this->xmlFromInfo($pkginfo);
  92. if (PEAR::isError($new_xml)) {
  93. return $this->raiseError($new_xml);
  94. }
  95. if (!($tmpdir = System::mktemp(array('-d')))) {
  96. return $this->raiseError("PEAR_Packager: mktemp failed");
  97. }
  98. $newpkgfile = $tmpdir . DIRECTORY_SEPARATOR . 'package.xml';
  99. $np = @fopen($newpkgfile, 'wb');
  100. if (!$np) {
  101. return $this->raiseError("PEAR_Packager: unable to rewrite $pkgfile as $newpkgfile");
  102. }
  103. fwrite($np, $new_xml);
  104. fclose($np);
  105. // }}}
  106. // {{{ TAR the Package -------------------------------------------
  107. $ext = $compress ? '.tgz' : '.tar';
  108. $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
  109. $tar =& new Archive_Tar($dest_package, $compress);
  110. $tar->setErrorHandling(PEAR_ERROR_RETURN); // XXX Don't print errors
  111. // ----- Creates with the package.xml file
  112. $ok = $tar->createModify(array($newpkgfile), '', $tmpdir);
  113. if (PEAR::isError($ok)) {
  114. return $this->raiseError($ok);
  115. } elseif (!$ok) {
  116. return $this->raiseError('PEAR_Packager: tarball creation failed');
  117. }
  118. // ----- Add the content of the package
  119. if (!$tar->addModify($filelist, $pkgver, $pkgdir)) {
  120. return $this->raiseError('PEAR_Packager: tarball creation failed');
  121. }
  122. $this->log(1, "Package $dest_package done");
  123. if (file_exists("$pkgdir/CVS/Root")) {
  124. $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $pkginfo['version']);
  125. $cvstag = "RELEASE_$cvsversion";
  126. $this->log(1, "Tag the released code with `pear cvstag $pkgfile'");
  127. $this->log(1, "(or set the CVS tag $cvstag by hand)");
  128. }
  129. // }}}
  130. return $dest_package;
  131. }
  132. // }}}
  133. }
  134. // {{{ md5_file() utility function
  135. if (!function_exists('md5_file')) {
  136. function md5_file($file) {
  137. if (!$fd = @fopen($file, 'r')) {
  138. return false;
  139. }
  140. $md5 = md5(fread($fd, filesize($file)));
  141. fclose($fd);
  142. return $md5;
  143. }
  144. }
  145. // }}}
  146. ?>